kalle
13 years ago
14 changed files with 202 additions and 72 deletions
@ -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); |
||||
} |
||||
} |
@ -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); |
||||
} |
||||
} |
@ -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>(); |
||||
} |
||||
} |
@ -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); |
||||
} |
||||
} |
Loading…
Reference in new issue