List of usage examples for com.google.gson JsonParseException JsonParseException
public JsonParseException(Throwable cause)
From source file:com.kurento.kmf.jsonrpcconnector.JsonUtils.java
License:Open Source License
@Override public Request<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!(json instanceof JsonObject)) { throw new JsonParseException( "Invalid JsonRpc request showning JsonElement type " + json.getClass().getSimpleName()); }/* w w w . j a v a 2s . c om*/ JsonObject jObject = (JsonObject) json; if (!jObject.has(JSON_RPC_PROPERTY)) { throw new JsonParseException( "Invalid JsonRpc request lacking version '" + JSON_RPC_PROPERTY + "' field"); } if (!jObject.get("jsonrpc").getAsString().equals(JSON_RPC_VERSION)) { throw new JsonParseException("Invalid JsonRpc version"); } if (!jObject.has(METHOD_PROPERTY)) { throw new JsonParseException("Invalid JsonRpc request lacking '" + METHOD_PROPERTY + "' field"); } Integer id = null; if (jObject.has(ID_PROPERTY)) { id = jObject.get(ID_PROPERTY).getAsInt(); } ParameterizedType parameterizedType = (ParameterizedType) typeOfT; return new Request<Object>(id, jObject.get(METHOD_PROPERTY).getAsString(), context.deserialize(jObject.get(PARAMS_PROPERTY), parameterizedType.getActualTypeArguments()[0])); }
From source file:com.kurento.kmf.jsonrpcconnector.JsonUtils.java
License:Open Source License
@Override public Props deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!(json instanceof JsonObject)) { throw new JsonParseException("Cannot convert " + json + " to Props object"); }//from w w w . ja v a 2 s. c om JsonObject jObject = (JsonObject) json; Props props = new Props(); for (Map.Entry<String, JsonElement> e : jObject.entrySet()) { Object value = deserialize(e.getValue(), context); props.add(e.getKey(), value); } return props; }
From source file:com.learn.mobile.library.dmobi.helper.RuntimeTypeAdapterFactory.java
License:Apache License
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (type.getRawType() != baseType) { return null; }/*from w ww .j av a 2 s. co m*/ final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<String, TypeAdapter<?>>(); final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<Class<?>, TypeAdapter<?>>(); for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) { TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue())); labelToDelegate.put(entry.getKey(), delegate); subtypeToDelegate.put(entry.getValue(), delegate); } return new TypeAdapter<R>() { @Override public R read(JsonReader in) throws IOException { JsonElement jsonElement = Streams.parse(in); // JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName); // Do not remove typeFieldName JsonElement labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName); if (labelJsonElement == null) { throw new JsonParseException("cannot deserialize " + baseType + " because it does not define a field named " + typeFieldName); } String label = labelJsonElement.getAsString(); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label); if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?"); } return delegate.fromJsonTree(jsonElement); } @Override public void write(JsonWriter out, R value) throws IOException { Class<?> srcType = value.getClass(); String label = subtypeToLabel.get(srcType); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType); if (delegate == null) { throw new JsonParseException( "cannot serialize " + srcType.getName() + "; did you forget to register a subtype?"); } JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject(); if (jsonObject.has(typeFieldName)) { throw new JsonParseException("cannot serialize " + srcType.getName() + " because it already defines a field named " + typeFieldName); } JsonObject clone = new JsonObject(); clone.add(typeFieldName, new JsonPrimitive(label)); for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) { clone.add(e.getKey(), e.getValue()); } Streams.write(clone, out); } }.nullSafe(); }
From source file:com.liferay.mobile.sdk.json.ActionDeserializer.java
License:Open Source License
@Override public Action deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject root = json.getAsJsonObject(); String path = root.get("path").getAsString(); String response = root.getAsJsonObject("returns").get("type").getAsString(); String serviceClassName = null; String methodName = null;//from w ww . ja v a 2 s . c o m String name = root.get("name").getAsString(); String[] values = name.split("#"); if (values.length == 2) { serviceClassName = values[0]; methodName = values[1]; } JsonArray jsonArray = root.getAsJsonArray("parameters").getAsJsonArray(); try { ArrayList<Parameter> parameters = JSONParser.fromJSON(jsonArray, new GenericListType<>(Parameter.class)); return new Action(serviceClassName, methodName, path, response, parameters); } catch (Exception e) { throw new JsonParseException(e); } }
From source file:com.liferay.mobile.sdk.json.DiscoveryDeserializer.java
License:Open Source License
@Override public Discovery deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject root = json.getAsJsonObject(); String contextName = root.get("contextName").getAsString(); JsonArray jsonArray = root.getAsJsonArray("services"); try {/*from w w w . j a va2s.c o m*/ ArrayList<Action> services = JSONParser.fromJSON(jsonArray, new GenericListType<>(Action.class)); return new Discovery(contextName, services); } catch (Exception e) { throw new JsonParseException(e); } }
From source file:com.liferay.mobile.sdk.json.JSONArrayDeserializer.java
License:Open Source License
@Override public JSONArray deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { try {/*from w ww .jav a 2 s. c om*/ String value = json.toString(); return new JSONArray(value); } catch (JSONException jsone) { throw new JsonParseException(jsone); } }
From source file:com.liferay.mobile.sdk.json.JSONObjectDeserializer.java
License:Open Source License
@Override public JSONObject deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { try {/*from ww w .j a v a 2s . c o m*/ String value = json.toString(); return new JSONObject(value); } catch (JSONException jsone) { throw new JsonParseException(jsone); } }
From source file:com.lion328.xenonlauncher.minecraft.launcher.json.data.type.DependencyNameTypeAdapter.java
License:Open Source License
@Override public DependencyName deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { try {/*ww w. j ava 2 s .c o m*/ return new DependencyName(jsonElement.getAsString()); } catch (IllegalArgumentException e) { throw new JsonParseException(e); } }
From source file:com.metinkale.prayerapp.vakit.times.gson.RuntimeTypeAdapterFactory.java
License:Apache License
@Override public <R> TypeAdapter<R> create(@NonNull Gson gson, @NonNull TypeToken<R> type) { if (type.getRawType() != baseType) { return null; }/*from www . jav a2 s. c o m*/ final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<>(); final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<>(); for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) { TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue())); labelToDelegate.put(entry.getKey(), delegate); subtypeToDelegate.put(entry.getValue(), delegate); } return new TypeAdapter<R>() { @Override public R read(JsonReader in) throws IOException { JsonElement jsonElement = Streams.parse(in); JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName); if (labelJsonElement == null) { throw new JsonParseException("cannot deserialize " + baseType + " because it does not define a field named " + typeFieldName); } String label = labelJsonElement.getAsString(); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label); if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?"); } return delegate.fromJsonTree(jsonElement); } @Override public void write(JsonWriter out, @NonNull R value) throws IOException { Class<?> srcType = value.getClass(); String label = subtypeToLabel.get(srcType); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType); if (delegate == null) { throw new JsonParseException( "cannot serialize " + srcType.getName() + "; did you forget to register a subtype?"); } JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject(); if (jsonObject.has(typeFieldName)) { throw new JsonParseException("cannot serialize " + srcType.getName() + " because it already defines a field named " + typeFieldName); } JsonObject clone = new JsonObject(); clone.add(typeFieldName, new JsonPrimitive(label)); for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) { clone.add(e.getKey(), e.getValue()); } Streams.write(clone, out); } }.nullSafe(); }
From source file:com.michaelfotiadis.crossyscore.core.data.parsers.gson.RuntimeTypeAdapterFactory.java
License:Apache License
public <R> TypeAdapter<R> create(final Gson gson, final TypeToken<R> type) { if (!type.getRawType().equals(baseType)) { return null; }//from www .j a v a 2 s. com final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<>(); final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<>(); for (final Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) { final TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue())); labelToDelegate.put(entry.getKey(), delegate); subtypeToDelegate.put(entry.getValue(), delegate); } return new TypeAdapter<R>() { @Override public void write(final JsonWriter out, final R value) throws IOException { if (value != null) { final Class<?> srcType = value.getClass(); final String label = subtypeToLabel.get(srcType); @SuppressWarnings("unchecked") final // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType); if (delegate == null) { throw new JsonParseException("cannot serialize " + srcType.getName() + "; did you forget to register a subtype?"); } final JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject(); if (jsonObject.has(typeFieldName)) { throw new JsonParseException("cannot serialize " + srcType.getName() + " because it already defines a field named " + typeFieldName); } final JsonObject clone = new JsonObject(); clone.add(typeFieldName, new JsonPrimitive(label)); for (final Map.Entry<String, JsonElement> e : jsonObject.entrySet()) { clone.add(e.getKey(), e.getValue()); } Streams.write(clone, out); } else { out.nullValue(); } } @Override public R read(final JsonReader in) throws IOException { final JsonElement jsonElement = Streams.parse(in); // fix for null Json Elements if (jsonElement.isJsonNull()) { return null; } final JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName); final String label = getBaseTypeLabel(labelJsonElement); @SuppressWarnings("unchecked") // registration requires that subtype extends T final TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label); if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?"); } return delegate.fromJsonTree(jsonElement); } private String getBaseTypeLabel(final JsonElement labelJsonElement) { final String label; if (labelJsonElement == null) { if (defaultSubTypeLabel == null) { throw new JsonParseException("cannot deserialize " + baseType + " because it does not define a field named " + typeFieldName); } else { label = defaultSubTypeLabel; if (!labelToDelegate.containsKey(label)) { throw new IllegalStateException( "WTF: Was looking for " + label + " in " + labelToDelegate.keySet()); } } } else { label = labelJsonElement.getAsString(); } return label; } }; }