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:io.datakernel.serializer.GsonSubclassesAdapter.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*w w  w  .j  a  v a  2s  .  co m*/
public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonPrimitive()) {
        if (!((JsonPrimitive) json).isString()) {
            throw new JsonParseException("Inner class name is expected");
        }
        String className = json.getAsString();
        InstanceCreator<T> creator = classCreators.get(className);
        if (creator != null) {
            return creator.createInstance(typeOfT);
        }
        try {
            Class<?> aClass = classTags.get(className);
            if (aClass == null) {
                aClass = Class.forName(className);
            }
            return (T) newInstance(aClass);
        } catch (InvocationTargetException | ClassNotFoundException | InstantiationException
                | IllegalAccessException | NoSuchMethodException e) {
            throw new JsonParseException(e);
        }
    }
    JsonObject object = json.getAsJsonObject();
    String subclassName = object.get(subclassField).getAsString();
    return context.deserialize(json, subclassNames.get(subclassName));
}

From source file:io.flowly.engine.parser.FlowObjectTypeAdapter.java

License:Open Source License

@Override
public FlowObject deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    String type = json.getAsJsonObject().get("type").getAsString();
    return context.deserialize(json, getFlowObjectType(type));
}

From source file:io.hawkcd.agent.utilities.deserializers.TaskDefinitionAdapter.java

License:Apache License

@Override
public TaskDefinition deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    String type = jsonObject.get("type").getAsString();
    try {/*from  www  .j  ava  2 s . c  om*/
        if (type.equals("EXEC")) {
            return context.deserialize(jsonObject, ExecTask.class);
        } else if (type.equals("UPLOAD_ARTIFACT")) {
            return context.deserialize(jsonObject, UploadArtifactTask.class);
        } else if (type.equals("FETCH_MATERIAL")) {
            return context.deserialize(jsonObject, FetchMaterialTask.class);
        } else if (type.equals("FETCH_ARTIFACT")) {
            return context.deserialize(jsonObject, FetchArtifactTask.class);
        }

        return context.deserialize(jsonObject, Class.forName("com.googlecode.whiteboard.model." + type));
    } catch (ClassNotFoundException cnfe) {
        throw new JsonParseException("Unknown element type: " + type, cnfe);
    }
}

From source file:io.hawkcd.core.subscriber.EnvelopeAdapter.java

License:Apache License

@Override
public Envelope deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    if (jsonObject == null) {
        return null;
    }//w  w w. ja v a  2s  .c o m

    JsonElement objectAsJsonElement = jsonObject.get("object");
    if (objectAsJsonElement == null || !objectAsJsonElement.isJsonArray() && !objectAsJsonElement.isJsonObject()
            && !objectAsJsonElement.isJsonPrimitive()) {
        return new Envelope();
    }

    Type resultObjectType = null;
    try {
        resultObjectType = Class.forName(jsonObject.get("packageName").getAsString());
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    Object result;
    if (objectAsJsonElement.isJsonArray()) {
        List<Object> resultAsList = new ArrayList<>();
        JsonArray objectAsJsonArray = objectAsJsonElement.getAsJsonArray();
        for (JsonElement element : objectAsJsonArray) {
            resultAsList.add(context.deserialize(element, resultObjectType));
        }

        result = resultAsList;
    } else {
        result = context.deserialize(objectAsJsonElement, resultObjectType);
    }

    return new Envelope(result);
}

From source file:io.imoji.sdk.objects.json.ArtistDeserializer.java

License:Open Source License

@Override
public Artist deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject root = json.getAsJsonObject();

    if (root.has("id")) {
        String identifier = root.get("id").getAsString();
        String name = root.get("name").getAsString();
        String description = root.get("description").getAsString();
        Imoji profileImoji = context.deserialize(root, Imoji.class);

        return new Artist(identifier, name, description, profileImoji);
    } else {// www  . ja v a  2  s  .c o  m
        return null;
    }
}

From source file:io.imoji.sdk.objects.json.AttributionDeserializer.java

License:Open Source License

@Override
public Category.Attribution deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject root = json.getAsJsonObject();
    Artist artist = context.deserialize(root, Artist.class);

    String attributionId = root.has("packId") ? root.get("packId").getAsString() : null;
    Uri uri = root.has("packURL") ? Uri.parse(root.get("packURL").getAsString()) : null;
    Category.URLCategory urlCategory = root.has("packURLCategory")
            ? URL_CATEGORY_MAP.get(root.get("packURLCategory").getAsString())
            : null;//  w  ww.  j a  va  2s.co m

    Imoji.LicenseStyle licenseStyle = Imoji.LicenseStyle.NonCommercial;
    if (root.has("licenseStyle")) {
        String licenseStyleStr = root.get("licenseStyle").getAsString();
        if ("commercialPrint".equals((licenseStyleStr))) {
            licenseStyle = Imoji.LicenseStyle.CommercialPrint;
        }
    }
    JsonArray relatedTagsArray = root.getAsJsonArray("relatedTags");
    List<String> relatedTags;

    if (relatedTagsArray != null && relatedTagsArray.size() > 0) {
        relatedTags = new ArrayList<>(relatedTagsArray.size());
        for (JsonElement tag : relatedTagsArray) {
            relatedTags.add(tag.getAsString());
        }
    } else {
        relatedTags = Collections.emptyList();
    }

    if (urlCategory == null && uri != null) {
        urlCategory = Category.URLCategory.Website;
    }

    return new Category.Attribution(attributionId, artist, uri, relatedTags, urlCategory, licenseStyle);
}

From source file:io.imoji.sdk.objects.json.CategoryDeserializer.java

License:Open Source License

@Override
public Category deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject root = json.getAsJsonObject();

    String identifier = root.get("searchText").getAsString();
    String title = root.get("title").getAsString();

    JsonArray imojisArray = root.get("imojis").getAsJsonArray();

    Category.Attribution attribution = null;
    if (root.has("artist") && !root.get("artist").isJsonNull()) {
        attribution = context.deserialize(root.getAsJsonObject("artist"), Category.Attribution.class);
    }/*from w ww.  ja  v a  2  s. c  o m*/

    List<Imoji> previewImojis;
    if (imojisArray != null && imojisArray.size() > 0) {
        previewImojis = new ArrayList<>(imojisArray.size());

        for (JsonElement imojiJson : imojisArray) {
            previewImojis.add(context.<Imoji>deserialize(imojiJson, Imoji.class));
        }
    } else {
        previewImojis = Collections.emptyList();
    }

    return new Category(identifier, title, previewImojis, attribution);
}

From source file:io.vertigo.vega.engines.webservice.json.DtListDeserializer.java

License:Apache License

/** {@inheritDoc} */
@Override/*from   w w  w . j a va2s  . c  o m*/
public DtList<D> deserialize(final JsonElement json, final Type typeOfT,
        final JsonDeserializationContext context) {
    final Type[] typeParameters = ((ParameterizedType) typeOfT).getActualTypeArguments();
    final Class<D> dtoClass = (Class<D>) typeParameters[0]; // Id has only one parameterized type T
    final JsonArray jsonArray = json.getAsJsonArray();

    final DtList<D> dtList = new DtList<>(dtoClass);
    for (final JsonElement element : jsonArray) {
        final D inputDto = context.deserialize(element, dtoClass);
        dtList.add(inputDto);
    }
    return dtList;
}

From source file:io.vertigo.vega.engines.webservice.json.UiListDeltaDeserializer.java

License:Apache License

private Map<String, UiObject<D>> parseUiObjectMap(final JsonObject jsonObject, final String propertyName,
        final Type uiObjectType, final JsonDeserializationContext context) {
    final Map<String, UiObject<D>> uiObjectMap = new HashMap<>();
    final JsonObject jsonUiObjectMap = jsonObject.getAsJsonObject(propertyName);
    if (jsonUiObjectMap != null) {
        for (final Entry<String, JsonElement> entry : jsonUiObjectMap.entrySet()) {
            final String entryName = entry.getKey();
            final UiObject<D> inputDto = context.deserialize(entry.getValue(), uiObjectType);
            uiObjectMap.put(entryName, inputDto);
        }/*  w w  w .ja va2s .  co m*/
    }
    return uiObjectMap;
}

From source file:io.vertigo.vega.engines.webservice.json.UiListDeserializer.java

License:Apache License

/** {@inheritDoc} */
@Override/* w  w  w  . j  a  va2 s  . co m*/
public UiListModifiable<D> deserialize(final JsonElement json, final Type typeOfT,
        final JsonDeserializationContext context) {
    final Type[] typeParameters = ((ParameterizedType) typeOfT).getActualTypeArguments();
    final Class<D> dtoClass = (Class<D>) typeParameters[0]; // Id has only one parameterized type T
    final Type uiObjectType = new KnownParameterizedType(UiObject.class, dtoClass);
    final JsonArray jsonArray = json.getAsJsonArray();

    final UiListModifiable<D> uiList = new UiListModifiable<>(dtoClass);
    for (final JsonElement element : jsonArray) {
        final UiObject<D> inputDto = context.deserialize(element, uiObjectType);
        uiList.add(inputDto);
    }
    return uiList;
}