Example usage for com.google.gson JsonPrimitive isBoolean

List of usage examples for com.google.gson JsonPrimitive isBoolean

Introduction

In this page you can find the example usage for com.google.gson JsonPrimitive isBoolean.

Prototype

public boolean isBoolean() 

Source Link

Document

Check whether this primitive contains a boolean value.

Usage

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 {//w w w.j  a  v a2 s.  c o  m
            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  a2  s .c o  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.leshan.server.demo.servlet.json.LwM2mNodeDeserializer.java

License:Open Source License

private org.eclipse.leshan.core.model.ResourceModel.Type getTypeFor(JsonPrimitive val) {
    if (val.isBoolean())
        return org.eclipse.leshan.core.model.ResourceModel.Type.BOOLEAN;
    if (val.isString())
        return org.eclipse.leshan.core.model.ResourceModel.Type.STRING;
    if (val.isNumber()) {
        if (val.getAsDouble() == val.getAsLong()) {
            return org.eclipse.leshan.core.model.ResourceModel.Type.INTEGER;
        } else {/* w  ww  .ja v  a  2  s.com*/
            return org.eclipse.leshan.core.model.ResourceModel.Type.FLOAT;
        }
    }
    // use string as default value
    return org.eclipse.leshan.core.model.ResourceModel.Type.STRING;
}

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

License:Open Source License

private Value<?> deserializeValue(JsonPrimitive val) {
    Value<?> value = null;/*  w  w w .j  a va2s  .c  o m*/
    if (val.isNumber()) {

        Number n = val.getAsNumber();
        if (n.doubleValue() == (long) n.doubleValue()) {
            Long lValue = Long.valueOf(n.longValue());
            if (lValue >= Integer.MIN_VALUE && lValue <= Integer.MAX_VALUE) {
                value = Value.newIntegerValue(lValue.intValue());
            } else {
                value = Value.newLongValue(lValue);
            }
        } else {
            Double dValue = Double.valueOf(n.doubleValue());
            if (dValue >= Float.MIN_VALUE && dValue <= Float.MAX_VALUE) {
                value = Value.newFloatValue(dValue.floatValue());
            } else {
                value = Value.newDoubleValue(dValue);
            }
        }

    } else if (val.isBoolean()) {
        value = Value.newBooleanValue(val.getAsBoolean());
    } else if (val.isString()) {
        value = Value.newStringValue(val.getAsString());
    }
    return value;
}

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();
            }/*w  w  w.  j  a v a2 s  . c om*/

            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.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();
        }/*w w  w.  j  av  a2  s  .c o  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.scada.base.json.VariantJsonDeserializer.java

License:Open Source License

private Variant decodeFromPrimitive(final JsonElement json) {
    final JsonPrimitive jsonPrim = (JsonPrimitive) json;
    if (jsonPrim.isBoolean()) {
        return Variant.valueOf(jsonPrim.getAsBoolean());
    } else if (jsonPrim.isNumber()) {
        return Variant.valueOf(jsonPrim.getAsNumber());
    } else {//w w  w . ja va 2s.  co  m
        return VariantEditor.toVariant(jsonPrim.getAsString());
    }
}

From source file:org.eclipse.smarthome.automation.parser.gson.internal.ConfigurationDeserializer.java

License:Open Source License

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

    Configuration configuration = new Configuration();
    JsonObject jo = (JsonObject) json;/*from   w  w  w .  j  ava2 s.  com*/
    for (Entry<String, JsonElement> e : jo.entrySet()) {
        JsonPrimitive value = e.getValue().getAsJsonPrimitive();
        if (value.isString()) {
            configuration.put(e.getKey(), value.getAsString());
        } else if (value.isNumber()) {
            configuration.put(e.getKey(), value.getAsBigDecimal());
        } else if (value.isBoolean()) {
            configuration.put(e.getKey(), value.getAsBoolean());
        }
    }
    return configuration;
}

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

License:Open Source License

private Object deserialize(JsonPrimitive primitive) {
    if (primitive.isString()) {
        return primitive.getAsString();
    } else if (primitive.isNumber()) {
        return primitive.getAsBigDecimal();
    } else if (primitive.isBoolean()) {
        return primitive.getAsBoolean();
    } else {/*from  www  .  j  a  v  a  2 s.c o m*/
        throw new IllegalArgumentException("Unsupported primitive: " + primitive);
    }
}

From source file:org.eel.kitchen.jsonschema.GsonProvider.java

License:Open Source License

private static JsonNode gsonToValueNode(final JsonPrimitive primitive) {
    if (primitive.isBoolean())
        return factory.booleanNode(primitive.getAsBoolean());
    if (primitive.isNumber())
        return toNumberNode(primitive.getAsBigDecimal());

    // Can only be a string,now
    return factory.textNode(primitive.getAsString());
}