23 changed files with 1704 additions and 0 deletions
@ -0,0 +1,9 @@ |
|||||||
|
package com.jayway.jsonpath.json; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public abstract class JsonArray extends JsonElement implements java.util.List<JsonElement> { |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,54 @@ |
|||||||
|
package com.jayway.jsonpath.json; |
||||||
|
|
||||||
|
|
||||||
|
import com.jayway.jsonpath.filter.JsonType; |
||||||
|
|
||||||
|
|
||||||
|
public abstract class JsonElement { |
||||||
|
|
||||||
|
public abstract JsonObject toJsonObject() throws JsonException; |
||||||
|
public abstract JsonArray toJsonArray() throws JsonException; |
||||||
|
public abstract JsonPrimitive toPrimitive() throws JsonException; |
||||||
|
public abstract Object toObject() throws JsonException; |
||||||
|
|
||||||
|
public abstract Object getWrappedElement(); |
||||||
|
|
||||||
|
|
||||||
|
public abstract boolean isJsonObject(); |
||||||
|
public abstract boolean isJsonArray(); |
||||||
|
public abstract boolean isJsonPrimitive(); |
||||||
|
public abstract boolean isContainer(); |
||||||
|
public boolean isJsonType(JsonType t){ |
||||||
|
return ((t.equals(JsonType.JsonArray) && isJsonArray()) |
||||||
|
|| (t.equals(JsonType.JsonObject) && isJsonObject()) |
||||||
|
|| (t.equals(JsonType.JsonPrimitive) && isJsonPrimitive())); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
private ParentReference parentRef; |
||||||
|
|
||||||
|
public void setParentReference(ParentReference pr){ |
||||||
|
this.parentRef = pr; |
||||||
|
} |
||||||
|
|
||||||
|
public ParentReference getParentReference(){ |
||||||
|
return parentRef; |
||||||
|
} |
||||||
|
|
||||||
|
public void setParentReference(JsonElement parent, String path) { |
||||||
|
this.parentRef = new ParentReference(parent,path); |
||||||
|
} |
||||||
|
public void setParentReference(JsonElement parent, int index) { |
||||||
|
this.parentRef = new ParentReference(parent,index); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public abstract boolean isJsonNull(); |
||||||
|
|
||||||
|
|
||||||
|
public abstract void merge(JsonElement o) throws JsonException; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,12 @@ |
|||||||
|
package com.jayway.jsonpath.json; |
||||||
|
|
||||||
|
public class JsonException extends Exception { |
||||||
|
|
||||||
|
public JsonException(String string) { |
||||||
|
super(string); |
||||||
|
} |
||||||
|
public JsonException() { |
||||||
|
super(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,49 @@ |
|||||||
|
package com.jayway.jsonpath.json; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public abstract class JsonFactory { |
||||||
|
|
||||||
|
private static JsonFactory instance; |
||||||
|
|
||||||
|
public static JsonFactory getInstance(){ |
||||||
|
if(instance == null) |
||||||
|
instance = new com.jayway.jsonpath.json.gson.GsonJsonFactory(); |
||||||
|
return instance; |
||||||
|
} |
||||||
|
public static void setInstance(JsonFactory gsonJsonFactory) { |
||||||
|
instance = gsonJsonFactory; |
||||||
|
} |
||||||
|
|
||||||
|
public abstract JsonObject createJsonObject(); |
||||||
|
public abstract JsonArray createJsonArray(); |
||||||
|
public abstract JsonElement parse(String json) throws ParseException, IOException; |
||||||
|
public JsonNull createJsonNull(String path,JsonElement parent) { |
||||||
|
JsonNull jn = new JsonNull(); |
||||||
|
jn.setParentReference(parent,path); |
||||||
|
return jn; |
||||||
|
} |
||||||
|
|
||||||
|
public JsonNull createJsonNull(Integer index,JsonElement parent) { |
||||||
|
JsonNull jn = new JsonNull(); |
||||||
|
jn.setParentReference(parent,index); |
||||||
|
return jn; |
||||||
|
} |
||||||
|
public JsonElement createJsonNull() { |
||||||
|
JsonNull jn = new JsonNull(); |
||||||
|
return jn; |
||||||
|
} |
||||||
|
|
||||||
|
public abstract JsonPrimitive createJsonPrimitive(Object obj); |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,64 @@ |
|||||||
|
package com.jayway.jsonpath.json; |
||||||
|
|
||||||
|
public class JsonNull extends JsonElement { |
||||||
|
public boolean isJsonNull() { |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Object getWrappedElement() { |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isContainer() { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isJsonArray() { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isJsonObject() { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isJsonPrimitive() { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public JsonArray toJsonArray() throws JsonException { |
||||||
|
throw new JsonException(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String toString(){ |
||||||
|
return null; |
||||||
|
} |
||||||
|
@Override |
||||||
|
public JsonObject toJsonObject() throws JsonException { |
||||||
|
throw new JsonException(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Object toObject() throws JsonException { |
||||||
|
throw new JsonException(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public JsonPrimitive toPrimitive() throws JsonException { |
||||||
|
throw new JsonException(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void merge(JsonElement inputObjectb) throws JsonException { |
||||||
|
throw new JsonException("Cannot merge a null"); // TODO Auto-generated method stub
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,19 @@ |
|||||||
|
package com.jayway.jsonpath.json; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
public abstract class JsonObject extends JsonElement implements Map<String,JsonElement> { |
||||||
|
|
||||||
|
public abstract List<JsonElement> getProperties() throws JsonException; |
||||||
|
|
||||||
|
public abstract boolean hasProperty(String pathFragment)throws JsonException; |
||||||
|
|
||||||
|
|
||||||
|
public abstract JsonElement getProperty(String pathFragment) throws JsonException; |
||||||
|
|
||||||
|
public abstract void setProperty(String name, JsonElement element) throws JsonException; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,9 @@ |
|||||||
|
package com.jayway.jsonpath.json; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
public interface JsonParser { |
||||||
|
|
||||||
|
public JsonElement parse(String json) throws ParseException,IOException; |
||||||
|
|
||||||
|
} |
@ -0,0 +1,8 @@ |
|||||||
|
package com.jayway.jsonpath.json; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
public class JsonPathResultList extends ArrayList<JsonElement>{ |
||||||
|
|
||||||
|
} |
@ -0,0 +1,5 @@ |
|||||||
|
package com.jayway.jsonpath.json; |
||||||
|
|
||||||
|
public abstract class JsonPrimitive extends JsonElement { |
||||||
|
|
||||||
|
} |
@ -0,0 +1,70 @@ |
|||||||
|
package com.jayway.jsonpath.json; |
||||||
|
|
||||||
|
public class ParentReference { |
||||||
|
protected JsonElement parent; |
||||||
|
protected Integer index; |
||||||
|
protected String field; |
||||||
|
|
||||||
|
public ParentReference(JsonElement parent, String field) { |
||||||
|
this.parent = parent; |
||||||
|
this.field = field; |
||||||
|
} |
||||||
|
|
||||||
|
public ParentReference(JsonElement parent, int index) { |
||||||
|
this.index = index; |
||||||
|
this.parent = parent; |
||||||
|
} |
||||||
|
|
||||||
|
public void setParentField(String field) throws JsonException { |
||||||
|
if(parent == null || !parent.isJsonObject()) |
||||||
|
throw new JsonException("Parent is null or not an object"); |
||||||
|
|
||||||
|
this.field = field; |
||||||
|
} |
||||||
|
|
||||||
|
public void setReference(JsonElement element) throws JsonException{ |
||||||
|
if(parent == null){ |
||||||
|
throw new JsonException("Parent is null"); |
||||||
|
} |
||||||
|
else if(parent.isJsonArray()){ |
||||||
|
if( index == null || index == -1) |
||||||
|
parent.toJsonArray().add(element); |
||||||
|
parent.toJsonArray().set(index, element); |
||||||
|
} |
||||||
|
else if(parent.isJsonObject()){ |
||||||
|
parent.toJsonObject().put(field, element); |
||||||
|
} |
||||||
|
else{ |
||||||
|
throw new RuntimeException("Unexpected error: Parent is not a container"); |
||||||
|
} |
||||||
|
element.setParentReference(this); |
||||||
|
} |
||||||
|
|
||||||
|
public String getField() { |
||||||
|
return field; |
||||||
|
} |
||||||
|
|
||||||
|
public void setField(String s) { |
||||||
|
this.field= s; |
||||||
|
} |
||||||
|
|
||||||
|
public void setParent(JsonElement parent) { |
||||||
|
|
||||||
|
this.parent = parent; |
||||||
|
} |
||||||
|
|
||||||
|
public Integer getIndex() throws JsonException { |
||||||
|
if(parent == null || !parent.isJsonArray()) |
||||||
|
throw new JsonException("Parent is null or not an object"); |
||||||
|
return this.index; |
||||||
|
} |
||||||
|
|
||||||
|
public void setIndex(Integer index) { |
||||||
|
this.index = index; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public JsonElement getParent() { |
||||||
|
return parent; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,21 @@ |
|||||||
|
package com.jayway.jsonpath.json; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public class ParseException extends Exception { |
||||||
|
|
||||||
|
public ParseException() { |
||||||
|
super(); |
||||||
|
} |
||||||
|
public ParseException(String s) { |
||||||
|
super(s); |
||||||
|
} |
||||||
|
|
||||||
|
public ParseException(Throwable e) { |
||||||
|
super(e); |
||||||
|
} |
||||||
|
public ParseException(String s,Throwable e) { |
||||||
|
super(s,e); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,236 @@ |
|||||||
|
package com.jayway.jsonpath.json.gson; |
||||||
|
|
||||||
|
|
||||||
|
import java.util.Collection; |
||||||
|
import java.util.Iterator; |
||||||
|
import java.util.List; |
||||||
|
import java.util.ListIterator; |
||||||
|
|
||||||
|
import com.google.gson.JsonArray; |
||||||
|
import com.jayway.jsonpath.json.JsonElement; |
||||||
|
import com.jayway.jsonpath.json.JsonException; |
||||||
|
import com.jayway.jsonpath.json.JsonObject; |
||||||
|
import com.jayway.jsonpath.json.JsonPrimitive; |
||||||
|
import com.jayway.jsonpath.json.ParentReference; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public class GsonJsonArray extends com.jayway.jsonpath.json.JsonArray { |
||||||
|
public com.google.gson.JsonArray element; |
||||||
|
public GsonJsonArray(com.google.gson.JsonArray elem){ |
||||||
|
element = elem; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public boolean add(com.jayway.jsonpath.json.JsonElement item) { |
||||||
|
element.add((com.google.gson.JsonElement)item.getWrappedElement()); |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
public void add(int index, com.jayway.jsonpath.json.JsonElement element) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
public boolean addAll( |
||||||
|
Collection<? extends com.jayway.jsonpath.json.JsonElement> c) { |
||||||
|
boolean ok = true; |
||||||
|
for(com.jayway.jsonpath.json.JsonElement e : c ){ |
||||||
|
element.add((com.google.gson.JsonElement)e.getWrappedElement()); |
||||||
|
} |
||||||
|
return ok; |
||||||
|
} |
||||||
|
public boolean addAll(int index, |
||||||
|
Collection<? extends com.jayway.jsonpath.json.JsonElement> c) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
} |
||||||
|
public void clear() { |
||||||
|
element = new com.google.gson.JsonArray(); |
||||||
|
} |
||||||
|
public boolean contains(Object o) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
} |
||||||
|
public boolean containsAll(Collection<?> c) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
} |
||||||
|
public com.jayway.jsonpath.json.JsonElement get(int index) { |
||||||
|
try { |
||||||
|
return GsonUtil.convertUp(element.get(index),new ParentReference(this,index)); |
||||||
|
} catch (JsonException e) { |
||||||
|
return null; |
||||||
|
|
||||||
|
} |
||||||
|
} |
||||||
|
public int indexOf(Object o) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
} |
||||||
|
public boolean isEmpty() { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
public Iterator<com.jayway.jsonpath.json.JsonElement> iterator() { |
||||||
|
final GsonJsonArray me = this; |
||||||
|
return new Iterator<com.jayway.jsonpath.json.JsonElement>() { |
||||||
|
Iterator<com.google.gson.JsonElement> inner; |
||||||
|
int count; |
||||||
|
{ |
||||||
|
inner = element.iterator(); |
||||||
|
count =0 ; |
||||||
|
} |
||||||
|
public boolean hasNext() { |
||||||
|
return inner.hasNext(); |
||||||
|
} |
||||||
|
|
||||||
|
public com.jayway.jsonpath.json.JsonElement next() { |
||||||
|
com.google.gson.JsonElement next = inner.next(); |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
try { |
||||||
|
return GsonUtil.convertUp(next, new ParentReference(me,count++)); |
||||||
|
} catch (JsonException e) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public void remove() { |
||||||
|
inner.remove(); |
||||||
|
|
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
} |
||||||
|
public int lastIndexOf(Object o) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
public ListIterator<com.jayway.jsonpath.json.JsonElement> listIterator() { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
public ListIterator<com.jayway.jsonpath.json.JsonElement> listIterator( |
||||||
|
int index) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
public boolean remove(Object o) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
} |
||||||
|
public com.jayway.jsonpath.json.JsonElement remove(int index) { |
||||||
|
|
||||||
|
try { |
||||||
|
com.jayway.jsonpath.json.JsonElement je = this.get(index); |
||||||
|
this.element = (JsonArray) GsonUtil.removeGsonArrayElement(index,this).getWrappedElement(); |
||||||
|
return je; |
||||||
|
} catch (JsonException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
||||||
|
public boolean removeAll(Collection<?> c) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
public boolean retainAll(Collection<?> c) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
public com.jayway.jsonpath.json.JsonElement set(int index, |
||||||
|
com.jayway.jsonpath.json.JsonElement element) { |
||||||
|
|
||||||
|
try { |
||||||
|
this.element = (JsonArray)GsonUtil.setGsonArrayElement(index,element,this).getWrappedElement(); |
||||||
|
return element; |
||||||
|
} catch (JsonException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public int size() { |
||||||
|
return element.size(); |
||||||
|
|
||||||
|
} |
||||||
|
public List<com.jayway.jsonpath.json.JsonElement> subList(int fromIndex, |
||||||
|
int toIndex) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
public Object[] toArray() { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
} |
||||||
|
public <T> T[] toArray(T[] a) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
public boolean isContainer() { |
||||||
|
return true; |
||||||
|
} |
||||||
|
public boolean isJsonArray() { |
||||||
|
return true; |
||||||
|
} |
||||||
|
public boolean isJsonObject() { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
public com.jayway.jsonpath.json.JsonArray toJsonArray() { |
||||||
|
return this; |
||||||
|
} |
||||||
|
public JsonObject toJsonObject() throws JsonException { |
||||||
|
throw new JsonException(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public JsonPrimitive toPrimitive() throws JsonException { |
||||||
|
|
||||||
|
throw new JsonException(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public Object getWrappedElement() { |
||||||
|
return element; |
||||||
|
} |
||||||
|
|
||||||
|
public String toString(){ |
||||||
|
return element.toString(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isJsonPrimitive() { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public Object toObject() throws JsonException { |
||||||
|
return element; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isJsonNull() { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public void merge(JsonElement inputObjectb) throws JsonException { |
||||||
|
if(inputObjectb.isJsonArray()){ |
||||||
|
this.merge(inputObjectb.toJsonArray()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void merge(com.jayway.jsonpath.json.JsonArray ja) |
||||||
|
throws JsonException { |
||||||
|
for(JsonElement j : ja){ |
||||||
|
this.add(j); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,44 @@ |
|||||||
|
package com.jayway.jsonpath.json.gson; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
import com.google.gson.JsonPrimitive; |
||||||
|
import com.jayway.jsonpath.json.JsonElement; |
||||||
|
import com.jayway.jsonpath.json.JsonNull; |
||||||
|
import com.jayway.jsonpath.json.JsonObject; |
||||||
|
import com.jayway.jsonpath.json.ParseException; |
||||||
|
|
||||||
|
import net.minidev.json.JSONArray; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public class GsonJsonFactory extends com.jayway.jsonpath.json.JsonFactory{ |
||||||
|
|
||||||
|
|
||||||
|
public GsonJsonArray createJsonArray() { |
||||||
|
return new GsonJsonArray(new com.google.gson.JsonArray()); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public JsonElement parse(String json) throws ParseException, IOException { |
||||||
|
return new GsonJsonParser().parse(json); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public JsonObject createJsonObject() { |
||||||
|
return new com.jayway.jsonpath.json.gson.GsonJsonObject(new com.google.gson.JsonObject()); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public com.jayway.jsonpath.json.JsonPrimitive createJsonPrimitive(Object obj) { |
||||||
|
return new GsonJsonPrimitive(obj); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,226 @@ |
|||||||
|
package com.jayway.jsonpath.json.gson; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.Collection; |
||||||
|
import java.util.HashSet; |
||||||
|
import java.util.Iterator; |
||||||
|
import java.util.List; |
||||||
|
import java.util.ListIterator; |
||||||
|
import java.util.Map; |
||||||
|
import java.util.Set; |
||||||
|
|
||||||
|
import net.minidev.json.JSONArray; |
||||||
|
import net.minidev.json.JSONObject; |
||||||
|
|
||||||
|
import com.jayway.jsonpath.json.JsonArray; |
||||||
|
import com.jayway.jsonpath.json.JsonElement; |
||||||
|
import com.jayway.jsonpath.json.JsonException; |
||||||
|
import com.jayway.jsonpath.json.JsonObject; |
||||||
|
import com.jayway.jsonpath.json.JsonPrimitive; |
||||||
|
import com.jayway.jsonpath.json.ParentReference; |
||||||
|
|
||||||
|
|
||||||
|
public class GsonJsonObject extends com.jayway.jsonpath.json.JsonObject{ |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public com.google.gson.JsonObject element; |
||||||
|
public GsonJsonObject(com.google.gson.JsonObject elem){ |
||||||
|
element = elem; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public List<JsonElement> getProperties() throws JsonException { |
||||||
|
List<JsonElement> out = new ArrayList<JsonElement>(); |
||||||
|
for(Entry o: element.entrySet()){ |
||||||
|
out.add(GsonUtil.convertUp((com.google.gson.JsonElement)o.getValue(),new ParentReference(this,o.getKey().toString()))); |
||||||
|
} |
||||||
|
return out; |
||||||
|
} |
||||||
|
|
||||||
|
public boolean hasProperty(String pathFragment) { |
||||||
|
return element.has(pathFragment); |
||||||
|
} |
||||||
|
|
||||||
|
public JsonElement getProperty(String pathFragment) throws JsonException { |
||||||
|
return GsonUtil.convertUp(element.get(pathFragment),new ParentReference(this,pathFragment)); |
||||||
|
} |
||||||
|
|
||||||
|
public boolean isContainer() { |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
public boolean isJsonArray() { |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
public boolean isJsonObject() { |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
public JsonArray toJsonArray() throws JsonException { |
||||||
|
throw new JsonException(); |
||||||
|
} |
||||||
|
|
||||||
|
public com.jayway.jsonpath.json.JsonObject toJsonObject() { |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
public void clear() { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public boolean containsKey(Object arg0) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
} |
||||||
|
|
||||||
|
public boolean containsValue(Object arg0) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public Set<java.util.Map.Entry<String, JsonElement>> entrySet() { |
||||||
|
Set<Entry<String,JsonElement>> newSet = new HashSet<Entry<String,JsonElement>>(); |
||||||
|
for(Entry o: element.entrySet()){ |
||||||
|
JsonElement e; |
||||||
|
try { |
||||||
|
e = GsonUtil.convertUp((com.google.gson.JsonElement)o.getValue(),new ParentReference(this,o.getKey().toString())); |
||||||
|
} catch (JsonException e1) { |
||||||
|
throw new RuntimeException(); |
||||||
|
} |
||||||
|
newSet.add(new java.util.AbstractMap.SimpleEntry( o.getKey(),e)); |
||||||
|
} |
||||||
|
return newSet; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public JsonElement get(Object arg0) { |
||||||
|
try { |
||||||
|
return GsonUtil.convertUp(element.get(arg0.toString()), new ParentReference(this,arg0.toString())); |
||||||
|
} catch (JsonException e) { |
||||||
|
throw new RuntimeException(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public boolean isEmpty() { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public Set<String> keySet() { |
||||||
|
Set<String> newSet = new HashSet<String>(); |
||||||
|
for(Entry o: element.entrySet()){ |
||||||
|
newSet.add(o.getKey().toString()); |
||||||
|
} |
||||||
|
return newSet; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public JsonElement put(String arg0, JsonElement arg1) { |
||||||
|
this.element.add(arg0,(com.google.gson.JsonElement)arg1.getWrappedElement()); |
||||||
|
return arg1; |
||||||
|
} |
||||||
|
|
||||||
|
public void putAll(Map<? extends String, ? extends JsonElement> arg0) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public JsonElement remove(Object arg0) { |
||||||
|
try { |
||||||
|
return GsonUtil.convertUp(this.element.remove(arg0.toString())); |
||||||
|
} catch (JsonException e) { |
||||||
|
throw new RuntimeException(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public int size() { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public Collection<JsonElement> values() { |
||||||
|
ArrayList<JsonElement> newSet = new ArrayList<JsonElement>(); |
||||||
|
for(Entry o: element.entrySet()){ |
||||||
|
JsonElement e; |
||||||
|
try { |
||||||
|
e = GsonUtil.convertUp((com.google.gson.JsonElement)o.getValue(),new ParentReference(this,o.getKey().toString())); |
||||||
|
} catch (JsonException e1) { |
||||||
|
throw new RuntimeException(); |
||||||
|
} |
||||||
|
newSet.add(e); |
||||||
|
} |
||||||
|
return newSet; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public JsonPrimitive toPrimitive() throws JsonException { |
||||||
|
throw new JsonException(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public Object getWrappedElement() { |
||||||
|
return element; |
||||||
|
} |
||||||
|
|
||||||
|
public String toString(){ |
||||||
|
return element.toString(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public void setProperty(String name, JsonElement element) |
||||||
|
throws JsonException { |
||||||
|
this.put(name, element); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isJsonPrimitive() { |
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public Object toObject() throws JsonException { |
||||||
|
return element; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isJsonNull() { |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public void merge(JsonObject inputObjectb) throws JsonException { |
||||||
|
for(Entry<String,JsonElement> j :inputObjectb.entrySet()){ |
||||||
|
this.setProperty(j.getKey(), j.getValue()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public void merge(JsonElement inputObjectb) throws JsonException { |
||||||
|
if(inputObjectb.isJsonObject()){ |
||||||
|
this.merge(inputObjectb.toJsonObject()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
@ -0,0 +1,23 @@ |
|||||||
|
package com.jayway.jsonpath.json.gson; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
import net.minidev.json.parser.JSONParser; |
||||||
|
|
||||||
|
import com.jayway.jsonpath.json.JsonElement; |
||||||
|
import com.jayway.jsonpath.json.JsonException; |
||||||
|
import com.jayway.jsonpath.json.ParseException; |
||||||
|
|
||||||
|
public class GsonJsonParser implements com.jayway.jsonpath.json.JsonParser { |
||||||
|
|
||||||
|
private static com.google.gson.JsonParser JSON_PARSER = new com.google.gson.JsonParser(); |
||||||
|
|
||||||
|
public JsonElement parse(String json) throws ParseException,IOException { |
||||||
|
try { |
||||||
|
return GsonUtil.convertUp(JSON_PARSER.parse(json)); |
||||||
|
} catch (JsonException e) { |
||||||
|
throw new ParseException(e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,132 @@ |
|||||||
|
package com.jayway.jsonpath.json.gson; |
||||||
|
|
||||||
|
|
||||||
|
import javax.swing.text.Element; |
||||||
|
|
||||||
|
import net.minidev.json.JSONValue; |
||||||
|
|
||||||
|
import com.jayway.jsonpath.json.JsonArray; |
||||||
|
|
||||||
|
import com.jayway.jsonpath.json.JsonElement; |
||||||
|
import com.jayway.jsonpath.json.JsonException; |
||||||
|
import com.jayway.jsonpath.json.JsonObject; |
||||||
|
import com.jayway.jsonpath.json.JsonPrimitive; |
||||||
|
|
||||||
|
|
||||||
|
public class GsonJsonPrimitive extends com.jayway.jsonpath.json.JsonPrimitive{ |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private com.google.gson.JsonPrimitive value; |
||||||
|
|
||||||
|
public GsonJsonPrimitive( Object obj) { |
||||||
|
value = unwrapit(obj); |
||||||
|
} |
||||||
|
|
||||||
|
public GsonJsonPrimitive( com.google.gson.JsonPrimitive obj) { |
||||||
|
value = obj; |
||||||
|
} |
||||||
|
|
||||||
|
public boolean isContainer() { |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
public boolean isJsonArray() { |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
public boolean isJsonObject() { |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
public JsonArray toJsonArray() throws JsonException { |
||||||
|
throw new JsonException(); |
||||||
|
} |
||||||
|
|
||||||
|
public JsonObject toJsonObject() throws JsonException { |
||||||
|
throw new JsonException(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public JsonPrimitive toPrimitive() { |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
public Object wrappit(com.google.gson.JsonPrimitive prim){ |
||||||
|
if(prim == null ) return null; |
||||||
|
if(prim.isBoolean()){ |
||||||
|
return new Boolean(prim.getAsBoolean()); |
||||||
|
} |
||||||
|
if(prim.isString()){ |
||||||
|
return new String(prim.getAsString()); |
||||||
|
} |
||||||
|
if(prim.isNumber()){ |
||||||
|
if(prim.getAsDouble()==prim.getAsInt()) |
||||||
|
return new Integer(prim.getAsInt()); |
||||||
|
else |
||||||
|
return new Double(prim.getAsDouble()); |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
public com.google.gson.JsonPrimitive unwrapit(Object o){ |
||||||
|
if(o == null ) return null; |
||||||
|
if(o instanceof Boolean ){ |
||||||
|
return new com.google.gson.JsonPrimitive((Boolean)o); |
||||||
|
} |
||||||
|
if(o instanceof String ){ |
||||||
|
return new com.google.gson.JsonPrimitive((String)o); |
||||||
|
} |
||||||
|
if(o instanceof Number ){ |
||||||
|
return new com.google.gson.JsonPrimitive((Number)o); |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public Object getWrappedElement() { |
||||||
|
return value; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean equals(Object o1){ |
||||||
|
return (o1!=null) && o1.equals(wrappit(value)); |
||||||
|
} |
||||||
|
@Override |
||||||
|
public int hashCode(){ |
||||||
|
return wrappit(value).hashCode(); |
||||||
|
} |
||||||
|
@Override |
||||||
|
public String toString(){ |
||||||
|
return wrappit(value).toString(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isJsonPrimitive() { |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Object toObject() throws JsonException { |
||||||
|
return wrappit(value); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isJsonNull() { |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void merge(JsonElement o) throws JsonException { |
||||||
|
throw new JsonException("Can't merge a primitive"); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,82 @@ |
|||||||
|
package com.jayway.jsonpath.json.gson; |
||||||
|
|
||||||
|
import com.google.gson.JsonNull; |
||||||
|
|
||||||
|
import com.jayway.jsonpath.json.JsonArray; |
||||||
|
import com.jayway.jsonpath.json.JsonElement; |
||||||
|
import com.jayway.jsonpath.json.JsonException; |
||||||
|
import com.jayway.jsonpath.json.JsonFactory; |
||||||
|
import com.jayway.jsonpath.json.JsonObject; |
||||||
|
import com.jayway.jsonpath.json.ParentReference; |
||||||
|
|
||||||
|
public class GsonUtil { |
||||||
|
|
||||||
|
private static final GsonJsonFactory CREATE_JSON_ARRAY = new GsonJsonFactory(); |
||||||
|
|
||||||
|
public static JsonElement convertUp(com.google.gson.JsonElement object, |
||||||
|
ParentReference parent) throws JsonException { |
||||||
|
JsonElement je = convertUp(object); |
||||||
|
je.setParentReference(parent); |
||||||
|
|
||||||
|
return je; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public static JsonElement convertUp(com.google.gson.JsonElement object) |
||||||
|
throws JsonException { |
||||||
|
if (object.isJsonArray()) |
||||||
|
return new GsonJsonArray((com.google.gson.JsonArray) object); |
||||||
|
else if (object.isJsonObject()) |
||||||
|
return new GsonJsonObject((com.google.gson.JsonObject) object); |
||||||
|
else if (object.isJsonPrimitive()) |
||||||
|
return new GsonJsonPrimitive(object.getAsJsonPrimitive()); |
||||||
|
else |
||||||
|
return new GsonJsonPrimitive(null); |
||||||
|
} |
||||||
|
|
||||||
|
public static GsonJsonArray removeGsonArrayElement(int index, |
||||||
|
GsonJsonArray jsonArray) throws JsonException { |
||||||
|
return setGsonArrayElement(index, null, jsonArray); |
||||||
|
} |
||||||
|
|
||||||
|
public static GsonJsonArray setGsonArrayElement(int index, |
||||||
|
JsonElement payload, GsonJsonArray jsonArray) throws JsonException { |
||||||
|
|
||||||
|
JsonElement arrayParent = jsonArray.getParentReference().getParent(); |
||||||
|
if (arrayParent == null) { |
||||||
|
return modifyArray(index, payload, jsonArray); |
||||||
|
} |
||||||
|
if (!arrayParent.isContainer()) |
||||||
|
throw new JsonException(); // WTF?!
|
||||||
|
|
||||||
|
GsonJsonArray ja = modifyArray(index, payload, jsonArray); |
||||||
|
|
||||||
|
if (arrayParent.isJsonArray()) { |
||||||
|
int i = ja.getParentReference().getIndex(); |
||||||
|
return setGsonArrayElement(i, ja, (GsonJsonArray) arrayParent |
||||||
|
.toJsonArray()); |
||||||
|
} else { // it's an object
|
||||||
|
JsonObject obj = arrayParent.toJsonObject(); |
||||||
|
obj.put(jsonArray.getParentReference().getField(), ja); |
||||||
|
|
||||||
|
return ja; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
private static GsonJsonArray modifyArray(int index, JsonElement payload, |
||||||
|
GsonJsonArray jsonArray) { |
||||||
|
GsonJsonArray out = CREATE_JSON_ARRAY.createJsonArray(); |
||||||
|
for (int i = 0; i < jsonArray.size(); i++) { |
||||||
|
if (index == i) { |
||||||
|
if (payload != null) |
||||||
|
out.add(payload); |
||||||
|
} else { |
||||||
|
out.add(jsonArray.get(i)); |
||||||
|
} |
||||||
|
} |
||||||
|
out.setParentReference(jsonArray.getParentReference()); |
||||||
|
return out; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,267 @@ |
|||||||
|
package com.jayway.jsonpath.json.minidev; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.util.Collection; |
||||||
|
import java.util.Iterator; |
||||||
|
import java.util.List; |
||||||
|
import java.util.ListIterator; |
||||||
|
|
||||||
|
import net.minidev.json.JSONArray; |
||||||
|
import net.minidev.json.JSONStyle; |
||||||
|
|
||||||
|
|
||||||
|
import com.jayway.jsonpath.json.JsonArray; |
||||||
|
import com.jayway.jsonpath.json.JsonElement; |
||||||
|
import com.jayway.jsonpath.json.JsonException; |
||||||
|
import com.jayway.jsonpath.json.JsonObject; |
||||||
|
import com.jayway.jsonpath.json.JsonPrimitive; |
||||||
|
import com.jayway.jsonpath.json.ParentReference; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public class MiniJsonArray extends com.jayway.jsonpath.json.JsonArray { |
||||||
|
|
||||||
|
|
||||||
|
public JSONArray element; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public Object clone() { |
||||||
|
return element.clone(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void ensureCapacity(int minCapacity) { |
||||||
|
element.ensureCapacity(minCapacity); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public boolean equals(Object o) { |
||||||
|
return element.equals(o); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public int hashCode() { |
||||||
|
return element.hashCode(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void merge(Object o2) { |
||||||
|
element.merge(o2); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public MiniJsonArray(JSONArray elem){ |
||||||
|
this.element = elem; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public boolean add(com.jayway.jsonpath.json.JsonElement item) { |
||||||
|
return element.add(item.getWrappedElement()); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public void add(int index, com.jayway.jsonpath.json.JsonElement element) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
public boolean addAll( |
||||||
|
Collection<? extends com.jayway.jsonpath.json.JsonElement> c) { |
||||||
|
boolean ok = true; |
||||||
|
for(com.jayway.jsonpath.json.JsonElement e : c ){ |
||||||
|
ok &= element.add(e.getWrappedElement()); |
||||||
|
} |
||||||
|
return ok; |
||||||
|
} |
||||||
|
public boolean addAll(int index, |
||||||
|
Collection<? extends com.jayway.jsonpath.json.JsonElement> c) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
} |
||||||
|
public void clear() { |
||||||
|
element.clear(); |
||||||
|
} |
||||||
|
public boolean contains(Object o) { |
||||||
|
return element.contains(o); |
||||||
|
} |
||||||
|
public boolean containsAll(Collection<?> c) { |
||||||
|
return element.containsAll(c); |
||||||
|
} |
||||||
|
public com.jayway.jsonpath.json.JsonElement get(int index) { |
||||||
|
try { |
||||||
|
return MiniUtil.convertUp(element.get(index), new ParentReference(this,index)); |
||||||
|
} catch (JsonException e) { |
||||||
|
return null; |
||||||
|
|
||||||
|
} |
||||||
|
} |
||||||
|
public int indexOf(Object o) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
} |
||||||
|
public boolean isEmpty() { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
public Iterator<com.jayway.jsonpath.json.JsonElement> iterator() { |
||||||
|
final MiniJsonArray ja = this; |
||||||
|
return new Iterator<com.jayway.jsonpath.json.JsonElement>() { |
||||||
|
Iterator<Object> inner; |
||||||
|
int count; |
||||||
|
{ |
||||||
|
count = 0; |
||||||
|
inner = element.iterator(); |
||||||
|
} |
||||||
|
public boolean hasNext() { |
||||||
|
return inner.hasNext(); |
||||||
|
} |
||||||
|
|
||||||
|
public com.jayway.jsonpath.json.JsonElement next() { |
||||||
|
Object next = inner.next(); |
||||||
|
|
||||||
|
|
||||||
|
try { |
||||||
|
return MiniUtil.convertUp(next, new ParentReference(ja,count++)); |
||||||
|
} catch (JsonException e) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public void remove() { |
||||||
|
inner.remove(); |
||||||
|
|
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
} |
||||||
|
public int lastIndexOf(Object o) { |
||||||
|
return element.lastIndexOf(o); |
||||||
|
|
||||||
|
} |
||||||
|
public ListIterator<com.jayway.jsonpath.json.JsonElement> listIterator() { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
public ListIterator<com.jayway.jsonpath.json.JsonElement> listIterator( |
||||||
|
int index) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
public boolean remove(Object o) { |
||||||
|
return element.remove(o); |
||||||
|
|
||||||
|
} |
||||||
|
public com.jayway.jsonpath.json.JsonElement remove(int index) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
public boolean removeAll(Collection<?> c) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
public boolean retainAll(Collection<?> c) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public int size() { |
||||||
|
return element.size(); |
||||||
|
|
||||||
|
} |
||||||
|
public List<com.jayway.jsonpath.json.JsonElement> subList(int fromIndex, |
||||||
|
int toIndex) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
public Object[] toArray() { |
||||||
|
|
||||||
|
return element.toArray(); |
||||||
|
} |
||||||
|
public <T> T[] toArray(T[] a) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
public boolean isContainer() { |
||||||
|
return true; |
||||||
|
} |
||||||
|
public boolean isJsonArray() { |
||||||
|
return true; |
||||||
|
} |
||||||
|
public boolean isJsonObject() { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
public com.jayway.jsonpath.json.JsonArray toJsonArray() { |
||||||
|
return this; |
||||||
|
} |
||||||
|
public JsonObject toJsonObject() throws JsonException { |
||||||
|
throw new JsonException(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public JsonPrimitive toPrimitive() throws JsonException { |
||||||
|
|
||||||
|
throw new JsonException(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public Object getWrappedElement() { |
||||||
|
return element; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isJsonPrimitive() { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public Object toObject() throws JsonException { |
||||||
|
return element; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isJsonNull() { |
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public JsonElement set(int index, JsonElement element) { |
||||||
|
this.element.set(index, element); |
||||||
|
return element; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public void merge(JsonElement o) throws JsonException { |
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,44 @@ |
|||||||
|
package com.jayway.jsonpath.json.minidev; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
import com.jayway.jsonpath.json.JsonElement; |
||||||
|
import com.jayway.jsonpath.json.JsonNull; |
||||||
|
import com.jayway.jsonpath.json.JsonObject; |
||||||
|
import com.jayway.jsonpath.json.JsonPrimitive; |
||||||
|
import com.jayway.jsonpath.json.ParseException; |
||||||
|
|
||||||
|
import net.minidev.json.JSONArray; |
||||||
|
import net.minidev.json.JSONObject; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public class MiniJsonFactory extends com.jayway.jsonpath.json.JsonFactory{ |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public MiniJsonArray createJsonArray() { |
||||||
|
return new MiniJsonArray(new JSONArray()); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public JsonElement parse(String json) throws ParseException, IOException { |
||||||
|
return new MiniJsonParser().parse(json); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public JsonObject createJsonObject() { |
||||||
|
return new com.jayway.jsonpath.json.minidev.MiniJsonObject(new JSONObject()); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public JsonPrimitive createJsonPrimitive(Object obj) { |
||||||
|
return new MiniJsonPrimitive(obj); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,185 @@ |
|||||||
|
package com.jayway.jsonpath.json.minidev; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.Collection; |
||||||
|
import java.util.Iterator; |
||||||
|
import java.util.List; |
||||||
|
import java.util.ListIterator; |
||||||
|
import java.util.Map; |
||||||
|
import java.util.Set; |
||||||
|
|
||||||
|
import net.minidev.json.JSONArray; |
||||||
|
import net.minidev.json.JSONObject; |
||||||
|
|
||||||
|
import com.jayway.jsonpath.json.JsonArray; |
||||||
|
import com.jayway.jsonpath.json.JsonElement; |
||||||
|
import com.jayway.jsonpath.json.JsonException; |
||||||
|
import com.jayway.jsonpath.json.JsonObject; |
||||||
|
import com.jayway.jsonpath.json.JsonPrimitive; |
||||||
|
import com.jayway.jsonpath.json.ParentReference; |
||||||
|
|
||||||
|
|
||||||
|
public class MiniJsonObject extends com.jayway.jsonpath.json.JsonObject{ |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public JSONObject element; |
||||||
|
public MiniJsonObject(JSONObject elem){ |
||||||
|
element = elem; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public List<JsonElement> getProperties() throws JsonException { |
||||||
|
List<JsonElement> out = new ArrayList<JsonElement>(); |
||||||
|
for(String s: element.keySet()){ |
||||||
|
out.add(MiniUtil.convertUp(element.get(s), new ParentReference(this,s))); |
||||||
|
} |
||||||
|
return out; |
||||||
|
} |
||||||
|
|
||||||
|
public boolean hasProperty(String pathFragment) { |
||||||
|
return element.containsKey(pathFragment); |
||||||
|
} |
||||||
|
|
||||||
|
public JsonElement getProperty(String pathFragment) throws JsonException { |
||||||
|
return MiniUtil.convertUp(element.get(pathFragment), new ParentReference(this,pathFragment)); |
||||||
|
} |
||||||
|
|
||||||
|
public boolean isContainer() { |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
public boolean isJsonArray() { |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
public boolean isJsonObject() { |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
public JsonArray toJsonArray() throws JsonException { |
||||||
|
throw new JsonException(); |
||||||
|
} |
||||||
|
|
||||||
|
public com.jayway.jsonpath.json.JsonObject toJsonObject() { |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
public void clear() { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public boolean containsKey(Object arg0) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
} |
||||||
|
|
||||||
|
public boolean containsValue(Object arg0) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public Set<java.util.Map.Entry<String, JsonElement>> entrySet() { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public JsonElement get(Object arg0) { |
||||||
|
try { |
||||||
|
return MiniUtil.convertUp(element.get(arg0), new ParentReference(this,arg0.toString())); |
||||||
|
} catch (JsonException e) { |
||||||
|
throw new RuntimeException(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public boolean isEmpty() { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public Set<String> keySet() { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public JsonElement put(String arg0, JsonElement arg1) { |
||||||
|
this.element.put(arg0, arg1.getWrappedElement()); |
||||||
|
return arg1; |
||||||
|
} |
||||||
|
|
||||||
|
public void putAll(Map<? extends String, ? extends JsonElement> arg0) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public JsonElement remove(Object arg0) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public int size() { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public Collection<JsonElement> values() { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public JsonPrimitive toPrimitive() throws JsonException { |
||||||
|
throw new JsonException(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public Object getWrappedElement() { |
||||||
|
return element; |
||||||
|
} |
||||||
|
|
||||||
|
public String toString(){ |
||||||
|
return element.toString(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public void setProperty(String name,JsonElement element) throws JsonException { |
||||||
|
this.put(name, element); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isJsonPrimitive() { |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public Object toObject() throws JsonException { |
||||||
|
return element; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isJsonNull() { |
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public void merge(JsonElement o) throws JsonException { |
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,27 @@ |
|||||||
|
package com.jayway.jsonpath.json.minidev; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
import net.minidev.json.parser.JSONParser; |
||||||
|
|
||||||
|
import com.jayway.jsonpath.json.JsonElement; |
||||||
|
import com.jayway.jsonpath.json.JsonException; |
||||||
|
import com.jayway.jsonpath.json.ParseException; |
||||||
|
|
||||||
|
public class MiniJsonParser implements com.jayway.jsonpath.json.JsonParser { |
||||||
|
|
||||||
|
private static JSONParser JSON_PARSER = new JSONParser(JSONParser.MODE_PERMISSIF); |
||||||
|
|
||||||
|
public JsonElement parse(String json) throws ParseException,IOException { |
||||||
|
try { |
||||||
|
return MiniUtil.convertUp(JSON_PARSER.parse(json)); |
||||||
|
|
||||||
|
} catch (JsonException e) { |
||||||
|
throw new ParseException(e); |
||||||
|
} |
||||||
|
catch (net.minidev.json.parser.ParseException e) { |
||||||
|
throw new ParseException(e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,90 @@ |
|||||||
|
package com.jayway.jsonpath.json.minidev; |
||||||
|
|
||||||
|
|
||||||
|
import javax.swing.text.Element; |
||||||
|
|
||||||
|
import net.minidev.json.JSONValue; |
||||||
|
|
||||||
|
import com.jayway.jsonpath.json.JsonArray; |
||||||
|
|
||||||
|
import com.jayway.jsonpath.json.JsonElement; |
||||||
|
import com.jayway.jsonpath.json.JsonException; |
||||||
|
import com.jayway.jsonpath.json.JsonObject; |
||||||
|
import com.jayway.jsonpath.json.JsonPrimitive; |
||||||
|
|
||||||
|
|
||||||
|
public class MiniJsonPrimitive extends com.jayway.jsonpath.json.JsonPrimitive{ |
||||||
|
|
||||||
|
|
||||||
|
private Object value; |
||||||
|
|
||||||
|
public MiniJsonPrimitive(Object object) { |
||||||
|
value = object; |
||||||
|
} |
||||||
|
|
||||||
|
public boolean isContainer() { |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
public boolean isJsonArray() { |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
public boolean isJsonObject() { |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
public JsonArray toJsonArray() throws JsonException { |
||||||
|
throw new JsonException(); |
||||||
|
} |
||||||
|
|
||||||
|
public JsonObject toJsonObject() throws JsonException { |
||||||
|
throw new JsonException(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public JsonPrimitive toPrimitive() { |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
public Object getWrappedElement() { |
||||||
|
return value; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isJsonPrimitive() { |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Object toObject() throws JsonException { |
||||||
|
return this.value; |
||||||
|
} |
||||||
|
@Override |
||||||
|
public String toString() { |
||||||
|
return this.value.toString(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isJsonNull() { |
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void merge(JsonElement o) throws JsonException { |
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public boolean equals(Object o1){ |
||||||
|
if(o1== null)return false; |
||||||
|
return (this.getWrappedElement().toString().equals(o1.toString())); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,28 @@ |
|||||||
|
package com.jayway.jsonpath.json.minidev; |
||||||
|
|
||||||
|
import com.jayway.jsonpath.json.JsonElement; |
||||||
|
import com.jayway.jsonpath.json.JsonException; |
||||||
|
import com.jayway.jsonpath.json.ParentReference; |
||||||
|
|
||||||
|
public class MiniUtil { |
||||||
|
|
||||||
|
public static JsonElement convertUp(Object object) throws JsonException { |
||||||
|
if(object instanceof net.minidev.json.JSONArray) |
||||||
|
return new MiniJsonArray( (net.minidev.json.JSONArray) object ); |
||||||
|
else if(object instanceof net.minidev.json.JSONObject) |
||||||
|
return new MiniJsonObject( (net.minidev.json.JSONObject) object ); |
||||||
|
else |
||||||
|
return new MiniJsonPrimitive( object ); |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
|
public static JsonElement convertUp(Object o,ParentReference pr) throws JsonException { |
||||||
|
JsonElement j = convertUp(o); |
||||||
|
j.setParentReference(pr); |
||||||
|
return j; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue