Browse Source

JsonSmartProvider more configurable. Deprecated JsonProvider Mode.

pull/57/head
Kalle Stenflo 10 years ago
parent
commit
d1869ca0a0
  1. 1
      json-path-web-test/src/main/java/com/jayway/jsonpath/web/boot/Main.java
  2. 6
      json-path-web-test/src/main/java/com/jayway/jsonpath/web/resource/ApiResource.java
  3. 35
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JsonSmartJsonProvider.java
  4. 1
      json-path/src/main/java/com/jayway/jsonpath/spi/json/Mode.java
  5. 2
      json-path/src/test/java/com/jayway/jsonpath/BaseTest.java
  6. 23
      json-path/src/test/java/com/jayway/jsonpath/EscapeTest.java

1
json-path-web-test/src/main/java/com/jayway/jsonpath/web/boot/Main.java

@ -24,6 +24,7 @@ public class Main {
if(args.length > 0){
configPort = args[0];
}
String port = System.getProperty("server.http.port", configPort);
System.out.println("Server started on port: " + port);

6
json-path-web-test/src/main/java/com/jayway/jsonpath/web/resource/ApiResource.java

@ -3,6 +3,8 @@ package com.jayway.jsonpath.web.resource;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.web.bench.Bench;
import com.jayway.jsonpath.web.bench.Result;
import net.minidev.json.JSONStyle;
import net.minidev.json.JSONValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -27,6 +29,10 @@ public class ApiResource {
private static final Logger logger = LoggerFactory.getLogger(ApiResource.class);
static {
JSONValue.COMPRESSION = JSONStyle.LT_COMPRESS;
}
@GET
@Path("/info")
@Produces(MediaType.APPLICATION_JSON)

35
json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JsonSmartJsonProvider.java

@ -19,6 +19,7 @@ import com.jayway.jsonpath.JsonPathException;
import com.jayway.jsonpath.spi.json.Mode;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONStyle;
import net.minidev.json.mapper.AMapper;
import net.minidev.json.mapper.DefaultMapperOrdered;
import net.minidev.json.parser.JSONParser;
@ -32,29 +33,39 @@ import java.util.Map;
public class JsonSmartJsonProvider extends AbstractJsonProvider {
private final Mode mode;
private final static AMapper<?> orderedMapper = DefaultMapperOrdered.DEFAULT;
private final int parseMode;
private final AMapper<?> mapper;
public JsonSmartJsonProvider() {
this(Mode.SLACK);
this(JSONParser.MODE_PERMISSIVE, DefaultMapperOrdered.DEFAULT);
}
public JsonSmartJsonProvider(int parseMode){
this(parseMode, DefaultMapperOrdered.DEFAULT);
}
public JsonSmartJsonProvider(int parseMode, AMapper<?> mapper){
this.parseMode = parseMode;
this.mapper = mapper;
}
@Deprecated
public JsonSmartJsonProvider(Mode mode) {
this.mode = mode;
this(mode.intValue(), DefaultMapperOrdered.DEFAULT);
}
public Object createArray() {
return orderedMapper.createArray();
return mapper.createArray();
}
public Object createMap() {
return orderedMapper.createObject();
return mapper.createObject();
}
public Object parse(String json) {
try {
return createParser().parse(json, orderedMapper);
return createParser().parse(json, mapper);
} catch (ParseException e) {
throw new InvalidJsonException(e);
}
@ -63,7 +74,7 @@ public class JsonSmartJsonProvider extends AbstractJsonProvider {
@Override
public Object parse(InputStream jsonStream, String charset) throws InvalidJsonException {
try {
return createParser().parse(new InputStreamReader(jsonStream, charset), orderedMapper);
return createParser().parse(new InputStreamReader(jsonStream, charset), mapper);
} catch (ParseException e) {
throw new InvalidJsonException(e);
} catch (UnsupportedEncodingException e) {
@ -75,15 +86,15 @@ public class JsonSmartJsonProvider extends AbstractJsonProvider {
public String toJson(Object obj) {
if (obj instanceof Map) {
return JSONObject.toJSONString((Map<String, ?>) obj);
return JSONObject.toJSONString((Map<String, ?>) obj, JSONStyle.LT_COMPRESS);
} else if (obj instanceof List) {
return JSONArray.toJSONString((List<?>) obj);
return JSONArray.toJSONString((List<?>) obj, JSONStyle.LT_COMPRESS);
} else {
throw new UnsupportedOperationException(obj.getClass().getName() + " can not be converted to JSON");
}
}
private JSONParser createParser() {
return new JSONParser(mode.intValue());
return new JSONParser(parseMode);
}
}

1
json-path/src/main/java/com/jayway/jsonpath/spi/json/Mode.java

@ -14,6 +14,7 @@
*/
package com.jayway.jsonpath.spi.json;
@Deprecated
public enum Mode {
SLACK(-1),

2
json-path/src/test/java/com/jayway/jsonpath/BaseTest.java

@ -23,6 +23,8 @@ public class BaseTest {
.jsonProvider(new JacksonJsonProvider())
.build();
public static final Configuration JSON_SMART_CONFIGURATION = Configuration.defaultConfiguration();
public static final String JSON_DOCUMENT = "{\n" +
" \"string-property\" : \"string-value\", \n" +
" \"int-max-property\" : " + Integer.MAX_VALUE + ", \n" +

23
json-path/src/test/java/com/jayway/jsonpath/EscapeTest.java

@ -0,0 +1,23 @@
package com.jayway.jsonpath;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class EscapeTest extends BaseTest {
@Test
public void urls_are_not_escaped() {
String json = "[" +
"\"https://a/b/1\"," +
"\"https://a/b/2\"," +
"\"https://a/b/3\"" +
"]";
String resAsString = JsonPath.using(JSON_SMART_CONFIGURATION).parse(json).read("$").toString();
assertThat(resAsString).contains("https://a/b/1");
}
}
Loading…
Cancel
Save