Example usage for com.google.gson JsonDeserializationContext deserialize

List of usage examples for com.google.gson JsonDeserializationContext deserialize

Introduction

In this page you can find the example usage for com.google.gson JsonDeserializationContext deserialize.

Prototype

public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException;

Source Link

Document

Invokes default deserialization on the specified object.

Usage

From source file:org.xacml4j.v30.marshal.json.StatusAdapter.java

License:Open Source License

@Override
public Status deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject o = json.getAsJsonObject();
    StatusDetail detail = null;/*from   w  ww.j av a 2 s .  c  om*/
    String statusMessage = GsonUtil.getAsString(o, STATUS_MESSAGE_PROPERTY, null);
    StatusCode code = context.deserialize(o.get(STATUS_CODE_PROPERTY), StatusCode.class);
    return Status.builder(code).message(statusMessage).detail(detail).build();
}

From source file:org.xacml4j.v30.marshal.json.StatusCodeAdapter.java

License:Open Source License

@Override
public StatusCode deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject o = json.getAsJsonObject();
    String value = GsonUtil.getAsString(o, VALUE_PROPERTY, null);
    checkNotNull(value);//from ww  w .  j a  v  a2 s  .c  om
    StatusCode.Builder builder = StatusCode.builder(StatusCodeIds.parse(value));

    StatusCode embeddedStatusCode = context.deserialize(o.getAsJsonObject(STATUS_CODE_PROPERTY),
            StatusCode.class);
    if (embeddedStatusCode != null) {
        builder.minorStatus(embeddedStatusCode);
    }

    return builder.build();
}

From source file:pl.llp.aircasting.api.gson.NoteAdapter.java

License:Open Source License

@Override
public Note deserialize(JsonElement element, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    Note note = new Note();

    JsonObject object = element.getAsJsonObject();

    try {//from   w w w  .j a va 2  s. c o m
        note.setDate((Date) context.deserialize(object.get("date"), Date.class));
        note.setNumber(object.get("number").getAsInt());
        note.setLatitude(object.get("latitude").getAsDouble());
        note.setLongitude(object.get("longitude").getAsDouble());
        note.setText((String) context.deserialize(object.get("text"), String.class));
        note.setPhotoPath((String) context.deserialize(object.get("photo_location"), String.class));
    } catch (Exception e) {
        Logger.w("Error deserializing note " + object, e);
    }

    return note;
}

From source file:pl.llp.aircasting.gson.NoteAdapter.java

License:Open Source License

@Override
public Note deserialize(JsonElement element, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    Note note = new Note();

    JsonObject object = element.getAsJsonObject();

    note.setDate((Date) context.deserialize(object.get("date"), Date.class));
    note.setNumber(object.get("number").getAsInt());
    note.setLatitude(object.get("latitude").getAsDouble());
    note.setLongitude(object.get("longitude").getAsDouble());
    note.setText((String) context.deserialize(object.get("text"), String.class));
    note.setPhotoPath((String) context.deserialize(object.get("photo_location"), String.class));

    return note;//w  ww. ja v  a2 s. co m
}

From source file:rest.client.gson.message.ResponseGsonDeserializer.java

@Override
public ResponseMessage<T> deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
        throws JsonParseException {
    ResponseMessage<T> response = new ResponseMessage<>();
    JsonObject jsonObject = je.getAsJsonObject();
    if (jsonObject.has(ACTION))
        response.visitAction(jsonObject.get(ACTION).getAsString());
    if (jsonObject.has(DATA_TYPE))
        response.visitDataType(jsonObject.get(DATA_TYPE).getAsString());
    if (jsonObject.has(LOG_LIST)) {
        Type logListType = new TypeToken<ArrayList<Log>>() {
        }.getType();/* w ww . j a va2  s. c  om*/
        List<Log> logs = jdc.deserialize(jsonObject.get(LOG_LIST), logListType);
        logs.stream().forEach((log) -> {
            response.visitLog(log);
        });
    }
    if (jsonObject.has(LINKS)) {
        deserializeLinks(jsonObject.get(LINKS)).stream().forEach((link) -> {
            response.visitPaginationLink(link);
        });
    }
    if (jsonObject.has(DATA_CONTAINER)) {
        response.visitDataContainer(jsonObject.get(DATA_CONTAINER).getAsString());
        if (jsonObject.has(DATA)) {
            switch (jsonObject.get(DATA_CONTAINER).getAsString()) {
            case LIST_CONTAINER:
                JsonArray arrayData = jsonObject.get(DATA).getAsJsonArray();
                int index = 0;
                for (JsonElement e : arrayData) {
                    deserializeData(response, index++, e, jdc);
                }
                break;
            case OBJECT_CONTAINER:
                deserializeData(response, 0, jsonObject.get(DATA), jdc);
                break;

            }
        }
    }
    return response;
}

From source file:rest.client.gson.message.ResponseGsonDeserializer.java

private T deserializeDataItem(JsonElement je, JsonDeserializationContext jdc) {
    if (deserializer != null)
        return deserializer.deserialize(je, clazz, jdc);
    else/*from www  .  j av a2s  .  c om*/
        return jdc.deserialize(je, clazz);
}

From source file:ro.agitman.customserializer.utils.CustomDeserializer.java

@Override
public AbstractSerializer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    JsonObject jsonObject = json.getAsJsonObject();
    String clazzName = jsonObject.get(JsonName.CLASS_NAME).getAsString();
    AbstractSerializer abstractSerializer = null;
    Class c = null;//from   w  ww  . j  a  va  2 s .  co m
    try {
        c = Class.forName(clazzName);
        abstractSerializer = (AbstractSerializer) c.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (abstractSerializer != null) {
        JsonObject jObject = jsonObject.get(JsonName.PROPS).getAsJsonObject();

        Map<String, Object> map = new HashMap<>();
        for (Map.Entry<String, JsonElement> entry : jObject.entrySet()) {

            if (entry.getValue().isJsonObject()) {
                JsonElement clazzObj = entry.getValue().getAsJsonObject().get(JsonName.CLASS_NAME);
                if (clazzObj != null) {
                    String clazz = clazzObj.getAsString();
                    try {
                        map.put(entry.getKey(), context.deserialize(entry.getValue(), Class.forName(clazz)));
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                if (entry.getValue().isJsonArray()) {
                    List<AbstractSerializer> list = context.deserialize(entry.getValue(),
                            new TypeToken<List<AbstractSerializer>>() {
                            }.getType());
                    map.put(entry.getKey(), list);
                } else {
                    map.put(entry.getKey(), context.deserialize(entry.getValue(), Object.class));
                }
            }
        }

        Field[] fields = c.getDeclaredFields();
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                Object obj = map.get(field.getName());

                if (field.getType().equals(obj.getClass())) {
                    field.set(abstractSerializer, obj);
                } else {
                    if (field.getType().equals(obj.getClass().getInterfaces()[0])) {
                        field.set(abstractSerializer, obj);
                    } else {
                        field.set(abstractSerializer,
                                context.deserialize(jObject.get(field.getName()), field.getType()));
                    }
                }
            }
        } catch (IllegalArgumentException | IllegalAccessException iae) {
            iae.printStackTrace();
        }
    }
    System.out.println(jsonObject);

    return abstractSerializer;
}

From source file:ru.tinkoff.acquiring.sdk.CardsListDeserializer.java

License:Apache License

@Override
public GetCardListResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    if (json.isJsonObject()) {
        return gson.fromJson(json, GetCardListResponse.class);
    }// w ww  . j a  v  a 2  s  . co m

    final Card[] cards = context.deserialize(json, Card[].class);
    return new GetCardListResponse(cards);
}

From source file:sbu.srl.datastructure.ILPSRLDataDeserializer.java

@Override
public JSONData deserialize(JsonElement json, Type type, JsonDeserializationContext jdc)
        throws JsonParseException {
    final JsonObject jsonObject = json.getAsJsonObject();
    String process = jsonObject.get("process").getAsString();
    ArrayList<Sentence> sentences = new ArrayList<Sentence>();

    sentences = jdc.deserialize(jsonObject.get("sentences"), new TypeToken<ArrayList<Sentence>>() {
    }.getType());//from   w  w  w. j  a v  a2  s  .c o  m
    JSONData jsonPredData = new JSONData();
    jsonPredData.setProcessName(process);
    jsonPredData.setSentence(sentences);
    return jsonPredData;
}

From source file:sbu.srl.datastructure.SentenceDeserializer.java

@Override
public Sentence deserialize(JsonElement json, Type type, JsonDeserializationContext jdc)
        throws JsonParseException {
    final JsonObject jsonObject = json.getAsJsonObject();
    int id = jsonObject.get("sentenceId").getAsInt();
    String text = jsonObject.get("text").getAsString();
    ArrayList<ArgumentSpan> annotatedArgumentSpan = new ArrayList<ArgumentSpan>();
    ArrayList<ArgumentSpan> predictedArgumentSpan = new ArrayList<ArgumentSpan>();
    if (!predict)
        annotatedArgumentSpan = jdc.deserialize(jsonObject.get("annotatedArgumentSpan"),
                new TypeToken<ArrayList<ArgumentSpan>>() {
                }.getType());/*from   w  w  w.  j a  v  a 2 s .c om*/

    predictedArgumentSpan = jdc.deserialize(jsonObject.get("predictionArgumentSpan"),
            new TypeToken<ArrayList<ArgumentSpan>>() {
            }.getType());
    Sentence sentence = new Sentence();
    sentence.setId(id);
    sentence.setRawText(text);
    if (!predict)
        sentence.setAnnotatedArgumentSpanJSON(annotatedArgumentSpan);

    sentence.setPredictedArgumentSpanJSON(predictedArgumentSpan);
    return sentence;
}