List of usage examples for com.google.gson JsonPrimitive getAsLong
@Override 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); }/*from w ww .ja va 2 s . co 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:rpc.server.data.JSONSerializer.java
License:Open Source License
private Object fromJsonElement(JsonElement jsonElement, Type expected) throws NoSuitableSerializableFactory { if (jsonElement == null) { return null; }//from w ww . ja v a 2s .c om // Null if (jsonElement.isJsonNull()) { return null; } // Boolean // Integer // Long // Float // Double // String // Enum if (jsonElement.isJsonPrimitive()) { JsonPrimitive asJsonPrimitive = jsonElement.getAsJsonPrimitive(); if (asJsonPrimitive.isBoolean()) { return asJsonPrimitive.getAsBoolean(); } if (asJsonPrimitive.isNumber()) { if (expected.isInteger()) { return asJsonPrimitive.getAsInt(); } if (expected.isLong()) { return asJsonPrimitive.getAsLong(); } if (expected.isFloat()) { return asJsonPrimitive.getAsFloat(); } if (expected.isDouble()) { return asJsonPrimitive.getAsDouble(); } return asJsonPrimitive.getAsNumber(); } if (asJsonPrimitive.isString()) { if (expected.isEnum()) { String value = asJsonPrimitive.getAsString(); return Enum.valueOf((Class) expected.getTypeClass(), value); } else { return asJsonPrimitive.getAsString(); } } } // Map // Serializable if (jsonElement.isJsonObject()) { JsonObject asJsonObject = jsonElement.getAsJsonObject(); if (expected.isMap()) { Map<Object, Object> map = new HashMap<Object, Object>(); Type keyType = expected.getParameterized(0); Type valueType = expected.getParameterized(1); if (!(keyType.isString() || keyType.isEnum())) { return null; } for (Map.Entry<String, JsonElement> entry : asJsonObject.entrySet()) { String key = entry.getKey(); JsonElement value = entry.getValue(); if (keyType.isString()) { map.put(entry.getKey(), fromJsonElement(value, valueType)); } if (keyType.isEnum()) { map.put(Enum.valueOf((Class) keyType.getTypeClass(), key), fromJsonElement(value, valueType)); } } return map; } else { if (provider == null) { throw new NoSuitableSerializableFactory(); } Serializable object = provider.make(expected); for (Map.Entry<String, Type> entry : object.fields().entrySet()) { String field = entry.getKey(); Type fieldType = entry.getValue(); JsonElement value = asJsonObject.get(field); object.set(field, fromJsonElement(value, fieldType)); } return object; } } // List if (jsonElement.isJsonArray()) { JsonArray asJsonArray = jsonElement.getAsJsonArray(); int size = asJsonArray.size(); List<Object> list = new ArrayList<Object>(); Type itemType = expected.getParameterized(0); for (int i = 0; i < size; i++) { JsonElement value = asJsonArray.get(i); list.add(fromJsonElement(value, itemType)); } return list; } return null; }