List of usage examples for com.google.gson JsonParseException JsonParseException
public JsonParseException(Throwable cause)
From source file:rest.ws.gson.deserializer.entity.SearchEntityDeserializer.java
@Override public T deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException { JsonObject jsonObject = je.getAsJsonObject(); Object id;//from w w w . j a va 2s. c om String property = entityMetadata.getPropertyId(); Class propertyType = entityMetadata.getPropertyIdType(); if (jsonObject.has(property)) { id = castProperty(jsonObject, property, propertyType); if (id == null) return null; else return createEntityDAO(clazz, entityManager).find(id); } else { throw new JsonParseException( String.format("Bad json configuration, '%s' property is missed", property)); } }
From source file:ru.teamrocket.csrsysteamdesktop.Service.RuntimeTypeAdapterFactory.java
License:Apache License
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (null == type || !baseType.isAssignableFrom(type.getRawType())) { return null; }/*from w ww . j a va 2 s.c om*/ 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().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:ru.tinkoff.acquiring.sdk.CardStatusSerializer.java
License:Apache License
@Override public CardStatus deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json != null) { final String stringRepresentation = json.getAsString(); if (stringRepresentation.length() != 1) { throw new JsonParseException("Card Status has wrong format: " + stringRepresentation); }//from w ww .j ava2 s. c o m return CardStatus.fromChar(stringRepresentation.charAt(0)); } return null; }
From source file:se.angstroms.blh.anders.context.serializing.FullContextJsonDeserializer.java
@Override public FullContext deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext jsonContext) throws JsonParseException { final JsonObject jsonObject = json.getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) { String name = entry.getKey(); if (name.equalsIgnoreCase("ingredientsList") || name.equalsIgnoreCase("beerType") || name.equalsIgnoreCase("name")) { continue; // TODO: Omg lol I suxx }//from ww w. ja va2 s. co m try { Field field = context.getClass().getDeclaredField(name); Object o2 = jsonContext.deserialize(entry.getValue(), field.getGenericType()); if (o2 == null) { throw new JsonParseException(name + " deserialized to null."); } field.setAccessible(true); Object o = field.get(context); if (o instanceof InputtedOrCalculatedValue && o2 instanceof InputtedOrCalculatedValue) { InputtedOrCalculatedValue iocv = (InputtedOrCalculatedValue<? extends Unit<?>>) o; InputtedOrCalculatedValue<? extends Unit<?>> deserialized = (InputtedOrCalculatedValue<? extends Unit<?>>) o2; if (deserialized.stateProperty().get() == STATE.CALCULATED) { if (deserialized.get() != null) { iocv.set(deserialized.get()); } iocv.setFormula(deserialized.formulaProperty().get()); } else if (deserialized.stateProperty().get() == STATE.INPUTTED) { if (deserialized.formulaProperty().get() != null) { iocv.setFormula(deserialized.formulaProperty().get()); } iocv.set(deserialized.get()); } } else if (o instanceof InputtedValue && o2 instanceof InputtedValue) { ((InputtedValue) o).set(((InputtedValue) o2).get()); } if (o instanceof CalculatedValue && o2 instanceof CalculatedValue) { ((CalculatedValue) o).formulaProperty().set(((CalculatedValue) o2).formulaProperty().get()); } } catch (Exception ex) { throw new JsonParseException("Failed to deserialize " + name, ex); } } return context; }
From source file:se.joelpet.android.toyreaderforreddit.gson.typeadapters.RuntimeTypeAdapterFactory.java
License:Apache License
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (type.getRawType() != baseType) { return null; }// w w w .j a va 2 s . com 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); 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?"); } // joep: take the "data" jsonElement return delegate.fromJsonTree(jsonElement.getAsJsonObject().remove("data")); } @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); } }; }
From source file:se.tillvaxtverket.tsltrust.common.json.DateTypeAdapter.java
License:Open Source License
@Override public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!(json instanceof JsonPrimitive)) { throw new JsonParseException("The date should be a string value"); }/*from www . j a va 2 s. c om*/ try { return format.parse(json.getAsString()); } catch (ParseException e) { throw new JsonParseException(e); } }
From source file:syncthing.api.model.event.EventDeserializer.java
License:Mozilla Public License
@Override public Event deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonObject()) { throw new JsonParseException("Element was not an object"); }/*from w w w.j a v a2 s.c om*/ JsonObject obj = json.getAsJsonObject(); long id = context.deserialize(obj.get("id"), long.class); EventType type = context.deserialize(obj.get("type"), EventType.class); DateTime time = context.deserialize(obj.get("time"), DateTime.class); JsonElement data = obj.get("data"); if (type == null) { type = EventType.UNKNOWN; } switch (type) { case CONFIG_SAVED: { return new ConfigSaved(id, time, type, context.deserialize(data, Config.class)); } case DEVICE_CONNECTED: { return new DeviceConnected(id, time, type, context.deserialize(data, DeviceConnected.Data.class)); } case DEVICE_DISCONNECTED: { return new DeviceDisconnected(id, time, type, context.deserialize(data, DeviceDisconnected.Data.class)); } case DEVICE_DISCOVERED: { return new DeviceDiscovered(id, time, type, context.deserialize(data, DeviceDiscovered.Data.class)); } case DEVICE_PAUSED: { return new DevicePaused(id, time, type, context.deserialize(data, DevicePaused.Data.class)); } case DEVICE_REJECTED: { return new DeviceRejected(id, time, type, context.deserialize(data, DeviceRejected.Data.class)); } case DEVICE_RESUMED: { return new DeviceResumed(id, time, type, context.deserialize(data, DeviceResumed.Data.class)); } case DOWNLOAD_PROGRESS: { return new DownloadProgress(id, time, type, context.deserialize(data, DownloadProgress.Data.class)); } case FOLDER_COMPLETION: { return new FolderCompletion(id, time, type, context.deserialize(data, FolderCompletion.Data.class)); } case FOLDER_ERRORS: { return new FolderErrors(id, time, type, context.deserialize(data, FolderErrors.Data.class)); } case FOLDER_REJECTED: { return new FolderRejected(id, time, type, context.deserialize(data, FolderRejected.Data.class)); } case FOLDER_SCAN_PROGRESS: { return new FolderScanProgress(id, time, type, context.deserialize(data, FolderScanProgress.Data.class)); } case FOLDER_SUMMARY: { return new FolderSummary(id, time, type, context.deserialize(data, FolderSummary.Data.class)); } case ITEM_FINISHED: { return new ItemFinished(id, time, type, context.deserialize(data, ItemFinished.Data.class)); } case ITEM_STARTED: { return new ItemStarted(id, time, type, context.deserialize(data, ItemStarted.Data.class)); } case LOCAL_INDEX_UPDATED: { return new LocalIndexUpdated(id, time, type, context.deserialize(data, LocalIndexUpdated.Data.class)); } case PING: { return new Ping(id, time, type); } case REMOTE_INDEX_UPDATED: { return new RemoteIndexUpdated(id, time, type, context.deserialize(data, RemoteIndexUpdated.Data.class)); } case STARTING: { return new Starting(id, time, type, context.deserialize(data, Starting.Data.class)); } case STARTUP_COMPLETE: { return new StartupComplete(id, time, type); } case STATE_CHANGED: { return new StateChanged(id, time, type, context.deserialize(data, StateChanged.Data.class)); } default: { return new UnknownEvent(id, time, obj.toString()); } } }
From source file:syncthing.api.model.VersioningTypeConverter.java
License:Mozilla Public License
@Override public Versioning deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonObject()) { throw new JsonParseException("Element was not an object"); }//from ww w .j a v a 2s . c om JsonObject obj = json.getAsJsonObject(); VersioningType type = context.deserialize(obj.get("type"), VersioningType.class); JsonElement params = obj.get("params"); if (type == null) { type = VersioningType.NONE; } switch (type) { case EXTERNAL: return new VersioningExternal(type, context.deserialize(params, VersioningExternal.Params.class)); case SIMPLE: return new VersioningSimple(type, context.deserialize(params, VersioningSimple.Params.class)); case STAGGERED: return new VersioningStaggered(type, context.deserialize(params, VersioningStaggered.Params.class)); case TRASHCAN: return new VersioningTrashCan(type, context.deserialize(params, VersioningTrashCan.Params.class)); case NONE: default: return new VersioningNone(type); } }
From source file:tajo.datum.json.DatumAdapter.java
License:Apache License
@Override public Datum deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObject = json.getAsJsonObject(); String className = jsonObject.get("classname").getAsJsonPrimitive().getAsString(); Class clazz = null;//from ww w. ja v a2 s.c om try { clazz = Class.forName(className); } catch (ClassNotFoundException e) { e.printStackTrace(); throw new JsonParseException(e); } return context.deserialize(jsonObject.get("property"), clazz); }
From source file:tools.devnull.boteco.client.rest.impl.DateTypeAdapter.java
License:Open Source License
@Override public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try {//w w w .j a va 2s .c om return dateFormat.parse(json.getAsString()); } catch (ParseException e) { throw new JsonParseException(e); } }