List of usage examples for com.google.gson JsonElement getAsLong
public long getAsLong()
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); }// ww w.j ava2s. 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; }
From source file:se.sperber.cryson.serialization.CrysonSerializer.java
License:Apache License
private <T> T augmentEntity(T object, JsonElement jsonElement, Map<Long, Long> replacedTemporaryIds) { try {// ww w .ja v a 2s . c om Set<Map.Entry<String, JsonElement>> attributes = jsonElement.getAsJsonObject().entrySet(); for (Map.Entry<String, JsonElement> attribute : attributes) { if (attribute.getKey().endsWith("_cryson_id")) { String attributeName = attribute.getKey().split("_cryson_id")[0]; Field field = reflectionHelper.getField(object, attributeName); if (field != null) { if (attribute.getValue() != JsonNull.INSTANCE) { Object placeHolderObject = field.getType().newInstance(); reflectionHelper.setPrimaryKey(placeHolderObject, primaryKeyForReplacementObject( attribute.getValue().getAsLong(), replacedTemporaryIds)); field.set(object, placeHolderObject); } } } else if (attribute.getKey().endsWith("_cryson_ids")) { String attributeName = attribute.getKey().split("_cryson_ids")[0]; Field field = reflectionHelper.getField(object, attributeName); if (field != null) { Iterator<JsonElement> attributeIterator = attribute.getValue().getAsJsonArray().iterator(); Collection<Object> placeHolderObjects = emptyCollectionForField(field); while (attributeIterator.hasNext()) { JsonElement attributeValue = attributeIterator.next(); Object placeHolderObject = ((Class) ((ParameterizedType) (field.getGenericType())) .getActualTypeArguments()[0]).newInstance(); reflectionHelper.setPrimaryKey(placeHolderObject, primaryKeyForReplacementObject( attributeValue.getAsLong(), replacedTemporaryIds)); placeHolderObjects.add(placeHolderObject); } field.set(object, placeHolderObjects); } } else if (attribute.getKey().endsWith("_cryson_usertype")) { String attributeName = attribute.getKey().split("_cryson_usertype")[0]; Field field = reflectionHelper.getField(object, attributeName); if (field != null && Map.class.isAssignableFrom(field.getType())) { Map<Object, Object> placeHolderObjects = new HashMap<Object, Object>(); for (Map.Entry<String, JsonElement> valueEntry : attribute.getValue().getAsJsonObject() .entrySet()) { String placeHolderKeyObject = valueEntry.getKey(); Object placeHolderValueObject = gson.fromJson(valueEntry.getValue(), Object.class); // Support string->string maps only placeHolderObjects.put(placeHolderKeyObject, placeHolderValueObject); } field.set(object, placeHolderObjects); } } } return object; } catch (Throwable t) { throw new RuntimeException(t); } }
From source file:vellum.json.JsonObjectDelegate.java
License:Apache License
public long getLong(String key, long defaultValue) { JsonElement element = get(key); if (element == null) { return defaultValue; }// w ww. ja v a 2s . c o m return element.getAsLong(); }
From source file:vellum.jx.JMaps.java
License:Apache License
public static Object parsePrimitive(JsonElement element) { String string = element.toString(); if (string.equals("true")) { return element.getAsBoolean(); } else if (string.equals("false")) { return element.getAsBoolean(); } else if (string.startsWith("\"")) { return element.getAsString(); } else if (string.contains(".")) { return element.getAsDouble(); } else if (string.matches("[0-9]*")) { return element.getAsLong(); }/*from www . j av a 2s . c om*/ return element.getAsString(); }