Browse Source

Code clean up.

pull/3/head
Kalle Stenflo 13 years ago
parent
commit
c03c029c7e
  1. 34
      README
  2. 24
      changelog.txt
  3. 18
      json-path/pom.xml
  4. 26
      json-path/src/main/java/com/jayway/jsonpath/JsonModel.java
  5. 6
      json-path/src/main/java/com/jayway/jsonpath/spi/JsonProvider.java
  6. 5
      json-path/src/main/java/com/jayway/jsonpath/spi/impl/JacksonProvider.java
  7. 11
      json-path/src/main/java/com/jayway/jsonpath/spi/impl/JsonSmartProvider.java
  8. 64
      json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java
  9. 60
      json-path/src/test/java/com/jayway/jsonpath/JsonPathExtendedTest.java
  10. 3
      json-path/src/test/java/com/jayway/jsonpath/SplitPathFragmentsTest.java
  11. 4
      json-path/src/test/resources/js/path.html
  12. 11
      pom.xml

34
README

@ -1,4 +1,34 @@
Java DSL for reading and testing JSON documents. Java DSL for reading and testing JSON documents.
COPY DEPENDENCIES : mvn clean compile dependency:copy-dependencies -DstripVersion ------------------------------------------
FONT LOGO : Santa Fe LET 50 0.5.6
------------------------------------------
- Replaced regexp with custom tokenizer
- Removed static declaration of JSON_PARSER
- Introduced SPI for JsonProvider
- Elaborating new concept with JsonModel
------------------------------------------
0.5.5
------------------------------------------
- Improved JSON bracket notation. $['store']['book'][*]['author name']
properties can also contain '.' eg $['store']['book'][*]['author.name']
------------------------------------------
0.5.4
------------------------------------------
- Replaced com.googlecode.json-simple with net.minidev.json-smart
- Introduced different parse modes, JsonPath.SLACK_MODE and JsonPath.STRICT_MODE (the slack mode lets you use single quotes or even no quotes at all)
------------------------------------------
0.5.3
------------------------------------------
- Major refactoring
- JsonPath does not always produce a List as response
------------------------------------------
0.5.2
------------------------------------------
- Fixed issue that path was never considered definite if containing a ':'
- Bracket notation is now first class citizen $.foo.bar == $.['foo'].['bar']
- Added JsonAsserter.assertNotDefined(String path) to allow checks for negative existence of a path

24
changelog.txt

@ -1,24 +0,0 @@
------------------------------------------
0.5.5
------------------------------------------
- Improved JSON bracket notation. $['store']['book'][*]['author name']
properties can also contain '.' eg $['store']['book'][*]['author.name']
------------------------------------------
0.5.4
------------------------------------------
- Replaced com.googlecode.json-simple with net.minidev.json-smart
- Introduced different parse modes, JsonPath.SLACK_MODE and JsonPath.STRICT_MODE (the slack mode lets you use single quotes or even no quotes at all)
------------------------------------------
0.5.3
------------------------------------------
- Major refactoring
- JsonPath does not always produce a List as response
------------------------------------------
0.5.2
------------------------------------------
- Fixed issue that path was never considered definite if containing a ':'
- Bracket notation is now first class citizen $.foo.bar == $.['foo'].['bar']
- Added JsonAsserter.assertNotDefined(String path) to allow checks for negative existence of a path

18
json-path/pom.xml

@ -30,26 +30,21 @@
<url>http://code.google.com/p/json-path/</url> <url>http://code.google.com/p/json-path/</url>
<dependencies> <dependencies>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
</dependency>
<!--
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
</dependency>
-->
<dependency> <dependency>
<groupId>net.minidev</groupId> <groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId> <artifactId>json-smart</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>commons-lang</groupId> <groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId> <artifactId>commons-lang</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<scope>provided</scope>
</dependency>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!-- test dependencies --> <!-- test dependencies -->
@ -66,5 +61,6 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

26
json-path/src/main/java/com/jayway/jsonpath/JsonModel.java

@ -14,7 +14,7 @@ public class JsonModel {
private JsonProvider jsonProvider; private JsonProvider jsonProvider;
private JsonModel(String json) { public JsonModel(String json) {
this.jsonProvider = JsonProvider.getInstance(); this.jsonProvider = JsonProvider.getInstance();
this.jsonObject = jsonProvider.parse(json); this.jsonObject = jsonProvider.parse(json);
} }
@ -28,24 +28,36 @@ public class JsonModel {
return new JsonModel(json); return new JsonModel(json);
} }
public <T> T read(String jsonPath) { public <T> T get(String jsonPath) {
JsonPath path = JsonPath.compile(jsonProvider, jsonPath); JsonPath path = JsonPath.compile(jsonProvider, jsonPath);
return (T)read(path); return (T) get(path);
} }
public <T> T read(JsonPath jsonPath) { public <T> T get(JsonPath jsonPath) {
return (T) jsonPath.read(jsonObject); return (T) jsonPath.read(jsonObject);
} }
public JsonModel get(String jsonPath) { public String getJson(JsonPath jsonPath) {
return jsonProvider.toJson(jsonPath.read(jsonObject));
}
public JsonModel getModel(String jsonPath) {
JsonPath path = JsonPath.compile(jsonProvider, jsonPath); JsonPath path = JsonPath.compile(jsonProvider, jsonPath);
return get(path); return getModel(path);
} }
public JsonModel get(JsonPath jsonPath) { public JsonModel getModel(JsonPath jsonPath) {
Object subModel = jsonPath.read(jsonObject); Object subModel = jsonPath.read(jsonObject);
return new JsonModel(subModel); return new JsonModel(subModel);
} }
public JsonProvider getJsonProvider() {
return jsonProvider;
}
public String getJson(){
return jsonProvider.toJson(jsonObject);
}
} }

6
json-path/src/main/java/com/jayway/jsonpath/spi/JsonProvider.java

@ -19,13 +19,15 @@ public abstract class JsonProvider {
public abstract Object parse(String json) throws InvalidJsonException; public abstract Object parse(String json) throws InvalidJsonException;
public abstract String toJson(Object obj);
public abstract Map<String, Object> createMap(); public abstract Map<String, Object> createMap();
public abstract List<Object> createList(); public abstract List<Object> createList();
public static JsonProvider getInstance(){ public static JsonProvider getInstance(){
//return new JsonSmartProvider(); return new JsonSmartProvider();
return new JacksonProvider(); //return new JacksonProvider();
} }
/** /**

5
json-path/src/main/java/com/jayway/jsonpath/spi/impl/JacksonProvider.java

@ -32,6 +32,11 @@ public class JacksonProvider extends JsonProvider {
} }
} }
@Override
public String toJson(Object obj) {
throw new UnsupportedOperationException();
}
@Override @Override
public Map<String, Object> createMap() { public Map<String, Object> createMap() {
return new HashMap<String, Object>(); return new HashMap<String, Object>();

11
json-path/src/main/java/com/jayway/jsonpath/spi/impl/JsonSmartProvider.java

@ -4,6 +4,7 @@ import com.jayway.jsonpath.InvalidJsonException;
import com.jayway.jsonpath.spi.JsonProvider; import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.Mode; import com.jayway.jsonpath.spi.Mode;
import net.minidev.json.JSONArray; import net.minidev.json.JSONArray;
import net.minidev.json.JSONAware;
import net.minidev.json.JSONObject; import net.minidev.json.JSONObject;
import net.minidev.json.parser.JSONParser; import net.minidev.json.parser.JSONParser;
import net.minidev.json.parser.ParseException; import net.minidev.json.parser.ParseException;
@ -48,6 +49,16 @@ public class JsonSmartProvider extends JsonProvider {
} }
} }
@Override
public String toJson(Object obj) {
if(!(obj instanceof JSONAware)){
throw new InvalidJsonException();
}
JSONAware aware = (JSONAware)obj;
return aware.toJSONString();
}
public Mode getMode() { public Mode getMode() {
return mode; return mode;
} }

64
json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java

@ -1,9 +1,8 @@
package com.jayway.jsonpath; package com.jayway.jsonpath;
import com.jayway.jsonpath.util.ScriptEngineJsonPath;
import org.junit.Test; import org.junit.Test;
import java.util.Map;
/** /**
* Created by IntelliJ IDEA. * Created by IntelliJ IDEA.
* User: kallestenflo * User: kallestenflo
@ -12,7 +11,7 @@ import java.util.Map;
*/ */
public class JsonModelTest { public class JsonModelTest {
public final static String DOCUMENT = public final static String DOCUMENT =
"{ \"store\": {\n" + "{ \"store\": {\n" +
" \"book\": [ \n" + " \"book\": [ \n" +
" { \"category\": \"reference\",\n" + " { \"category\": \"reference\",\n" +
@ -46,20 +45,65 @@ public class JsonModelTest {
" }\n" + " }\n" +
" }\n" + " }\n" +
"}"; "}";
public final static JsonModel MODEL = new JsonModel(DOCUMENT);
@Test @Test
public void a_path_can_be_read() throws Exception { public void a_path_can_be_read() throws Exception {
JsonModel pathReader = JsonModel.create(DOCUMENT); JsonPath path = JsonPath.compile("$.store.book[*].title");
JsonModel model = new JsonModel(DOCUMENT);
Map<String, Object> book = pathReader.read("store.book[1]"); Object result = model.get(path);
JsonModel pathReader1 = pathReader.get("store.book[1]"); System.out.println(model.getJsonProvider().toJson(result));
String author = pathReader1.read("author");
System.out.println("d"); }
@Test
public void test2() throws Exception {
JsonPath path = JsonPath.compile("$..");
System.out.println(ScriptEngineJsonPath.eval(DOCUMENT, path.getPath()));
JsonModel model = new JsonModel(DOCUMENT);
System.out.println(model.getJson(path));
}
@Test
public void test3() throws Exception {
JsonPath path = JsonPath.compile("$..[0]");
//System.out.println(ScriptEngineJsonPath.eval(DOCUMENT, path.getPath()));
System.out.println(MODEL.getJson(path));
} }
@Test
public void test4() throws Exception {
JsonPath path = JsonPath.compile("$..*");
System.out.println(ScriptEngineJsonPath.eval(DOCUMENT, path.getPath()));
System.out.println("--------------------------------");
JsonModel model = new JsonModel(DOCUMENT);
System.out.println(model.getJson(path));
}
@Test
public void test5() throws Exception {
JsonModel model = new JsonModel(DOCUMENT);
JsonModel model2 = model.getModel("store.book[0]");
System.out.println(model2.getJson());
}
} }

60
json-path/src/test/java/com/jayway/jsonpath/JsonPathExtendedTest.java

@ -0,0 +1,60 @@
package com.jayway.jsonpath;
import org.junit.Test;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 11/12/11
* Time: 8:25 PM
*/
public class JsonPathExtendedTest {
public final static String DOCUMENT =
"{ \"store\": {\n" +
" \"book\": [ \n" +
" { \"category\": \"reference\",\n" +
" \"author\": \"Nigel Rees\",\n" +
" \"title\": \"Sayings of the Century\",\n" +
" \"price\": 8.95\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"Evelyn Waugh\",\n" +
" \"title\": \"Sword of Honour\",\n" +
" \"price\": 12.99\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"Herman Melville\",\n" +
" \"title\": \"Moby Dick\",\n" +
" \"isbn\": \"0-553-21311-3\",\n" +
" \"price\": 8.99\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"J. R. R. Tolkien\",\n" +
" \"title\": \"The Lord of the Rings\",\n" +
" \"isbn\": \"0-395-19395-8\",\n" +
" \"price\": 22.99\n" +
" }\n" +
" ],\n" +
" \"bicycle\": {\n" +
" \"color\": \"red\",\n" +
" \"price\": 19.95,\n" +
" \"foo:bar\": \"fooBar\",\n" +
" \"dot.notation\": \"new\"\n" +
" }\n" +
" }\n" +
"}";
@Test
public void test_1() throws Exception {
JsonPath path = JsonPath.compile("$.store.book[*].title");
}
}

3
json-path/src/test/java/com/jayway/jsonpath/SplitPathFragmentsTest.java

@ -100,11 +100,8 @@ public class SplitPathFragmentsTest {
private void assertPath(String path, Matcher<Iterable<String>> matcher) { private void assertPath(String path, Matcher<Iterable<String>> matcher) {
System.out.println("PATH: " + path); System.out.println("PATH: " + path);
PathTokenizer tokenizer = new PathTokenizer(path, jsonProvider); PathTokenizer tokenizer = new PathTokenizer(path, jsonProvider);
for (String fragment : tokenizer.getFragments()) { for (String fragment : tokenizer.getFragments()) {
System.out.println(fragment); System.out.println(fragment);
} }

4
json-path/src/test/resources/js/path.html

@ -37,6 +37,7 @@
} }
}; };
$('#src').append(JSON.stringify(window.doc, null, 4));
$('#btnEval').click(function(e) { $('#btnEval').click(function(e) {
$('#result').empty(); $('#result').empty();
@ -51,7 +52,8 @@
</script> </script>
</head> </head>
<body> <body>
<input id="txtPath" type="text" value="$.store.book[?(@.price<10)].title"><button id="btnEval">eval</button> <pre id="src"></pre>
<input id="txtPath" type="text" style="width:600px;" value="$.store.book[?(@.price<10)].title"><button id="btnEval">eval</button>
<pre id="result"></pre> <pre id="result"></pre>
</body> </body>
</html> </html>

11
pom.xml

@ -26,7 +26,7 @@
<artifactId>json-path-parent</artifactId> <artifactId>json-path-parent</artifactId>
<packaging>pom</packaging> <packaging>pom</packaging>
<version>0.5.6-SNAPSHOT</version> <version>0.5.6-SNAPSHOT</version>
<url>http://code.google.com/p/json-path</url> <url>https://github.com/jayway/JsonPath</url>
<name>json-path-parent-pom</name> <name>json-path-parent-pom</name>
<description>Java JsonPath implementation</description> <description>Java JsonPath implementation</description>
<inceptionYear>2011</inceptionYear> <inceptionYear>2011</inceptionYear>
@ -154,18 +154,11 @@
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>
<!--
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
-->
<dependency> <dependency>
<groupId>net.minidev</groupId> <groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId> <artifactId>json-smart</artifactId>
<version>1.0.9</version> <version>1.0.9-1</version>
</dependency> </dependency>
<dependency> <dependency>

Loading…
Cancel
Save