List of usage examples for com.google.gson JsonElement getAsBigDecimal
public BigDecimal getAsBigDecimal()
From source file:org.openhab.binding.miio.handler.MiIoBasicHandler.java
License:Open Source License
void updateProperties(MiIoSendCommand response) { JsonArray res = response.getResult().getAsJsonArray(); JsonArray para = parser.parse(response.getCommandString()).getAsJsonObject().get("params").getAsJsonArray(); if (res.size() != para.size()) { logger.debug("Unexpected size different. Request size {}, response size {}. (Req: {}, Resp:{})", para.size(), res.size(), para.toString(), res.toString()); }//from ww w . j a va 2 s . c om for (int i = 0; i < para.size(); i++) { JsonElement val = res.get(i); if (val.isJsonNull()) { logger.debug("Property '{}' returned null (is it supported?).", para.get(i).getAsString()); continue; } MiIoBasicChannel basicChannel = getChannel(para.get(i).getAsString()); if (basicChannel != null) { if (basicChannel.getTransfortmation() != null) { JsonElement transformed = Conversions.execute(basicChannel.getTransfortmation(), val); logger.debug("Transformed with '{}': {} {} -> {} ", basicChannel.getTransfortmation(), basicChannel.getFriendlyName(), val, transformed); val = transformed; } try { if (basicChannel.getType().equals("Number")) { updateState(basicChannel.getChannel(), new DecimalType(val.getAsBigDecimal())); } if (basicChannel.getType().equals("String")) { updateState(basicChannel.getChannel(), new StringType(val.getAsString())); } if (basicChannel.getType().equals("Switch")) { updateState(basicChannel.getChannel(), val.getAsString().toLowerCase().equals("on") || val.getAsString().toLowerCase().equals("true") ? OnOffType.ON : OnOffType.OFF); } if (basicChannel.getType().equals("Color")) { Color rgb = new Color(val.getAsInt()); HSBType hsb = HSBType.fromRGB(rgb.getRed(), rgb.getGreen(), rgb.getBlue()); updateState(basicChannel.getChannel(), hsb); } } catch (Exception e) { logger.debug("Error updating {} property {} with '{}' : {}", getThing().getUID().getAsString(), basicChannel.getChannel(), val.getAsString(), e.getMessage()); logger.trace("Property update error detail:", e); } } else { logger.debug("Channel not found for {}", para.get(i).getAsString()); } } }
From source file:org.structr.core.PropertySetGSONAdapter.java
License:Open Source License
private Object getTypedValue(JsonElement valueElement, String type) { Object value = null;//from w w w . j a va2 s . co m if ((type == null) || type.equals("null")) { value = valueElement.getAsJsonNull(); } else if (type.equals("String")) { value = valueElement.getAsString(); } else if (type.equals("Number")) { value = valueElement.getAsNumber(); } else if (type.equals("Boolean")) { value = valueElement.getAsBoolean(); } else if (type.equals("JsonArray")) { value = valueElement.getAsJsonArray(); } else if (type.equals("JsonObject")) { value = valueElement.getAsJsonObject(); } else if (type.equals("Integer")) { value = valueElement.getAsInt(); } else if (type.equals("Long")) { value = valueElement.getAsLong(); } else if (type.equals("Double")) { value = valueElement.getAsDouble(); } else if (type.equals("Float")) { value = valueElement.getAsFloat(); } else if (type.equals("Byte")) { value = valueElement.getAsByte(); } else if (type.equals("Short")) { value = valueElement.getAsShort(); } else if (type.equals("Character")) { value = valueElement.getAsCharacter(); } else if (type.equals("BigDecimal")) { value = valueElement.getAsBigDecimal(); } else if (type.equals("BigInteger")) { value = valueElement.getAsBigInteger(); } return value; }
From source file:org.uorm.serializer.DefaultJsonSerializer.java
License:Apache License
@Override public Map<String, Object> deserialize2(Class<?> cls, String json) throws Exception { JsonStreamParser parser = new JsonStreamParser(json); if (parser.hasNext()) { JsonObject jsonobj = parser.next().getAsJsonObject(); Set<Entry<String, JsonElement>> jset = jsonobj.entrySet(); if (!jset.isEmpty()) { Map<String, PropertyDescriptor> propMap = ObjectMappingCache.getInstance() .getObjectPropertyMap(cls); Map<String, Object> instance = new HashMap<String, Object>(); for (Entry<String, JsonElement> entry : jset) { String name = entry.getKey(); JsonElement val = entry.getValue(); if (!val.isJsonNull()) { PropertyDescriptor descriptor = propMap.get(name); if (descriptor != null) { Class<?> memberType = descriptor.getPropertyType(); if (memberType == String.class) { instance.put(name, val.getAsString()); } else if (memberType == Integer.class || memberType == Integer.TYPE) { instance.put(name, val.getAsInt()); } else if (memberType == Byte.class || memberType == Byte.TYPE) { instance.put(name, val.getAsByte()); } else if (memberType == Double.class || memberType == Double.TYPE) { instance.put(name, val.getAsDouble()); } else if (memberType == Float.class || memberType == Float.TYPE) { instance.put(name, val.getAsFloat()); } else if (memberType == Long.class || memberType == Long.TYPE) { instance.put(name, val.getAsLong()); } else if (memberType == Short.class || memberType == Short.TYPE) { instance.put(name, val.getAsShort()); } else if (memberType == Boolean.class || memberType == Boolean.TYPE) { instance.put(name, val.getAsBoolean()); } else if (memberType == BigDecimal.class) { instance.put(name, val.getAsBigDecimal()); } else if (memberType == BigInteger.class) { instance.put(name, val.getAsBigInteger()); } else if (memberType == byte[].class) { instance.put(name, val.getAsString().getBytes()); } else if (memberType == java.sql.Timestamp.class) { Long time = parserDate(val.getAsString()); if (time == null) { instance.put(name, null); } else { instance.put(name, new java.sql.Timestamp(time)); }/* w w w . j a va 2 s. com*/ } else if (memberType == java.sql.Date.class) { Long time = parserDate(val.getAsString()); if (time == null) { instance.put(name, null); } else { instance.put(name, new java.sql.Date(time)); } } else if (memberType == java.util.Date.class) { Long time = parserDate(val.getAsString()); if (time == null) { instance.put(name, null); } else { instance.put(name, new java.util.Date(time)); } } else {//default String instance.put(name, val.getAsString()); } } else {//String instance.put(name, val.getAsString()); } } } return instance; } } return null; }
From source file:org.zoxweb.server.util.GSONUtil.java
License:Apache License
@SuppressWarnings("unchecked") private static NVEntity fromJSON(JsonObject jo, Class<? extends NVEntity> clazz, Base64Type b64Type) throws AccessException, APIException { // check if the jo has class name setup // before creating the new instance JsonElement classType = jo.get(MetaToken.CLASS_TYPE.getName()); if (classType != null) { if (!classType.isJsonNull()) { try { clazz = (Class<? extends NVEntity>) Class.forName(classType.getAsString()); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block //e.printStackTrace(); throw new APIException(e.getMessage(), Reason.NOT_FOUND); }//from w w w.j a v a 2 s . c o m } } NVEntity nve = null; try { try { nve = clazz.getDeclaredConstructor().newInstance(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block //e.printStackTrace(); throw new APIException(e.getMessage(), Reason.NOT_FOUND); } } catch (InstantiationException | InvocationTargetException | NoSuchMethodException ie) { ie.printStackTrace(); log.info("Error class:" + clazz); log.info("" + jo.toString()); throw new APIException(ie.getMessage(), Reason.NOT_FOUND); // if (ie instanceof InstantiationException) // throw (InstantiationException)ie; // else // throw new InstantiationException(ie.getMessage()); } catch (SecurityException ie) { throw new AccessException(ie.getMessage(), Reason.ACCESS_DENIED); } if (jo.get(MetaToken.REFERENCE_ID.getName()) != null && !jo.get(MetaToken.REFERENCE_ID.getName()).isJsonNull()) { nve.setReferenceID(jo.get(MetaToken.REFERENCE_ID.getName()).getAsString()); } NVConfigEntity mcEntity = (NVConfigEntity) nve.getNVConfig(); List<NVConfig> nvconfigs = mcEntity.getAttributes(); for (NVConfig nvc : nvconfigs) { Class<?> metaType = nvc.getMetaType(); JsonElement je = jo.get(nvc.getName()); if (je != null && !je.isJsonNull()) { NVBase<?> nvb = nve.lookup(nvc.getName()); if (nvc.isArray()) { //if ( nvb instanceof NVBase<List<NVEntity>>) //if ( NVEntity.class.isAssignableFrom( metaType.getComponentType())) if (NVEntity.class.isAssignableFrom(nvc.getMetaTypeBase())) { ArrayValues<NVEntity> tempArray = (ArrayValues<NVEntity>) nvb; JsonArray jsonArray = je.getAsJsonArray(); for (int i = 0; i < jsonArray.size(); i++) { JsonObject jobj = jsonArray.get(i).getAsJsonObject(); // try { tempArray.add( fromJSON(jobj, (Class<? extends NVEntity>) nvc.getMetaTypeBase(), b64Type)); } // catch (InstantiationException ie) // { // log.info("nvc:" + nvc.getName() + ":" + nvc.getMetaTypeBase()); // throw ie; // } //nvl.getValue().add( toNVPair( jobj)); } } // enum must be checked first else if (metaType.getComponentType().isEnum()) { JsonArray jsonArray = je.getAsJsonArray(); NVBase<List<Enum<?>>> nel = (NVBase<List<Enum<?>>>) nvb; for (int i = 0; i < jsonArray.size(); i++) { String jobj = jsonArray.get(i).getAsString(); nel.getValue().add(SharedUtil.enumValue(metaType.getComponentType(), jobj)); } } else if (String[].class.equals(metaType)) { JsonArray jsonArray = je.getAsJsonArray(); ArrayValues<NVPair> nvpm = (ArrayValues<NVPair>) nvb; for (int i = 0; i < jsonArray.size(); i++) { JsonObject jobj = jsonArray.get(i).getAsJsonObject(); nvpm.add(toNVPair(jobj)); } } else if (Long[].class.equals(metaType)) { JsonArray jsonArray = je.getAsJsonArray(); NVBase<ArrayList<Long>> nval = (NVBase<ArrayList<Long>>) nvb; for (int i = 0; i < jsonArray.size(); i++) { nval.getValue().add(jsonArray.get(i).getAsLong()); } } else if (byte[].class.equals(metaType)) { String byteArray64 = je.getAsString(); if (byteArray64 != null) { nve.setValue(nvc, SharedBase64.decode(b64Type, byteArray64.getBytes())); } } else if (Integer[].class.equals(metaType)) { JsonArray jsonArray = je.getAsJsonArray(); NVBase<ArrayList<Integer>> nval = (NVBase<ArrayList<Integer>>) nvb; for (int i = 0; i < jsonArray.size(); i++) { nval.getValue().add((int) jsonArray.get(i).getAsLong()); } } else if (Float[].class.equals(metaType)) { JsonArray jsonArray = je.getAsJsonArray(); NVBase<ArrayList<Float>> nval = (NVBase<ArrayList<Float>>) nvb; for (int i = 0; i < jsonArray.size(); i++) { nval.getValue().add((float) jsonArray.get(i).getAsDouble()); } } else if (Double[].class.equals(metaType)) { JsonArray jsonArray = je.getAsJsonArray(); NVBase<ArrayList<Double>> nval = (NVBase<ArrayList<Double>>) nvb; for (int i = 0; i < jsonArray.size(); i++) { nval.getValue().add(jsonArray.get(i).getAsDouble()); } } else if (Date[].class.equals(metaType)) { JsonArray jsonArray = je.getAsJsonArray(); NVBase<ArrayList<Long>> nval = (NVBase<ArrayList<Long>>) nvb; for (int i = 0; i < jsonArray.size(); i++) { JsonPrimitive jp = (JsonPrimitive) jsonArray.get(i); long tempDate = 0; if (jp.isString() && nvc.getValueFilter() != null) { tempDate = (Long) nvc.getValueFilter().validate(jp.getAsString()); } else { tempDate = jp.getAsLong(); } nval.getValue().add(tempDate); } } else if (BigDecimal[].class.equals(metaType)) { JsonArray jsonArray = je.getAsJsonArray(); NVBase<ArrayList<BigDecimal>> nval = (NVBase<ArrayList<BigDecimal>>) nvb; for (int i = 0; i < jsonArray.size(); i++) { nval.getValue().add(jsonArray.get(i).getAsBigDecimal()); } } } else { // not array if (nvc instanceof NVConfigEntity) { if (!(je instanceof JsonNull)) { ((NVBase<NVEntity>) nvb).setValue(fromJSON(je.getAsJsonObject(), (Class<? extends NVEntity>) nvc.getMetaType(), b64Type)); } } else if (NVGenericMap.class.equals(metaType)) { if (!(je instanceof JsonNull)) { NVGenericMap nvgm = fromJSONGenericMap(je.getAsJsonObject(), null, b64Type); ((NVGenericMap) nve.lookup(nvc)).add(nvgm.values(), true); } } else if (NVStringList.class.equals(metaType)) { JsonArray jsonArray = je.getAsJsonArray(); NVStringList nval = (NVStringList) nvb; for (int i = 0; i < jsonArray.size(); i++) { nval.getValue().add(jsonArray.get(i).getAsString()); } } else if (nvc.isEnum()) { if (!(je instanceof JsonNull)) { // if (metaType.isAssignableFrom( DynamicEnumMap.class)) // { // // ((NVDynamicEnum)nvb).setValue(je.getAsString()); // } // else { ((NVBase<Enum<?>>) nvb).setValue(SharedUtil.enumValue(metaType, je.getAsString())); } } } else if (String.class.equals(metaType)) { if (!(je instanceof JsonNull)) { ((NVPair) nvb).setValue(je.getAsString()); } } else if (Long.class.equals(metaType)) { ((NVBase<Long>) nvb).setValue(je.getAsLong()); } else if (Boolean.class.equals(metaType)) { ((NVBase<Boolean>) nvb).setValue(je.getAsBoolean()); } else if (Integer.class.equals(metaType)) { ((NVBase<Integer>) nvb).setValue((int) je.getAsLong()); } else if (Float.class.equals(metaType)) { ((NVBase<Float>) nvb).setValue((float) je.getAsDouble()); } else if (Double.class.equals(metaType)) { ((NVBase<Double>) nvb).setValue(je.getAsDouble()); } else if (Date.class.equals(metaType)) { JsonPrimitive jp = (JsonPrimitive) je; if (jp.isString()) { if (nvc.getValueFilter() != null) ((NVBase<Long>) nvb) .setValue((Long) nvc.getValueFilter().validate(jp.getAsString())); else ((NVBase<Long>) nvb).setValue(TimestampFilter.SINGLETON.validate(jp.getAsString())); } else { ((NVBase<Long>) nvb).setValue(jp.getAsLong()); } } else if (BigDecimal.class.equals(metaType)) { ((NVBase<BigDecimal>) nvb).setValue(je.getAsBigDecimal()); } } } } if (nve instanceof SubjectID) { ((SubjectID<?>) nve).getSubjectID(); } return nve; }