List of usage examples for com.google.gson JsonPrimitive isString
public boolean isString()
From source file:com.iheart.quickio.client.QuickIo.java
License:Open Source License
private void move(final JsonElement json) { if (!json.isJsonPrimitive()) { return;//from w w w.j av a 2 s. co m } JsonPrimitive pr = json.getAsJsonPrimitive(); if (!pr.isString()) { return; } this.reconnect(pr.getAsString()); }
From source file:com.ikanow.infinit.e.data_model.store.MongoDbUtil.java
License:Apache License
public static Object encodeUnknown(JsonElement from) { if (from.isJsonArray()) { // Array return encodeArray(from.getAsJsonArray()); } //TESTED/*from w w w. j a va 2 s. c o m*/ else if (from.isJsonObject()) { // Object JsonObject obj = from.getAsJsonObject(); // Check for OID/Date: if (1 == obj.entrySet().size()) { if (obj.has("$date")) { try { return _format.parse(obj.get("$date").getAsString()); } catch (ParseException e) { try { return _format2.parse(obj.get("$date").getAsString()); } catch (ParseException e2) { return null; } } } //TESTED else if (obj.has("$oid")) { return new ObjectId(obj.get("$oid").getAsString()); } //TESTED } return encode(obj); } //TESTED else if (from.isJsonPrimitive()) { // Primitive JsonPrimitive val = from.getAsJsonPrimitive(); if (val.isNumber()) { return val.getAsNumber(); } //TESTED else if (val.isBoolean()) { return val.getAsBoolean(); } //TESTED else if (val.isString()) { return val.getAsString(); } //TESTED } //TESTED return null; }
From source file:com.indragie.cmput301as1.JSONHelpers.java
License:Open Source License
/** * Gets the value of the JSON element as a string if possible. * @param element The JSON element./*w w w. ja va 2 s.co m*/ * @return A string if the JSON element was a string, or * null otherwise. */ public static String getStringIfPossible(JsonElement element) { if (element == null) return null; if (element.isJsonPrimitive()) { JsonPrimitive primitive = element.getAsJsonPrimitive(); if (primitive.isString()) { return primitive.getAsString(); } } return null; }
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 ww.jav a2 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 w w. j av a 2 s . c om*/ 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; }//from ww w . ja v 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 a va2 s.c o 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. *//*from w w w. jav a2s . c om*/ 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.make.json2java.ClassField.java
License:Apache License
private boolean isBooleanValue(JsonPrimitive primitive) { boolean isBoolean = primitive.isBoolean(); if (!isBoolean && primitive.isString()) { String value = primitive.getAsString(); isBoolean = value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false"); }/*from w w w . j a va 2s.co m*/ return isBoolean; }
From source file:com.microsoft.windowsazure.mobileservices.table.MobileServiceTableBase.java
License:Open Source License
/** * Validates if the object represents a string value. * * @param o/*from w w w . j a va2 s . co m*/ * @return */ protected boolean isStringType(Object o) { boolean result = (o instanceof String); if (o instanceof JsonElement) { JsonElement json = (JsonElement) o; if (json.isJsonPrimitive()) { JsonPrimitive primitive = json.getAsJsonPrimitive(); result = primitive.isString(); } } return result; }