diff --git a/README b/README index 72320ecf..2f1919a2 100644 --- a/README +++ b/README @@ -1,5 +1,15 @@ Java DSL for reading and testing JSON documents. + +------------------------------------------ +0.8.1 2012-04-16 +------------------------------------------ +- Fixed issue http://code.google.com/p/json-path/issues/detail?id=11 +- HttpProvider SPI +- Improved JavaDoc +- More experimental work on JsonModel + + ------------------------------------------ 0.8.0 2012-03-08 ------------------------------------------ diff --git a/json-path/src/main/java/com/jayway/jsonpath/JsonModel.java b/json-path/src/main/java/com/jayway/jsonpath/JsonModel.java index 002e6f8e..5981c760 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/JsonModel.java +++ b/json-path/src/main/java/com/jayway/jsonpath/JsonModel.java @@ -15,6 +15,7 @@ package com.jayway.jsonpath; import com.jayway.jsonpath.internal.ConvertUtils; +import com.jayway.jsonpath.internal.JsonFormatter; import com.jayway.jsonpath.internal.PathToken; import com.jayway.jsonpath.internal.IOUtils; import com.jayway.jsonpath.spi.JsonProvider; @@ -122,8 +123,12 @@ public class JsonModel { return jsonProvider.isMap(jsonObject); } + /** + * Prints this JsonModel to standard out + */ public void print(){ - System.out.println(toJson()); + String json = toJson(); + System.out.println(JsonFormatter.prettyPrint(json)); } /** @@ -324,7 +329,22 @@ public class JsonModel { * @return model as Json */ public String toJson() { - return jsonProvider.toJson(jsonObject); + return toJson(false); + } + + + /** + * Creates a JSON representation of this JsonModel + * + * @param prettyPrint if the model should be pretty printed + * @return + */ + public String toJson(boolean prettyPrint) { + String json = jsonProvider.toJson(jsonObject); + if(prettyPrint) + return JsonFormatter.prettyPrint(json); + else + return json; } /** diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/JsonFormatter.java b/json-path/src/main/java/com/jayway/jsonpath/internal/JsonFormatter.java new file mode 100644 index 00000000..84d1c0a2 --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/JsonFormatter.java @@ -0,0 +1,83 @@ +package com.jayway.jsonpath.internal; + +public class JsonFormatter +{ + private static final String INDENT = " "; + + private static final String NEW_LINE = System.getProperty("line.separator"); + + private static void appendIndent(StringBuilder sb, int count) + { + for (; count > 0; --count) sb.append(INDENT); + } + + private static boolean isEscaped(StringBuilder sb, int index) + { + boolean escaped = false; + while (index > 0 && sb.charAt(--index) == '\\') escaped = !escaped; + return escaped; + } + + public static String prettyPrint(String input) + { + StringBuilder output = new StringBuilder(input.length() * 2); + boolean quoteOpened = false; + int depth = 0; + + for(int i=0; i result = JsonPath.read(json, "$.foo[?(@.rel= 'item')][0].uri"); + + System.out.println(JsonPath.compile("$.foo[?(@.rel= 'item')][0].uri").isPathDefinite()); + System.out.println(JsonPath.compile("$.foo[?(@.rel= 'item')][0]").isPathDefinite()); + System.out.println(JsonPath.compile("$.foo[?(@.rel= 'item')]").isPathDefinite()); + + assertTrue(result.isEmpty()); } } diff --git a/json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java b/json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java index 9a12d1e3..f811880a 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java @@ -56,7 +56,14 @@ public class JsonModelTest { " }\n" + "}"; - + + @Test + public void a_model_can_be_pretty_printed() throws Exception { + JsonModel model = JsonModel.model(DOCUMENT); + + model.print(); + } + @Test public void has_path_validates() throws Exception { assertFalse(JsonModel.model(DOCUMENT).hasPath("store.invalid")); diff --git a/pom.xml b/pom.xml index 5150d64a..7e374c04 100644 --- a/pom.xml +++ b/pom.xml @@ -212,7 +212,13 @@ net.minidev json-smart - 1.1 + 1.1.1 + + + + commons-lang + commons-lang + 2.6 @@ -234,26 +240,6 @@ 1.2.1 - - commons-lang - commons-lang - 2.6 - - - - - org.slf4j - slf4j-api - 1.6.4 - - - - org.slf4j - slf4j-log4j12 - 1.6.4 - test - - junit junit