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.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 av a2s.  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 {//  ww 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;
                    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.  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 {/*from  w w w .j a  v  a 2  s. co  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.jbpm.form.builder.services.encoders.FormRepresentationDecoderImpl.java

License:Apache License

private Object fromJsonValue(JsonElement elem) {
    if (elem.isJsonPrimitive() && elem.getAsJsonPrimitive().isString()) {
        return elem.getAsString();
    } else if (elem.isJsonPrimitive() && elem.getAsJsonPrimitive().isNumber()) {
        return elem.getAsNumber();
    } else if (elem.isJsonArray()) {
        return asList(elem.getAsJsonArray());
    } else if (elem.isJsonNull()) {
        return null;
    } else if (elem.isJsonObject()) {
        return asMap(elem.getAsJsonObject());
    } else {/*from  w w w  . j ava2s  .  co  m*/
        return "";
    }
}

From source file:org.jclouds.gogrid.config.DateSecondsAdapter.java

License:Apache License

public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    String toParse = json.getAsJsonPrimitive().getAsString();
    return new Date(Long.valueOf(toParse));
}

From source file:org.jclouds.json.internal.ParseObjectFromElement.java

License:Apache License

public Object apply(JsonElement input) {
    Object value = null;//from w  ww  .j ava  2 s.  c o  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.  jav  a2  s  . co  m*/
        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 www.  ja  va2s  .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.locationtech.geogig.spring.service.LegacyBatchObjectsService.java

License:Open Source License

public BatchObjects batchObjects(RepositoryProvider provider, String repoName, InputStream request) {
    final BatchObjects batchObjects = new BatchObjects();
    final Reader body = new InputStreamReader(request);
    final JsonParser parser = new JsonParser();
    final JsonElement messageJson = parser.parse(body);
    final ArrayList<ObjectId> want = new ArrayList<>();
    final ArrayList<ObjectId> have = new ArrayList<>();

    if (messageJson.isJsonObject()) {
        final JsonObject message = messageJson.getAsJsonObject();
        final JsonArray wantArray;
        final JsonArray haveArray;
        if (message.has("want") && message.get("want").isJsonArray()) {
            wantArray = message.get("want").getAsJsonArray();
        } else {/*w  w  w  .jav  a2 s.  c  o m*/
            wantArray = new JsonArray();
        }
        if (message.has("have") && message.get("have").isJsonArray()) {
            haveArray = message.get("have").getAsJsonArray();
        } else {
            haveArray = new JsonArray();
        }
        for (final JsonElement e : wantArray) {
            if (e.isJsonPrimitive()) {
                want.add(ObjectId.valueOf(e.getAsJsonPrimitive().getAsString()));
            }
        }
        for (final JsonElement e : haveArray) {
            if (e.isJsonPrimitive()) {
                have.add(ObjectId.valueOf(e.getAsJsonPrimitive().getAsString()));
            }
        }
    }

    final Repository repository = getRepository(provider, repoName);
    final Deduplicator deduplicator = DeduplicationService.create();
    BinaryPackedObjects packer = new BinaryPackedObjects(repository.objectDatabase());

    batchObjects.setDeduplicator(deduplicator).setHave(have).setPacker(packer).setWant(want);
    return batchObjects;
}