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:org.debux.webmotion.server.call.ClientSession.java

License:Open Source License

/**
 * Return only JsonElement use getAttribute and getAttributes with class as 
 * parameter to get a value.//from  w  w  w .j  a  va  2s .  c  om
 */
@Override
public Object getAttribute(String name) {
    JsonElement value = attributes.get(name);
    if (value == null) {
        return value;

    } else if (value.isJsonPrimitive()) {
        JsonPrimitive primitive = value.getAsJsonPrimitive();
        if (primitive.isString()) {
            return primitive.getAsString();

        } else if (primitive.isBoolean()) {
            return primitive.getAsBoolean();

        } else if (primitive.isNumber()) {
            return primitive.getAsDouble();
        }

    } else if (value.isJsonArray()) {
        return value.getAsJsonArray();

    } else if (value.isJsonObject()) {
        return value.getAsJsonObject();

    } else if (value.isJsonNull()) {
        return value.getAsJsonNull();
    }
    return value;
}

From source file:org.devnexus.aerogear.ShadowStore.java

License:Apache License

private void saveElement(JsonObject serialized, String path, Serializable id) {
    String sql = String.format(
            "insert into shadow_%s_property (PROPERTY_NAME, PROPERTY_VALUE, PARENT_ID) values (?,?,?)",
            className);//from ww w .j a v a  2  s.  c om
    Set<Map.Entry<String, JsonElement>> members = serialized.entrySet();
    String pathVar = path.isEmpty() ? "" : ".";
    for (Map.Entry<String, JsonElement> member : members) {
        JsonElement jsonValue = member.getValue();
        String propertyName = member.getKey();
        if (jsonValue.isJsonObject()) {
            saveElement((JsonObject) jsonValue, path + pathVar + propertyName, id);
        } else if (jsonValue.isJsonArray()) {
            JsonArray jsonArray = jsonValue.getAsJsonArray();
            for (int index = 0; index < jsonArray.size(); index++) {
                JsonElement arrayElement = jsonArray.get(index);
                if (arrayElement.isJsonPrimitive()) {
                    JsonPrimitive primitive = arrayElement.getAsJsonPrimitive();
                    if (primitive.isBoolean()) {
                        String value = primitive.getAsBoolean() ? "true" : "false";
                        getDatabase().execSQL(sql, new Object[] {
                                path + pathVar + propertyName + String.format("[%d]", index), value, id });
                    } else if (primitive.isNumber()) {
                        Number value = primitive.getAsNumber();
                        getDatabase().execSQL(sql, new Object[] {
                                path + pathVar + propertyName + String.format("[%d]", index), value, id });
                    } else if (primitive.isString()) {
                        String value = primitive.getAsString();
                        getDatabase().execSQL(sql, new Object[] {
                                path + pathVar + propertyName + String.format("[%d]", index), value, id });
                    } else {
                        throw new IllegalArgumentException(
                                arrayElement + " isn't a number, boolean, or string");
                    }

                } else {
                    saveElement(arrayElement.getAsJsonObject(),
                            path + pathVar + propertyName + String.format("[%d]", index), id);
                }

            }
        } else {
            if (jsonValue.isJsonPrimitive()) {
                JsonPrimitive primitive = jsonValue.getAsJsonPrimitive();
                if (primitive.isBoolean()) {
                    String value = primitive.getAsBoolean() ? "true" : "false";
                    getDatabase().execSQL(sql, new Object[] { path + pathVar + propertyName, value, id });
                } else if (primitive.isNumber()) {
                    Number value = primitive.getAsNumber();
                    getDatabase().execSQL(sql, new Object[] { path + pathVar + propertyName, value, id });
                } else if (primitive.isString()) {
                    String value = primitive.getAsString();
                    getDatabase().execSQL(sql, new Object[] { path + pathVar + propertyName, value, id });
                } else {
                    throw new IllegalArgumentException(jsonValue + " isn't a number, boolean, or string");
                }

            } else {
                throw new IllegalArgumentException(jsonValue + " isn't a JsonPrimitive");
            }

        }
    }
}

From source file:org.devnexus.aerogear.ShadowStore.java

License:Apache License

private void buildKeyValuePairs(JsonObject where, List<Pair<String, String>> keyValues, String parentPath) {
    Set<Map.Entry<String, JsonElement>> keys = where.entrySet();
    String pathVar = parentPath.isEmpty() ? "" : ".";//Set a dot if parent path is not empty
    for (Map.Entry<String, JsonElement> entry : keys) {
        String key = entry.getKey();
        String path = parentPath + pathVar + key;
        JsonElement jsonValue = entry.getValue();
        if (jsonValue.isJsonObject()) {
            buildKeyValuePairs((JsonObject) jsonValue, keyValues, path);
        } else {//from   w w  w .ja v a 2 s  .  c  om
            if (jsonValue.isJsonPrimitive()) {
                JsonPrimitive primitive = jsonValue.getAsJsonPrimitive();
                if (primitive.isBoolean()) {
                    String value = primitive.getAsBoolean() ? "true" : "false";
                    keyValues.add(new Pair<String, String>(path, value));
                } else if (primitive.isNumber()) {
                    Number value = primitive.getAsNumber();
                    keyValues.add(new Pair<String, String>(path, value.toString()));
                } else if (primitive.isString()) {
                    String value = primitive.getAsString();
                    keyValues.add(new Pair<String, String>(path, value));
                } else {
                    throw new IllegalArgumentException(jsonValue + " isn't a number, boolean, or string");
                }

            } else {
                throw new IllegalArgumentException(jsonValue + " isn't a JsonPrimitive");
            }

        }
    }
}

From source file:org.eclim.Eclim.java

License:Open Source License

public static Object toType(JsonElement json) {
    // null//from  w ww . j a v  a2s .  co  m
    if (json.isJsonNull()) {
        return null;

        // int, double, boolean, String
    } else if (json.isJsonPrimitive()) {
        JsonPrimitive prim = json.getAsJsonPrimitive();
        if (prim.isBoolean()) {
            return prim.getAsBoolean();
        } else if (prim.isString()) {
            return prim.getAsString();
        } else if (prim.isNumber()) {
            if (prim.getAsString().indexOf('.') != -1) {
                return prim.getAsDouble();
            }
            return prim.getAsInt();
        }

        // List
    } else if (json.isJsonArray()) {
        ArrayList<Object> type = new ArrayList<Object>();
        for (JsonElement element : json.getAsJsonArray()) {
            type.add(toType(element));
        }
        return type;

        // Map
    } else if (json.isJsonObject()) {
        HashMap<String, Object> type = new HashMap<String, Object>();
        for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {
            type.put(entry.getKey(), toType(entry.getValue()));
        }
        return type;
    }

    return null;
}

From source file:org.eclipse.che.api.core.jsonrpc.impl.GsonJsonRpcUnmarshaller.java

License:Open Source License

private Object getInnerItem(JsonElement jsonElement) {
    if (jsonElement.isJsonNull()) {
        return null;
    }/*from   ww w  .ja  v a2s.  com*/

    if (jsonElement.isJsonObject()) {
        return jsonElement.getAsJsonObject();
    }

    if (jsonElement.isJsonPrimitive()) {
        JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive();
        if (jsonPrimitive.isNumber()) {
            return jsonPrimitive.getAsDouble();
        } else if (jsonPrimitive.isString()) {
            return jsonPrimitive.getAsString();
        } else {
            return jsonPrimitive.getAsBoolean();
        }
    }

    throw new IllegalStateException("Unexpected json element type");
}

From source file:org.eclipse.leshan.standalone.servlet.json.LwM2mNodeDeserializer.java

License:Open Source License

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

    if (json == null) {
        return null;
    }//from  ww w . j a v  a  2  s . c  om

    LwM2mNode node = null;

    if (json.isJsonObject()) {
        JsonObject object = (JsonObject) json;

        if (!object.has("id")) {
            throw new JsonParseException("Missing id");
        }
        int id = object.get("id").getAsInt();

        if (object.has("instances")) {

            JsonArray array = object.get("instances").getAsJsonArray();
            LwM2mObjectInstance[] instances = new LwM2mObjectInstance[array.size()];

            for (int i = 0; i < array.size(); i++) {
                instances[i] = context.deserialize(array.get(i), LwM2mNode.class);
            }
            node = new LwM2mObject(id, instances);

        } else if (object.has("resources")) {
            JsonArray array = object.get("resources").getAsJsonArray();
            LwM2mResource[] resources = new LwM2mResource[array.size()];

            for (int i = 0; i < array.size(); i++) {
                resources[i] = context.deserialize(array.get(i), LwM2mNode.class);
            }
            node = new LwM2mObjectInstance(id, resources);

        } else if (object.has("value")) {
            // single value resource
            node = new LwM2mResource(id, this.deserializeValue(object.get("value").getAsJsonPrimitive()));
        } else if (object.has("values")) {
            // multi-instances resource
            Collection<Value<?>> values = new ArrayList<>();
            for (JsonElement val : object.get("values").getAsJsonArray()) {
                values.add(this.deserializeValue(val.getAsJsonPrimitive()));
            }
            node = new LwM2mResource(id, values.toArray(new Value<?>[0]));
        } else {
            throw new JsonParseException("Invalid node element");
        }
    } else {
        throw new JsonParseException("Invalid node element");
    }

    return node;
}

From source file:org.eclipse.milo.opcua.binaryschema.gson.JsonStructureCodec.java

License:Open Source License

@Override
protected Object memberTypeToOpcUaScalar(JsonElement member, String typeName) {
    if (member == null || member.isJsonNull()) {
        return null;
    } else if (member.isJsonArray()) {
        JsonArray array = member.getAsJsonArray();

        switch (typeName) {
        case "ByteString": {
            byte[] bs = new byte[array.size()];

            for (int i = 0; i < array.size(); i++) {
                bs[i] = array.get(i).getAsByte();
            }//ww  w .j  ava 2  s.co m

            return ByteString.of(bs);
        }

        default:
            return array;
        }
    } else if (member.isJsonObject()) {
        JsonObject jsonObject = member.getAsJsonObject();

        switch (typeName) {
        case "QualifiedName": {
            return new QualifiedName(jsonObject.get("namespaceIndex").getAsInt(),
                    jsonObject.get("name").getAsString());
        }
        case "LocalizedText": {
            return new LocalizedText(jsonObject.get("locale").getAsString(),
                    jsonObject.get("text").getAsString());
        }

        default:
            return jsonObject;
        }
    } else if (member.isJsonPrimitive()) {
        JsonPrimitive primitive = member.getAsJsonPrimitive();

        if (primitive.isBoolean()) {
            return primitive.getAsBoolean();
        } else if (primitive.isString()) {
            switch (typeName) {
            case "Guid":
                return UUID.fromString(primitive.getAsString());

            case "NodeId":
                return NodeId.parseSafe(primitive.getAsString()).orElse(NodeId.NULL_VALUE);

            case "ExpandedNodeId":
                return ExpandedNodeId.parse(primitive.getAsString());

            case "XmlElement":
                return new XmlElement(primitive.getAsString());

            default:
                return primitive.getAsString();
            }
        } else if (primitive.isNumber()) {
            switch (typeName) {
            case "SByte":
                return primitive.getAsByte();
            case "Int16":
                return primitive.getAsShort();
            case "Int32":
                return primitive.getAsInt();
            case "Int64":
                return primitive.getAsLong();

            case "Byte":
                return ubyte(primitive.getAsShort());
            case "UInt16":
                return ushort(primitive.getAsInt());
            case "UInt32":
                return uint(primitive.getAsLong());
            case "UInt64":
                return ulong(primitive.getAsBigInteger());

            case "Float":
                return primitive.getAsFloat();
            case "Double":
                return primitive.getAsDouble();

            case "DateTime":
                return new DateTime(primitive.getAsLong());

            case "StatusCode":
                return new StatusCode(primitive.getAsLong());

            default:
                return primitive.getAsNumber();
            }
        }
    }

    return null;
}

From source file:org.eclipse.milo.opcua.binaryschema.gson.JsonStructureCodec.java

License:Open Source License

@Override
protected Object memberTypeToOpcUaArray(JsonElement member, String typeName) {
    if ("Bit".equals(typeName)) {
        return member.getAsJsonPrimitive().getAsInt();
    } else {/*from   w w  w .  j a v a 2s .co m*/
        JsonArray array = member.getAsJsonArray();

        Object[] values = new Object[array.size()];

        for (int i = 0; i < array.size(); i++) {
            JsonElement element = array.get(i);
            values[i] = memberTypeToOpcUaScalar(element, typeName);
        }

        return values;
    }
}

From source file:org.eclipse.php.composer.api.json.JsonParser.java

License:Open Source License

private Object buildTree(JsonElement entity) {

    if (entity.isJsonPrimitive()) {
        JsonPrimitive p = entity.getAsJsonPrimitive();
        if (p.isBoolean()) {
            return p.getAsBoolean();
        }//from   w w w. j a v  a  2 s.  co m
        if (p.isNumber()) {
            return p.getAsLong();
        }
        return p.getAsString();
    } else if (entity.isJsonNull()) {
        return null;
    } else if (entity.isJsonArray()) {
        LinkedList<Object> arr = new LinkedList<Object>();
        for (JsonElement el : entity.getAsJsonArray()) {
            arr.add(buildTree(el));
        }
        return arr;
    } else if (entity.isJsonObject()) {
        LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
        for (Entry<String, JsonElement> el : entity.getAsJsonObject().entrySet()) {
            map.put(el.getKey(), buildTree(el.getValue()));
        }
        return map;
    }

    return null;
}

From source file:org.eclipse.smarthome.config.core.ConfigurationDeserializer.java

License:Open Source License

private Configuration deserialize(JsonObject propertiesObject) {
    Configuration configuration = new Configuration();
    for (Entry<String, JsonElement> entry : propertiesObject.entrySet()) {
        JsonElement value = entry.getValue();
        String key = entry.getKey();
        if (value.isJsonPrimitive()) {
            JsonPrimitive primitive = value.getAsJsonPrimitive();
            configuration.put(key, deserialize(primitive));
        } else if (value.isJsonArray()) {
            JsonArray array = value.getAsJsonArray();
            configuration.put(key, deserialize(array));
        } else {/*from  www . j  av a 2 s  .  c  o  m*/
            throw new IllegalArgumentException(
                    "Configuration parameters must be primitives or arrays of primities only but was " + value);
        }
    }
    return configuration;
}