List of usage examples for com.google.gson JsonPrimitive getAsBoolean
@Override public boolean getAsBoolean()
From source file:de.csdev.ebus.cfg.std.EBusValueJsonDeserializer.java
License:Open Source License
@Override public List<EBusValueDTO> deserialize(JsonElement jElement, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonArray asJsonArray = jElement.getAsJsonArray(); ArrayList<EBusValueDTO> result = new ArrayList<EBusValueDTO>(); ArrayList<String> fields = new ArrayList<String>(); for (Field field : EBusValueDTO.class.getDeclaredFields()) { SerializedName annotation = field.getAnnotation(SerializedName.class); if (annotation != null) { fields.add(annotation.value()); } else {// w ww . java2 s.c om fields.add(field.getName()); } } for (JsonElement jsonElement : asJsonArray) { JsonObject jObject = jsonElement.getAsJsonObject(); EBusValueDTO valueDTO = context.deserialize(jObject, EBusValueDTO.class); for (Entry<String, JsonElement> entry : jObject.entrySet()) { if (!fields.contains(entry.getKey())) { if (entry.getValue().isJsonPrimitive()) { JsonPrimitive primitive = (JsonPrimitive) entry.getValue(); if (primitive.isNumber()) { valueDTO.setProperty(entry.getKey(), primitive.getAsBigDecimal()); } else if (primitive.isBoolean()) { valueDTO.setProperty(entry.getKey(), primitive.getAsBoolean()); } else if (primitive.isString()) { valueDTO.setProperty(entry.getKey(), primitive.getAsString()); } } else { valueDTO.setProperty(entry.getKey(), entry.getValue().getAsString()); } } } result.add(valueDTO); } return result; }
From source file:de.innovationgate.wgpublisher.webtml.utils.JsonUtils.java
License:Open Source License
public Object jsonToJava(JsonElement jsonValue) { Object value = null;/*from w ww. j a va 2 s . c om*/ if (jsonValue.isJsonNull()) { value = null; } else if (jsonValue.isJsonPrimitive()) { JsonPrimitive prim = (JsonPrimitive) jsonValue; if (prim.isNumber()) { value = prim.getAsDouble(); } else if (prim.isBoolean()) { value = prim.getAsBoolean(); } else { value = prim.getAsString(); } value = jsonToJavaConversions(value); } else if (jsonValue.isJsonArray()) { JsonArray array = jsonValue.getAsJsonArray(); List<Object> list = new ArrayList<Object>(); for (JsonElement element : array) { list.add(jsonToJava(element)); } } else if (jsonValue.isJsonObject()) { JsonObject obj = jsonValue.getAsJsonObject(); Map<String, Object> map = new HashMap<String, Object>(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { map.put(String.valueOf(entry.getKey()), jsonToJava(entry.getValue())); } } return value; }
From source file:fr.zcraft.MultipleInventories.snaphots.ItemStackSnapshot.java
License:Open Source License
/** * From a JSON element, constructs a data structure representing * the same structure (recursively) using native types. * * <p>We had to re-implement this to ensure the generated structure to have the * right data type (instead of all numbers being doubles) and precision.</p> * * @param element The json element to be decoded. * @return A native data structure (either a {@link Map Map<String, Object>}, * a {@link List List<Object>}, or a native type) representing the same * structure (recursively).//from w w w. ja v a2 s . com * * @see #jsonToNative(JsonObject) Converts a json object to an explicit {@link Map}. * The JavaDoc also contains explainations on why this is needed. */ private static Object jsonToNative(final JsonElement element) { if (element.isJsonObject()) { return jsonToNative(element.getAsJsonObject()); } else if (element.isJsonArray()) { final List<Object> list = new ArrayList<>(); element.getAsJsonArray().forEach(listElement -> { final Object nativeValue = jsonToNative(listElement); if (nativeValue != null) { list.add(nativeValue); } }); return list; } else if (element.isJsonPrimitive()) { final JsonPrimitive primitive = element.getAsJsonPrimitive(); if (primitive.isBoolean()) { return primitive.getAsBoolean(); } else if (primitive.isString()) { return primitive.getAsString(); } else /* it's a number we yet have to find the type. */ { final BigDecimal number = primitive.getAsBigDecimal(); try { return number.byteValueExact(); } catch (final ArithmeticException e1) { try { return number.shortValueExact(); } catch (final ArithmeticException e2) { try { return number.intValueExact(); } catch (final ArithmeticException e3) { try { return number.longValueExact(); } catch (final ArithmeticException e4) { try { return number.doubleValue(); } catch (final ArithmeticException | NumberFormatException e5) { return number; } } } } } } } // Else the element is null. return null; }
From source file:hdm.stuttgart.esell.router.GeneralObjectDeserializer.java
License:Apache License
public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonNull()) { return null; } else if (json.isJsonPrimitive()) { JsonPrimitive primitive = json.getAsJsonPrimitive(); if (primitive.isString()) { return primitive.getAsString(); } else if (primitive.isNumber()) { return primitive.getAsNumber(); } else if (primitive.isBoolean()) { return primitive.getAsBoolean(); }//from w w w . j av a 2s .c o m } else if (json.isJsonArray()) { JsonArray array = json.getAsJsonArray(); Object[] result = new Object[array.size()]; int i = 0; for (JsonElement element : array) { result[i] = deserialize(element, null, context); ++i; } return result; } else if (json.isJsonObject()) { JsonObject object = json.getAsJsonObject(); Map<String, Object> result = new HashMap<String, Object>(); for (Map.Entry<String, JsonElement> entry : object.entrySet()) { Object value = deserialize(entry.getValue(), null, context); result.put(entry.getKey(), value); } return result; } else { throw new JsonParseException("Unknown JSON type for JsonElement " + json.toString()); } return null; }
From source file:io.thinger.thinger.views.Element.java
License:Open Source License
public static Element createPrimitiveElement(String name, JsonPrimitive primitive, LinearLayout layout, boolean output) { if (primitive.isBoolean()) { return new BoolValue(layout, name, primitive.getAsBoolean(), output); } else if (primitive.isNumber()) { return new NumberValue(layout, name, primitive.getAsNumber(), output); } else if (primitive.isString()) { return new StringValue(layout, name, primitive.getAsString(), output); }//from ww w . j a va2s . c o m return null; }
From source file:it.polimi.tower4clouds.data_analyzer.DAInputDataUnmarshaller.java
License:Apache License
@Override public Model unmarshal(String inputData) throws Exception { // logger.debug("Unmarshalling data"); // long startTime = System.currentTimeMillis(); Model model = ModelFactory.createDefaultModel(); JsonArray jsonData = jsonParser.parse(inputData).getAsJsonArray(); // logger.debug("{} monitoring datum json object(s) received", // jsonData.size()); for (JsonElement jsonElement : jsonData) { JsonObject jsonDatum = jsonElement.getAsJsonObject(); Resource resourceDatum = model.createResource(UUID.randomUUID().toString()).addProperty(RDF.type, MO.MonitoringDatum);// ww w . j a v a2 s .co m for (Entry<String, JsonElement> pair : jsonDatum.entrySet()) { String property = pair.getKey(); JsonPrimitive value = pair.getValue().getAsJsonPrimitive(); if (value.isBoolean()) { resourceDatum.addProperty(MO.makeProperty(property), model.createTypedLiteral(value.getAsBoolean(), XSDDatatype.XSDboolean)); } else if (value.isString()) { resourceDatum.addProperty(MO.makeProperty(property), model.createTypedLiteral(value.getAsString(), XSDDatatype.XSDstring)); } else if (value.isNumber()) { resourceDatum.addProperty(MO.makeProperty(property), model.createTypedLiteral(value.getAsNumber().doubleValue(), XSDDatatype.XSDdouble)); } else { logger.error("Unknown datum property: {}", value); } } } // logger.debug("Unmarshalling completed in {} seconds", // ((double) (System.currentTimeMillis() - startTime)) / 1000); return model; }
From source file:jp.pay.model.EventDataDeserializer.java
License:Open Source License
private Object deserializeJsonPrimitive(JsonPrimitive element) { if (element.isBoolean()) { return element.getAsBoolean(); } else if (element.isNumber()) { return element.getAsNumber(); } else {// w ww . j a v a 2 s. co m return element.getAsString(); } }
From source file:json.GraphSONNodeDeserializer.java
License:Apache License
private Object getTypedValue(JsonPrimitive valueJson) { if (valueJson.isBoolean()) { return valueJson.getAsBoolean(); } else if (valueJson.isNumber()) { return valueJson.getAsInt(); } else {/*w ww. j a va 2s . c om*/ return valueJson.getAsString(); } }
From source file:leola.web.WebLeolaLibrary.java
License:Open Source License
/** * Converts the {@link JsonElement} into the equivalent {@link LeoObject} * /*from ww w . j av a 2 s . c o m*/ * @param element * @return the {@link LeoObject} */ private static LeoObject toLeoObject(JsonElement element) { if (element == null || element.isJsonNull()) { return LeoObject.NULL; } if (element.isJsonArray()) { JsonArray array = element.getAsJsonArray(); LeoArray leoArray = new LeoArray(array.size()); array.forEach(e -> leoArray.add(toLeoObject(e))); return leoArray; } if (element.isJsonObject()) { JsonObject object = element.getAsJsonObject(); LeoMap leoMap = new LeoMap(); object.entrySet().forEach(entry -> { leoMap.putByString(entry.getKey(), toLeoObject(entry.getValue())); }); return leoMap; } if (element.isJsonPrimitive()) { JsonPrimitive primitive = element.getAsJsonPrimitive(); if (primitive.isBoolean()) { return LeoObject.valueOf(primitive.getAsBoolean()); } if (primitive.isNumber()) { return LeoObject.valueOf(primitive.getAsDouble()); } if (primitive.isString()) { return LeoString.valueOf(primitive.getAsString()); } } return LeoObject.NULL; }
From source file:me.boomboompower.togglechat.utils.BetterJsonObject.java
License:Open Source License
/** * The optional boolean method, returns the default value if * the key is null, empty or the data does not contain * the key. This will also return the default value if * the data value is not a boolean// w ww .j a v a 2s . c o m * * @param key the key the value will be loaded from * @return the value in the json data set or the default if the key cannot be found */ public boolean optBoolean(String key, boolean value) { if (key == null || key.isEmpty() || !has(key)) { return value; } JsonPrimitive primitive = asPrimitive(get(key)); if (primitive != null && primitive.isBoolean()) { return primitive.getAsBoolean(); } return value; }