List of usage examples for com.google.gson JsonPrimitive isBoolean
public boolean isBoolean()
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 {/* ww w . j a va 2s .co m*/ 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.java 2 s . c o m*/ 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 a 2 s . c o m*/ * * @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 ww w . jav a2 s. com*/ } 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 w w w .j av a 2 s .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);/* w w w . ja v a 2s . c o 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 {/*from w w w. j a va2s . c o 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 w w.j av a 2 s .c o m return valueJson.getAsString(); } }
From source file:JsonParser.ParseJson.java
public static void dumpJSONElement(JsonElement element, String type) { if (element.isJsonObject()) { //System.out.println("Is an object"); JsonObject obj = element.getAsJsonObject(); java.util.Set<java.util.Map.Entry<String, JsonElement>> entries = obj.entrySet(); java.util.Iterator<java.util.Map.Entry<String, JsonElement>> iter = entries.iterator(); while (iter.hasNext()) { java.util.Map.Entry<String, JsonElement> entry = iter.next(); // System.out.println("Key: " + entry.getKey()); if (entry.getKey().toString().equals("instances")) { System.out.println("............Topic: "); dumpJSONElement(entry.getValue(), "topic"); }//from w w w.j av a 2 s. c o m if (entry.getKey().toString().equals("aspects")) { System.out.println("............aspects: "); dumpJSONElement(entry.getValue(), "aspect"); } else if (entry.getKey().toString().equals("positives")) { System.out.println(".............Positive Words "); dumpJSONElement(entry.getValue(), "positive"); } else if (entry.getKey().toString().equals("negatives")) { System.out.println(".............negatives Words "); dumpJSONElement(entry.getValue(), "negative"); } else { dumpJSONElement(entry.getValue(), ""); } } } else if (element.isJsonArray()) { JsonArray array = element.getAsJsonArray(); //System.out.println("Is an array. Number of values: " + array.size()); java.util.Iterator<JsonElement> iter = array.iterator(); while (iter.hasNext()) { JsonElement entry = iter.next(); dumpJSONElement(entry, ""); } } else if (element.isJsonPrimitive()) { //System.out.println("Is a primitive"); JsonPrimitive value = element.getAsJsonPrimitive(); if (value.isBoolean()) { //System.out.println("Is boolean: " + value.getAsBoolean()); } else if (value.isNumber()) { // System.out.println("Is number: " + value.getAsNumber()); } else if (value.isString()) { //if(!value.getAsString().equals("empty")) //{ //if(type.equals("topic")) //{ System.out.println(type + " :" + value.getAsString()); //} } } else if (element.isJsonNull()) { System.out.println("Is NULL"); } else { System.out.println("Error. Unknown type of element"); } }
From source file:leola.web.WebLeolaLibrary.java
License:Open Source License
/** * Converts the {@link JsonElement} into the equivalent {@link LeoObject} * /*from w ww .ja va2 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; }