public static NSObject toNsObject(JsonElement e) { if(e.isArray()) { return toNsObject(e.asArray()); } else if(e.isObject()) { return toNsObject(e.asObject()); } else { return toNsObject(e.asPrimitive()); } }
/** * Get a value at a particular path in an object structure. * * @param labels * one or more text labels * @return value or null if it doesn't exist at the specified path */ default Float getFloat(String... labels) { JsonElement jsonElement = get(labels); if (jsonElement == null || jsonElement.isNull()) { return null; } else { return jsonElement.asFloat(); } }
/** * Get a value at a particular path in an object structure. * * @param labels * one or more text labels * @return value or null if it doesn't exist at the specified path */ default Boolean getBoolean(final String... labels) { JsonElement jsonElement = get(labels); if (jsonElement == null || jsonElement.isNull()) { return null; } else { if (jsonElement.isBoolean()) { return jsonElement.asBoolean(); } else if (jsonElement.isNumber()) { return jsonElement.asInt() > 0; } else if (jsonElement.isPrimitive()) { return Boolean.valueOf(jsonElement.asString()); } else { throw new JsonTypeMismatchException("expected primitive value but was " + jsonElement.type()); } } }
/** * Get a JsonObject at a particular path in an object structure. * * @param labels * one or more text labels * @return value or null if it doesn't exist at the specified path */ default JsonObject getObject(String... labels) { JsonElement jsonElement = get(labels); if (jsonElement == null || jsonElement.isNull()) { return null; } else { return jsonElement.asObject(); } }
private void serialize(YAMLGenerator yaml, JsonElement e) throws IOException { switch (e.type()) { case object: yaml.writeStartObject(); e.asObject().forEach((k,v) -> { try { yaml.writeFieldName(k); case array: yaml.writeStartArray(); e.asArray().forEach(element -> { try { serialize(yaml, element); break; case string: yaml.writeString(e.asString()); break; case bool: yaml.writeBoolean(e.asBoolean()); break; case number: Number number = e.asNumber(); Class<? extends Number> clazz = number.getClass(); if(clazz.isAssignableFrom(Double.class) || clazz.isAssignableFrom(Float.class)) {
/** * Convenient method providing a few alternate ways of extracting elements * from a JsonArray. * * @param label label * @return the first element in the array matching the label or the n-th * element if the label is an integer and the element an object or * an array. */ public JsonElement get(String label) { int i = 0; try{ for (JsonElement e : this) { if(e.isPrimitive() && e.asPrimitive().asString().equals(label)) { return e; } else if((e.isObject() || e.isArray()) && Integer.valueOf(label).equals(i)) { return e; } i++; } } catch(NumberFormatException e) { // fail gracefully return null; } // the element was not found return null; }
public void forEachObject(@Nonnull Consumer<? super JsonObject> action) { for (JsonElement e : this) { action.accept(e.asObject()); } }
return; JsonType type = json.type(); switch (type) { case object: bw.write('{'); newline(bw, indent+1, pretty); Iterator<Entry<String, JsonElement>> iterator = json.asObject().entrySet().iterator(); while (iterator.hasNext()) { Entry<String, JsonElement> entry = iterator.next(); bw.write('['); newline(bw, indent+1, pretty); Iterator<JsonElement> arrayIterator = json.asArray().iterator(); while (arrayIterator.hasNext()) { JsonElement value = arrayIterator.next(); boolean nestedPretty=false; if(value.isObject()) { nestedPretty=true;
default Optional<JsonArray> maybeGetArray(String...labels) { return maybeGet(labels).map(e -> e.asArray()); }
public boolean endObjectEntry() { JsonElement value = stack.pollLast(); JsonElement e = stack.peekLast(); if (e.isPrimitive()) { // field name e = stack.pollLast(); JsonObject container = stack.peekLast().asObject(); String key = e.asPrimitive().asString(); container.put(key, value); if(container.size()>settings.upgradeThresholdToMapBasedJsonObject() && !MapBasedJsonObject.class.equals(container.getClass())) { JsonElement removed = stack.pollLast(); MapBasedJsonObject newContainer = new MapBasedJsonObject(removed.asObject()); stack.add(newContainer); } } return true; }
/** * Get a json element at a particular path in an object structure. * * @param labels * list of field names that describe the location to a particular json node. * @return a json element at a particular path in an object or null if it can't be found. */ default JsonElement get(final String... labels) { JsonElement e = this; int n = 0; for (String label : labels) { e = e.asObject().get(label); if (e == null) { return null; } if (n == labels.length - 1) { return e; } if (!e.isObject()) { break; } n++; } return null; }
public void shouldAddJsonElements() { JsonArray arr = new JsonArray(); arr.add(primitive(1),object(field("1", "2")),array(42)); assertThat(arr.size(), is(3)); assertThat(arr.get(0).isPrimitive(), is(true)); assertThat(arr.get(1).isObject(), is(true)); assertThat(arr.get(2).isArray(), is(true)); }
protected void flatten(@Nonnull JsonObject root, @Nonnull String path, @Nonnull String separator, JsonElement element) { JsonType type = element.type(); switch (type) { case array: JsonArray arr = element.asArray(); for(int i=0; i<arr.size();i++) { if(path.length()>0) { flatten(root,path+separator+i,separator,arr.get(i)); } else { flatten(root,""+i,separator,arr.get(i)); } } break; case object: if(path.length()>0) { element.asObject().forEach((key, value) -> flatten(root,path+separator+key,separator,value)); } else { element.asObject().forEach((key, value) -> flatten(root,key,separator,value)); } break; default: root.put(path, element); break; } }
public String[] asStringArray() { String[] result = new String[size()]; int i=0; for(JsonElement e: this) { result[i++] = e.asPrimitive().asString(); } return result; }
/** * Get a JsonArray at a particular path in an object structure. * * @param labels * one or more text labels * @return value or null if it doesn't exist at the specified path */ default JsonArray getArray(String... labels) { JsonElement jsonElement = get(labels); if (jsonElement == null || jsonElement.isNull()) { return null; } else { return jsonElement.asArray(); } }
/** * Get a value at a particular path in an object structure. * * @param labels * one or more text labels * @return value or null if it doesn't exist at the specified path */ default String getString(final String... labels) { JsonElement jsonElement = get(labels); if (jsonElement == null || jsonElement.isNull()) { return null; } else { return jsonElement.asString(); } }
/** * Get a value at a particular path in an object structure. * * @param labels * one or more text labels * @return value or null if it doesn't exist at the specified path */ default Long getLong(String... labels) { JsonElement jsonElement = get(labels); if (jsonElement == null || jsonElement.isNull()) { return null; } else { return jsonElement.asLong(); } }