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.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());
    }//from   w w  w.  jav  a  2 s .  co m

    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.ireas.intuition.IntuitionLoader.java

License:Open Source License

private Optional<JsonObject> getDomainObject(final JsonElement element) {
    Optional<JsonObject> domainObject = Optional.absent();

    // two types of valid responses:
    // (1) {"messages": {"<domain>": {  } } }
    // --> messages found
    // (2) {"messages": {"<domain>": false} }
    // --> no messages available

    if (!element.isJsonObject()) {
        throw new IllegalArgumentException();
    }/*from   ww w . j  av  a  2s.  com*/
    JsonObject rootObject = element.getAsJsonObject();
    if (!rootObject.has(KEY_MESSAGES)) {
        throw new IllegalArgumentException();
    }
    JsonElement messagesElement = rootObject.get(KEY_MESSAGES);
    if (!messagesElement.isJsonObject()) {
        throw new IllegalArgumentException();
    }
    JsonObject messagesObject = messagesElement.getAsJsonObject();
    if (!messagesObject.has(domain)) {
        throw new IllegalArgumentException();
    }
    JsonElement domainElement = messagesObject.get(domain);

    if (domainElement.isJsonObject()) {
        // valid response (1): messages found
        domainObject = Optional.of(domainElement.getAsJsonObject());
    } else if (domainElement.isJsonPrimitive()) {
        JsonPrimitive domainPrimitive = domainElement.getAsJsonPrimitive();
        if (!domainPrimitive.isBoolean()) {
            throw new IllegalArgumentException();
        }
        boolean domainBoolean = domainPrimitive.getAsBoolean();
        if (domainBoolean) {
            throw new IllegalArgumentException();
        }
        // valid response (2): no messages available
    } else {
        throw new IllegalArgumentException();
    }

    return domainObject;
}

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   ww w.j a v a2 s .c o m*/
            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 {/*ww  w.ja  v  a2  s  .c o  m*/
            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);
                }/* w w  w.  ja  va 2  s  .c om*/
            } 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 {// www  . j  a  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.jclouds.json.internal.ParseObjectFromElement.java

License:Apache License

public Object apply(JsonElement input) {
    Object value = null;/*from w w  w  .jav  a2s  . co m*/
    if (input == null || input.isJsonNull()) {
        value = null;
    } else if (input.isJsonPrimitive()) {
        JsonPrimitive primitive = input.getAsJsonPrimitive();
        if (primitive.isNumber()) {
            value = primitive.getAsNumber();
        } else if (primitive.isBoolean()) {
            value = primitive.getAsBoolean();
        } else {
            value = primitive.getAsString();
        }
    } else if (input.isJsonArray()) {
        value = Lists.newArrayList(Iterables.transform(input.getAsJsonArray(), this));
    } else if (input.isJsonObject()) {
        value = Maps.<String, Object>newLinkedHashMap(
                Maps.transformValues(JsonObjectAsMap.INSTANCE.apply(input.getAsJsonObject()), this));
    }
    return value;
}

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();
        }/*from   w  w w  . j a  v  a  2  s . com*/
        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.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   ww  w .  j av  a2s .c  o m
        } else if (prim.isString()) {
            return prim.getAsString();
        } else {
            throw new RuntimeException("Unrecognized value: " + value);
        }
    } else {
        return value.toString();
    }
}

From source file:org.kurento.jsonrpc.JsonUtils.java

License:Apache License

public Object toPrimitiveObject(JsonElement element) {

    JsonPrimitive primitive = (JsonPrimitive) element;
    if (primitive.isBoolean()) {
        return Boolean.valueOf(primitive.getAsBoolean());
    } else if (primitive.isNumber()) {
        Number number = primitive.getAsNumber();
        double value = number.doubleValue();
        if ((int) value == value) {
            return Integer.valueOf((int) value);
        }//  w w w.j  a v a2s.  c o m

        return Float.valueOf((float) value);

    } else if (primitive.isString()) {
        return primitive.getAsString();
    } else {
        throw new JsonRpcException("Unrecognized JsonPrimitive: " + primitive);
    }
}