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:ch.ethz.inf.vs.hypermedia.corehal.LinkListDeserializer.java

License:Open Source License

@Override
public LinkList deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    LinkList list = new LinkList();
    if (json.isJsonArray()) {
        for (JsonElement el : json.getAsJsonArray()) {
            list.add(context.deserialize(el, Link.class));
        }/*from   w  w w  .  ja v a2s .c om*/
    } else {
        list.add(context.deserialize(json, Link.class));
    }
    return list;
}

From source file:ch.ethz.inf.vs.hypermedia.corehal.OptionalListDeserializer.java

License:Open Source License

@Override
public OptionalList deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    OptionalList list = new OptionalList();
    Type elementType = ((ParameterizedType) typeOfT).getActualTypeArguments()[0];
    if (json.isJsonArray()) {
        for (JsonElement el : json.getAsJsonArray()) {
            list.add(context.deserialize(el, elementType));
        }/*from   w  w w .j  ava2 s.c o m*/
    } else {
        list.add(context.deserialize(json, elementType));
    }
    return list;
}

From source file:cl.niclabs.cb.common.MethodParser.java

License:Open Source License

@Override
public Method deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = jsonElement.getAsJsonObject();
    String method = jsonObject.get("method").getAsString();
    JsonElement argsJsonElement = jsonObject.get("args");
    switch (method) {
    case "OpenSession": {
        return methodFactory.makeOpenSessionMethod();
    }/* w w  w  .j av  a2 s  .c  o  m*/
    case "CloseSession": {
        CloseSessionMethod.Args args = context.deserialize(argsJsonElement, CloseSessionMethod.Args.class);
        return methodFactory.makeCloseSessionMethod(args);
    }
    case "DeleteKeyPair": {
        DeleteKeyPairMethod.Args args = context.deserialize(argsJsonElement, DeleteKeyPairMethod.Args.class);
        return methodFactory.makeDeleteKeyMethod(args);
    }
    case "GenerateKeyPair": {
        GenerateKeyPairMethod.Args args = context.deserialize(argsJsonElement,
                GenerateKeyPairMethod.Args.class);
        return methodFactory.makeGenerateKeyPairMethod(args);
    }
    case "SignInit": {
        SignInitMethod.Args args = context.deserialize(argsJsonElement, SignInitMethod.Args.class);
        return methodFactory.makeSignInitMethod(args);
    }
    case "Sign": {
        SignMethod.Args args = context.deserialize(argsJsonElement, SignMethod.Args.class);
        return methodFactory.makeSignMethod(args);
    }
    case "FindKey": {
        FindKeyMethod.Args args = context.deserialize(argsJsonElement, FindKeyMethod.Args.class);
        return methodFactory.makeFindKeyMethod(args);
    }
    case "SeedRandom": {
        SeedRandomMethod.Args args = context.deserialize(argsJsonElement, SeedRandomMethod.Args.class);
        return methodFactory.makeSeedRandomMethod(args);
    }
    case "GenerateRandom": {
        GenerateRandomMethod.Args args = context.deserialize(argsJsonElement, GenerateRandomMethod.Args.class);
        return methodFactory.makeGenerateRandomMethod(args);
    }
    case "DigestInit": {
        DigestInitMethod.Args args = context.deserialize(argsJsonElement, DigestInitMethod.Args.class);
        return methodFactory.makeDigestInitMethod(args);
    }
    case "Digest": {
        DigestMethod.Args args = context.deserialize(argsJsonElement, DigestMethod.Args.class);
        return methodFactory.makeDigestMethod(args);
    }
    default: {
        throw new JsonParseException("Cannot parse method: " + method);
    }
    }

}

From source file:cl.niclabs.tscrypto.common.messages.TSMessageParser.java

License:Open Source License

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

    JsonObject jobject = (JsonObject) json;
    String type = jobject.get("type").getAsString();
    String version = jobject.get("version").getAsString();

    // TODO check version

    if (type.equals("encrypted-data")) {
        return context.deserialize(json, EncryptedData.class);
    } else if (type.matches(".*-query")) {
        return parseQuery(type, version, json, context);
    } else if (type.matches(".*-answer")) {
        return parseAnswer(type, version, json, context);
    }/*w  ww  .j  a v a2  s.c o m*/

    throw new JsonParseException("Illegal TSMessage type/version:" + type + "/" + version);
}

From source file:cl.niclabs.tscrypto.common.messages.TSMessageParser.java

License:Open Source License

private TSMessage parseQuery(String type, String version, JsonElement json,
        JsonDeserializationContext context) {

    switch (type) {
    case "ping-query":
        return context.deserialize(json, PingQuery.class);
    case "signShare-query":
        return context.deserialize(json, SignShareQuery.class);
    case "sendKey-query":
        return context.deserialize(json, SendKeyQuery.class);
    case "deleteKey-query":
        return context.deserialize(json, DeleteKeyQuery.class);
    }/*from   ww w .j  a v a2 s .com*/

    throw new JsonParseException("Illegal TSMessage Query type/version:" + type + "/" + version);

}

From source file:cl.niclabs.tscrypto.common.messages.TSMessageParser.java

License:Open Source License

private TSMessage parseAnswer(String type, String version, JsonElement json,
        JsonDeserializationContext context) {

    switch (type) {
    case "ping-answer":
        return context.deserialize(json, PingAnswer.class);
    case "signShare-answer":
        return context.deserialize(json, SignShareAnswer.class);
    case "sendKey-answer":
        return context.deserialize(json, SendKeyAnswer.class);
    case "deleteKey-answer":
        return context.deserialize(json, DeleteKeyAnswer.class);
    }/*from   w  w  w  .j a va2 s . c o  m*/

    throw new JsonParseException("Illegal TSMessage Answer type/version:" + type + "/" + version);

}

From source file:cmput301.f13t01.elasticsearch.InterfaceAdapter.java

License:GNU General Public License

/**
 * This allows for the deserialization of objects that use an interface in
 * order to retain information of the implementing object.
 * //from  w  w w  . j  a  va 2s .c om
 * @return Returns the actual implementing object, with memory of its type.
 */
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:co.aurasphere.botmill.fb.internal.util.json.AttachmentDeserializer.java

License:Open Source License

public Attachment deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    Attachment attachment = delegateGson.fromJson(json, Attachment.class);
    AttachmentType type = attachment.getType();
    Class<? extends Payload> payloadClass = null;
    JsonElement payloadJson = json.getAsJsonObject().get("payload");

    switch (type) {
    case AUDIO://w  w w  .j  a  v a  2 s. c  om
    case FILE:
    case IMAGE:
    case VIDEO:
        payloadClass = UrlPayload.class;
        break;
    case LOCATION:
        payloadClass = QuickReplyLocationPayload.class;
        break;
    case FALLBACK:
        // In case of Fallback attachment the payload will be null so I do
        // nothing.
        break;
    case TEMPLATE:
        // In case of a template I need to check which one to instantiate.
        String payloadTypeString = payloadJson.getAsJsonObject().get("template_type").getAsString();
        PayloadType templateType = PayloadType.valueOf(payloadTypeString.toUpperCase());

        switch (templateType) {
        case AIRLINE_BOARDINGPASS:
            payloadClass = AirlineBoardingPassTemplatePayload.class;
            break;
        case AIRLINE_CHECKIN:
            payloadClass = AirlineCheckinTemplatePayload.class;
            break;
        case AIRLINE_ITINERARY:
            payloadClass = AirlineItineraryTemplatePayload.class;
            break;
        case AIRLINE_UPDATE:
            payloadClass = AirlineFlightUpdateTemplatePayload.class;
            break;
        case BUTTON:
            payloadClass = ButtonTemplatePayload.class;
            break;
        case GENERIC:
            payloadClass = GenericTemplatePayload.class;
            break;
        case LIST:
            payloadClass = ListTemplatePayload.class;
            break;
        case RECEIPT:
            payloadClass = ReceiptTemplatePayload.class;
            break;
        }
        break;
    }
    Payload payload = context.deserialize(payloadJson, payloadClass);
    attachment.setPayload(payload);
    return attachment;
}

From source file:co.aurasphere.botmill.fb.internal.util.json.ButtonDeserializer.java

License:Open Source License

public Button deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    String buttonTypeString = json.getAsJsonObject().get("type").getAsString();
    ButtonType buttonType = ButtonType.valueOf(buttonTypeString.toUpperCase());
    Class<? extends Button> buttonClass = null;
    switch (buttonType) {
    case ACCOUNT_LINK:
        buttonClass = LoginButton.class;
        break;//ww  w.  j a v a 2s .c  om
    case ACCOUNT_UNLINK:
        buttonClass = LogoutButton.class;
        break;
    case ELEMENT_SHARE:
        buttonClass = ShareButton.class;
        break;
    case PAYMENT:
        buttonClass = BuyButton.class;
        break;
    case PHONE_NUMBER:
        buttonClass = PostbackButton.class;
        break;
    case POSTBACK:
        buttonClass = PostbackButton.class;
        break;
    case WEB_URL:
        buttonClass = WebUrlButton.class;
        break;
    }
    return context.deserialize(json, buttonClass);
}

From source file:co.aurasphere.botmill.fb.internal.util.json.ButtonSerializer.java

License:Open Source License

public Button deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    String buttonTypeString = json.getAsJsonObject().get("type").getAsString();
    ButtonType buttonType = ButtonType.valueOf(buttonTypeString.toUpperCase());
    Class<?> buttonClass = getButtonClass(buttonType);
    return context.deserialize(json, buttonClass);
}