List of usage examples for com.google.gson JsonPrimitive isNumber
public boolean isNumber()
From source file:org.hibernate.search.elasticsearch.schema.impl.DefaultElasticsearchSchemaValidator.java
License:LGPL
private static void validateJsonPrimitive(ValidationErrorCollector errorCollector, DataType type, String attributeName, JsonPrimitive expectedValue, JsonPrimitive actualValue) { DataType defaultedType = type == null ? DataType.OBJECT : type; // We can't just use equal, mainly because of floating-point numbers switch (defaultedType) { case DOUBLE://from w ww . ja v a2 s . c o m if (expectedValue.isNumber() && actualValue.isNumber()) { validateEqualWithDefault(errorCollector, attributeName, expectedValue.getAsDouble(), actualValue.getAsDouble(), DEFAULT_DOUBLE_DELTA, null); } else { errorCollector.addError(MESSAGES.invalidAttributeValue(attributeName, expectedValue, actualValue)); } break; case FLOAT: if (expectedValue.isNumber() && actualValue.isNumber()) { validateEqualWithDefault(errorCollector, attributeName, expectedValue.getAsFloat(), actualValue.getAsFloat(), DEFAULT_FLOAT_DELTA, null); } else { errorCollector.addError(MESSAGES.invalidAttributeValue(attributeName, expectedValue, actualValue)); } break; case INTEGER: case LONG: case DATE: case BOOLEAN: case STRING: case OBJECT: case GEO_POINT: default: validateEqualWithDefault(errorCollector, attributeName, expectedValue, actualValue, null); break; } }
From source file:org.hibernate.search.elasticsearch.schema.impl.Elasticsearch2SchemaValidator.java
License:LGPL
@SuppressWarnings("deprecation") protected void doValidateJsonPrimitive(ValidationErrorCollector errorCollector, DataType type, String attributeName, JsonPrimitive expectedValue, JsonPrimitive actualValue) { // We can't just use equal, mainly because of floating-point numbers switch (type) { case DOUBLE:/*from ww w . ja v a 2s .c o m*/ if (expectedValue.isNumber() && actualValue.isNumber()) { validateEqualWithDefault(errorCollector, attributeName, expectedValue.getAsDouble(), actualValue.getAsDouble(), DEFAULT_DOUBLE_DELTA, null); } else { errorCollector.addError(MESSAGES.invalidAttributeValue(attributeName, expectedValue, actualValue)); } break; case FLOAT: if (expectedValue.isNumber() && actualValue.isNumber()) { validateEqualWithDefault(errorCollector, attributeName, expectedValue.getAsFloat(), actualValue.getAsFloat(), DEFAULT_FLOAT_DELTA, null); } else { errorCollector.addError(MESSAGES.invalidAttributeValue(attributeName, expectedValue, actualValue)); } break; case INTEGER: case LONG: case DATE: case BOOLEAN: case STRING: case OBJECT: case GEO_POINT: default: validateEqualWithDefault(errorCollector, attributeName, expectedValue, actualValue, null); break; } }
From source file:org.hibernate.search.elasticsearch.schema.impl.Elasticsearch56SchemaValidator.java
License:LGPL
private void doValidateJsonPrimitive(ValidationErrorCollector errorCollector, DataType type, String attributeName, JsonPrimitive expectedValue, JsonPrimitive actualValue) { // We can't just use equal, mainly because of floating-point numbers switch (type) { case TEXT:/*from w ww. j a va 2 s .c o m*/ case KEYWORD: validateEqualWithDefault(errorCollector, attributeName, expectedValue, actualValue, null); break; case DOUBLE: if (expectedValue.isNumber() && actualValue.isNumber()) { validateEqualWithDefault(errorCollector, attributeName, expectedValue.getAsDouble(), actualValue.getAsDouble(), DEFAULT_DOUBLE_DELTA, null); } else { errorCollector.addError(MESSAGES.invalidAttributeValue(attributeName, expectedValue, actualValue)); } break; case FLOAT: if (expectedValue.isNumber() && actualValue.isNumber()) { validateEqualWithDefault(errorCollector, attributeName, expectedValue.getAsFloat(), actualValue.getAsFloat(), DEFAULT_FLOAT_DELTA, null); } else { errorCollector.addError(MESSAGES.invalidAttributeValue(attributeName, expectedValue, actualValue)); } break; case INTEGER: case LONG: case DATE: case BOOLEAN: case OBJECT: case GEO_POINT: default: validateEqualWithDefault(errorCollector, attributeName, expectedValue, actualValue, null); break; } }
From source file:org.hillview.storage.JsonFileLoader.java
License:Open Source License
private static ContentsKind getKind(@Nullable JsonElement e) { if (e == null || e.isJsonNull()) return ContentsKind.None; if (e.isJsonArray() || e.isJsonObject()) throw new RuntimeException("Values must be simple " + e); JsonPrimitive prim = e.getAsJsonPrimitive(); if (prim.isBoolean()) return ContentsKind.String; if (prim.isNumber()) return ContentsKind.Double; if (prim.isString()) return ContentsKind.String; throw new RuntimeException("Unexpected JSON value " + prim); }
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();//ww w.j a v a 2s . c o m 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.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 . j a 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 www . 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 {/* 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); }//from w w w. java 2 s .co 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 {//w w w .j a va 2s .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"); } } } }