Example usage for com.google.gson JsonPrimitive isString

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

Introduction

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

Prototype

public boolean isString() 

Source Link

Document

Check whether this primitive contains a String value.

Usage

From source file:org.hillview.storage.JsonFileLoader.java

License:Open Source License

void append(IAppendableColumn[] columns, JsonElement e) {
    if (!e.isJsonObject())
        this.error("JSON array element is not a JsonObject");
    JsonObject obj = e.getAsJsonObject();
    for (IAppendableColumn col : columns) {
        JsonElement el = obj.get(col.getName());
        if (el == null || el.isJsonNull()) {
            col.appendMissing();/*from   w w  w  .  ja  v  a  2s. c  om*/
            continue;
        }

        if (!el.isJsonPrimitive())
            this.error("JSON array element is a non-primitive field");
        JsonPrimitive prim = el.getAsJsonPrimitive();

        if (prim.isBoolean()) {
            col.append(prim.getAsBoolean() ? "true" : "false");
        } else if (prim.isNumber()) {
            col.append(prim.getAsDouble());
        } else if (prim.isString()) {
            col.parseAndAppendString(prim.getAsString());
        } else {
            this.error("Unexpected Json value" + prim.toString());
        }
    }
}

From source file:org.i3xx.step.uno.impl.util.GsonHashMapDeserializer.java

License:Apache License

private Object handlePrimitive(JsonPrimitive json) {
    if (json.isBoolean())
        return json.getAsBoolean();
    else if (json.isString())
        return json.getAsString();
    else {/*from   w  w w. ja va  2s  .c  o  m*/
        //System.out.println("GND handlePrimitive: "+json.getAsString());
        BigDecimal bigDec = json.getAsBigDecimal();
        if (json.getAsString().indexOf('.') == -1) {
            // Find out if it is an int type
            try {
                //System.out.println("GND: "+bigDec.toPlainString());
                /*
                bigDec.toBigIntegerExact();
                try { 
                   return bigDec.intValueExact(); }
                catch(ArithmeticException e) {}
                */
                return bigDec.longValue();
            } catch (ArithmeticException e) {
            }
        }
        // Just return it as a double
        return bigDec.doubleValue();
    }
}

From source file:org.immutables.mongo.fixture.holder.HolderJsonSerializer.java

License:Apache License

@Override
public Holder deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject root = (JsonObject) json;

    ImmutableHolder.Builder builder = ImmutableHolder.builder();

    if (root.has("id")) {
        builder.id(root.get("id").getAsString());
    }// w  w  w .  ja v  a2  s.  com

    JsonElement value = root.get(VALUE_PROPERTY);
    if (value == null) {
        throw new JsonParseException(String.format("%s not found for %s in JSON", VALUE_PROPERTY, type));
    }

    if (value.isJsonObject()) {
        final String valueTypeName = value.getAsJsonObject().get(Holder.TYPE_PROPERTY).getAsString();
        try {
            Class<?> valueType = Class.forName(valueTypeName);
            builder.value(context.deserialize(value, valueType));
        } catch (ClassNotFoundException e) {
            throw new JsonParseException(
                    String.format("Couldn't construct value class %s for %s", valueTypeName, type), e);
        }
    } else if (value.isJsonPrimitive()) {
        final JsonPrimitive primitive = value.getAsJsonPrimitive();
        if (primitive.isString()) {
            builder.value(primitive.getAsString());
        } else if (primitive.isNumber()) {
            builder.value(primitive.getAsInt());
        } else if (primitive.isBoolean()) {
            builder.value(primitive.getAsBoolean());
        }
    } else {
        throw new JsonParseException(String.format("Couldn't deserialize %s : %s. Not a primitive or object",
                VALUE_PROPERTY, value));
    }

    return builder.build();

}

From source file:org.jboss.aerogear.android.impl.datamanager.SQLStore.java

License:Apache License

private void saveElement(JsonObject serialized, String path, Serializable id) {
    String sql = String.format(
            "insert into %s_property (PROPERTY_NAME, PROPERTY_VALUE, PARENT_ID) values (?,?,?)", className);
    Set<Entry<String, JsonElement>> members = serialized.entrySet();
    String pathVar = path.isEmpty() ? "" : ".";
    for (Entry<String, JsonElement> member : members) {
        JsonElement jsonValue = member.getValue();
        String propertyName = member.getKey();
        if (jsonValue.isJsonObject()) {
            saveElement((JsonObject) jsonValue, path + pathVar + propertyName, id);
        } else {/*from   w w w .j a  v  a  2  s  .  c  om*/
            if (jsonValue.isJsonPrimitive()) {
                JsonPrimitive primitive = jsonValue.getAsJsonPrimitive();
                if (primitive.isBoolean()) {
                    Integer value = primitive.getAsBoolean() ? 1 : 0;
                    database.execSQL(sql, new Object[] { path + pathVar + propertyName, value, id });
                } else if (primitive.isNumber()) {
                    Number value = primitive.getAsNumber();
                    database.execSQL(sql, new Object[] { path + pathVar + propertyName, value, id });
                } else if (primitive.isString()) {
                    String value = primitive.getAsString();
                    database.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.jboss.aerogear.android.impl.datamanager.SQLStore.java

License:Apache License

private void buildKeyValuePairs(JsonObject where, List<Pair<String, String>> keyValues, String parentPath) {
    Set<Entry<String, JsonElement>> keys = where.entrySet();
    String pathVar = parentPath.isEmpty() ? "" : ".";//Set a dot if parent path is not empty
    for (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  ww.j  a  v  a 2s . c om
            if (jsonValue.isJsonPrimitive()) {
                JsonPrimitive primitive = jsonValue.getAsJsonPrimitive();
                if (primitive.isBoolean()) {
                    Integer value = primitive.getAsBoolean() ? 1 : 0;
                    keyValues.add(new Pair<String, String>(path, value.toString()));
                } 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.jboss.aerogear.android.store.sql.SQLStore.java

License:Apache License

private void saveElement(JsonElement serialized, String path, Serializable id) {
    String sql = String.format(
            "insert into %s_property (PROPERTY_NAME, PROPERTY_VALUE, PARENT_ID) values (?,?,?)", className);

    if (serialized.isJsonObject()) {
        Set<Entry<String, JsonElement>> members = ((JsonObject) serialized).entrySet();
        String pathVar = path.isEmpty() ? "" : ".";

        for (Entry<String, JsonElement> member : members) {
            JsonElement jsonValue = member.getValue();
            String propertyName = member.getKey();

            if (jsonValue.isJsonArray()) {
                JsonArray jsonArray = jsonValue.getAsJsonArray();
                for (int index = 0; index < jsonArray.size(); index++) {
                    saveElement(jsonArray.get(index),
                            path + pathVar + propertyName + String.format("[%d]", index), id);
                }// ww  w .  j a va2s .  c o  m
            } else {
                saveElement(jsonValue, path + pathVar + propertyName, id);
            }
        }
    } else if (serialized.isJsonPrimitive()) {
        JsonPrimitive primitive = serialized.getAsJsonPrimitive();
        if (primitive.isBoolean()) {
            String value = primitive.getAsBoolean() ? "true" : "false";
            database.execSQL(sql, new Object[] { path, value, id });
        } else if (primitive.isNumber()) {
            Number value = primitive.getAsNumber();
            database.execSQL(sql, new Object[] { path, value, id });
        } else if (primitive.isString()) {
            String value = primitive.getAsString();
            database.execSQL(sql, new Object[] { path, value, id });
        } else {
            throw new IllegalArgumentException(serialized + " isn't a number, boolean, or string");
        }
    } else {
        throw new IllegalArgumentException(serialized + " isn't a JsonObject or JsonPrimitive");
    }
}

From source file:org.jboss.aerogear.android.store.sql.SQLStore.java

License:Apache License

private void buildKeyValuePairs(JsonObject where, List<Pair<String, String>> keyValues, String parentPath) {
    Set<Entry<String, JsonElement>> keys = where.entrySet();
    String pathVar = parentPath.isEmpty() ? "" : ".";// Set a dot if parent path is not empty
    for (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  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.jsonddl.generator.InferSchema.java

License:Apache License

private Type inferType(String propertyName, JsonElement node) {
    if (node.isJsonPrimitive()) {
        JsonPrimitive p = node.getAsJsonPrimitive();
        if (p.isBoolean()) {
            return new Type.Builder().withKind(Kind.BOOLEAN).build();
        }/* w  w w. j av a 2  s.c  om*/
        if (p.isNumber()) {
            return new Type.Builder().withKind(Kind.DOUBLE).build();
        }
        if (p.isString()) {
            return new Type.Builder().withKind(Kind.STRING).build();
        }
        throw new RuntimeException("Unhandled primitive type " + p.toString());
    }

    if (node.isJsonObject()) {
        JsonObject object = node.getAsJsonObject();
        return inferFromObject(propertyName, object);
    }

    if (node.isJsonArray()) {
        return inferFromArray(propertyName, node.getAsJsonArray());
    }

    throw new RuntimeException("Could not infer type from node " + node.toString());

}

From source file:org.kairosdb.core.http.rest.json.DataPointsParser.java

License:Apache License

private String findType(JsonElement value) {
    checkState(value.isJsonPrimitive());

    JsonPrimitive primitiveValue = (JsonPrimitive) value;
    if (primitiveValue.isNumber() || (primitiveValue.isString() && Util.isNumber(value.getAsString()))) {
        String v = value.getAsString();

        if (!v.contains(".")) {
            return "long";
        } else {/*from  ww w .  j  a  v a2 s  . c o m*/
            return "double";
        }
    } else
        return "string";
}

From source file:org.kurento.commons.BasicJsonUtils.java

License:Apache License

private static Object convertValue(JsonElement value) {
    if (value.isJsonNull()) {
        return null;
    } else if (value.isJsonPrimitive()) {
        JsonPrimitive prim = value.getAsJsonPrimitive();
        if (prim.isBoolean()) {
            return prim.getAsBoolean();
        } else if (prim.isNumber()) {
            Number n = prim.getAsNumber();
            if (n.doubleValue() == n.intValue()) {
                return n.intValue();
            } else {
                return n.doubleValue();
            }/*from w w w.  j  a va2  s. c  om*/
        } else if (prim.isString()) {
            return prim.getAsString();
        } else {
            throw new RuntimeException("Unrecognized value: " + value);
        }
    } else {
        return value.toString();
    }
}