List of usage examples for com.google.gson JsonPrimitive getAsString
@Override
public String 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 www . j a va2s .co 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.//from www . j ava2s .c o 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; }/* w ww .j a v a2s.com*/ 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; }/* w w w . ja v 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 w w w. ja va 2 s. co m*/ 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.kaltura.client.types.ListResponse.java
License:Open Source License
@SuppressWarnings("unchecked") public ListResponse(JsonObject jsonObject) throws APIException { if (jsonObject == null) return;//from w ww .j av a2 s. c o m Class<ObjectBase> cls = ObjectBase.class; JsonPrimitive objectTypeElement = jsonObject.getAsJsonPrimitive("objectType"); if (objectTypeElement != null) { String objectType = objectTypeElement.getAsString().replaceAll("ListResponse$", ""); cls = GsonParser.getObjectClass(objectType, cls); } // set members values: totalCount = GsonParser.parseInt(jsonObject.get("totalCount")); objects = (List<T>) GsonParser.parseArray(jsonObject.getAsJsonArray("objects"), cls); }
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.jav a2 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.liferay.sync.hook.security.auth.SyncAuthVerifier.java
License:Open Source License
public String getUserId(String tokenString) { try {/*from w ww.j a v a 2 s . c o m*/ JsonTokenParser jsonTokenParser = getJsonTokenParser(); JsonToken jsonToken = jsonTokenParser.verifyAndDeserialize(tokenString); JsonPrimitive userIdJsonPrimitive = jsonToken.getParamAsPrimitive("userId"); if (userIdJsonPrimitive == null) { return null; } return userIdJsonPrimitive.getAsString(); } catch (Exception e) { return null; } }
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 ww. j av a 2 s . com*/ return isBoolean; }
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 .java 2s . c o m */ 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"); } } } }