Ignore content in json when bracket when remove padding #124
parent
2913da4763
commit
dde2d89bbe
2
pom.xml
2
pom.xml
|
@ -88,7 +88,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>us.codecraft</groupId>
|
<groupId>us.codecraft</groupId>
|
||||||
<artifactId>xsoup</artifactId>
|
<artifactId>xsoup</artifactId>
|
||||||
<version>0.2.2</version>
|
<version>0.2.3</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alibaba</groupId>
|
<groupId>com.alibaba</groupId>
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package us.codecraft.webmagic.selector;
|
package us.codecraft.webmagic.selector;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import org.jsoup.parser.TokenQueue;
|
import us.codecraft.xsoup.XTokenQueue;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -27,11 +27,11 @@ public class Json extends PlainText {
|
||||||
*/
|
*/
|
||||||
public Json removePadding(String padding) {
|
public Json removePadding(String padding) {
|
||||||
String text = getText();
|
String text = getText();
|
||||||
TokenQueue tokenQueue = new TokenQueue(text);
|
XTokenQueue tokenQueue = new XTokenQueue(text);
|
||||||
tokenQueue.consumeWhitespace();
|
tokenQueue.consumeWhitespace();
|
||||||
tokenQueue.consume(padding);
|
tokenQueue.consume(padding);
|
||||||
tokenQueue.consumeWhitespace();
|
tokenQueue.consumeWhitespace();
|
||||||
String chompBalanced = tokenQueue.chompBalanced('(', ')');
|
String chompBalanced = tokenQueue.chompBalancedNotInQuotes('(', ')');
|
||||||
return new Json(chompBalanced);
|
return new Json(chompBalanced);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,9 +12,17 @@ public class JsonTest {
|
||||||
|
|
||||||
private String text = "callback({\"name\":\"json\"})";
|
private String text = "callback({\"name\":\"json\"})";
|
||||||
|
|
||||||
|
private String textWithBrackerInContent = "callback({\"name\":\"json)\"})";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRemovePadding() throws Exception {
|
public void testRemovePadding() throws Exception {
|
||||||
String name = new Json(text).removePadding("callback").jsonPath("$.name").get();
|
String name = new Json(text).removePadding("callback").jsonPath("$.name").get();
|
||||||
assertThat(name).isEqualTo("json");
|
assertThat(name).isEqualTo("json");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRemovePaddingForQuotes() throws Exception {
|
||||||
|
String name = new Json(textWithBrackerInContent).removePadding("callback").jsonPath("$.name").get();
|
||||||
|
assertThat(name).isEqualTo("json)");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue