List of usage examples for com.google.gson JsonDeserializationContext deserialize
public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException;
From source file:fr.dudie.keolis.gson.LinesOfLineAlertDeserializer.java
License:Open Source License
@Override public List<String> deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) { JsonElement lines = json.getAsJsonObject().get("line"); if (lines instanceof JsonArray) { return KeoUtils.getGsonInstance().fromJson(lines, typeOfT); }//from w w w.j a v a 2 s. co m String line = context.deserialize(lines, String.class); ArrayList<String> list = new ArrayList<String>(); list.add(line); return list; }
From source file:fr.dudie.keolis.gson.SubwayStationDeserializer.java
License:Open Source License
@Override public List<SubwayStation> deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) { final ArrayList<SubwayStation> subwayStations = new ArrayList<SubwayStation>(); if (json instanceof JsonArray) { for (int i = 0; i < ((JsonArray) json).size(); i++) { subwayStations// w w w. ja v a 2 s.c o m .add(KeoUtils.getGsonInstance().fromJson(((JsonArray) json).get(i), SubwayStation.class)); } } else { subwayStations.add((SubwayStation) context.deserialize(json, SubwayStation.class)); } return subwayStations; }
From source file:fr.zcraft.zbanque.json.ContainerTypeAdapter.java
License:Open Source License
@Override public Container deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { final JsonObject rawContainer = jsonElement.getAsJsonObject(); final Location main = jsonDeserializationContext.deserialize(rawContainer.get("mainLocation"), Location.class); Location secondary = null;/*from w w w . j a v a 2 s. com*/ final JsonElement rawSecondaryLocation = rawContainer.get("secondaryLocation"); if (rawSecondaryLocation != null) secondary = jsonDeserializationContext.deserialize(rawSecondaryLocation, Location.class); final Container container = new Container(main, secondary, true); final JsonObject content = rawContainer.getAsJsonObject("content"); for (Map.Entry<String, JsonElement> entry : content.entrySet()) { final String[] rawType = entry.getKey().split(":"); if (rawType.length != 2) { PluginLogger.error("Malformed JSON content: malformed item type {0} in {1}", entry.getKey(), jsonElement.toString()); continue; } final Material itemType = Material.matchMaterial(rawType[0]); if (itemType == null) { PluginLogger.error("Malformed JSON content: unknown item type {0} in {1}", rawType[0], jsonElement.toString()); continue; } final Short itemData; try { itemData = Short.valueOf(rawType[1]); } catch (NumberFormatException e) { PluginLogger.error("Malformed JSON content: badly formatted data value {0} in {1}", e, rawType[1], jsonElement.toString()); continue; } BlockType blockType = new BlockType(itemType, itemData); Integer amount = entry.getValue().getAsInt(); container.updateBlockType(blockType, amount); } final JsonElement rawContainerType = rawContainer.get("containerType"); if (rawContainerType != null) container.setContainerType(Material.matchMaterial(rawContainerType.getAsString())); return container; }
From source file:gov.nasa.jpf.jdart.summaries.json.SubClassHandler.java
License:Open Source License
@Override public T deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException { JsonObject jo = je.getAsJsonObject(); String className = jo.get(SUB_CLASS).getAsString(); try {//from ww w . j a v a 2 s . co m Class<?> clazz = Class.forName(className); return jdc.deserialize(je, clazz); } catch (ClassNotFoundException e) { throw new JsonParseException(e); } }
From source file:gov.va.isaac.util.json.InterfaceAdapter.java
License:Apache License
public T deserialize(JsonElement elem, Type interfaceType, JsonDeserializationContext context) throws JsonParseException { final JsonObject wrapper = (JsonObject) elem; final JsonElement typeName = get(wrapper, "type"); final JsonElement data = get(wrapper, "data"); final Type actualType = typeForName(typeName); return context.deserialize(data, actualType); }
From source file:gson.util.MessageAdapter.java
@Override public Message deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException { final JsonObject jsonObject = je.getAsJsonObject(); final String name = jsonObject.get("name").getAsString(); final MessageBody body = jdc.deserialize(jsonObject.get("message_body"), MessageBody.class); Message message = new Message(); message.setName(name);/*from ww w. j a va2 s . co m*/ message.setMessageBody(body); return message; }
From source file:gson.util.MessageBodyDeserializer.java
@Override public MessageBody deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext jdc) throws JsonParseException { final JsonObject jsonObject = json.getAsJsonObject(); MessageBody message = null;/* www. ja v a 2s .c o m*/ if (jsonObject.get("text_message") != null) { message = new StringMessage(); ((StringMessage) message).setTextMessage(jsonObject.get("text_message").getAsString()); } else if (jsonObject.get("date_from") != null) { message = new GetMessage(); String data = jsonObject.get("date_from").getAsString(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); formatter.setTimeZone(TimeZone.getTimeZone("Europe/Warsaw")); try { Date dataFrom = new Date(formatter.parse(data).getTime()); ((GetMessage) message).setDateFrom(dataFrom); } catch (ParseException ex) { Logger.getLogger(MessageBodyDeserializer.class.getName()).log(Level.SEVERE, null, ex); } } else if (jsonObject.get("healthInfo") != null) { List<Wynik> healthInfo = new ArrayList<Wynik>(); message = new HealthInfoMessage(); JsonArray healthInfoArray = jsonObject.get("healthInfo").getAsJsonArray(); for (JsonElement js : healthInfoArray) { Wynik wynik = jdc.deserialize(js, Wynik.class); healthInfo.add(wynik); } ((HealthInfoMessage) message).setHealthInfo(healthInfo); } else if (jsonObject.get("patientsInfo") != null) { List<Pacjent> patientsInfo = new ArrayList<Pacjent>(); message = new DocHealthInfoMessage(); JsonArray patientsInfoArray = jsonObject.get("patientsInfo").getAsJsonArray(); for (JsonElement js : patientsInfoArray) { Pacjent pacjent = jdc.deserialize(js, Pacjent.class); patientsInfo.add(pacjent); } ((DocHealthInfoMessage) message).setPatientsInfo(patientsInfo); } else throw new JsonParseException("Not supported json!"); if (message != null) message.setKey(jsonObject.get("key").getAsString()); return message; }
From source file:gson.util.PacjentAdapter.java
@Override public Pacjent deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException { final JsonObject jsonObject = je.getAsJsonObject(); Pacjent pacjent = new Pacjent(); pacjent.setImie(jsonObject.get("imie").getAsString()); pacjent.setPesel(jsonObject.get("pesel").getAsString()); JsonArray wynikiArray = jsonObject.get("wyniki").getAsJsonArray(); for (JsonElement js : wynikiArray) { Wynik wynik = jdc.deserialize(js, Wynik.class); pacjent.getWyniki().add(wynik);/* w ww . j a v a 2s. c om*/ } return pacjent; }
From source file:io.datakernel.cube.api.ReportingQueryResponseDeserializer.java
License:Apache License
@Override public ReportingQueryResult deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext ctx) throws JsonParseException { JsonObject json = jsonElement.getAsJsonObject(); int count = json.get(COUNT_FIELD).getAsInt(); JsonArray jsonRecords = json.get(RECORDS_FIELD).getAsJsonArray(); List<Map<String, Object>> records = deserializeRecords(jsonRecords); Type map = new TypeToken<Map<String, Object>>() { }.getType();// www .jav a 2 s . c om JsonObject jsonTotals = json.get(TOTALS_FIELD) == null ? null : json.get(TOTALS_FIELD).getAsJsonObject(); Map<String, Object> totals = ctx.deserialize(jsonTotals, map); Type listOfStrings = new TypeToken<List<String>>() { }.getType(); if (json.get(METADATA_FIELD) == null) return new ReportingQueryResult(records, totals, count, null, null, null, null, null, null); JsonObject jsonMetadata = json.get(METADATA_FIELD).getAsJsonObject(); List<String> dimensions = ctx.deserialize(jsonMetadata.get(DIMENSIONS_FIELD), listOfStrings); List<String> measures = ctx.deserialize(jsonMetadata.get(MEASURES_FIELD), listOfStrings); List<String> attributes = ctx.deserialize(jsonMetadata.get(ATTRIBUTES_FIELD), listOfStrings); Map<String, Object> filterAttributes = ctx.deserialize(jsonMetadata.get(FILTER_ATTRIBUTES_FIELD), map); Set<DrillDown> drillDowns = deserializeDrillDowns(jsonMetadata.get(DRILLDOWNS_FIELD), listOfStrings, ctx); List<String> sortedBy = ctx.deserialize(jsonMetadata.get(SORTED_BY_FIELD), listOfStrings); return new ReportingQueryResult(records, totals, count, drillDowns, dimensions, attributes, measures, filterAttributes, sortedBy); }
From source file:io.datakernel.cube.api.ReportingQueryResponseDeserializer.java
License:Apache License
private Set<DrillDown> deserializeDrillDowns(JsonElement json, Type listOfStrings, JsonDeserializationContext ctx) { if (json == null) return newHashSet(); Set<DrillDown> drillDowns = newHashSet(); Type setOfStrings = new TypeToken<Set<String>>() { }.getType();//from www .ja v a 2 s. co m for (JsonElement jsonDrillDown : json.getAsJsonArray()) { List<String> dimensions = ctx.deserialize(jsonDrillDown.getAsJsonObject().get(DIMENSIONS_FIELD), listOfStrings); Set<String> measures = ctx.deserialize(jsonDrillDown.getAsJsonObject().get(MEASURES_FIELD), setOfStrings); drillDowns.add(new DrillDown(dimensions, measures)); } return drillDowns; }