List of usage examples for com.google.gson JsonPrimitive isBoolean
public boolean isBoolean()
From source file:org.openqa.selenium.json.JsonToBeanConverter.java
License:Apache License
private Object convertJsonPrimitive(JsonPrimitive json) { if (json.isBoolean()) { return json.getAsBoolean(); } else if (json.isNumber()) { if (json.getAsLong() == json.getAsDouble()) { return json.getAsLong(); }/* w ww . j a va 2s.c om*/ return json.getAsDouble(); } else if (json.isString()) { return json.getAsString(); } else { return null; } }
From source file:org.plos.crepo.model.metadata.RepoMetadata.java
License:Open Source License
@VisibleForTesting static Object convertJsonToImmutable(JsonElement element) { if (element.isJsonNull()) { return null; }//from ww w .ja v a 2s. c o m if (element.isJsonPrimitive()) { JsonPrimitive primitive = element.getAsJsonPrimitive(); if (primitive.isString()) return primitive.getAsString(); if (primitive.isNumber()) return asNumber(primitive); if (primitive.isBoolean()) return primitive.getAsBoolean(); throw new RuntimeException("JsonPrimitive is not one of the expected primitive types"); } if (element.isJsonArray()) { JsonArray array = element.getAsJsonArray(); if (array.size() == 0) return Collections.emptyList(); List<Object> convertedList = new ArrayList<>(array.size()); for (JsonElement arrayElement : array) { Object convertedElement = convertJsonToImmutable(arrayElement); convertedList.add(convertedElement); } return Collections.unmodifiableList(convertedList); } if (element.isJsonObject()) { Set<Map.Entry<String, JsonElement>> entries = element.getAsJsonObject().entrySet(); if (entries.size() == 0) return Collections.emptyMap(); Map<String, Object> convertedMap = Maps.newHashMapWithExpectedSize(entries.size()); for (Map.Entry<String, JsonElement> entry : entries) { String key = Preconditions.checkNotNull(entry.getKey()); Object value = convertJsonToImmutable(entry.getValue()); convertedMap.put(key, value); } return Collections.unmodifiableMap(convertedMap); } throw new RuntimeException("JsonElement is not one of the expected subtypes"); }
From source file:org.ppojo.data.JsonElementTypes.java
License:Apache License
public static JsonElementTypes getType(JsonElement json) { if (json.isJsonObject()) return JsonElementTypes.JsonObject; if (json.isJsonArray()) return JsonElementTypes.JsonArray; if (json.isJsonNull()) return JsonElementTypes.JsonNull; if (json.isJsonPrimitive()) { JsonPrimitive jsonPrimitive = json.getAsJsonPrimitive(); if (jsonPrimitive.isBoolean()) return JsonElementTypes.Boolean; if (jsonPrimitive.isNumber()) return JsonElementTypes.Number; if (jsonPrimitive.isString()) return JsonElementTypes.String; }/*from w ww . jav a2 s . c o m*/ return JsonElementTypes.Unknown; }
From source file:org.qcert.camp.translator.Rule2CAMP.java
License:Open Source License
/** * Obtain the correct kind of value from a JsonPrimitive node. The kinds we support are int, String, and boolean * @param primitive the JsonPrimitive node * @return the value//from w w w . ja v a 2 s .co m */ private static Object valueFromJson(JsonPrimitive primitive) { if (primitive.isString()) return primitive.getAsString(); if (primitive.isNumber()) return primitive.getAsInt(); if (primitive.isBoolean()) return primitive.getAsBoolean(); throw new IllegalStateException(); }
From source file:org.qcert.runtime.DataComparator.java
License:Apache License
private static DType getType(JsonElement obj) { if (obj == null) { return DType.DT_JNULL; } else if (obj.isJsonNull()) { return DType.DT_NULL; } else if (obj.isJsonPrimitive()) { final JsonPrimitive prim = obj.getAsJsonPrimitive(); if (prim.isBoolean()) { return DType.DT_BOOL; } else if (prim.isString()) { return DType.DT_STRING; } else if (prim.isNumber()) { final Number num = prim.getAsNumber(); if (num instanceof LazilyParsedNumber) { return DType.DT_LAZYNUM; } else if (num instanceof Long || num instanceof Short || num instanceof Integer) { return DType.DT_LONG; } else if (num instanceof Double || num instanceof Short || num instanceof Float) { return DType.DT_DOUBLE; } else { throw new RuntimeException( "Unknown primitive json number type: " + num + " of type " + num.getClass()); }/* ww w . ja va 2 s. c o m*/ } else { throw new RuntimeException("Unknown primitive json type: " + prim); } } else if (obj.isJsonArray()) { return DType.DT_COLL; } else if (obj.isJsonObject()) { return DType.DT_REC; } else { throw new RuntimeException("Unknown json type: " + obj + " of type " + obj.getClass()); } }
From source file:org.spongepowered.common.util.persistence.JsonTranslator.java
License:MIT License
private static Object convert(JsonElement element) { if (element.isJsonObject()) { MemoryDataView container = new MemoryDataContainer(); for (Entry<String, JsonElement> entry : ((JsonObject) element).entrySet()) { Object value = convert(entry.getValue()); if (value != null) { container.set(DataQuery.of(entry.getKey()), value); }/*www .jav a 2s . co m*/ } return container; } else if (element.isJsonArray()) { return Lists.newArrayList(Iterables.transform((JsonArray) element, JsonTranslator::convert)); } else if (element.isJsonPrimitive()) { JsonPrimitive primitive = (JsonPrimitive) element; if (primitive.isString()) { return primitive.getAsString(); } else if (primitive.isBoolean()) { return primitive.getAsBoolean(); } else if (primitive.isNumber()) { return primitive.getAsNumber(); } } return null; }
From source file:org.springframework.data.cloudant.core.UnmappedDataAdapter.java
License:Apache License
@Override public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") .registerTypeAdapter(DateTime.class, new DateTimeDataAdapter()).create(); T doc = gson.fromJson(json, typeOfT); Map<String, Object> unmapped = new HashMap<>(); List<String> nameList = getNestedField(doc.getClass()); JsonObject object = json.getAsJsonObject(); //add support for annotated fields ...hack for now nameList.add("_id"); nameList.add("_rev"); nameList.add("doc_type"); for (Map.Entry<String, JsonElement> entry : object.entrySet()) { if (!nameList.contains(entry.getKey())) { if (entry.getValue().isJsonArray()) { unmapped.put(entry.getKey(), entry.getValue().getAsJsonArray()); } else if (entry.getValue().isJsonObject()) { unmapped.put(entry.getKey(), entry.getValue().getAsJsonObject()); } else if (entry.getValue().isJsonPrimitive()) { JsonPrimitive v = entry.getValue().getAsJsonPrimitive(); if (v.isBoolean()) { unmapped.put(entry.getKey(), v.getAsBoolean()); } else if (v.isNumber()) { unmapped.put(entry.getKey(), v.getAsNumber()); } else if (v.isString()) { unmapped.put(entry.getKey(), v.getAsString()); } else if (v.isJsonNull()) { unmapped.put(entry.getKey(), null); }/*from w w w . ja v a2s .c o m*/ } else if (entry.getValue().isJsonNull()) { unmapped.put(entry.getKey(), null); } else { unmapped.put(entry.getKey(), entry.getValue().getAsString()); } } } doc.setUnmappedFields(unmapped); return doc; }
From source file:org.structr.core.rest.JsonInputGSONAdapter.java
License:Open Source License
public static Object fromPrimitive(final JsonPrimitive p) { if (p.isNumber()) { Number number = p.getAsNumber(); // Detect if value is floating point if (p.getAsString().contains(".")) { return number.doubleValue(); } else {/*w w w . j a v a 2 s .c o m*/ return number.longValue(); } } else if (p.isBoolean()) { return p.getAsBoolean(); } return p.getAsString(); }
From source file:org.terasology.rendering.nui.widgets.models.JsonTreeConverter.java
License:Apache License
/** * @param name The name to be given to this node (if null, is replaced by a default name). * @param json The {@link JsonElement} to be converted to a {@link JsonTree}. * @return A tree representation of the JSON hierarchy. */// w w w. j av a 2s .c o m private static JsonTree serialize(String name, JsonElement json) { if (json.isJsonPrimitive()) { JsonPrimitive primitive = json.getAsJsonPrimitive(); if (primitive.isBoolean()) { return new JsonTree(new JsonTreeNode(name, json.getAsBoolean(), name != null ? JsonTreeNode.ElementType.KEY_VALUE_PAIR : JsonTreeNode.ElementType.VALUE)); } else if (primitive.isNumber()) { return new JsonTree(new JsonTreeNode(name, json.getAsNumber(), name != null ? JsonTreeNode.ElementType.KEY_VALUE_PAIR : JsonTreeNode.ElementType.VALUE)); } else if (primitive.isString()) { return new JsonTree(new JsonTreeNode(name, json.getAsString(), name != null ? JsonTreeNode.ElementType.KEY_VALUE_PAIR : JsonTreeNode.ElementType.VALUE)); } else { return new JsonTree(new JsonTreeNode(name, null, name != null ? JsonTreeNode.ElementType.KEY_VALUE_PAIR : JsonTreeNode.ElementType.VALUE)); } } else if (json.isJsonArray()) { JsonTree tree = new JsonTree(new JsonTreeNode(name, null, JsonTreeNode.ElementType.ARRAY)); JsonArray array = json.getAsJsonArray(); for (int i = 0; i < array.size(); i++) { tree.addChild(serialize(array.get(i))); } return tree; } else if (json.isJsonObject()) { JsonTree tree = new JsonTree(new JsonTreeNode(name, null, JsonTreeNode.ElementType.OBJECT)); JsonObject object = json.getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : object.entrySet()) { tree.addChild(serialize(entry.getKey(), entry.getValue())); } return tree; } else { return new JsonTree(new JsonTreeNode(name, null, JsonTreeNode.ElementType.NULL)); } }
From source file:org.terasology.rendering.nui.widgets.treeView.JsonTreeConverter.java
License:Apache License
/** * @param name The name to be given to this node (if null, is replaced by a default name). * @param json The {@link JsonElement} to be converted to a {@link JsonTree}. * @return A tree representation of the JSON hierarchy. *//*from www. j av a 2s .c om*/ private static JsonTree serialize(String name, JsonElement json) { if (json.isJsonPrimitive()) { JsonPrimitive primitive = json.getAsJsonPrimitive(); if (primitive.isBoolean()) { return new JsonTree(new JsonTreeValue(name, json.getAsBoolean(), name != null ? JsonTreeValue.Type.KEY_VALUE_PAIR : JsonTreeValue.Type.VALUE)); } else if (primitive.isNumber()) { return new JsonTree(new JsonTreeValue(name, json.getAsNumber(), name != null ? JsonTreeValue.Type.KEY_VALUE_PAIR : JsonTreeValue.Type.VALUE)); } else if (primitive.isString()) { return new JsonTree(new JsonTreeValue(name, json.getAsString(), name != null ? JsonTreeValue.Type.KEY_VALUE_PAIR : JsonTreeValue.Type.VALUE)); } else { return new JsonTree(new JsonTreeValue(name, null, name != null ? JsonTreeValue.Type.KEY_VALUE_PAIR : JsonTreeValue.Type.VALUE)); } } else if (json.isJsonArray()) { JsonTree tree = new JsonTree(new JsonTreeValue(name, null, JsonTreeValue.Type.ARRAY)); JsonArray array = json.getAsJsonArray(); for (int i = 0; i < array.size(); i++) { tree.addChild(serialize(array.get(i))); } return tree; } else if (json.isJsonObject()) { JsonTree tree = new JsonTree(new JsonTreeValue(name, null, JsonTreeValue.Type.OBJECT)); JsonObject object = json.getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : object.entrySet()) { tree.addChild(serialize(entry.getKey(), entry.getValue())); } return tree; } else { return new JsonTree(new JsonTreeValue(name, null, JsonTreeValue.Type.NULL)); } }