Browse Source

Added Jackson provider

pull/3/head
kalle 13 years ago
parent
commit
f7a506434b
  1. 10
      json-path-assert/src/main/java/com/jayway/jsonassert/JsonAssert.java
  2. 8
      json-path/pom.xml
  3. 51
      json-path/src/main/java/com/jayway/jsonpath/JsonModel.java
  4. 3
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  5. 45
      json-path/src/main/java/com/jayway/jsonpath/PathReader.java
  6. 7
      json-path/src/main/java/com/jayway/jsonpath/spi/JsonProvider.java
  7. 44
      json-path/src/main/java/com/jayway/jsonpath/spi/impl/JacksonProvider.java
  8. 9
      json-path/src/main/java/com/jayway/jsonpath/spi/impl/JsonSmartProvider.java
  9. 71
      json-path/src/test/java/com/jayway/jsonpath/JacksonProviderTest.java
  10. 6
      json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java
  11. 3
      json-path/src/test/java/com/jayway/jsonpath/PathTokenizerTest.java
  12. 3
      json-path/src/test/java/com/jayway/jsonpath/SplitPathFragmentsTest.java
  13. 1
      json-path/src/test/java/com/jayway/jsonpath/util/ScriptEngineJsonPath.java
  14. 11
      pom.xml

10
json-path-assert/src/main/java/com/jayway/jsonassert/JsonAssert.java

@ -4,7 +4,6 @@ package com.jayway.jsonassert;
import com.jayway.jsonassert.impl.JsonAsserterImpl; import com.jayway.jsonassert.impl.JsonAsserterImpl;
import com.jayway.jsonassert.impl.matcher.*; import com.jayway.jsonassert.impl.matcher.*;
import com.jayway.jsonpath.spi.JsonProvider; import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.impl.DefaultJsonProvider;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import java.io.*; import java.io.*;
@ -19,12 +18,7 @@ import java.util.Map;
*/ */
public class JsonAssert { public class JsonAssert {
private static JsonProvider jsonProvider = new DefaultJsonProvider(); private static JsonProvider jsonProvider = JsonProvider.getInstance();
public final static int STRICT_MODE = 0;
public final static int SLACK_MODE = -1;
private static int mode = SLACK_MODE;
public static void setJsonProvider(JsonProvider jsonProvider) { public static void setJsonProvider(JsonProvider jsonProvider) {
JsonAssert.jsonProvider = jsonProvider; JsonAssert.jsonProvider = jsonProvider;
@ -61,7 +55,7 @@ public class JsonAssert {
* @return a JSON asserter initialized with the provided document * @return a JSON asserter initialized with the provided document
* @throws ParseException when the given JSON could not be parsed * @throws ParseException when the given JSON could not be parsed
*/ */
public static JsonAsserter with(InputStream is) throws ParseException, IOException { public static JsonAsserter with(InputStream is) throws IOException {
Reader reader = new InputStreamReader(is); Reader reader = new InputStreamReader(is);
return with(reader); return with(reader);
} }

8
json-path/pom.xml

@ -14,7 +14,8 @@
~ See the License for the specific language governing permissions and ~ See the License for the specific language governing permissions and
~ limitations under the License. ~ limitations under the License.
--> -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>com.jayway.jsonpath</groupId> <groupId>com.jayway.jsonpath</groupId>
@ -28,6 +29,11 @@
<name>json-path</name> <name>json-path</name>
<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> <dependency>
<groupId>com.googlecode.json-simple</groupId> <groupId>com.googlecode.json-simple</groupId>

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

@ -0,0 +1,51 @@
package com.jayway.jsonpath;
import com.jayway.jsonpath.spi.JsonProvider;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 11/8/11
* Time: 7:40 PM
*/
public class JsonModel {
private Object jsonObject;
private JsonProvider jsonProvider;
private JsonModel(String json) {
this.jsonProvider = JsonProvider.getInstance();
this.jsonObject = jsonProvider.parse(json);
}
private JsonModel(Object jsonObject) {
this.jsonProvider = JsonProvider.getInstance();
this.jsonObject = jsonObject;
}
public static JsonModel create(String json) {
return new JsonModel(json);
}
public <T> T read(String jsonPath) {
JsonPath path = JsonPath.compile(jsonProvider, jsonPath);
return (T)read(path);
}
public <T> T read(JsonPath jsonPath) {
return (T) jsonPath.read(jsonObject);
}
public JsonModel get(String jsonPath) {
JsonPath path = JsonPath.compile(jsonProvider, jsonPath);
return get(path);
}
public JsonModel get(JsonPath jsonPath) {
Object subModel = jsonPath.read(jsonObject);
return new JsonModel(subModel);
}
}

3
json-path/src/main/java/com/jayway/jsonpath/JsonPath.java

@ -4,7 +4,6 @@ package com.jayway.jsonpath;
import com.jayway.jsonpath.reader.PathToken; import com.jayway.jsonpath.reader.PathToken;
import com.jayway.jsonpath.reader.PathTokenizer; import com.jayway.jsonpath.reader.PathTokenizer;
import com.jayway.jsonpath.spi.JsonProvider; import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.impl.DefaultJsonProvider;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -87,7 +86,7 @@ public class JsonPath {
* @param jsonPath the path statement * @param jsonPath the path statement
*/ */
private JsonPath(String jsonPath) { private JsonPath(String jsonPath) {
this(new DefaultJsonProvider(), jsonPath); this(JsonProvider.getInstance(), jsonPath);
} }

45
json-path/src/main/java/com/jayway/jsonpath/PathReader.java

@ -1,45 +0,0 @@
package com.jayway.jsonpath;
import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.impl.DefaultJsonProvider;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 11/8/11
* Time: 7:40 PM
*/
public class PathReader {
private Object jsonModel;
private JsonProvider jsonProvider;
private PathReader(String json) {
this.jsonProvider = new DefaultJsonProvider();
this.jsonModel = jsonProvider.parse(json);
}
private PathReader(Object jsonModel) {
this.jsonProvider = new DefaultJsonProvider();
this.jsonModel = jsonModel;
}
public static PathReader create(String json) {
return new PathReader(json);
}
public <T> T read(String jsonPath) {
return (T)JsonPath.compile(jsonProvider, jsonPath).read(jsonModel);
}
public <T> T read(JsonPath jsonPath) {
return (T)jsonPath.read(jsonModel);
}
public PathReader get(String jsonPath){
Object subModel = read(jsonPath);
return new PathReader(subModel);
}
}

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

@ -1,6 +1,8 @@
package com.jayway.jsonpath.spi; package com.jayway.jsonpath.spi;
import com.jayway.jsonpath.InvalidJsonException; import com.jayway.jsonpath.InvalidJsonException;
import com.jayway.jsonpath.spi.impl.JacksonProvider;
import com.jayway.jsonpath.spi.impl.JsonSmartProvider;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -21,6 +23,11 @@ public abstract class JsonProvider {
public abstract List<Object> createList(); public abstract List<Object> createList();
public static JsonProvider getInstance(){
//return new JsonSmartProvider();
return new JacksonProvider();
}
/** /**
* checks if object is <code>instanceof</code> <code>java.util.List</code> or <code>java.util.Map</code> * checks if object is <code>instanceof</code> <code>java.util.List</code> or <code>java.util.Map</code>
* *

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

@ -0,0 +1,44 @@
package com.jayway.jsonpath.spi.impl;
import com.jayway.jsonpath.InvalidJsonException;
import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.Mode;
import org.codehaus.jackson.map.ObjectMapper;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 11/8/11
* Time: 10:34 PM
*/
public class JacksonProvider extends JsonProvider {
@Override
public Mode getMode() {
return Mode.STRICT;
}
@Override
public Object parse(String json) throws InvalidJsonException {
try {
return new ObjectMapper().readValue(json, Object.class);
} catch (IOException e) {
throw new InvalidJsonException(e);
}
}
@Override
public Map<String, Object> createMap() {
return new HashMap<String, Object>();
}
@Override
public List<Object> createList() {
return new LinkedList<Object>();
}
}

9
json-path/src/main/java/com/jayway/jsonpath/spi/impl/DefaultJsonProvider.java → json-path/src/main/java/com/jayway/jsonpath/spi/impl/JsonSmartProvider.java

@ -8,7 +8,6 @@ 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;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -18,17 +17,17 @@ import java.util.Map;
* Date: 11/8/11 * Date: 11/8/11
* Time: 3:56 PM * Time: 3:56 PM
*/ */
public class DefaultJsonProvider extends JsonProvider { public class JsonSmartProvider extends JsonProvider {
private Mode mode; private Mode mode;
private JSONParser parser; private JSONParser parser;
public DefaultJsonProvider() { public JsonSmartProvider() {
this(Mode.SLACK); this(Mode.SLACK);
} }
public DefaultJsonProvider(Mode mode) { public JsonSmartProvider(Mode mode) {
this.mode = mode; this.mode = mode;
this.parser = new JSONParser(mode.intValue()); this.parser = new JSONParser(mode.intValue());
} }
@ -46,8 +45,6 @@ public class DefaultJsonProvider extends JsonProvider {
return parser.parse(json); return parser.parse(json);
} catch (ParseException e) { } catch (ParseException e) {
throw new InvalidJsonException(e); throw new InvalidJsonException(e);
} catch (IOException e) {
throw new InvalidJsonException(e);
} }
} }

71
json-path/src/test/java/com/jayway/jsonpath/JacksonProviderTest.java

@ -0,0 +1,71 @@
package com.jayway.jsonpath;
import com.jayway.jsonpath.spi.impl.JacksonProvider;
import org.junit.Test;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 11/8/11
* Time: 10:40 PM
*/
public class JacksonProviderTest {
public final static String ARRAY = "[{\"value\": 1},{\"value\": 2}, {\"value\": 3},{\"value\": 4}]";
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 parse_document() throws Exception {
JacksonProvider provider = new JacksonProvider();
Object o = provider.parse(DOCUMENT);
System.out.println(o);
}
@Test
public void parse_array() throws Exception {
JacksonProvider provider = new JacksonProvider();
Object o = provider.parse(ARRAY);
System.out.println(o);
}
}

6
json-path/src/test/java/com/jayway/jsonpath/PathReaderTest.java → json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java

@ -10,7 +10,7 @@ import java.util.Map;
* Date: 11/8/11 * Date: 11/8/11
* Time: 7:52 PM * Time: 7:52 PM
*/ */
public class PathReaderTest { public class JsonModelTest {
public final static String DOCUMENT = public final static String DOCUMENT =
"{ \"store\": {\n" + "{ \"store\": {\n" +
@ -50,11 +50,11 @@ public class PathReaderTest {
@Test @Test
public void a_path_can_be_read() throws Exception { public void a_path_can_be_read() throws Exception {
PathReader pathReader = PathReader.create(DOCUMENT); JsonModel pathReader = JsonModel.create(DOCUMENT);
Map<String, Object> book = pathReader.read("store.book[1]"); Map<String, Object> book = pathReader.read("store.book[1]");
PathReader pathReader1 = pathReader.get("store.book[1]"); JsonModel pathReader1 = pathReader.get("store.book[1]");
String author = pathReader1.read("author"); String author = pathReader1.read("author");

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

@ -3,7 +3,6 @@ package com.jayway.jsonpath;
import com.jayway.jsonpath.reader.PathToken; import com.jayway.jsonpath.reader.PathToken;
import com.jayway.jsonpath.reader.PathTokenizer; import com.jayway.jsonpath.reader.PathTokenizer;
import com.jayway.jsonpath.spi.JsonProvider; import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.impl.DefaultJsonProvider;
import org.junit.Test; import org.junit.Test;
import java.util.List; import java.util.List;
@ -20,7 +19,7 @@ import static org.junit.Assert.assertThat;
*/ */
public class PathTokenizerTest { public class PathTokenizerTest {
private JsonProvider jsonProvider = new DefaultJsonProvider(); private JsonProvider jsonProvider = JsonProvider.getInstance();
public final static String DOCUMENT = public final static String DOCUMENT =
"{ \"store\": {\n" + "{ \"store\": {\n" +

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

@ -2,7 +2,6 @@ package com.jayway.jsonpath;
import com.jayway.jsonpath.reader.PathTokenizer; import com.jayway.jsonpath.reader.PathTokenizer;
import com.jayway.jsonpath.spi.JsonProvider; import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.impl.DefaultJsonProvider;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import org.junit.Test; import org.junit.Test;
@ -18,7 +17,7 @@ import static org.junit.Assert.assertTrue;
*/ */
public class SplitPathFragmentsTest { public class SplitPathFragmentsTest {
private JsonProvider jsonProvider = new DefaultJsonProvider(); private JsonProvider jsonProvider = JsonProvider.getInstance();
@Test @Test
public void valid_path_is_split_correctly() throws Exception { public void valid_path_is_split_correctly() throws Exception {

1
json-path/src/test/java/com/jayway/jsonpath/util/ScriptEngineJsonPath.java

@ -43,6 +43,7 @@ public class ScriptEngineJsonPath {
private static String readScript(String script) { private static String readScript(String script) {
InputStream is = null; InputStream is = null;
try { try {
is = ScriptEngineJsonPath.class.getClassLoader().getSystemResourceAsStream("js/" + script); is = ScriptEngineJsonPath.class.getClassLoader().getSystemResourceAsStream("js/" + script);
return new Scanner(is).useDelimiter("\\A").next(); return new Scanner(is).useDelimiter("\\A").next();

11
pom.xml

@ -14,7 +14,8 @@
~ See the License for the specific language governing permissions and ~ See the License for the specific language governing permissions and
~ limitations under the License. ~ limitations under the License.
--> -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>org.sonatype.oss</groupId> <groupId>org.sonatype.oss</groupId>
@ -164,7 +165,13 @@
<dependency> <dependency>
<groupId>net.minidev</groupId> <groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId> <artifactId>json-smart</artifactId>
<version>1.0.6.3</version> <version>1.0.9</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.7.1</version>
</dependency> </dependency>
<dependency> <dependency>

Loading…
Cancel
Save