private JsonBuilder() { // use the static methods this(new JsonObject()); }
@Override public Object clone() { return deepClone(); }
default Entry<String, JsonElement> first() { return getJsonObject().first(); }
@Override public int hashCode() { if (idField != null) { JsonElement jsonElement = get(idField); if (jsonElement != null) { return jsonElement.hashCode(); } } return defaultHashCode(); }
/** * Alternative to using the object() builder that allows you to add {@link Entry} instances. * @param fields one or more Entry instances (use the field method to create them). * @return the JsonObject with the entries added. */ @SafeVarargs public static @Nonnull JsonObject object(Entry<String,JsonElement>...fields) { JsonObject object = new JsonObject(); object.add(fields); return object; }
assertTrue(object.get("a","b","c").asPrimitive().asBoolean(), "extract stuff from a nested object"); assertTrue(object.getBoolean("a","b","c"), "or like this"); assertTrue(object.getInt("a","b","d") == 42, "or an integer"); assertTrue(object.getString("a","b","e").equals("hi!"), "or a string"); assertTrue(object.getArray("array").isArray(), "works for arrays as well"); assertTrue(object.getObject("a","b").isObject(), "and objects"); object.getOrCreateObject("1","2","3","4").put("5", "xxx"); assertTrue(object.getString("1","2","3","4","5").equals("xxx"), "yep, we just added a string value 5 levels deep that did not exist so far"); object.getOrCreateArray("5","4","3","2","1").add("xxx"); assertTrue(object.getArray("5","4","3","2","1").contains("xxx"), "naturally it works for arrays too"); assertTrue(object.equals(object), "equals is implemented as a deep equals"); assertTrue( object().put("a", 1).put("b", 2).get() .equals( object().put("b", 2).put("a", 1).get()), "true for objects as well");
public void shouldHandleJsonNullsOnGet() { JsonObject o = object().put("x", JsonPrimitive.JSON_NULL).get(); // should return the json null assertThat((JsonPrimitive)o.get("x"), CoreMatchers.notNullValue()); // these should all return a java null assertThat(o.getInt("x"), CoreMatchers.nullValue()); assertThat(o.getLong("x"), CoreMatchers.nullValue()); assertThat(o.getFloat("x"), CoreMatchers.nullValue()); assertThat(o.getDouble("x"), CoreMatchers.nullValue()); assertThat(o.getBoolean("x"), CoreMatchers.nullValue()); assertThat(o.getString("x"), CoreMatchers.nullValue()); assertThat(o.getArray("x"), CoreMatchers.nullValue()); assertThat(o.getObject("x"), CoreMatchers.nullValue()); }
public void shouldDoDeepClone() { JsonObject o = object().put("1", 42).put("2", "Hello world").get(); JsonObject cloneOfO = o.deepClone(); Assert.assertTrue(o.equals(cloneOfO)); o.remove("1"); Assert.assertFalse(o.equals(cloneOfO)); o.put("1", cloneOfO); Object clone = o.clone(); Assert.assertTrue(o.equals(clone)); cloneOfO.remove("2"); Assert.assertFalse(o.equals(clone)); }
public void shouldParseAsMapBasedJsonObject() { JsonObject jsonObject = new JsonObject(); for(int i=0;i<300;i++) { jsonObject.put("key_"+i, i); } JsonElement parsed = parser.parse(jsonObject.toString()); // because > 100 keys assertThat(parsed).isInstanceOf(MapBasedJsonObject.class); }
default JsonElement get(final String... labels) { return getJsonObject().get(labels); }
public void shouldScaleNumberOfKeys() { // this test proves two points: // 1) inserting 1000000 elements is fast o(1) // 2) establishing a non existent key is not there is fast-ish // if it is not, both will take ages JsonObject jsonObject = new MapBasedJsonObject(); for(int i=0;i<100000;i++) { jsonObject.put("key_"+i, i); } JsonElement jsonElement = jsonObject.get("key_IDONTEXIST"); assertThat(jsonElement).isNull(); JsonElement parsed = parser.parse(jsonObject.toString()); assertThat(parsed).isEqualTo(jsonObject); }
public void shouldConvertFieldToSet() { JsonObject object = object(field("f", array(1,1,1,1,1))); assertThat(object.getOrCreateArray("f").size(), is(5)); assertThat(object.getOrCreateSet("f").size(), is(1)); assertThat(object.getArray("f").size(), is(1)); object.getArray("f").add(1); assertThat(object.getArray("f").size(), is(1)); }
public void shouldFlatten() { JsonObject obj = object(field("x", object(field("y", array(primitive(1),primitive(2),object(field("foo","bar"))))))); JsonObject flattened = obj.flatten(":"); assertThat(flattened.getInt("x:y:0")).isEqualTo(1); assertThat(flattened.getInt("x:y:1")).isEqualTo(2); assertThat(flattened.getString("x:y:2:foo")).isEqualTo("bar"); }
public void shouldAddFields() { JsonObject object = object(field("meaningoflife", 42), field("foo", primitive("bar")), field("list",array("stuff"))); assertThat(object.getInt("meaningoflife"), is(42)); assertThat(object.getString("foo"), is("bar")); assertThat(object.getArray("list").get(0).asString(), is("stuff")); }
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 void shouldNotRevertToJsonObjectWhenCloning() { JsonObject jsonObject = new MapBasedJsonObject(); for(int i=0;i<1000;i++) { jsonObject.put("key_"+i, i); } assertThat(jsonObject.deepClone()).isInstanceOf(MapBasedJsonObject.class); assertThat(jsonObject.immutableClone()).isInstanceOf(MapBasedJsonObject.class); } }
public void shouldParseHugeObjects() { // test for an obscure pretty printing bug that was caused by using the raw outputstream instead of the buffered writer for newlines // only triggered once you go beyond the buffer size for the buffered writer; the newline appears in the wrong place then // keep this test around to ensure we don't regress on this JsonObject o=new JsonObject(); // generate a sufficiently large json object for(int i=0; i<10; i++) { JsonSet s=set(); for(int j=0; j<100; j++) { s.add(UUID.randomUUID().toString()); } o.put(UUID.randomUUID().toString(), s); } // this should not throw an exception jsonParser.parseObject(o.prettyPrint()); }
public void shouldRemoveEmptyElements() { JsonObject jsonObject = object().put("empty", object().get()).put("empty2", nullValue()).put("empty3", new JsonArray()).get(); jsonObject.removeEmpty(); assertThat("should leave empty objects",jsonObject.getObject("empty"), is(object().get())); Assert.assertEquals(jsonObject.get("empty2"), null); Assert.assertEquals(jsonObject.get("empty3"), null); }