Commit dde2d89b authored by yihua.huang's avatar yihua.huang

Ignore content in json when bracket when remove padding #124

parent 2913da47
......@@ -88,7 +88,7 @@
<dependency>
<groupId>us.codecraft</groupId>
<artifactId>xsoup</artifactId>
<version>0.2.2</version>
<version>0.2.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
......
package us.codecraft.webmagic.selector;
import com.alibaba.fastjson.JSON;
import org.jsoup.parser.TokenQueue;
import us.codecraft.xsoup.XTokenQueue;
import java.util.List;
......@@ -27,11 +27,11 @@ public class Json extends PlainText {
*/
public Json removePadding(String padding) {
String text = getText();
TokenQueue tokenQueue = new TokenQueue(text);
XTokenQueue tokenQueue = new XTokenQueue(text);
tokenQueue.consumeWhitespace();
tokenQueue.consume(padding);
tokenQueue.consumeWhitespace();
String chompBalanced = tokenQueue.chompBalanced('(', ')');
String chompBalanced = tokenQueue.chompBalancedNotInQuotes('(', ')');
return new Json(chompBalanced);
}
......
......@@ -12,9 +12,17 @@ public class JsonTest {
private String text = "callback({\"name\":\"json\"})";
private String textWithBrackerInContent = "callback({\"name\":\"json)\"})";
@Test
public void testRemovePadding() throws Exception {
String name = new Json(text).removePadding("callback").jsonPath("$.name").get();
assertThat(name).isEqualTo("json");
}
@Test
public void testRemovePaddingForQuotes() throws Exception {
String name = new Json(textWithBrackerInContent).removePadding("callback").jsonPath("$.name").get();
assertThat(name).isEqualTo("json)");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment