Browse Source

Updated GsonJsonProvider using also a customized Gson object.

pull/190/head
Spyridon Papadopoulos 9 years ago
parent
commit
18ff41336f
  1. 112
      json-path/src/main/java/com/jayway/jsonpath/spi/json/GsonJsonProvider.java

112
json-path/src/main/java/com/jayway/jsonpath/spi/json/GsonJsonProvider.java

@ -14,37 +14,55 @@
*/ */
package com.jayway.jsonpath.spi.json; package com.jayway.jsonpath.spi.json;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive; import com.google.gson.JsonPrimitive;
import com.google.gson.internal.LazilyParsedNumber; import com.google.gson.internal.LazilyParsedNumber;
import com.jayway.jsonpath.InvalidJsonException; import com.jayway.jsonpath.InvalidJsonException;
import com.jayway.jsonpath.JsonPathException; import com.jayway.jsonpath.JsonPathException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public class GsonJsonProvider extends AbstractJsonProvider { public class GsonJsonProvider extends AbstractJsonProvider {
private static final JsonParser parser = new JsonParser(); private static final JsonParser PARSER = new JsonParser();
private static final Gson gson = new GsonBuilder().create(); private final Gson gson;
/**
* Initializes the {@code GsonJsonProvider} using the default {@link Gson} object.
*/
public GsonJsonProvider() {
this(new Gson());
}
/**
* Initializes the {@code GsonJsonProvider} using a customized {@link Gson} object.
*
* @param gson the customized Gson object.
*/
public GsonJsonProvider(final Gson gson) {
this.gson = gson;
}
public Object unwrap(Object o) { public Object unwrap(final Object o) {
if (o == null) { if (o == null) {
return null; return null;
} }
if (!(o instanceof JsonElement)) { if (!(o instanceof JsonElement)) {
return o; return o;
} }
@ -64,10 +82,11 @@ public class GsonJsonProvider extends AbstractJsonProvider {
return unwrapNumber(p.getAsNumber()); return unwrapNumber(p.getAsNumber());
} }
} }
return o; return o;
} }
private static Number unwrapNumber(Number n) { private static Number unwrapNumber(final Number n) {
Number unwrapped; Number unwrapped;
if (n instanceof LazilyParsedNumber) { if (n instanceof LazilyParsedNumber) {
@ -85,27 +104,28 @@ public class GsonJsonProvider extends AbstractJsonProvider {
} else { } else {
unwrapped = n; unwrapped = n;
} }
return unwrapped; return unwrapped;
} }
@Override @Override
public Object parse(String json) throws InvalidJsonException { public Object parse(final String json) throws InvalidJsonException {
return parser.parse(json); return PARSER.parse(json);
} }
@Override @Override
public Object parse(InputStream jsonStream, String charset) throws InvalidJsonException { public Object parse(final InputStream jsonStream, final String charset) throws InvalidJsonException {
try { try {
return parser.parse(new InputStreamReader(jsonStream, charset)); return PARSER.parse(new InputStreamReader(jsonStream, charset));
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
throw new JsonPathException(e); throw new JsonPathException(e);
} }
} }
@Override @Override
public String toJson(Object obj) { public String toJson(final Object obj) {
return obj.toString(); return gson.toJson(obj);
} }
@Override @Override
@ -119,31 +139,31 @@ public class GsonJsonProvider extends AbstractJsonProvider {
} }
@Override @Override
public boolean isArray(Object obj) { public boolean isArray(final Object obj) {
return (obj instanceof JsonArray || obj instanceof List); return (obj instanceof JsonArray || obj instanceof List);
} }
@Override @Override
public Object getArrayIndex(Object obj, int idx) { public Object getArrayIndex(final Object obj, final int idx) {
return toJsonArray(obj).get(idx); return toJsonArray(obj).get(idx);
} }
@Override @Override
public void setArrayIndex(Object array, int index, Object newValue) { public void setArrayIndex(final Object array, final int index, final Object newValue) {
if (!isArray(array)) { if (!isArray(array)) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} else { } else {
JsonArray arr = toJsonArray(array); JsonArray arr = toJsonArray(array);
if (index == arr.size()){ if (index == arr.size()) {
arr.add(createJsonElement(newValue)); arr.add(createJsonElement(newValue));
}else { } else {
arr.set(index, createJsonElement(newValue)); arr.set(index, createJsonElement(newValue));
} }
} }
} }
@Override @Override
public Object getMapValue(Object obj, String key) { public Object getMapValue(final Object obj, final String key) {
JsonObject jsonObject = toJsonObject(obj); JsonObject jsonObject = toJsonObject(obj);
Object o = jsonObject.get(key); Object o = jsonObject.get(key);
if (!jsonObject.has(key)) { if (!jsonObject.has(key)) {
@ -154,10 +174,10 @@ public class GsonJsonProvider extends AbstractJsonProvider {
} }
@Override @Override
public void setProperty(Object obj, Object key, Object value) { public void setProperty(final Object obj, final Object key, final Object value) {
if (isMap(obj)) if (isMap(obj)) {
toJsonObject(obj).add(key.toString(), createJsonElement(value)); toJsonObject(obj).add(key.toString(), createJsonElement(value));
else { } else {
JsonArray array = toJsonArray(obj); JsonArray array = toJsonArray(obj);
int index; int index;
if (key != null) { if (key != null) {
@ -165,6 +185,7 @@ public class GsonJsonProvider extends AbstractJsonProvider {
} else { } else {
index = array.size(); index = array.size();
} }
if (index == array.size()) { if (index == array.size()) {
array.add(createJsonElement(value)); array.add(createJsonElement(value));
} else { } else {
@ -173,13 +194,11 @@ public class GsonJsonProvider extends AbstractJsonProvider {
} }
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void removeProperty(Object obj, Object key) { public void removeProperty(final Object obj, final Object key) {
if (isMap(obj)) if (isMap(obj)) {
toJsonObject(obj).remove(key.toString()); toJsonObject(obj).remove(key.toString());
else { } else {
JsonArray array = toJsonArray(obj); JsonArray array = toJsonArray(obj);
int index = key instanceof Integer ? (Integer) key : Integer.parseInt(key.toString()); int index = key instanceof Integer ? (Integer) key : Integer.parseInt(key.toString());
array.remove(index); array.remove(index);
@ -187,22 +206,24 @@ public class GsonJsonProvider extends AbstractJsonProvider {
} }
@Override @Override
public boolean isMap(Object obj) { public boolean isMap(final Object obj) {
//return (obj instanceof JsonObject || obj instanceof Map);
// return (obj instanceof JsonObject || obj instanceof Map);
return (obj instanceof JsonObject); return (obj instanceof JsonObject);
} }
@Override @Override
public Collection<String> getPropertyKeys(Object obj) { public Collection<String> getPropertyKeys(final Object obj) {
List<String> keys = new ArrayList<String>(); List<String> keys = new ArrayList<String>();
for (Map.Entry<String, JsonElement> entry : toJsonObject(obj).entrySet()) { for (Map.Entry<String, JsonElement> entry : toJsonObject(obj).entrySet()) {
keys.add(entry.getKey()); keys.add(entry.getKey());
} }
return keys; return keys;
} }
@Override @Override
public int length(Object obj) { public int length(final Object obj) {
if (isArray(obj)) { if (isArray(obj)) {
return toJsonArray(obj).size(); return toJsonArray(obj).size();
} else if (isMap(obj)) { } else if (isMap(obj)) {
@ -215,32 +236,35 @@ public class GsonJsonProvider extends AbstractJsonProvider {
} }
} }
} }
throw new JsonPathException("length operation can not applied to " + obj != null ? obj.getClass().getName() : "null");
throw new JsonPathException("length operation can not applied to " + obj != null ? obj.getClass().getName()
: "null");
} }
@Override @Override
public Iterable<?> toIterable(Object obj) { public Iterable<?> toIterable(final Object obj) {
JsonArray arr = toJsonArray(obj); JsonArray arr = toJsonArray(obj);
List<Object> values = new ArrayList<Object>(arr.size()); List<Object> values = new ArrayList<Object>(arr.size());
for (Object o : arr) { for (Object o : arr) {
values.add(unwrap(o)); values.add(unwrap(o));
} }
return values; return values;
} }
private JsonElement createJsonElement(Object o) { private JsonElement createJsonElement(final Object o) {
return gson.toJsonTree(o); return gson.toJsonTree(o);
} }
private JsonArray toJsonArray(Object o) { private JsonArray toJsonArray(final Object o) {
return (JsonArray) o; return (JsonArray) o;
} }
private JsonObject toJsonObject(Object o) { private JsonObject toJsonObject(final Object o) {
return (JsonObject) o; return (JsonObject) o;
} }
private JsonElement toJsonElement(Object o) { private JsonElement toJsonElement(final Object o) {
return (JsonElement) o; return (JsonElement) o;
} }
} }

Loading…
Cancel
Save