List of usage examples for com.google.gson JsonDeserializationContext deserialize
public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException;
From source file:com.tapchatapp.android.client.MessageDeserializer.java
License:Apache License
@Override public Message deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (mDebug) { Log.d("MessageDeserializer", "Got message: " + json.toString()); }/* w w w . java 2s . c o m*/ JsonObject jsonObject = json.getAsJsonObject(); Class<? extends Message> klass; if (jsonObject.has("_reqid") && !jsonObject.get("_reqid").isJsonNull()) { klass = ResponseMessage.class; } else { JsonElement messageType = jsonObject.get("type"); if (messageType != null) { String type = messageType.getAsString(); klass = TYPES.get(type); if (klass == null) { klass = UnknownMessage.class; } } else { klass = UnknownMessage.class; } } return context.deserialize(json, klass); }
From source file:com.thoughtworks.go.plugin.access.configrepo.codec.TypeAdapter.java
License:Apache License
public <T> T determineJsonElementForDistinguishingImplementers(JsonElement json, JsonDeserializationContext context, String field, String origin) { JsonObject jsonObject = json.getAsJsonObject(); JsonPrimitive prim = (JsonPrimitive) jsonObject.get(field); JsonPrimitive originField = (JsonPrimitive) jsonObject.get(origin); String typeName = prim.getAsString(); Class<?> klass = classForName(typeName, originField == null ? null : originField.getAsString()); return context.deserialize(jsonObject, klass); }
From source file:com.thoughtworks.go.plugin.configrepo.codec.TypeAdapter.java
License:Apache License
public <T> T determineJsonElementForDistinguishingImplementers(JsonElement json, JsonDeserializationContext context, String field, String origin) { JsonObject jsonObject = json.getAsJsonObject(); JsonPrimitive prim = (JsonPrimitive) jsonObject.get(field); JsonPrimitive originField = (JsonPrimitive) jsonObject.get(origin); String typeName = prim.getAsString(); Class<?> klass = classForName(typeName, originField == null ? "gocd" : originField.getAsString()); return context.deserialize(jsonObject, klass); }
From source file:com.twitter.intellij.pants.service.project.model.TargetInfoDeserializer.java
License:Apache License
@Override public TargetInfo deserialize(JsonElement element, Type type, final JsonDeserializationContext context) throws JsonParseException { final JsonObject object = element.getAsJsonObject(); final List<String> targets = getStringListField(object, "targets"); final List<String> libraries = getStringListField(object, "libraries"); final List<String> excludes = getStringListField(object, "excludes"); final List<SourceRoot> sourceRoots = getFieldAsList(object.getAsJsonArray("roots"), new Function<JsonElement, SourceRoot>() { @Override/*from w w w . j av a2 s.c o m*/ public SourceRoot fun(JsonElement element) { return context.deserialize(element, SourceRoot.class); } }); final TargetAddressInfo addressInfo = context.deserialize(element, TargetAddressInfo.class); return new TargetInfo(new HashSet<TargetAddressInfo>(Collections.singleton(addressInfo)), new HashSet<String>(targets), new HashSet<String>(libraries), new HashSet<String>(excludes), new HashSet<SourceRoot>(sourceRoots)); }
From source file:com.twitter.sdk.android.core.models.BindingValuesAdapter.java
License:Apache License
Object getValue(JsonObject obj, JsonDeserializationContext context) { final JsonElement typeObj = obj.get(TYPE_MEMBER); if (typeObj == null || !typeObj.isJsonPrimitive()) { return null; }//from w ww . ja va 2s. c o m switch (typeObj.getAsString()) { case STRING_TYPE: return context.deserialize(obj.get(TYPE_VALUE_MEMBER), String.class); case IMAGE_TYPE: return context.deserialize(obj.get(IMAGE_VALUE_MEMBER), ImageValue.class); case USER_TYPE: return context.deserialize(obj.get(USER_VALUE_MEMBER), UserValue.class); case BOOLEAN_TYPE: return context.deserialize(obj.get(BOOLEAN_MEMBER), Boolean.class); default: return null; } }
From source file:com.unovo.frame.utils.gson.deserializer.ListJsonDeserializer.java
License:Open Source License
@Override public List<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonArray()) { JsonArray array = json.getAsJsonArray(); Type itemType = ((ParameterizedType) typeOfT).getActualTypeArguments()[0]; List list = new ArrayList<>(); for (int i = 0; i < array.size(); i++) { JsonElement element = array.get(i); Object item = context.deserialize(element, itemType); list.add(item);/*ww w . j a v a2 s . c om*/ } return list; } else { //??List return Collections.EMPTY_LIST; } }
From source file:com.yandex.money.api.typeadapters.model.showcase.container.GroupTypeAdapter.java
License:Open Source License
@Override protected Component deserializeItem(JsonElement src, JsonDeserializationContext context) { return context.deserialize(src, ComponentsTypeProvider.getClassOfComponentType(getTypeFromJsonElement(src))); }
From source file:com.yandex.money.api.typeadapters.model.showcase.uicontrol.AmountTypeAdapter.java
License:Open Source License
@Override protected void deserialize(JsonObject src, Amount.Builder builder, JsonDeserializationContext context) { builder.setCurrency(Currency.parseAlphaCode(src.get(MEMBER_CURRENCY).getAsString())); builder.setFee((Fee) context.deserialize(src.get(MEMBER_FEE), Fee.class)); super.deserialize(src, builder, context); }
From source file:data.JSONDataInserter.java
License:Open Source License
@Override public Category deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { Category category = new Category(); JsonObject object = json.getAsJsonObject(); category.setNameDE(object.get("nameDE").getAsString()); category.setNameEN(object.get("nameEN").getAsString()); for (JsonElement jsonQuestion : object.get("questions").getAsJsonArray()) { Question question = context.deserialize(jsonQuestion, new TypeToken<Question>() { }.getType());/*from w w w . j av a 2 s.c om*/ category.addQuestion(question); } return category; }
From source file:data.JSONDataInserter.java
License:Open Source License
@Override public Question deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { Question question = new Question(); JsonObject object = json.getAsJsonObject(); question.setTextDE(object.get("textDE").getAsString()); question.setTextEN(object.get("textEN").getAsString()); question.setValue(object.get("value").getAsInt()); for (JsonElement wrongAnswer : object.get("wrongAnswers").getAsJsonArray()) { Answer answer = context.deserialize(wrongAnswer, new TypeToken<Answer>() { }.getType());//ww w . j a v a 2 s . c om question.addWrongAnswer(answer); } for (JsonElement correctAnswer : object.get("rightAnswers").getAsJsonArray()) { Answer answer = context.deserialize(correctAnswer, new TypeToken<Answer>() { }.getType()); question.addRightAnswer(answer); } return question; }