List of usage examples for com.google.gson JsonElement isJsonArray
public boolean isJsonArray()
From source file:com.skysql.manager.api.MonitorData.java
License:Open Source License
public MonitorData deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException, NullPointerException { MonitorData monitorData = new MonitorData(); JsonElement jsonElement = json.getAsJsonObject().get("monitor_data"); if (jsonElement.isJsonNull() || jsonElement.isJsonArray()) { return monitorData; }//from w w w . ja v a 2s . com JsonObject jsonObject = jsonElement.getAsJsonObject(); if (jsonObject.has("timestamp")) { JsonElement jsonTime = jsonElement.getAsJsonObject().get("timestamp"); if (jsonTime != null && !jsonTime.isJsonNull()) { JsonArray array = jsonTime.getAsJsonArray(); int length = array.size(); ArrayList<Long> timeStamps = new ArrayList<Long>(length); for (int i = 0; i < length; i++) { Long timeStamp = array.get(i).getAsLong(); timeStamps.add(timeStamp); } monitorData.setTimeStamps(timeStamps); } } if (jsonObject.has("value")) { JsonElement jsonValue = jsonElement.getAsJsonObject().get("value"); if (jsonValue != null && !jsonValue.isJsonNull()) { JsonArray array = jsonValue.getAsJsonArray(); int length = array.size(); ArrayList<Number> avgPoints = new ArrayList<Number>(length); for (int i = 0; i < length; i++) { Double dataPoint = array.get(i).getAsDouble(); avgPoints.add(sanitize(dataPoint)); } monitorData.setAvgPoints(avgPoints); } } if (jsonObject.has("min")) { JsonElement jsonValue = jsonElement.getAsJsonObject().get("min"); if (jsonValue != null && !jsonValue.isJsonNull()) { JsonArray array = jsonValue.getAsJsonArray(); int length = array.size(); ArrayList<Number> minPoints = new ArrayList<Number>(length); for (int i = 0; i < length; i++) { Double dataPoint = array.get(i).getAsDouble(); minPoints.add(sanitize(dataPoint)); } monitorData.setMinPoints(minPoints); } } if (jsonObject.has("max")) { JsonElement jsonValue = jsonElement.getAsJsonObject().get("max"); if (jsonValue != null && !jsonValue.isJsonNull()) { JsonArray array = jsonValue.getAsJsonArray(); int length = array.size(); ArrayList<Number> maxPoints = new ArrayList<Number>(length); for (int i = 0; i < length; i++) { Double dataPoint = array.get(i).getAsDouble(); maxPoints.add(sanitize(dataPoint)); } monitorData.setMaxPoints(maxPoints); } } return monitorData; }
From source file:com.skysql.manager.api.MonitorDataRaw.java
License:Open Source License
public MonitorDataRaw deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException, NullPointerException { MonitorDataRaw monitorData = new MonitorDataRaw(); JsonElement jsonElement = json.getAsJsonObject().get("monitor_rawdata"); if (jsonElement.isJsonNull() || jsonElement.isJsonArray()) { return monitorData; }// w w w .j ava 2 s. co m JsonObject jsonObject = jsonElement.getAsJsonObject(); if (jsonObject.has("timestamp")) { JsonElement jsonTime = jsonElement.getAsJsonObject().get("timestamp"); if (jsonTime != null && !jsonTime.isJsonNull()) { JsonArray array = jsonTime.getAsJsonArray(); int length = array.size(); ArrayList<Long> timeStamps = new ArrayList<Long>(length); for (int i = 0; i < length; i++) { Long timeStamp = array.get(i).getAsLong(); timeStamps.add(timeStamp); } monitorData.setTimeStamps(timeStamps); } } if (jsonObject.has("value")) { JsonElement jsonValue = jsonElement.getAsJsonObject().get("value"); if (jsonValue != null && !jsonValue.isJsonNull()) { JsonArray array = jsonValue.getAsJsonArray(); int length = array.size(); ArrayList<Double> dataPoints = new ArrayList<Double>(length); for (int i = 0; i < length; i++) { Double dataPoint = array.get(i).getAsDouble(); dataPoints.add(dataPoint); } monitorData.setDataPoints(dataPoints); } } return monitorData; }
From source file:com.skysql.manager.api.Monitors.java
License:Open Source License
public Monitors deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException, NullPointerException { Monitors monitors = new Monitors(); JsonArray array = null;/*w w w . j ava2s. c om*/ JsonObject object = null; if (json.getAsJsonObject().has("monitorclasses")) { JsonElement monitorsElement = json.getAsJsonObject().get("monitorclasses"); if (monitorsElement.isJsonArray()) { array = monitorsElement.getAsJsonArray(); } else { object = monitorsElement.getAsJsonObject(); } } else if (json.getAsJsonObject().has("monitorclass")) { array = json.getAsJsonObject().get("monitorclass").getAsJsonArray(); } else { monitors.setMonitorsMap(null); return monitors; } LinkedHashMap<String, LinkedHashMap<String, MonitorRecord>> monitorsMap = new LinkedHashMap<String, LinkedHashMap<String, MonitorRecord>>(); monitors.setMonitorsMap(monitorsMap); for (String type : SystemTypes.getList().keySet()) { monitorsMap.put(type, new LinkedHashMap<String, MonitorRecord>()); } if (array != null) { parseMonitors(array, monitorsMap); } else { for (String type : SystemTypes.getList().keySet()) { array = object.get(type).getAsJsonArray(); parseMonitors(array, monitorsMap); } } return monitors; }
From source file:com.slx.funstream.utils.ChatMessageDeserializer.java
License:Apache License
@Override public ChatMessage[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonArray()) { JsonArray jsonMessages = json.getAsJsonArray(); ChatMessage[] messages = new ChatMessage[jsonMessages.size()]; for (int i = 0; i < jsonMessages.size(); i++) { JsonObject jsonMessage = jsonMessages.get(i).getAsJsonObject(); ChatMessage chatMessage = new ChatMessage(); chatMessage.setId(jsonMessage.get(ID).getAsLong()); chatMessage.setChannel(jsonMessage.get(CHANNEL).getAsLong()); chatMessage/* w ww . ja va2 s .c o m*/ .setFrom(context.deserialize(jsonMessage.get(FROM).getAsJsonObject(), CurrentUser.class)); chatMessage.setTo(context.deserialize(jsonMessage.get(TO).getAsJsonObject(), CurrentUser.class)); chatMessage.setText(jsonMessage.get(TEXT).getAsString()); chatMessage.setTime(jsonMessage.get(TIME).getAsString()); messages[i] = chatMessage; } return messages; } return null; }
From source file:com.softwarementors.extjs.djn.router.processor.standard.json.JsonRequestProcessor.java
License:Open Source License
private @CheckForNull Object jsonToJavaObject(JsonElement jsonValue, Class<?> parameterType, Type gsonType) { Object value;//from w w w . j a v a 2 s .c om if (jsonValue.isJsonNull()) { return null; } // Handle string in a special way, due to possibility of having a Java char type in the Java // side if (isString(jsonValue)) { if (parameterType.equals(String.class)) { value = jsonValue.getAsString(); return value; } if (parameterType.equals(char.class) || parameterType.equals(Character.class)) { value = Character.valueOf(jsonValue.getAsString().charAt(0)); return value; } } // If Java parameter is Object, we perform 'magic': json string, number, boolean and // null are converted to Java String, Double, Boolean and null. For json objects, // we create a Map<String,Object>, and for json arrays an Object[], and then perform // internal object conversion recursively using the same technique if (parameterType.equals(Object.class) && SUPPORTS_OBJECT_TYPE_PARAMETER) { value = toSimpleJavaType(jsonValue); return value; } // If the Java parameter is an array, but we are receiving a single item, we try to convert // the item to a single item array so that the Java method can digest it boolean useCustomGsonType = gsonType != null; boolean fakeJsonArrayForManyValuedClasses = JsonDeserializationManager.isManyValuedClass(parameterType) && !jsonValue.isJsonArray(); Type typeToInstantiate = parameterType; if (useCustomGsonType) { typeToInstantiate = gsonType; } JsonElement json = jsonValue; if (fakeJsonArrayForManyValuedClasses) { JsonArray fakeJson = new JsonArray(); fakeJson.add(jsonValue); json = fakeJson; } value = getGson().fromJson(json, typeToInstantiate); return value; }
From source file:com.softwarementors.extjs.djn.router.processor.standard.json.JsonRequestProcessor.java
License:Open Source License
private @CheckForNull Object toSimpleJavaType(JsonElement jsonValue) { if (jsonValue == null) return null; //VR Object value = null;/* w ww. j av a 2s . co m*/ if (!jsonValue.isJsonNull()) { if (jsonValue.isJsonPrimitive()) { JsonPrimitive primitive = jsonValue.getAsJsonPrimitive(); if (primitive.isBoolean()) { value = Boolean.valueOf(primitive.getAsBoolean()); } else if (primitive.isNumber()) { value = Double.valueOf(primitive.getAsDouble()); } else if (primitive.isString()) { value = primitive.getAsString(); } else { throw UnexpectedException.forUnexpectedCodeBranchExecuted(); } } else if (jsonValue.isJsonArray()) { //This simply does not work (?) JsonArray array = jsonValue.getAsJsonArray(); Object[] result = new Object[array.size()]; for (int i = 0; i < array.size(); i++) { result[i] = toSimpleJavaType(array.get(i)); } value = result; } else if (jsonValue.isJsonObject()) { //This simply does not work (?) //value = getGson().fromJson(jsonValue, Map.class ); JsonObject obj = jsonValue.getAsJsonObject(); Iterator<Entry<String, JsonElement>> properties = obj.entrySet().iterator(); Map<String, Object> result = new HashMap<String, Object>(); while (properties.hasNext()) { Entry<String, JsonElement> property = properties.next(); JsonElement propertyValue = property.getValue(); result.put(property.getKey(), toSimpleJavaType(propertyValue)); } value = result; } else { throw UnexpectedException.forUnexpectedCodeBranchExecuted(); } } return value; }
From source file:com.softwarementors.extjs.djn.router.processor.standard.json.JsonRequestProcessor.java
License:Open Source License
private static JsonObject[] parseIndividualJsonRequests(String requestString, JsonParser parser) { assert !StringUtils.isEmpty(requestString); assert parser != null; JsonObject[] individualRequests;//w w w . jav a 2s . c o m JsonElement root = parser.parse(requestString); if (root.isJsonArray()) { JsonArray rootArray = (JsonArray) root; if (rootArray.size() == 0) { RequestException ex = RequestException.forRequestBatchMustHaveAtLeastOneRequest(); logger.error(ex.getMessage(), ex); throw ex; } individualRequests = new JsonObject[rootArray.size()]; int i = 0; for (JsonElement item : rootArray) { if (!item.isJsonObject()) { RequestException ex = RequestException.forRequestBatchItemMustBeAValidJsonObject(i); logger.error(ex.getMessage(), ex); throw ex; } individualRequests[i] = (JsonObject) item; i++; } } else if (root.isJsonObject()) { individualRequests = new JsonObject[] { (JsonObject) root }; } else { RequestException ex = RequestException.forRequestMustBeAValidJsonObjectOrArray(); logger.error(ex.getMessage(), ex); throw ex; } return individualRequests; }
From source file:com.softwarementors.extjs.djn.router.processor.standard.json.JsonRequestProcessor.java
License:Open Source License
@CheckForNull private static JsonArray getMethodParametersJsonData(JsonObject object) { assert object != null; JsonElement data = object.get(JsonRequestData.DATA_ELEMENT); if (data == null) { RequestException ex = RequestException.forJsonElementMissing(JsonRequestData.DATA_ELEMENT); logger.error(ex.getMessage(), ex); throw ex; }//from w w w . j a v a 2s .c o m if (data.isJsonNull()) { return null; } if (!data.isJsonNull() && !data.isJsonArray()) { RequestException ex = RequestException.forJsonElementMustBeAJsonArray(JsonRequestData.DATA_ELEMENT, data.toString()); logger.error(ex.getMessage(), ex); throw ex; } return (JsonArray) data; }
From source file:com.solidfire.jsvcgen.serialization.OptionalAdapter.java
License:Open Source License
/** * Deserializes an Optional object.//from w w w . j av a 2 s.co m * * @param json the element to deserialize. * @param typeOfT type of the expected return value. * @param context Context used for deserialization. * @return An Optional object containing an object of type typeOfT. */ public Optional<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { if (!json.isJsonObject() && !json.isJsonArray()) { if (json.isJsonNull() || json.getAsString() == null) { return Optional.empty(); } } ParameterizedType pType = (ParameterizedType) typeOfT; Type genericType = pType.getActualTypeArguments()[0]; // Special handling for string, "" will return Optional.of("") if (genericType.equals(String.class)) { return Optional.of(json.getAsString()); } if (!json.isJsonObject() && !json.isJsonArray() && json.getAsString().trim().length() == 0) { return Optional.empty(); } if (json.isJsonObject() || json.isJsonArray()) { if (json.isJsonNull()) { return Optional.empty(); } } if (genericType.equals(Integer.class)) { return Optional.of(json.getAsInt()); } else if (genericType.equals(Long.class)) { return Optional.of(json.getAsLong()); } else if (genericType.equals(Double.class)) { return Optional.of(json.getAsDouble()); } // Defer deserialization to handler for type contained in Optional Object obj = context.deserialize(json, genericType); OptionalAdaptorUtils.initializeAllNullOptionalFieldsAsEmpty(obj); return Optional.of(obj); }
From source file:com.sonaive.v2ex.io.NodesHandler.java
License:Open Source License
@Override public void process(JsonElement element) { if (element.isJsonArray()) { for (Node node : new Gson().fromJson(element, Node[].class)) { mNodes.put(String.valueOf(node.id), node); }/*from ww w.j a v a 2 s. c o m*/ } else { Node node = new Gson().fromJson(element, Node.class); mNodes.put(String.valueOf(node.id), node); } }