Example usage for com.google.gson JsonElement getAsJsonPrimitive

List of usage examples for com.google.gson JsonElement getAsJsonPrimitive

Introduction

In this page you can find the example usage for com.google.gson JsonElement getAsJsonPrimitive.

Prototype

public JsonPrimitive getAsJsonPrimitive() 

Source Link

Document

convenience method to get this element as a JsonPrimitive .

Usage

From source file:net.praqma.tracey.tracey_rabbitmq_neo4j_bridge.Tracey2Neo.java

private Object getPrimitiveType(JsonElement jo) {
    if (!jo.isJsonPrimitive()) {
        return null;
    }//from   w  ww .j a  va  2 s.com
    JsonPrimitive jp = jo.getAsJsonPrimitive();
    if (jp.isBoolean()) {
        return jp.getAsBoolean();
    }
    if (jp.isNumber()) {
        return jp.getAsNumber();
    }
    if (jp.isString()) {
        return jp.getAsString();
    }
    return null;
}

From source file:net.riotopsys.json_patch.gson.JsonPathDeserializer.java

License:Apache License

@Override
public JsonPath deserialize(JsonElement element, Type type,
        JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    return new JsonPath(element.getAsJsonPrimitive().getAsString());
}

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

License:Apache License

public ASBase deserialize(JsonElement el, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject obj = (JsonObject) el;//from w  w w.ja v  a2  s . co  m
    ASBase.Builder<?, ?> builder;
    if (type == Collection.class)
        builder = Collection.makeCollection();
    else if (type == Activity.class)
        builder = Activity.makeActivity();
    else if (type == MediaLink.class)
        builder = MediaLink.makeMediaLink();
    else if (type == PlaceObject.class)
        builder = PlaceObject.makePlace();
    else if (type == Mood.class)
        builder = Mood.makeMood();
    else if (type == Address.class)
        builder = Address.makeAddress();
    else {
        JsonPrimitive ot = obj.getAsJsonPrimitive("objectType");
        if (ot != null) {
            String ots = ot.getAsString();
            Class<? extends ASObject.Builder> _class = objsmap.get(ots);
            if (_class != null) {
                builder = Discover.locate(_class, _class.getName());
                try {
                    builder = _class.getConstructor(String.class).newInstance(ots);
                } catch (Throwable t) {
                }

            } else
                builder = ASObject.makeObject(ots);
        } else {
            if (obj.has("verb") && (obj.has("actor") || obj.has("object") || obj.has("target"))) {
                builder = Activity.makeActivity();
            } else if (obj.has("items")) {
                builder = Collection.makeCollection();
            } else {
                builder = ASObject.makeObject(); // anonymous
            }
        }
    }
    for (Entry<String, JsonElement> entry : obj.entrySet()) {
        String name = entry.getKey();
        if (name.equalsIgnoreCase("objectType"))
            continue;
        Class<?> _class = map.get(name);
        JsonElement val = entry.getValue();
        if (val.isJsonPrimitive()) {
            if (_class != null) {
                builder.set(name, context.deserialize(val, _class));
            } else {
                JsonPrimitive prim = val.getAsJsonPrimitive();
                if (prim.isBoolean())
                    builder.set(name, prim.getAsBoolean());
                else if (prim.isNumber())
                    builder.set(name, prim.getAsNumber());
                else {
                    builder.set(name, prim.getAsString());
                }
            }
        } else if (val.isJsonArray()) {
            ImmutableList.Builder<Object> list = ImmutableList.builder();
            JsonArray arr = val.getAsJsonArray();
            processArray(arr, _class, context, list);
            builder.set(name, list.build());
        } else if (val.isJsonObject()) {
            if (map.containsKey(name)) {
                builder.set(name, context.deserialize(val, map.get(name)));
            } else
                builder.set(name, context.deserialize(val, ASObject.class));
        }
    }
    return builder.get();
}

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);/*www.  j  av  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.abdera2.activities.io.gson.SimpleAdapter.java

License:Apache License

public T deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    return deserialize(json.getAsJsonPrimitive().getAsString());
}

From source file:org.apache.lens.driver.es.client.jest.JestResultSetTransformer.java

License:Apache License

private Type getDataType(int colPosition, JsonElement jsonObjectValue) {
    if (columnDataTypes.get(colPosition) != Type.NULL_TYPE) {
        return columnDataTypes.get(colPosition);
    }/*from w  w  w  .  j  ava 2 s.  c o  m*/

    final JsonPrimitive jsonPrimitive = jsonObjectValue.getAsJsonPrimitive();
    if (jsonPrimitive.isJsonNull()) {
        return Type.NULL_TYPE;
    }

    final Type type;
    if (jsonPrimitive.isBoolean()) {
        type = Type.BOOLEAN_TYPE;
    } else if (jsonPrimitive.isNumber()) {
        type = Type.DOUBLE_TYPE;
    } else {
        type = Type.STRING_TYPE;
    }
    columnDataTypes.set(colPosition, type);
    return type;
}

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 ww .  j a  va2  s.  c  om
        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.sling.jms.Json.java

License:Apache License

private static <T> T toMapValue(JsonElement element) {
    if (element.isJsonObject()) {
        return (T) toMapValue(element.getAsJsonObject());
    } else if (element.isJsonArray()) {
        return (T) toMapValue(element.getAsJsonArray());
    } else if (element.isJsonNull()) {
        return null;
    } else if (element.isJsonPrimitive()) {
        return (T) toMapValue(element.getAsJsonPrimitive());
    }//w  w  w . java2 s .  com
    throw new IllegalArgumentException(
            "Encountered JsonElement that is not an object, array, primitive or null: " + element);
}

From source file:org.apache.sling.query.mock.json.JsonToResource.java

License:Apache License

private static Resource parseResource(JsonElement object, String name, Resource parent) {
    if (object.isJsonArray()) {
        return parseResource(object.getAsJsonArray(), name, parent);
    } else if (object.isJsonPrimitive()) {
        return parseResource(object.getAsJsonPrimitive(), name, parent);
    } else if (object.isJsonObject()) {
        return parseResource(object.getAsJsonObject(), name, parent);
    } else {//  ww w .j a  va  2 s  .  c  o m
        return null;
    }
}