List of usage examples for com.google.gson JsonElement isJsonNull
public boolean isJsonNull()
From source file:org.openmuc.framework.lib.json.FromJson.java
License:Open Source License
public String[] getStringArray(String listName) { String stringArray[] = null;/*from www . ja va2s . c o m*/ JsonElement jse = jsonObject.get(listName); if (!jse.isJsonNull() && jse.isJsonArray()) { stringArray = gson.fromJson(jse, String[].class); } return stringArray; }
From source file:org.openmuc.framework.lib.json.FromJson.java
License:Open Source License
public ArrayList<RestChannel> getRestChannelArrayList() throws ClassCastException { ArrayList<RestChannel> recordList = new ArrayList<RestChannel>(); JsonElement jse = jsonObject.get("records"); JsonArray jsa;/* w w w .jav a 2 s . c o m*/ if (!jse.isJsonNull() && jse.isJsonArray()) { jsa = jse.getAsJsonArray(); Iterator<JsonElement> jseIterator = jsa.iterator(); while (jseIterator.hasNext()) { RestChannel rc = new RestChannel(); JsonObject jsoIterated = jseIterator.next().getAsJsonObject(); rc.setId(jsoIterated.get(Const.ID).getAsString()); rc.setType(gson.fromJson(jsoIterated.get(Const.TYPE), ValueType.class)); // TODO: need valueType in json // or something else // otherwise null pointer // exception rc.setRecord(getRecord(jsoIterated, rc.getType())); recordList.add(rc); } } if (recordList.size() == 0) { return null; } return recordList; }
From source file:org.openmuc.framework.lib.json.FromJson.java
License:Open Source License
private Record getRecord(JsonObject jso, ValueType valueType) throws ClassCastException { Record record = null;/*w w w . j a va2s . c om*/ JsonElement jse = jso.get(Const.RECORD); if (jse != null && !jse.isJsonNull()) { record = getRecord(gson.fromJson(jse, RestRecord.class), valueType); } return record; }
From source file:org.openqa.selendroid.server.handler.OpenUrl.java
License:Apache License
@Override public Response handle() { SelendroidLogger.log("Open URL command"); JsonElement url = getPayload().get("url"); if (url.isJsonNull()) { return new Response(getSessionId(), 13, new SelendroidException("Not able to open Url because Url is missing.")); }//from ww w . jav a2 s .c o m getAndroidDriver().get(url.getAsString()); return new Response(getSessionId(), ""); }
From source file:org.openqa.selendroid.server.handler.SendKeys.java
License:Apache License
@Override public Response handle() { SelendroidLogger.log("send keys command"); Long id = getElementId();/*w ww .j ava2 s .c o m*/ JsonElement value = getPayload().get("value"); if (value.isJsonNull()) { return new Response(getSessionId(), new SelendroidException("No key to send to an element was found.")); } String text = value.getAsString(); AndroidElement element = getElementFromCache(id); Long start = System.currentTimeMillis(); element.enterText(text); System.out.println("Send keys done in " + (System.currentTimeMillis() - start) / 1000 + " seconds"); return new Response(getSessionId(), ""); }
From source file:org.openqa.selendroid.server.handler.SendKeyToActiveElement.java
License:Apache License
@Override public Response handle() { SelendroidLogger.log("send key to active element command"); JsonElement value = getPayload().get("value"); if (value.isJsonNull()) { return new Response(getSessionId(), new SelendroidException("No key to send to an element was found.")); }/*w w w . ja v a2 s. co m*/ String text = value.getAsString(); getAndroidDriver().getKeyboard().sendKeys(text); return new Response(getSessionId(), ""); }
From source file:org.openqa.selenium.json.JsonToBeanConverter.java
License:Apache License
@SuppressWarnings("unchecked") private <T> T convert(Class<T> clazz, Object source, int depth) { if (source == null || source instanceof JsonNull) { return null; }// w w w.j a v a2 s .co m if (source instanceof JsonElement) { JsonElement json = (JsonElement) source; if (json.isJsonPrimitive()) { JsonPrimitive jp = json.getAsJsonPrimitive(); if (String.class.equals(clazz)) { return (T) jp.getAsString(); } if (jp.isNumber()) { if (Integer.class.isAssignableFrom(clazz) || int.class.equals(clazz)) { return (T) Integer.valueOf(jp.getAsNumber().intValue()); } else if (Long.class.isAssignableFrom(clazz) || long.class.equals(clazz)) { return (T) Long.valueOf(jp.getAsNumber().longValue()); } else if (Float.class.isAssignableFrom(clazz) || float.class.equals(clazz)) { return (T) Float.valueOf(jp.getAsNumber().floatValue()); } else if (Double.class.isAssignableFrom(clazz) || double.class.equals(clazz)) { return (T) Double.valueOf(jp.getAsNumber().doubleValue()); } else { return (T) convertJsonPrimitive(jp); } } } } if (isPrimitive(source.getClass())) { return (T) source; } if (isEnum(clazz, source)) { return (T) convertEnum(clazz, source); } if ("".equals(String.valueOf(source))) { return (T) source; } if (Command.class.equals(clazz)) { JsonObject json = new JsonParser().parse((String) source).getAsJsonObject(); SessionId sessionId = null; if (json.has("sessionId") && !json.get("sessionId").isJsonNull()) { sessionId = convert(SessionId.class, json.get("sessionId"), depth + 1); } String name = json.get("name").getAsString(); if (json.has("parameters")) { Map<String, ?> args = (Map<String, ?>) convert(HashMap.class, json.get("parameters"), depth + 1); return (T) new Command(sessionId, name, args); } return (T) new Command(sessionId, name); } if (Response.class.equals(clazz)) { Response response = new Response(); JsonObject json = source instanceof JsonObject ? (JsonObject) source : new JsonParser().parse((String) source).getAsJsonObject(); if (json.has("error") && !json.get("error").isJsonNull()) { String state = json.get("error").getAsString(); response.setState(state); response.setStatus(errorCodes.toStatus(state, Optional.empty())); response.setValue(convert(Object.class, json.get("message"))); } if (json.has("state") && !json.get("state").isJsonNull()) { String state = json.get("state").getAsString(); response.setState(state); response.setStatus(errorCodes.toStatus(state, Optional.empty())); } if (json.has("status") && !json.get("status").isJsonNull()) { JsonElement status = json.get("status"); if (status.getAsJsonPrimitive().isString()) { String state = status.getAsString(); response.setState(state); response.setStatus(errorCodes.toStatus(state, Optional.empty())); } else { int intStatus = status.getAsInt(); response.setState(errorCodes.toState(intStatus)); response.setStatus(intStatus); } } if (json.has("sessionId") && !json.get("sessionId").isJsonNull()) { response.setSessionId(json.get("sessionId").getAsString()); } if (json.has("value")) { response.setValue(convert(Object.class, json.get("value"))); } else { response.setValue(convert(Object.class, json)); } return (T) response; } if (SessionId.class.equals(clazz)) { // Stupid heuristic to tell if we are dealing with a selenium 2 or 3 session id. JsonElement json = source instanceof String ? new JsonParser().parse((String) source).getAsJsonObject() : (JsonElement) source; if (json.isJsonPrimitive()) { return (T) new SessionId(json.getAsString()); } return (T) new SessionId(json.getAsJsonObject().get("value").getAsString()); } if (Capabilities.class.isAssignableFrom(clazz)) { JsonObject json = source instanceof JsonElement ? ((JsonElement) source).getAsJsonObject() : new JsonParser().parse(source.toString()).getAsJsonObject(); Map<String, Object> map = convertMap(json.getAsJsonObject(), depth); return (T) new MutableCapabilities(map); } if (Date.class.equals(clazz)) { return (T) new Date(Long.valueOf(String.valueOf(source))); } if (source instanceof String && !((String) source).startsWith("{") && Object.class.equals(clazz)) { return (T) source; } Method fromJson = getMethod(clazz, "fromJson"); if (fromJson != null) { try { return (T) fromJson.invoke(null, source.toString()); } catch (ReflectiveOperationException e) { throw new WebDriverException(e); } } if (depth == 0) { if (source instanceof String) { source = new JsonParser().parse((String) source); } } if (source instanceof JsonElement) { JsonElement element = (JsonElement) source; if (element.isJsonNull()) { return null; } if (element.isJsonPrimitive()) { return (T) convertJsonPrimitive(element.getAsJsonPrimitive()); } if (element.isJsonArray()) { return (T) convertList(element.getAsJsonArray(), depth); } if (element.isJsonObject()) { if (Map.class.isAssignableFrom(clazz)) { return (T) convertMap(element.getAsJsonObject(), depth); } if (Object.class.equals(clazz)) { return (T) convertMap(element.getAsJsonObject(), depth); } return convertBean(clazz, element.getAsJsonObject(), depth); } } return (T) source; // Crap shoot here; probably a string. }
From source file:org.openqa.selenium.json.JsonToBeanConverter.java
License:Apache License
private <T> T convertBean(Class<T> clazz, JsonObject toConvert, int depth) { T t = newInstance(clazz);/*w w w . ja va 2 s . c o m*/ SimplePropertyDescriptor[] allProperties = SimplePropertyDescriptor.getPropertyDescriptors(clazz); for (SimplePropertyDescriptor property : allProperties) { if (!toConvert.has(property.getName())) continue; JsonElement value = toConvert.get(property.getName()); Method write = property.getWriteMethod(); if (write == null) { continue; } Class<?> type = write.getParameterTypes()[0]; try { if (value.isJsonNull()) { value = null; } write.invoke(t, convert(type, value, depth + 1)); } catch (IllegalArgumentException e) { throw propertyWriteException(property, value, type, e); } catch (IllegalAccessException e) { throw propertyWriteException(property, value, type, e); } catch (InvocationTargetException e) { throw propertyWriteException(property, value, type, e); } } return t; }
From source file:org.openqa.selenium.remote.JsonToBeanConverter.java
License:Apache License
@SuppressWarnings("unchecked") private <T> T convert(Class<T> clazz, Object source, int depth) { if (source == null || source instanceof JsonNull) { return null; }//from ww w. j a v a 2s.co m if (source instanceof JsonElement) { JsonElement json = (JsonElement) source; if (json.isJsonPrimitive()) { JsonPrimitive jp = json.getAsJsonPrimitive(); if (String.class.equals(clazz)) { return (T) jp.getAsString(); } if (jp.isNumber()) { if (Integer.class.isAssignableFrom(clazz) || int.class.equals(clazz)) { return (T) Integer.valueOf(jp.getAsNumber().intValue()); } else if (Long.class.isAssignableFrom(clazz) || long.class.equals(clazz)) { return (T) Long.valueOf(jp.getAsNumber().longValue()); } else if (Float.class.isAssignableFrom(clazz) || float.class.equals(clazz)) { return (T) Float.valueOf(jp.getAsNumber().floatValue()); } else if (Double.class.isAssignableFrom(clazz) || double.class.equals(clazz)) { return (T) Double.valueOf(jp.getAsNumber().doubleValue()); } else { return (T) convertJsonPrimitive(jp); } } } } if (isPrimitive(source.getClass())) { return (T) source; } if (isEnum(clazz, source)) { return (T) convertEnum(clazz, source); } if ("".equals(String.valueOf(source))) { return (T) source; } if (Command.class.equals(clazz)) { JsonObject json = new JsonParser().parse((String) source).getAsJsonObject(); SessionId sessionId = null; if (json.has("sessionId") && !json.get("sessionId").isJsonNull()) { sessionId = convert(SessionId.class, json.get("sessionId"), depth + 1); } String name = json.get("name").getAsString(); if (json.has("parameters")) { Map<String, ?> args = (Map<String, ?>) convert(HashMap.class, json.get("parameters"), depth + 1); return (T) new Command(sessionId, name, args); } return (T) new Command(sessionId, name); } if (Response.class.equals(clazz)) { Response response = new Response(); JsonObject json = source instanceof JsonObject ? (JsonObject) source : new JsonParser().parse((String) source).getAsJsonObject(); if (json.has("error") && !json.get("error").isJsonNull()) { String state = json.get("error").getAsString(); response.setState(state); response.setStatus(errorCodes.toStatus(state, Optional.empty())); response.setValue(convert(Object.class, json.get("message"))); } if (json.has("state") && !json.get("state").isJsonNull()) { String state = json.get("state").getAsString(); response.setState(state); response.setStatus(errorCodes.toStatus(state, Optional.empty())); } if (json.has("status") && !json.get("status").isJsonNull()) { JsonElement status = json.get("status"); if (status.getAsJsonPrimitive().isString()) { String state = status.getAsString(); response.setState(state); response.setStatus(errorCodes.toStatus(state, Optional.empty())); } else { int intStatus = status.getAsInt(); response.setState(errorCodes.toState(intStatus)); response.setStatus(intStatus); } } if (json.has("sessionId") && !json.get("sessionId").isJsonNull()) { response.setSessionId(json.get("sessionId").getAsString()); } if (json.has("value")) { response.setValue(convert(Object.class, json.get("value"))); } else { response.setValue(convert(Object.class, json)); } return (T) response; } if (SessionId.class.equals(clazz)) { // Stupid heuristic to tell if we are dealing with a selenium 2 or 3 session id. JsonElement json = source instanceof String ? new JsonParser().parse((String) source).getAsJsonObject() : (JsonElement) source; if (json.isJsonPrimitive()) { return (T) new SessionId(json.getAsString()); } return (T) new SessionId(json.getAsJsonObject().get("value").getAsString()); } if (Capabilities.class.isAssignableFrom(clazz)) { JsonObject json = source instanceof JsonElement ? ((JsonElement) source).getAsJsonObject() : new JsonParser().parse(source.toString()).getAsJsonObject(); Map<String, Object> map = convertMap(json.getAsJsonObject(), depth); return (T) new DesiredCapabilities(map); } if (Date.class.equals(clazz)) { return (T) new Date(Long.valueOf(String.valueOf(source))); } if (source instanceof String && !((String) source).startsWith("{") && Object.class.equals(clazz)) { return (T) source; } Method fromJson = getMethod(clazz, "fromJson"); if (fromJson != null) { try { return (T) fromJson.invoke(null, source.toString()); } catch (IllegalArgumentException e) { throw new WebDriverException(e); } catch (IllegalAccessException e) { throw new WebDriverException(e); } catch (InvocationTargetException e) { throw new WebDriverException(e); } } if (depth == 0) { if (source instanceof String) { source = new JsonParser().parse((String) source); } } if (source instanceof JsonElement) { JsonElement element = (JsonElement) source; if (element.isJsonNull()) { return null; } if (element.isJsonPrimitive()) { return (T) convertJsonPrimitive(element.getAsJsonPrimitive()); } if (element.isJsonArray()) { return (T) convertList(element.getAsJsonArray(), depth); } if (element.isJsonObject()) { if (Map.class.isAssignableFrom(clazz)) { return (T) convertMap(element.getAsJsonObject(), depth); } if (Object.class.equals(clazz)) { return (T) convertMap(element.getAsJsonObject(), depth); } return convertBean(clazz, element.getAsJsonObject(), depth); } } return (T) source; // Crap shoot here; probably a string. }
From source file:org.plos.crepo.model.metadata.RepoMetadata.java
License:Open Source License
@VisibleForTesting static Object convertJsonToImmutable(JsonElement element) { if (element.isJsonNull()) { return null; }//from w w w. j ava 2 s . c o m if (element.isJsonPrimitive()) { JsonPrimitive primitive = element.getAsJsonPrimitive(); if (primitive.isString()) return primitive.getAsString(); if (primitive.isNumber()) return asNumber(primitive); if (primitive.isBoolean()) return primitive.getAsBoolean(); throw new RuntimeException("JsonPrimitive is not one of the expected primitive types"); } if (element.isJsonArray()) { JsonArray array = element.getAsJsonArray(); if (array.size() == 0) return Collections.emptyList(); List<Object> convertedList = new ArrayList<>(array.size()); for (JsonElement arrayElement : array) { Object convertedElement = convertJsonToImmutable(arrayElement); convertedList.add(convertedElement); } return Collections.unmodifiableList(convertedList); } if (element.isJsonObject()) { Set<Map.Entry<String, JsonElement>> entries = element.getAsJsonObject().entrySet(); if (entries.size() == 0) return Collections.emptyMap(); Map<String, Object> convertedMap = Maps.newHashMapWithExpectedSize(entries.size()); for (Map.Entry<String, JsonElement> entry : entries) { String key = Preconditions.checkNotNull(entry.getKey()); Object value = convertJsonToImmutable(entry.getValue()); convertedMap.put(key, value); } return Collections.unmodifiableMap(convertedMap); } throw new RuntimeException("JsonElement is not one of the expected subtypes"); }