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. 60
      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.
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>
<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>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<scope>provided</scope>
</dependency>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!-- test dependencies -->
@ -66,5 +61,6 @@
<scope>test</scope>
</dependency>
</dependencies>
</project>

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

@ -14,7 +14,7 @@ public class JsonModel {
private JsonProvider jsonProvider;
private JsonModel(String json) {
public JsonModel(String json) {
this.jsonProvider = JsonProvider.getInstance();
this.jsonObject = jsonProvider.parse(json);
}
@ -28,24 +28,36 @@ public class JsonModel {
return new JsonModel(json);
}
public <T> T read(String jsonPath) {
public <T> T get(String 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);
}
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);
return get(path);
return getModel(path);
}
public JsonModel get(JsonPath jsonPath) {
public JsonModel getModel(JsonPath jsonPath) {
Object subModel = jsonPath.read(jsonObject);
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 String toJson(Object obj);
public abstract Map<String, Object> createMap();
public abstract List<Object> createList();
public static JsonProvider getInstance(){
//return new JsonSmartProvider();
return new JacksonProvider();
return new JsonSmartProvider();
//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
public Map<String, Object> createMap() {
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.Mode;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONAware;
import net.minidev.json.JSONObject;
import net.minidev.json.parser.JSONParser;
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() {
return mode;
}

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

@ -1,9 +1,8 @@
package com.jayway.jsonpath;
import com.jayway.jsonpath.util.ScriptEngineJsonPath;
import org.junit.Test;
import java.util.Map;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
@ -12,7 +11,7 @@ import java.util.Map;
*/
public class JsonModelTest {
public final static String DOCUMENT =
public final static String DOCUMENT =
"{ \"store\": {\n" +
" \"book\": [ \n" +
" { \"category\": \"reference\",\n" +
@ -47,18 +46,63 @@ public class JsonModelTest {
" }\n" +
"}";
public final static JsonModel MODEL = new JsonModel(DOCUMENT);
@Test
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);
Object result = model.get(path);
System.out.println(model.getJsonProvider().toJson(result));
}
@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 {
Map<String, Object> book = pathReader.read("store.book[1]");
JsonModel model = new JsonModel(DOCUMENT);
JsonModel pathReader1 = pathReader.get("store.book[1]");
JsonModel model2 = model.getModel("store.book[0]");
String author = pathReader1.read("author");
System.out.println("d");
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) {
System.out.println("PATH: " + path);
PathTokenizer tokenizer = new PathTokenizer(path, jsonProvider);
for (String fragment : tokenizer.getFragments()) {
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) {
$('#result').empty();
@ -51,7 +52,8 @@
</script>
</head>
<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>
</body>
</html>

11
pom.xml

@ -26,7 +26,7 @@
<artifactId>json-path-parent</artifactId>
<packaging>pom</packaging>
<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>
<description>Java JsonPath implementation</description>
<inceptionYear>2011</inceptionYear>
@ -154,18 +154,11 @@
<dependencyManagement>
<dependencies>
<!--
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
-->
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
<version>1.0.9</version>
<version>1.0.9-1</version>
</dependency>
<dependency>

Loading…
Cancel
Save