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.apache.abdera2.activities.io.gson.BaseAdapter.java

License:Apache License

private void processArray(JsonArray arr, Class<?> _class, JsonDeserializationContext context,
        ImmutableList.Builder<Object> list) {
    for (JsonElement mem : arr) {
        if (mem.isJsonPrimitive()) {
            if (_class != null) {
                list.add(context.deserialize(mem, _class));
            } else {
                JsonPrimitive prim2 = (JsonPrimitive) mem;
                if (prim2.isBoolean())
                    list.add(prim2.getAsBoolean());
                else if (prim2.isNumber())
                    list.add(prim2.getAsNumber());
                else
                    list.add(prim2.getAsString());
            }/*  w w w  .j  a va  2 s .  co m*/
        } else if (mem.isJsonObject()) {
            list.add(context.deserialize(mem, _class != null ? _class : ASObject.class));
        } else if (mem.isJsonArray()) {
            JsonArray array = mem.getAsJsonArray();
            ImmutableList.Builder<Object> objs = ImmutableList.builder();
            processArray(array, _class, context, objs);
            list.add(objs.build());
        }
    }
}

From source file:org.apache.abdera2.activities.io.gson.MultimapAdapter.java

License:Apache License

protected static ImmutableList<Object> arraydes(JsonArray array, JsonDeserializationContext context) {
    ImmutableList.Builder<Object> builder = ImmutableList.builder();
    for (JsonElement child : array)
        if (child.isJsonArray())
            builder.add(arraydes(child.getAsJsonArray(), context));
        else if (child.isJsonObject())
            builder.add(context.deserialize(child, ASBase.class));
        else if (child.isJsonPrimitive())
            builder.add(primdes(child.getAsJsonPrimitive()));
    return builder.build();
}

From source file:org.apache.abdera2.activities.io.gson.MultimapAdapter.java

License:Apache License

public Multimap deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Multimap mm = create(typeOfT);/*from  w  w w . ja  v a 2 s.  c o  m*/
    JsonObject obj = json.getAsJsonObject();
    for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
        String key = entry.getKey();
        JsonElement val = entry.getValue();
        if (val.isJsonArray())
            for (JsonElement el : val.getAsJsonArray())
                if (el.isJsonArray())
                    mm.put(key, arraydes(el.getAsJsonArray(), context));
                else if (el.isJsonObject())
                    mm.put(key, context.deserialize(el, ASBase.class));
                else if (el.isJsonNull())
                    mm.put(key, null);
                else if (el.isJsonPrimitive())
                    mm.put(key, primdes(el.getAsJsonPrimitive()));
                else if (val.isJsonObject())
                    mm.put(key, context.deserialize(val, ASBase.class));
                else if (val.isJsonPrimitive())
                    mm.put(key, primdes(val.getAsJsonPrimitive()));
    }
    return mm;
}

From source file:org.apache.atlas.notification.hook.HookNotification.java

License:Apache License

@Override
public HookNotificationMessage deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
    HookNotificationType type = context.deserialize(((JsonObject) json).get("type"),
            HookNotificationType.class);
    switch (type) {
    case ENTITY_CREATE:
        return context.deserialize(json, EntityCreateRequest.class);

    case ENTITY_FULL_UPDATE:
        return context.deserialize(json, EntityUpdateRequest.class);

    case ENTITY_PARTIAL_UPDATE:
        return context.deserialize(json, EntityPartialUpdateRequest.class);

    case ENTITY_DELETE:
        return context.deserialize(json, EntityDeleteRequest.class);

    case TYPE_CREATE:
    case TYPE_UPDATE:
        return context.deserialize(json, TypeRequest.class);

    default:/* w  w  w. ja va2  s . c om*/
        throw new IllegalStateException("Unhandled type " + type);
    }
}

From source file:org.apache.gobblin.runtime.spec_serde.FlowSpecDeserializer.java

License:Apache License

@Override
public FlowSpec deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
    JsonObject jsonObject = json.getAsJsonObject();

    String uri = jsonObject.get(FlowSpecSerializer.FLOW_SPEC_URI_KEY).getAsString();
    String version = jsonObject.get(FlowSpecSerializer.FLOW_SPEC_VERSION_KEY).getAsString();
    String description = jsonObject.get(FlowSpecSerializer.FLOW_SPEC_DESCRIPTION_KEY).getAsString();
    Config config = ConfigFactory/*w ww.  ja  va2  s.c o  m*/
            .parseString(jsonObject.get(FlowSpecSerializer.FLOW_SPEC_CONFIG_KEY).getAsString());

    Properties properties = new Properties();
    try {
        properties.load(new StringReader(
                jsonObject.get(FlowSpecSerializer.FLOW_SPEC_CONFIG_AS_PROPERTIES_KEY).getAsString()));
    } catch (IOException e) {
        throw new JsonParseException(e);
    }

    Set<URI> templateURIs = new HashSet<>();
    try {
        for (JsonElement template : jsonObject.get(FlowSpecSerializer.FLOW_SPEC_TEMPLATE_URIS_KEY)
                .getAsJsonArray()) {
            templateURIs.add(new URI(template.getAsString()));
        }
    } catch (URISyntaxException e) {
        throw new JsonParseException(e);
    }

    List<Spec> childSpecs = new ArrayList<>();
    for (JsonElement spec : jsonObject.get(FlowSpecSerializer.FLOW_SPEC_CHILD_SPECS_KEY).getAsJsonArray()) {
        childSpecs.add(context.deserialize(spec, FlowSpec.class));
    }

    FlowSpec.Builder builder = FlowSpec.builder(uri).withVersion(version).withDescription(description)
            .withConfig(config).withConfigAsProperties(properties);
    if (!templateURIs.isEmpty()) {
        builder = builder.withTemplates(templateURIs);
    }
    if (!childSpecs.isEmpty()) {
        builder = builder.withChildSpecs(childSpecs);
    }

    return builder.build();
}

From source file:org.apache.qpid.disttest.json.PropertyValueAdapter.java

License:Apache License

@Override
public PropertyValue deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonNull()) {
        return null;
    } else if (json.isJsonPrimitive()) {
        Object result = null;/*from   w  w w  . j a  v  a 2  s .  co m*/
        JsonPrimitive primitive = json.getAsJsonPrimitive();
        if (primitive.isString()) {
            result = primitive.getAsString();
        } else if (primitive.isNumber()) {
            String asString = primitive.getAsString();
            if (asString.indexOf('.') != -1 || asString.indexOf('e') != -1) {
                result = primitive.getAsDouble();
            } else {
                result = primitive.getAsLong();
            }
        } else if (primitive.isBoolean()) {
            result = primitive.getAsBoolean();
        } else {
            throw new JsonParseException("Unsupported primitive value " + primitive);
        }
        return new SimplePropertyValue(result);
    } else if (json.isJsonArray()) {
        JsonArray array = json.getAsJsonArray();
        List<Object> result = new ArrayList<Object>(array.size());
        for (JsonElement element : array) {
            result.add(context.deserialize(element, Object.class));
        }
        return new SimplePropertyValue(result);
    } else if (json.isJsonObject()) {
        JsonObject object = json.getAsJsonObject();
        JsonElement defElement = object.getAsJsonPrimitive(DEF_FIELD);
        Class<?> classInstance = null;
        if (defElement != null) {
            try {
                classInstance = _factory.getPropertyValueClass(defElement.getAsString());
            } catch (ClassNotFoundException e) {
                // ignore
            }
        }
        if (classInstance == null) {
            Map<String, Object> result = new HashMap<String, Object>();
            for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
                Object value = context.deserialize(entry.getValue(), Object.class);
                result.put(entry.getKey(), value);
            }
            return new SimplePropertyValue(result);
        } else {
            return context.deserialize(json, classInstance);
        }
    } else {
        throw new JsonParseException("Unsupported JSON type " + json);
    }
}

From source file:org.apache.tajo.catalog.json.FunctionAdapter.java

License:Apache License

@Override
public Function deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    String className = CommonGsonHelper.getOrDie(jsonObject, "class").getAsJsonPrimitive().getAsString();

    Class clazz;/*w w  w .  j a v  a2 s  . c om*/
    try {
        clazz = Class.forName(className);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        throw new JsonParseException(e);
    }
    return context.deserialize(CommonGsonHelper.getOrDie(jsonObject, "body"), clazz);
}

From source file:org.apache.tajo.catalog.json.SchemaAdapter.java

License:Apache License

@Override
public Schema deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    int version = CommonGsonHelper.getOrDie(jsonObject, "version").getAsJsonPrimitive().getAsInt();

    if (version == 1) {
        return context.deserialize(CommonGsonHelper.getOrDie(jsonObject, "body"), SchemaLegacy.class);
    } else {/*from w ww. j  a v a2  s. c  o m*/
        throw new TajoInternalError("Schema version 2 is not supported yet");
    }
}

From source file:org.apache.tajo.catalog.json.TableDescAdapter.java

License:Apache License

@Override
public TableDesc deserialize(JsonElement json, Type type, JsonDeserializationContext ctx)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    String className = jsonObject.get("classname").getAsJsonPrimitive().getAsString();

    Class clazz = null;/*from   w ww  . j  av a 2s .c  o  m*/
    try {
        clazz = Class.forName(className);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        throw new JsonParseException(e);
    }
    return ctx.deserialize(jsonObject.get("property"), clazz);
}

From source file:org.apache.tajo.catalog.json.TableMetaAdapter.java

License:Apache License

@Override
public TableMeta deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Preconditions.checkNotNull(json);/*from   www  .  j  av a 2  s  . co  m*/
    JsonObject jsonObject = json.getAsJsonObject();

    CatalogProtos.StoreType type = CatalogProtos.StoreType
            .valueOf(CommonGsonHelper.getOrDie(jsonObject, "store").getAsString());

    KeyValueSet keyValueSet = context.deserialize(CommonGsonHelper.getOrDie(jsonObject, "options"),
            KeyValueSet.class);
    return new TableMeta(type, keyValueSet);
}