default float get(@Nonnull String field, float defaultValue) { return getJsonObject().get(field, defaultValue); }
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 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); }
@Override public int hashCode() { if (idField != null) { JsonElement jsonElement = get(idField); if (jsonElement != null) { return jsonElement.hashCode(); } } return defaultHashCode(); }
default JsonElement get(final String... labels) { return getJsonObject().get(labels); }
default long get(@Nonnull String field, long defaultValue) { return getJsonObject().get(field, defaultValue); }
default Entry<String, JsonElement> get(int index) { return getJsonObject().get(index); }
default JsonElement get(Object key) { return getJsonObject().get(key); }
default boolean get(@Nonnull String field, boolean defaultValue) { return getJsonObject().get(field, defaultValue); }
default int get(@Nonnull String field, int defaultValue) { return getJsonObject().get(field, defaultValue); }
default double get(@Nonnull String field, double defaultValue) { return getJsonObject().get(field, defaultValue); }
@Override public boolean equals(JsonElement t1, JsonElement t2) { JsonObject left = t1.asObject(); JsonObject right = t2.asObject(); for(String field: fields) { JsonElement lv = left.get(field); JsonElement rv = right.get(field); if(lv != null && !lv.equals(rv)) { return false; } else if(rv!=null && !rv.equals(lv)) { return false; } } return true; // Map<String, JsonElement> e1 = new HashMap<>(); // for (String f: fields) { // Validate.notNull(t1.asObject().get(f)); // e1.put(f, t1.asObject().get(f)); // } // Map<String, JsonElement> e2 = new HashMap<>(); // for (String f: fields) { // Validate.notNull(t2.asObject().get(f)); // e2.put(f, t2.asObject().get(f)); // } // return e1.equals(e2); } }
@Test(dataProvider="objectConstructors") public void shouldGetPrimitiveDefaultValues(Supplier<JsonObject> supplier) { JsonObject object = supplier.get(); assertThat(object.get("field", 42), is(42)); assertThat(object.get("field", 42l), is(42l)); assertThat(object.get("field", 42.0), is(42.0)); assertThat(object.get("field", 42.0f), is(42.0f)); assertThat(object.get("field", true), is(true)); }
public void shouldGetPrimitiveNumber() { JsonObject object = object(field("field",42)); assertThat(object.get("field", 1), is(42)); assertThat(object.get("field", 1l), is(42l)); assertThat(object.get("field", 1.0), is(42.0)); assertThat(object.get("field", 1.0f), is(42.0f)); assertThat(object.get("field", false), is(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 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 shouldReturn2ndEntry() { assertThat(object().put("1", 1).put("2", 2).put("3", 3).get().get(1).getValue(), is((JsonElement)primitive(2))); }
public void shouldExtractValue() { JsonObject o = object().put("a", object().put("b", object().put("c", "d").get()).get()).get(); Assert.assertEquals("d", o.get("a", "b", "c").asPrimitive().asString()); assertThat(o.maybeGet("a", "b", "c").isPresent()).isTrue(); assertThat(o.maybeGetString("a", "b", "c").get()).isEqualTo("d"); }