@Override public void removeEmpty() { Iterator<java.util.Map.Entry<String, JsonElement>> iterator = entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, JsonElement> entry = iterator.next(); JsonElement element = entry.getValue(); if (element.isEmpty() && !element.isObject()) { iterator.remove(); } else { element.removeEmpty(); } } }
@Override default void removeEmpty() { Iterator<java.util.Map.Entry<String, JsonElement>> iterator = entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, JsonElement> entry = iterator.next(); JsonElement element = entry.getValue(); if (element.isEmpty() && !element.isObject()) { iterator.remove(); } else { element.removeEmpty(); } } }
/** * Allows you to lookup objects from an array by e.g. their id. * @param fieldName field to match on * @param value value of the field * @return the first object where field == value, or null */ public Optional<JsonObject> findFirstWithFieldValue(@Nonnull String fieldName, String value) { JsonElement result = findFirstMatching(e -> { if(!e.isObject()) { return false; } JsonObject object = e.asObject(); String fieldValue = object.getString(fieldName); if(fieldValue !=null) { return fieldValue.equals(value); } else { return false; } }).orElse(null); if(result != null) { return Optional.of(result.asObject()); } else { return Optional.empty(); } }
public JsonElementAssert isObject() { isNotNull(); if(!actual.isObject()) { failWithMessage("should be object but was <%s>", actual.type()); } return this; }
/** * 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; }
/** * 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; }
private Object translate(JsonElement e) { if(e.isObject()) { JsonObject o = e.asObject(); Map<String,Object> map = new LinkedHashMap<>(); o.forEach((key,value) -> { map.put(key,translate(value)); }); return map; } else if(e.isArray()) { JsonArray a = e.asArray(); List<Object> list = new ArrayList<>(); a.forEach(element -> { list.add(translate(element)); }); return list; } else { return e.asPrimitive().value(); } }
private static void append(Element e, JsonObject o) { for(Entry<String, JsonElement> entry:o.entrySet()) { Element child; try { child = new Element(entry.getKey()); } catch (IllegalNameException exc1) { child = new Element("_" + entry.getKey()); } JsonElement value = entry.getValue(); if(value.isArray()) { append(child, value.asArray()); } else if(value.isObject()) { append(child, value.asObject()); } else { append(child, value.asPrimitive()); } e.appendChild(child); } }
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()); } }
private static void serialize(BsonGenerator generator, JsonObject object) throws JsonGenerationException, IOException { generator.writeStartObject(); for(Entry<String, JsonElement> entry: object.entrySet()) { generator.writeFieldName(entry.getKey()); JsonElement element = entry.getValue(); if(element.isObject()) { serialize(generator, element.asObject()); } else if(element.isArray()) { serialize(generator, element.asArray()); } else { serialize(generator, element.asPrimitive()); } } generator.writeEndObject(); }
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)); }
static Element getElement(JsonElement value, String rootName) { Element root = new Element(rootName); if(value.isArray()) { append(root, value.asArray()); } else if(value.isObject()) { append(root, value.asObject()); } else { append(root, value.asPrimitive()); } return root; } }
private static void serialize(BsonGenerator generator, JsonArray array) throws JsonGenerationException, IOException { generator.writeStartArray(); for(JsonElement element: array) { if(element.isObject()) { serialize(generator, element.asObject()); } else if(element.isArray()) { serialize(generator, element.asArray()); } else { serialize(generator, element.asPrimitive()); } } generator.writeEndArray(); }
public static void serialize(JsonElement element, OutputStream out) throws IOException { BsonFactory bsonFactory = new BsonFactory(); BsonGenerator generator = bsonFactory.createGenerator(out); if(element.isObject()) { serialize(generator, element.asObject()); } else if(element.isArray()) { serialize(generator, element.asArray()); } else { serialize(generator, element.asPrimitive()); } generator.close(); }
public void shouldSerializeAndParseObject() throws IOException { JsonObject o = object( field("x",42), field("o",object( field("x",42), field("a",array(1,2,3)) )), field("a", array(array(0.1,0.2))) ); ByteArrayOutputStream bos = new ByteArrayOutputStream(); JsonJPlistSerializer.serialize(o, bos); bos.flush(); byte[] bytes = bos.toByteArray(); JsonElement e = JsonJPlistParser.parse(bytes); assertThat(e.isObject()).isEqualTo(true); }
public static NSObject toNsObject(JsonArray a) { NSArray array = new NSArray(a.size()); int index=0; for(JsonElement v: a) { NSObject value; if(v.isObject()) { value=toNsObject(v.asObject()); } else if(v.isArray()) { value = toNsObject(v.asArray()); } else { value=toNsObject(v.asPrimitive()); } array.setValue(index++, value); } return array; }
public void shouldSerializeAndParseObject() throws IOException { JsonObject o = object( field("x",42), field("o",object( field("x",42), field("a",array(1,2,3)) )), field("a", array(array(0.1,0.2))) ); ByteArrayOutputStream bos = new ByteArrayOutputStream(); YamlSerializer yamlSerializer = new YamlSerializer(); yamlSerializer.serialize(bos, o); bos.flush(); byte[] bytes = bos.toByteArray(); YamlParser yamlParser = new YamlParser(); ByteArrayInputStream is = new ByteArrayInputStream(bytes); JsonElement e = yamlParser.parse(is); assertThat(e.isObject()).isEqualTo(true); }
public static NSObject toNsObject(JsonObject o) { NSDictionary dict = new NSDictionary(); o.forEach((k,v) -> { if(v.isObject()) { dict.put(k, toNsObject(v.asObject())); } else if(v.isArray()) { dict.put(k, toNsObject(v.asArray())); } else { dict.put(k, toNsObject(v.asPrimitive())); } }); return dict; }
private static void append(Element e, JsonArray p) { Element list = new Element("ol"); for(JsonElement value: p) { Element li = new Element("li"); if(value.isArray()) { append(li, value.asArray()); } else if(value.isObject()) { append(li, value.asObject()); } else { append(li, value.asPrimitive()); } list.appendChild(li); } e.appendChild(list); }
public void shouldGetObjectByIndex() { JsonElement element = array(primitive("foo"), object().put("bar", true).get()).get("1"); AssertJUnit.assertNotNull(element); AssertJUnit.assertTrue(element.isObject()); }