List of usage examples for com.google.gson JsonDeserializationContext deserialize
public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException;
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 . j a va2 s . c o 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.kth.infosys.login.couchbase.AbstractRegisteredServiceJsonSerializer.java
License:Apache License
/** * @see JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext) * @param json JSON encoded representation of CAS Service. * @param typeOfT (ignored).//from w w w.j av a 2 s. c o m * @param context Gson de-serialization context. * @return de-serialized version of a CAS service. */ public final AbstractRegisteredService deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) { JsonObject jsonObject = json.getAsJsonObject(); String type = jsonObject.get("type").getAsString(); JsonElement element = jsonObject.get("properties"); try { return context.deserialize(element, Class.forName(type)); } catch (final ClassNotFoundException e) { throw new JsonParseException("Unknown element type: " + type, e); } }
From source file:simpleserver.CampaignDeserializer.java
@Override public Campaign deserialize(JsonElement json, java.lang.reflect.Type type, JsonDeserializationContext context) throws JsonParseException { //System.out.println("Campaign deserializing"); final JsonObject jsonObject = json.getAsJsonObject(); final JsonElement jsonId = jsonObject.get("campaign_name"); final String name = jsonId.getAsString(); final double price = jsonObject.get("price").getAsDouble(); target[] target_list = context.deserialize(jsonObject.get("target_list"), target[].class); /*/*ww w .ja v a2s.c o m*/ final String date = jsonObject.get("date").getAsString(); final String text = jsonObject.get("text").getAsString(); final float latitude = jsonObject.get("latitude").getAsFloat(); final float longitude = jsonObject.get("longitude").getAsFloat(); final String share = jsonObject.get("share").getAsString(); final String image = jsonObject.get("image").getAsString(); final String imageurl = jsonObject.get("imageurl").getAsString(); final String audio = jsonObject.get("sound").getAsString(); final String userid = jsonObject.get("userid").getAsString(); //Log.d("http","getAsString() till imageUri"); final String imageUri = jsonObject.get("imageUri").getAsString(); //Log.d("http","getAsString() finished upon imageUri"); /* final JsonArray jsonAuthorsArray = jsonObject.get("authors").getAsJsonArray(); final String[] authors = new String[jsonAuthorsArray.size()]; for (int i = 0; i < authors.length; i++) { final JsonElement jsonAuthor = jsonAuthorsArray.get(i); authors[i] = jsonAuthor.getAsString(); }*/ final Campaign d = new Campaign(); d.setName(name); d.setPrice(price); d.setTarget(target_list); return d; }
From source file:simpleserver.CampaignsDeserializer.java
@Override public Campaigns deserialize(JsonElement json, java.lang.reflect.Type type, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObject;/*from w w w. j a v a2s. c om*/ jsonObject = json.getAsJsonObject(); Campaign[] campaign = context.deserialize(jsonObject.get(""), Campaign[].class); final Campaigns campaigns = new Campaigns(); campaigns.setCampaigns(campaign); return campaigns; }
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 ww.j a v a2 s.co m*/ 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 a2s . 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;/* ww w . j av a 2 s . co m*/ try { clazz = Class.forName(className); } catch (ClassNotFoundException e) { e.printStackTrace(); throw new JsonParseException(e); } return context.deserialize(jsonObject.get("property"), clazz); }
From source file:tajo.engine.json.EvalNodeAdapter.java
License:Apache License
@Override public EvalNode deserialize(JsonElement json, Type type, JsonDeserializationContext ctx) throws JsonParseException { JsonObject jsonObj = json.getAsJsonObject(); String className = jsonObj.get("type").getAsString(); JsonElement elem = jsonObj.get("properties"); try {//ww w. j a v a2s . c o m return ctx.deserialize(elem, Class.forName(className)); } catch (ClassNotFoundException e) { e.printStackTrace(); } return null; }
From source file:uk.ac.horizon.aestheticodes.controllers.MarkerMapAdapter.java
License:Open Source License
@Override public Map<String, Marker> deserialize(JsonElement json, Type unused, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonArray()) { throw new JsonParseException("Unexpected type: " + json.getClass().getSimpleName()); }/*from w w w . ja v a 2 s.c o m*/ Map<String, Marker> result = new HashMap<String, Marker>(); JsonArray array = json.getAsJsonArray(); for (JsonElement element : array) { if (element.isJsonObject()) { Marker marker = context.deserialize(element, Marker.class); result.put(marker.getCode(), marker); } else { throw new JsonParseException("some meaningful message"); } } return result; }