diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/JsonArray.java b/json-path/src/main/java/com/jayway/jsonpath/json/JsonArray.java new file mode 100755 index 00000000..b7c160f6 --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/json/JsonArray.java @@ -0,0 +1,9 @@ +package com.jayway.jsonpath.json; + + + +public abstract class JsonArray extends JsonElement implements java.util.List { + + + +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/JsonElement.java b/json-path/src/main/java/com/jayway/jsonpath/json/JsonElement.java new file mode 100755 index 00000000..a8e3f478 --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/json/JsonElement.java @@ -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; + + + + +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/JsonException.java b/json-path/src/main/java/com/jayway/jsonpath/json/JsonException.java new file mode 100755 index 00000000..42fd67d8 --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/json/JsonException.java @@ -0,0 +1,12 @@ +package com.jayway.jsonpath.json; + +public class JsonException extends Exception { + + public JsonException(String string) { + super(string); + } + public JsonException() { + super(); + } + +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/JsonFactory.java b/json-path/src/main/java/com/jayway/jsonpath/json/JsonFactory.java new file mode 100755 index 00000000..fa0d1b66 --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/json/JsonFactory.java @@ -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); + + + + + + + + + +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/JsonNull.java b/json-path/src/main/java/com/jayway/jsonpath/json/JsonNull.java new file mode 100755 index 00000000..2c04c472 --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/json/JsonNull.java @@ -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 + } + + + +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/JsonObject.java b/json-path/src/main/java/com/jayway/jsonpath/json/JsonObject.java new file mode 100755 index 00000000..3743598c --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/json/JsonObject.java @@ -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 { + + public abstract List 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; + + + +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/JsonParser.java b/json-path/src/main/java/com/jayway/jsonpath/json/JsonParser.java new file mode 100755 index 00000000..466477ab --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/json/JsonParser.java @@ -0,0 +1,9 @@ +package com.jayway.jsonpath.json; + +import java.io.IOException; + +public interface JsonParser { + + public JsonElement parse(String json) throws ParseException,IOException; + +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/JsonPathResultList.java b/json-path/src/main/java/com/jayway/jsonpath/json/JsonPathResultList.java new file mode 100755 index 00000000..019bb735 --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/json/JsonPathResultList.java @@ -0,0 +1,8 @@ +package com.jayway.jsonpath.json; + +import java.util.ArrayList; +import java.util.List; + +public class JsonPathResultList extends ArrayList{ + +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/JsonPrimitive.java b/json-path/src/main/java/com/jayway/jsonpath/json/JsonPrimitive.java new file mode 100755 index 00000000..993ccd53 --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/json/JsonPrimitive.java @@ -0,0 +1,5 @@ +package com.jayway.jsonpath.json; + +public abstract class JsonPrimitive extends JsonElement { + +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/ParentReference.java b/json-path/src/main/java/com/jayway/jsonpath/json/ParentReference.java new file mode 100755 index 00000000..d585d59c --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/json/ParentReference.java @@ -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; + } +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/ParseException.java b/json-path/src/main/java/com/jayway/jsonpath/json/ParseException.java new file mode 100755 index 00000000..a75d6bd4 --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/json/ParseException.java @@ -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); + } + +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonJsonArray.java b/json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonJsonArray.java new file mode 100755 index 00000000..58bc6062 --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonJsonArray.java @@ -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 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 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 iterator() { + final GsonJsonArray me = this; + return new Iterator() { + Iterator 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 listIterator() { + throw new UnsupportedOperationException(); + + } + public ListIterator 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 subList(int fromIndex, + int toIndex) { + throw new UnsupportedOperationException(); + + } + public Object[] toArray() { + throw new UnsupportedOperationException(); + } + public 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); + } + } + + +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonJsonFactory.java b/json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonJsonFactory.java new file mode 100755 index 00000000..90260be3 --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonJsonFactory.java @@ -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); + } + + + + + + +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonJsonObject.java b/json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonJsonObject.java new file mode 100755 index 00000000..b1f0d96a --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonJsonObject.java @@ -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 getProperties() throws JsonException { + List out = new ArrayList(); + 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> entrySet() { + Set> newSet = new HashSet>(); + 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 keySet() { + Set newSet = new HashSet(); + 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 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 values() { + ArrayList newSet = new ArrayList(); + 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 j :inputObjectb.entrySet()){ + this.setProperty(j.getKey(), j.getValue()); + } + } + + + @Override + public void merge(JsonElement inputObjectb) throws JsonException { + if(inputObjectb.isJsonObject()){ + this.merge(inputObjectb.toJsonObject()); + } + } + + +} + + diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonJsonParser.java b/json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonJsonParser.java new file mode 100755 index 00000000..a7d544f9 --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonJsonParser.java @@ -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); + } + } + +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonJsonPrimitive.java b/json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonJsonPrimitive.java new file mode 100755 index 00000000..8199c387 --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonJsonPrimitive.java @@ -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"); + } + + + + + +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonUtil.java b/json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonUtil.java new file mode 100755 index 00000000..f1137116 --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonUtil.java @@ -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; + } + +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/minidev/MiniJsonArray.java b/json-path/src/main/java/com/jayway/jsonpath/json/minidev/MiniJsonArray.java new file mode 100755 index 00000000..94f5d72e --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/json/minidev/MiniJsonArray.java @@ -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 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 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 iterator() { + final MiniJsonArray ja = this; + return new Iterator() { + Iterator 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 listIterator() { + throw new UnsupportedOperationException(); + + } + public ListIterator 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 subList(int fromIndex, + int toIndex) { + throw new UnsupportedOperationException(); + + } + public Object[] toArray() { + + return element.toArray(); + } + public 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 + + } + + + + + + + +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/minidev/MiniJsonFactory.java b/json-path/src/main/java/com/jayway/jsonpath/json/minidev/MiniJsonFactory.java new file mode 100755 index 00000000..d9d9f83c --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/json/minidev/MiniJsonFactory.java @@ -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); + + } + + + +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/minidev/MiniJsonObject.java b/json-path/src/main/java/com/jayway/jsonpath/json/minidev/MiniJsonObject.java new file mode 100755 index 00000000..0a8e1281 --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/json/minidev/MiniJsonObject.java @@ -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 getProperties() throws JsonException { + List out = new ArrayList(); + 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> 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 keySet() { + throw new UnsupportedOperationException(); + + } + + public JsonElement put(String arg0, JsonElement arg1) { + this.element.put(arg0, arg1.getWrappedElement()); + return arg1; + } + + public void putAll(Map arg0) { + throw new UnsupportedOperationException(); + + } + + public JsonElement remove(Object arg0) { + throw new UnsupportedOperationException(); + + } + + public int size() { + throw new UnsupportedOperationException(); + + } + + public Collection 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 + + } + + + + + + + +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/minidev/MiniJsonParser.java b/json-path/src/main/java/com/jayway/jsonpath/json/minidev/MiniJsonParser.java new file mode 100755 index 00000000..e6ece907 --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/json/minidev/MiniJsonParser.java @@ -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); + } + } + +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/minidev/MiniJsonPrimitive.java b/json-path/src/main/java/com/jayway/jsonpath/json/minidev/MiniJsonPrimitive.java new file mode 100755 index 00000000..2cba1ab5 --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/json/minidev/MiniJsonPrimitive.java @@ -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())); + + } + + +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/minidev/MiniUtil.java b/json-path/src/main/java/com/jayway/jsonpath/json/minidev/MiniUtil.java new file mode 100755 index 00000000..97f3a5dd --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/json/minidev/MiniUtil.java @@ -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; + } + + + + +}