List of usage examples for com.google.gson JsonElement getAsBigDecimal
public BigDecimal getAsBigDecimal()
From source file:brooklyn.event.feed.http.JsonFunctions.java
License:Apache License
@SuppressWarnings("unchecked") public static <T> Function<JsonElement, T> cast(final Class<T> expected) { return new Function<JsonElement, T>() { @Override//from www.ja v a2 s .co m public T apply(JsonElement input) { if (input == null) { return (T) null; } else if (input.isJsonNull()) { return (T) null; } else if (expected == boolean.class || expected == Boolean.class) { return (T) (Boolean) input.getAsBoolean(); } else if (expected == char.class || expected == Character.class) { return (T) (Character) input.getAsCharacter(); } else if (expected == byte.class || expected == Byte.class) { return (T) (Byte) input.getAsByte(); } else if (expected == short.class || expected == Short.class) { return (T) (Short) input.getAsShort(); } else if (expected == int.class || expected == Integer.class) { return (T) (Integer) input.getAsInt(); } else if (expected == long.class || expected == Long.class) { return (T) (Long) input.getAsLong(); } else if (expected == float.class || expected == Float.class) { return (T) (Float) input.getAsFloat(); } else if (expected == double.class || expected == Double.class) { return (T) (Double) input.getAsDouble(); } else if (expected == BigDecimal.class) { return (T) input.getAsBigDecimal(); } else if (expected == BigInteger.class) { return (T) input.getAsBigInteger(); } else if (Number.class.isAssignableFrom(expected)) { // TODO Will result in a class-cast if it's an unexpected sub-type of Number not handled above return (T) input.getAsNumber(); } else if (expected == String.class) { return (T) input.getAsString(); } else if (expected.isArray()) { JsonArray array = input.getAsJsonArray(); Class<?> componentType = expected.getComponentType(); if (JsonElement.class.isAssignableFrom(componentType)) { JsonElement[] result = new JsonElement[array.size()]; for (int i = 0; i < array.size(); i++) { result[i] = array.get(i); } return (T) result; } else { Object[] result = (Object[]) Array.newInstance(componentType, array.size()); for (int i = 0; i < array.size(); i++) { result[i] = cast(componentType).apply(array.get(i)); } return (T) result; } } else { throw new IllegalArgumentException("Cannot cast json element to type " + expected); } } }; }
From source file:com.adr.datasql.orm.KindResultsJson.java
License:Apache License
@Override public BigDecimal getBigDecimal(String columnName) throws DataLinkException { JsonElement element = json.get(columnName); return element == null || element.isJsonNull() ? null : element.getAsBigDecimal(); }
From source file:com.asakusafw.testdriver.json.JsonObjectDriver.java
License:Apache License
@Override public void decimalProperty(PropertyName name, JsonObject context) throws IOException { JsonElement prop = property(context, name); if (prop == null) { return;/*from ww w .j a v a 2 s. c om*/ } builder.add(name, prop.getAsBigDecimal()); }
From source file:com.cloud.bridge.util.JsonAccessor.java
License:Apache License
public BigDecimal getAsBigDecimal(String propPath) { JsonElement jsonElement = eval(propPath); return jsonElement.getAsBigDecimal(); }
From source file:com.getperka.flatpack.codexes.NumberCodex.java
License:Apache License
@Override public N readNotNull(JsonElement element, DeserializationContext context) { Object toReturn;/*from www.jav a 2s. com*/ if (BigDecimal.class.equals(clazz)) { toReturn = element.getAsBigDecimal(); } else if (BigInteger.class.equals(clazz)) { toReturn = element.getAsBigInteger(); } else if (Byte.class.equals(clazz)) { toReturn = element.getAsByte(); } else if (Double.class.equals(clazz)) { toReturn = element.getAsDouble(); } else if (Float.class.equals(clazz)) { toReturn = element.getAsFloat(); } else if (Integer.class.equals(clazz)) { toReturn = element.getAsInt(); } else if (Long.class.equals(clazz)) { toReturn = element.getAsLong(); } else if (Short.class.equals(clazz)) { toReturn = element.getAsShort(); } else { throw new UnsupportedOperationException("Unimplemented Number type " + clazz.getName()); } return clazz.cast(toReturn); }
From source file:edu.mit.media.funf.json.JsonUtils.java
License:Open Source License
/** * Return an immutable JsonElement. Either IJsonObject, IJsonArray, or other immutable JsonPrimitive. * @param el// ww w . ja v a 2 s . co m * @return */ public static JsonElement immutable(JsonElement el) { if (el instanceof JsonObject) { return new IJsonObject(el.getAsJsonObject()); } else if (el instanceof JsonArray) { return new IJsonArray(el.getAsJsonArray()); } else if (el instanceof JsonPrimitive && el.getAsJsonPrimitive().isNumber()) { // Ensure that all LazilyParsedNumbers have been parsed into a consistent Number type. // Otherwise hash codes are not consistent, because LazilyParsedNumbers are never seen as integral. return new JsonPrimitive(el.getAsBigDecimal()); } return el; }
From source file:edu.mit.media.funf.probe.builtin.DatedContentProviderProbe.java
License:Open Source License
@Override public void setCheckpoint(JsonElement checkpoint) { latestTimestamp = checkpoint == null ? null : checkpoint.getAsBigDecimal(); }
From source file:io.robusta.rra.representation.implementation.GsonRepresentation.java
License:Apache License
/** * convert a jsonElement into Object/*from w ww . j av a2 s . c o m*/ * * @param type * @param element * @return * @throws RepresentationException */ protected <T> T get(Class<T> type, JsonElement element) throws RepresentationException { if (type == Long.class) { return (T) (Long) element.getAsLong(); } else if (type == Integer.class) { return (T) (Integer) element.getAsInt(); } else if (type == Short.class) { return (T) (Short) element.getAsShort(); } else if (type == Byte.class) { return (T) (Byte) element.getAsByte(); } else if (type == BigInteger.class) { return (T) (BigInteger) element.getAsBigInteger(); } else if (type == Double.class) { return (T) (Double) element.getAsDouble(); } else if (type == Float.class) { return (T) (Float) element.getAsFloat(); } else if (type == BigDecimal.class) { return (T) (BigDecimal) element.getAsBigDecimal(); } else if (type == Boolean.class) { return (T) (Boolean) element.getAsBoolean(); } else if (type == String.class) { return (T) element.getAsString(); } else { return (T) gson.fromJson(element, type); } }
From source file:org.eclipse.smarthome.storage.json.StringObjectMapDeserializer.java
License:Open Source License
@Override public Map<String, Object> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { Map<String, Object> map = new HashMap<String, Object>(); JsonObject obj = json.getAsJsonObject(); for (Map.Entry<String, JsonElement> me : obj.entrySet()) { String k = me.getKey();// w w w. j av a 2s . c o m JsonElement v = me.getValue(); if (v.isJsonPrimitive() && ((JsonPrimitive) v).isNumber()) { map.put(k, v.getAsBigDecimal()); } else { Object value = context.deserialize(v, Object.class); map.put(k, value); } } return map; }
From source file:org.fenixedu.spaces.domain.Information.java
License:Open Source License
@SuppressWarnings("unchecked") public <T extends Object> Optional<T> getMetadata(String field) { Optional<JsonElement> spec = getClassification().getMetadataSpecJson(field); if (spec.isPresent()) { JsonObject jsObj = spec.get().getAsJsonObject(); final String type = jsObj.get("type").getAsString(); final JsonObject metadata = getMetadata().getAsJsonObject(); JsonElement fieldValue = metadata.get(field); if (fieldValue == null || fieldValue.isJsonNull()) { return Optional.empty(); }//w ww . ja va 2s . c om if (Boolean.class.getName().equalsIgnoreCase(type)) { return (Optional<T>) Optional.of(new Boolean(fieldValue.getAsBoolean())); } if (Integer.class.getName().equalsIgnoreCase(type)) { return (Optional<T>) Optional.of(new Integer(fieldValue.getAsInt())); } if (BigDecimal.class.getName().equalsIgnoreCase(type)) { return (Optional<T>) Optional.of(fieldValue.getAsBigDecimal()); } return (Optional<T>) Optional.of(new String(fieldValue.getAsString())); } return Optional.empty(); }