List of usage examples for com.google.gson JsonDeserializationContext deserialize
public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException;
From source file:de.arkraft.jenkins.JenkinsHelper.java
License:Apache License
public static GsonBuilder getGsonBuilder() { GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() { @Override// w w w. ja v a 2 s . c om public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { // using the correct parser right away should save init time compared to new DateTime(<string>) return ISO_8601_WITH_MILLIS.parseDateTime(json.getAsString()); } }); builder.registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() { @Override public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(src.toString()); } }); builder.registerTypeAdapter(Duration.class, new JsonDeserializer() { @Override public Object deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { return Duration.parse("PT" + json.getAsString() + "S"); } }); builder.registerTypeAdapter(Action.class, new JsonDeserializer<Action>() { @Override public Action deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { JsonObject object = ((JsonObject) jsonElement); if (object.has("causes")) { JsonElement element = object.get("causes"); if (element.isJsonArray()) { return jsonDeserializationContext.deserialize(element, Causes.class); } } if (object.has("failCount")) { return jsonDeserializationContext.deserialize(object, CountAction.class); } return null; } }); builder.registerTypeAdapter(ChangeSet.class, new JsonDeserializer<ChangeSet>() { @Override public ChangeSet deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException { ChangeSet changeSet = new ChangeSet(); JsonObject object = (JsonObject) jsonElement; if (object.has("items") && object.get("items").isJsonArray()) { for (JsonElement element : object.get("items").getAsJsonArray()) { ChangeSet.Change change = context.deserialize(element, ChangeSet.Change.class); changeSet.add(change); } } if (object.has("kind")) { changeSet.setKind(object.get("kind").getAsString()); } return changeSet; } }); builder.registerTypeAdapter(Duration.class, new JsonSerializer<Duration>() { @Override public JsonElement serialize(Duration duration, Type type, JsonSerializationContext jsonSerializationContext) { return new JsonPrimitive(duration.toString().replace("PT", "").replace("S", "")); } }); builder.registerTypeAdapter(EditType.class, new JsonDeserializer<EditType>() { @Override public EditType deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { return EditType.byName(jsonElement.getAsString()); } }); builder.registerTypeAdapter(HealthIcon.class, new JsonDeserializer<HealthIcon>() { @Override public HealthIcon deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { return HealthIcon.fromName(jsonElement.toString()); } }); builder.registerTypeAdapter(Queue.class, new JsonDeserializer<Queue>() { @Override public Queue deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException { Queue queue = new Queue(); JsonObject jsonObject = (JsonObject) jsonElement; if (jsonObject.has("items") && jsonObject.get("items").isJsonArray()) { for (JsonElement element : jsonObject.get("items").getAsJsonArray()) { Queue.Item build = context.deserialize(element, Queue.Item.class); queue.add(build); } } return queue; } }); builder.registerTypeAdapter(JobCollection.class, new JsonDeserializer<JobCollection>() { @Override public JobCollection deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { JobCollection jobs = new JobCollection(); JsonObject object = (JsonObject) json; if (object.has("jobs") && object.get("jobs").isJsonArray()) { for (JsonElement element : object.get("jobs").getAsJsonArray()) { Job job = context.deserialize(element, Job.class); jobs.add(job); } } return jobs; } }); builder.registerTypeAdapter(BallColor.class, new JsonDeserializer<BallColor>() { @Override public BallColor deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext json) throws JsonParseException { return BallColor.valueOf(jsonElement.getAsString().toUpperCase()); } }); return builder; }
From source file:de.baydev.hueemulator.util.philips.ResponseDeserializer.java
License:Open Source License
@Override public HueResponse deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException { HueResponse response = new HueResponse(); JsonObject jsonObject = jsonElement.getAsJsonObject(); if (jsonObject.get("success").isJsonObject()) { for (Map.Entry<String, JsonElement> entry : jsonObject.getAsJsonObject("success").entrySet()) { response.setSuccess(new HashMap<String, Object>() { {/*from w w w . j av a 2 s . c om*/ put(entry.getKey(), entry.getValue()); } }); } } else { response.setSuccessMessage(jsonObject.get("success").getAsString()); } if (jsonObject.has("error") && jsonObject.get("error").isJsonObject()) { for (Map.Entry<String, JsonElement> entry : jsonObject.getAsJsonObject("error").entrySet()) { response.setError(context.deserialize(entry.getValue(), HueResponse.Error.class)); } } return response; }
From source file:de.bwravencl.controllerbuddy.json.ActionTypeAdapter.java
License:Open Source License
@Override public IAction<?> deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { final var wrapper = json.getAsJsonObject(); final var typeName = get(wrapper, PROPERTY_TYPE); final var typeNameString = typeName.getAsString(); final var data = get(wrapper, PROPERTY_DATA); try {// w ww . j a va2 s. c o m final var actualType = Class.forName(typeNameString); return context.deserialize(data, actualType); } catch (final ClassNotFoundException e) { if (typeOfT == IAction.class) { log.log(Logger.Level.WARNING, "Action class '" + typeNameString + "' not found, substituting with '" + NullAction.class.getSimpleName() + "'"); unknownActionClasses.add(typeNameString); return new NullAction(); } throw new JsonParseException(e); } }
From source file:de.chaosfisch.util.DateTimeTypeConverter.java
License:Open Source License
@Override public DateTime deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { try {/* w w w. ja v a 2 s. co m*/ return new DateTime(json.getAsString()); } catch (final IllegalArgumentException e) { // May be it came in formatted as a java.util.Date, so try that final Date date = context.deserialize(json, Date.class); return new DateTime(date); } }
From source file:de.csdev.ebus.cfg.std.EBusValueJsonDeserializer.java
License:Open Source License
@Override public List<EBusValueDTO> deserialize(JsonElement jElement, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonArray asJsonArray = jElement.getAsJsonArray(); ArrayList<EBusValueDTO> result = new ArrayList<EBusValueDTO>(); ArrayList<String> fields = new ArrayList<String>(); for (Field field : EBusValueDTO.class.getDeclaredFields()) { SerializedName annotation = field.getAnnotation(SerializedName.class); if (annotation != null) { fields.add(annotation.value()); } else {/*from w ww. j a va2s. co m*/ fields.add(field.getName()); } } for (JsonElement jsonElement : asJsonArray) { JsonObject jObject = jsonElement.getAsJsonObject(); EBusValueDTO valueDTO = context.deserialize(jObject, EBusValueDTO.class); for (Entry<String, JsonElement> entry : jObject.entrySet()) { if (!fields.contains(entry.getKey())) { if (entry.getValue().isJsonPrimitive()) { JsonPrimitive primitive = (JsonPrimitive) entry.getValue(); if (primitive.isNumber()) { valueDTO.setProperty(entry.getKey(), primitive.getAsBigDecimal()); } else if (primitive.isBoolean()) { valueDTO.setProperty(entry.getKey(), primitive.getAsBoolean()); } else if (primitive.isString()) { valueDTO.setProperty(entry.getKey(), primitive.getAsString()); } } else { valueDTO.setProperty(entry.getKey(), entry.getValue().getAsString()); } } } result.add(valueDTO); } return result; }
From source file:de.dentrassi.pm.maven.NodeAdapter.java
License:Open Source License
@Override public Node deserialize(final JsonElement element, final Type type, final JsonDeserializationContext ctx) throws JsonParseException { final JsonObject o = element.getAsJsonObject(); final String typeString = o.get("type").getAsString(); final JsonObject val = o.get("node").getAsJsonObject(); switch (typeString) { case "DirectoryNode": return ctx.deserialize(val, DirectoryNode.class); case "DataNode": return ctx.deserialize(val, DataNode.class); case "ArtifactNode": return ctx.deserialize(val, ArtifactNode.class); }/*ww w . j av a 2 s . c o m*/ // TODO Auto-generated method stub return null; }
From source file:de.elomagic.vaadin.addon.speechrecognition.SpeechRecognitionResultDeserializer.java
License:Apache License
@Override public SpeechRecognitionResult deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { JsonObject o = json.getAsJsonObject(); int length = o.get("length").getAsInt(); SpeechRecognitionResult results = new SpeechRecognitionResult(); for (int i = 0; i < length; i++) { JsonObject resultObject = o.getAsJsonObject(Integer.toString(i)); results.add((SpeechRecognitionAlternative) context.deserialize(resultObject, SpeechRecognitionAlternative.class)); }//w w w .j a v a 2s . c om return results; }
From source file:de.elomagic.vaadin.addon.speechrecognition.SpeechRecognitionResultsDeserializer.java
License:Apache License
@Override public SpeechRecognitionResults deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { JsonObject o = json.getAsJsonObject(); int length = o.get("length").getAsInt(); SpeechRecognitionResults results = new SpeechRecognitionResults(); for (int i = 0; i < length; i++) { JsonObject resultObject = o.getAsJsonObject(Integer.toString(i)); results.add((SpeechRecognitionResult) context.deserialize(resultObject, SpeechRecognitionResult.class)); }/* w w w. j a v a 2s . c o m*/ return results; }
From source file:de.justi.yagw2api.common.json.TupleTypeAdapter.java
License:Apache License
@Override public Tuple deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { if (!(json instanceof JsonArray)) { throw new JsonParseException("The tuple should be a json array but was " + json.getClass()); }//from w ww. ja v a2s. c o m final JsonArray jsonArray = (JsonArray) json; final ImmutableList.Builder<?> tupleParts = ImmutableList.builder(); for (JsonElement element : jsonArray) { tupleParts.add(context.deserialize(element, Object.class)); } return Tuples.from(tupleParts.build()); }
From source file:de.winniehell.battlebeavers.storage.SoldierDeserializer.java
License:Open Source License
@Override public Soldier deserialize(final JsonElement pJson, final Type pType, final JsonDeserializationContext pContext) throws JsonParseException { if (!pJson.isJsonObject()) { return null; }//from www. j a v a 2s .c om final JsonObject object = pJson.getAsJsonObject(); if (!object.has("id") || !object.has("team") || !object.has("tile") || !object.has("view_angle") || !object.has("hp")) { return null; } final Soldier soldier = new Soldier(object.get("team").getAsInt(), (Tile) pContext.deserialize(object.get("tile"), Tile.class)); soldier.setId(object.get("id").getAsInt()); soldier.setRotation(object.get("view_angle").getAsFloat()); soldier.setHp(object.get("hp").getAsInt()); if (object.has("waypoints") && object.get("waypoints").isJsonArray()) { currentSoldier = soldier; synchronized (currentSoldier) { final JsonArray waypoints = object.get("waypoints").getAsJsonArray(); final Iterator<JsonElement> it = waypoints.iterator(); if (it.hasNext()) { final WayPoint firstWaypoint = pContext.deserialize(it.next(), WayPoint.class); if (firstWaypoint.getAim() != null) { soldier.getFirstWaypoint().setAim(firstWaypoint.getAim().getTile()); } } while (it.hasNext()) { final WayPoint waypoint = pContext.deserialize(it.next(), WayPoint.class); soldier.addWayPoint(waypoint); } currentSoldier = null; } } return soldier; }