List of usage examples for com.google.gson JsonPrimitive isNumber
public boolean isNumber()
From source file:com.jayway.jsonpath.internal.spi.json.GsonJsonProvider.java
License:Apache License
public static Object unwrap(Object o) { if (o == null) { return null; }//from w w w.j av a 2 s. c om if (!(o instanceof JsonElement)) { return o; } JsonElement e = (JsonElement) o; if (e.isJsonNull()) { return null; } else if (e.isJsonPrimitive()) { JsonPrimitive p = e.getAsJsonPrimitive(); if (p.isString()) { return p.getAsString(); } else if (p.isBoolean()) { return p.getAsBoolean(); } else if (p.isNumber()) { return unwrapNumber(p.getAsNumber()); } } return o; }
From source file:com.jayway.jsonpath.internal.spi.mapper.GsonMapper.java
License:Apache License
@Override public Object convert(Object src, Class<?> srcType, Class<?> targetType, Configuration conf) { assertValidConversion(src, srcType, targetType); if (src == null || src.getClass().equals(JsonNull.class)) { return null; }/*from w ww .jav a 2 s . c o m*/ if (JsonPrimitive.class.isAssignableFrom(srcType)) { JsonPrimitive primitive = (JsonPrimitive) src; if (targetType.equals(Long.class)) { return primitive.getAsLong(); } else if (targetType.equals(Integer.class)) { return primitive.getAsInt(); } else if (targetType.equals(BigInteger.class)) { return primitive.getAsBigInteger(); } else if (targetType.equals(Byte.class)) { return primitive.getAsByte(); } else if (targetType.equals(BigDecimal.class)) { return primitive.getAsBigDecimal(); } else if (targetType.equals(Double.class)) { return primitive.getAsDouble(); } else if (targetType.equals(Float.class)) { return primitive.getAsFloat(); } else if (targetType.equals(String.class)) { return primitive.getAsString(); } else if (targetType.equals(Boolean.class)) { return primitive.getAsBoolean(); } else if (targetType.equals(Date.class)) { if (primitive.isNumber()) { return new Date(primitive.getAsLong()); } else if (primitive.isString()) { try { return DateFormat.getInstance().parse(primitive.getAsString()); } catch (ParseException e) { throw new MappingException(e); } } } } else if (JsonObject.class.isAssignableFrom(srcType)) { JsonObject srcObject = (JsonObject) src; if (targetType.equals(Map.class)) { Map<String, Object> targetMap = new LinkedHashMap<String, Object>(); for (Map.Entry<String, JsonElement> entry : srcObject.entrySet()) { Object val = null; JsonElement element = entry.getValue(); if (element.isJsonPrimitive()) { val = GsonJsonProvider.unwrap(element); } else if (element.isJsonArray()) { val = convert(element, element.getClass(), List.class, conf); } else if (element.isJsonObject()) { val = convert(element, element.getClass(), Map.class, conf); } else if (element.isJsonNull()) { val = null; } targetMap.put(entry.getKey(), val); } return targetMap; } } else if (JsonArray.class.isAssignableFrom(srcType)) { JsonArray srcArray = (JsonArray) src; if (targetType.equals(List.class)) { List<Object> targetList = new ArrayList<Object>(); for (JsonElement element : srcArray) { if (element.isJsonPrimitive()) { targetList.add(GsonJsonProvider.unwrap(element)); } else if (element.isJsonArray()) { targetList.add(convert(element, element.getClass(), List.class, conf)); } else if (element.isJsonObject()) { targetList.add(convert(element, element.getClass(), Map.class, conf)); } else if (element.isJsonNull()) { targetList.add(null); } } return targetList; } } throw new MappingException("Can not map: " + srcType.getName() + " to: " + targetType.getName()); }
From source file:com.jayway.jsonpath.spi.json.GsonJsonProvider.java
License:Apache License
public Object unwrap(final Object o) { if (o == null) { return null; }/* w w w .j av a2s .c om*/ if (!(o instanceof JsonElement)) { return o; } JsonElement e = (JsonElement) o; if (e.isJsonNull()) { return null; } else if (e.isJsonPrimitive()) { JsonPrimitive p = e.getAsJsonPrimitive(); if (p.isString()) { return p.getAsString(); } else if (p.isBoolean()) { return p.getAsBoolean(); } else if (p.isNumber()) { return unwrapNumber(p.getAsNumber()); } } return o; }
From source file:com.kurento.kmf.jsonrpcconnector.JsonUtils.java
License:Open Source License
public Object toPrimitiveObject(JsonElement element) { JsonPrimitive primitive = (JsonPrimitive) element; if (primitive.isBoolean()) { return primitive.getAsBoolean(); } else if (primitive.isNumber()) { Number number = primitive.getAsNumber(); double value = number.doubleValue(); if (((int) value == value)) { return (int) value; } else {/*from w w w . j av a2 s . co m*/ return (float) value; } } else if (primitive.isString()) { return primitive.getAsString(); } else { throw new RuntimeException("Unrecognized JsonPrimitive: " + primitive); } }
From source file:com.make.json2java.ClassField.java
License:Apache License
/** * Based on the seen json values, infer a type for this field. * Strings are mapped to String. numbers are preferred mapped to ints, then longs and finally as doubles. */// w ww .j a v a 2 s. co m private InferredType inferType(Iterable<JsonElement> jsonValues, String type, boolean isArrayType) { if (mappedType) return new InferredType(type, isArrayType); InferredType inferredType = new InferredType(type, isArrayType); for (JsonElement jsonValue : jsonValues) { if (jsonValue instanceof JsonPrimitive) { JsonPrimitive primitive = jsonValue.getAsJsonPrimitive(); if (isBooleanValue(primitive)) { inferredType = new InferredType("boolean", false); } else if (primitive.isString()) { inferredType = new InferredType("String", false); } else if (primitive.isNumber()) { double number = primitive.getAsDouble(); boolean isWholeNumber = number - Math.ceil(number) == 0; if (isWholeNumber) { // int is preferred over long so look for that long longValue = (long) number; boolean isLargerThanInt = longValue > Integer.MAX_VALUE || longValue < Integer.MIN_VALUE; if (isLargerThanInt && !inferredType.type.equals("double")) { // some other value was a floating point inferredType = new InferredType("long", false); } else { // some other jsonValue was big enough to fit in long if (!inferredType.equals("long") && !inferredType.equals("double")) { inferredType = new InferredType("int", false); } } } else { // double is preferred over float inferredType = new InferredType("double", false); } } } else if (jsonValue instanceof JsonArray) { this.isArrayType = true; inferredType = new InferredType(inferType(jsonValue.getAsJsonArray(), type, false).type, true); } } return inferredType; }
From source file:com.microsoft.windowsazure.mobileservices.table.MobileServiceTableBase.java
License:Open Source License
/** * Updates the JsonObject to have an id property * * @param json the element to evaluate//from w w w .j a va 2 s. c om */ protected void updateIdProperty(final JsonObject json) throws IllegalArgumentException { for (Entry<String, JsonElement> entry : json.entrySet()) { String key = entry.getKey(); if (key.equalsIgnoreCase("id")) { JsonElement element = entry.getValue(); if (isValidTypeId(element)) { if (!key.equals("id")) { // force the id name to 'id', no matter the casing json.remove(key); // Create a new id property using the given property // name JsonPrimitive value = entry.getValue().getAsJsonPrimitive(); if (value.isNumber()) { json.addProperty("id", value.getAsLong()); } else { json.addProperty("id", value.getAsString()); } } return; } else { throw new IllegalArgumentException("The id must be numeric or string"); } } } }
From source file:com.microsoft.windowsazure.mobileservices.table.MobileServiceTableBase.java
License:Open Source License
/** * Validates if the object represents a numeric value. * * @param o//from ww w. j a v a 2 s . c om * @return */ protected boolean isNumericType(Object o) { boolean result = (o instanceof Integer) || (o instanceof Long); if (o instanceof JsonElement) { JsonElement json = (JsonElement) o; if (json.isJsonPrimitive()) { JsonPrimitive primitive = json.getAsJsonPrimitive(); result = primitive.isNumber(); } } return result; }
From source file:com.microsoft.windowsazure.mobileservices.table.MobileServiceTableBase.java
License:Open Source License
/** * Returns the numeric value represented by the object. * * @param o/*from w ww . j a v a 2 s . c o m*/ * @return */ protected long getNumericValue(Object o) { long result; if (o instanceof Integer) { result = (Integer) o; } else if (o instanceof Long) { result = (Long) o; } else if (o instanceof JsonElement) { JsonElement json = (JsonElement) o; if (json.isJsonPrimitive()) { JsonPrimitive primitive = json.getAsJsonPrimitive(); if (primitive.isNumber()) { result = primitive.getAsLong(); } else { throw new IllegalArgumentException("Object does not represent a string value."); } } else { throw new IllegalArgumentException("Object does not represent a string value."); } } else { throw new IllegalArgumentException("Object does not represent a string value."); } return result; }
From source file:com.nextdoor.bender.operation.json.key.KeyNameOperation.java
License:Apache License
protected void perform(JsonObject obj) { Set<Entry<String, JsonElement>> entries = obj.entrySet(); Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries); for (Entry<String, JsonElement> entry : orgEntries) { JsonElement val = entry.getValue(); obj.remove(entry.getKey());// w ww . jav a2 s. c o m String key = entry.getKey().toLowerCase().replaceAll("[ .]", "_"); if (val.isJsonPrimitive()) { JsonPrimitive prim = val.getAsJsonPrimitive(); if (prim.isBoolean()) { obj.add(key + "__bool", val); } else if (prim.isNumber()) { if (prim.toString().contains(".")) { obj.add(key + "__float", val); } else { obj.add(key + "__long", val); } } else if (prim.isString()) { obj.add(key + "__str", val); } } else if (val.isJsonObject()) { obj.add(key, val); perform(val.getAsJsonObject()); } else if (val.isJsonArray()) { obj.add(key + "__arr", val); } } }
From source file:com.qmetry.qaf.automation.gson.GsonObjectDeserializer.java
License:Open Source License
public static Object read(JsonElement in) { if (in.isJsonArray()) { List<Object> list = new ArrayList<Object>(); JsonArray arr = in.getAsJsonArray(); for (JsonElement anArr : arr) { list.add(read(anArr));//ww w . j a va 2 s .c o m } return list; } else if (in.isJsonObject()) { Map<String, Object> map = new LinkedTreeMap<String, Object>(); JsonObject obj = in.getAsJsonObject(); Set<Map.Entry<String, JsonElement>> entitySet = obj.entrySet(); for (Map.Entry<String, JsonElement> entry : entitySet) { map.put(entry.getKey(), read(entry.getValue())); } return map; } else if (in.isJsonPrimitive()) { JsonPrimitive prim = in.getAsJsonPrimitive(); if (prim.isBoolean()) { return prim.getAsBoolean(); } else if (prim.isString()) { return prim.getAsString(); } else if (prim.isNumber()) { if (prim.getAsString().contains(".")) return prim.getAsDouble(); else { return prim.getAsLong(); } } } return null; }