From d1869ca0a02b3d23710eb344064c4875c31f0015 Mon Sep 17 00:00:00 2001 From: Kalle Stenflo Date: Wed, 8 Oct 2014 14:37:46 +0200 Subject: [PATCH] JsonSmartProvider more configurable. Deprecated JsonProvider Mode. --- .../com/jayway/jsonpath/web/boot/Main.java | 1 + .../jsonpath/web/resource/ApiResource.java | 6 ++++ .../spi/json/JsonSmartJsonProvider.java | 35 ++++++++++++------- .../com/jayway/jsonpath/spi/json/Mode.java | 1 + .../java/com/jayway/jsonpath/BaseTest.java | 2 ++ .../java/com/jayway/jsonpath/EscapeTest.java | 23 ++++++++++++ 6 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 json-path/src/test/java/com/jayway/jsonpath/EscapeTest.java diff --git a/json-path-web-test/src/main/java/com/jayway/jsonpath/web/boot/Main.java b/json-path-web-test/src/main/java/com/jayway/jsonpath/web/boot/Main.java index 0a908fb8..9bd72f4c 100644 --- a/json-path-web-test/src/main/java/com/jayway/jsonpath/web/boot/Main.java +++ b/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); diff --git a/json-path-web-test/src/main/java/com/jayway/jsonpath/web/resource/ApiResource.java b/json-path-web-test/src/main/java/com/jayway/jsonpath/web/resource/ApiResource.java index fa6a0433..69aa4358 100644 --- a/json-path-web-test/src/main/java/com/jayway/jsonpath/web/resource/ApiResource.java +++ b/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) diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JsonSmartJsonProvider.java b/json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JsonSmartJsonProvider.java index 91f65214..4e61c196 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JsonSmartJsonProvider.java +++ b/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) obj); + return JSONObject.toJSONString((Map) 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); } } diff --git a/json-path/src/main/java/com/jayway/jsonpath/spi/json/Mode.java b/json-path/src/main/java/com/jayway/jsonpath/spi/json/Mode.java index fb6d472d..4fc45982 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/spi/json/Mode.java +++ b/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), diff --git a/json-path/src/test/java/com/jayway/jsonpath/BaseTest.java b/json-path/src/test/java/com/jayway/jsonpath/BaseTest.java index 0c8a4a32..c9fc7ead 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/BaseTest.java +++ b/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" + diff --git a/json-path/src/test/java/com/jayway/jsonpath/EscapeTest.java b/json-path/src/test/java/com/jayway/jsonpath/EscapeTest.java new file mode 100644 index 00000000..d57baf68 --- /dev/null +++ b/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"); + + } +}