public Double getDouble(final String key) { if (jsonObject == null) { throw new NullPointerException("json object is null"); } if (is(Map.class)) { final Object value = get(key); return value != null ? Double.valueOf(value.toString()) : null; } else { throw new IllegalArgumentException("not a map but a " + jsonObject.getClass().getSimpleName()); } }
@Test public void shouldGetInnerValueByDottedKeys() { /** * { * "outer" : { * "innter": { * "key" : "value" * } * } */ final Map<String, ?> inner = singletonMap("key", "value"); final Map<String, ?> map = singletonMap("outer", singletonMap("inner", inner)); assertThat(jsonMapFrom(map).get("outer.inner").getString("key"), is("value")); }
public Integer getInt(final String key) { if (jsonObject == null) { throw new NullPointerException("json object is null"); } if (is(Map.class)) { final Object value = get(key); return value != null ? Integer.valueOf(value.toString()) : null; } else { throw new IllegalArgumentException("not a map but a " + jsonObject.getClass().getSimpleName()); } }
@Test public void shouldCreateStatusRepresentationWithDetailInclUrl() { // given final StatusRepresentation json = statusRepresentationOf( applicationStatus( mock(ApplicationInfo.class), mock(ClusterInfo.class), mock(SystemInfo.class), mock(VersionInfo.class), mock(TeamInfo.class), singletonList( statusDetail("someDetail", OK, "some message", link("item", "http://example.org/some/url", "some title")) ) ) ); // then assertThat(json.application.status, is(OK)); final JsonMap jsonMap = jsonMapFrom(json.application.statusDetails.get("someDetail")); final JsonMap link = jsonMap.get("links").asListOf(JsonMap.class).get(0); assertThat(link.getString("href"), is("http://example.org/some/url")); assertThat(link.getString("title"), is("some title")); assertThat(link.getString("rel"), is("item")); }
public Object getObject(final String key) { if (jsonObject == null) { throw new NullPointerException("json object is null"); } if (is(Map.class)) { return get(key); } else { throw new IllegalArgumentException("not a map but a " + jsonObject.getClass().getSimpleName()); } }
public Boolean getBoolean(final String key) { if (jsonObject == null) { throw new NullPointerException("json object is null"); } if (is(Map.class)) { final Object value = get(key); return value != null ? Boolean.valueOf(value.toString()) : null; } else { throw new IllegalArgumentException("not a map but a " + jsonObject.getClass().getSimpleName()); } }
public Long getLong(final String key) { if (jsonObject == null) { throw new NullPointerException("json object is null"); } if (is(Map.class)) { final Object value = get(key); return value != null ? Long.valueOf(value.toString()) : null; } else { throw new IllegalArgumentException("not a map but a " + jsonObject.getClass().getSimpleName()); } }
public Float getFloat(final String key) { if (jsonObject == null) { throw new NullPointerException("json object is null"); } if (is(Map.class)) { final Object value = get(key); return value != null ? Float.valueOf(value.toString()) : null; } else { throw new IllegalArgumentException("not a map but a " + jsonObject.getClass().getSimpleName()); } }
public String getString(final String key) { if (jsonObject == null) { throw new NullPointerException("json object is null"); } if (is(Map.class)) { final Object value = get(key); if (value == null) { return null; } else { return value.toString(); } } else { throw new IllegalArgumentException("not a map but a " + jsonObject.getClass().getSimpleName()); } }
public JsonMap get(final String s) { if (jsonObject == null) { throw new NullPointerException("json object is null"); } if (is(Map.class)) { String[] segments = s.split("\\."); if (segments.length < 2) { final Object object = ((Map) jsonObject).get(s); return object != null ? new JsonMap(object) : null; } else { return get(segments[0]).get(s.substring(s.indexOf(".")+1)); } } else { throw new IllegalArgumentException("not a map but a " + jsonObject.getClass().getSimpleName()); } }
public Date getDate(final String key) { if (jsonObject == null) { throw new NullPointerException("json object is null"); } if (is(Map.class)) { final JsonMap value = get(key); if (value == null) { return null; } else { if (value.is(Date.class)) { return (Date) value.jsonObject; } if (value.is(String.class)) { return from(parse(value.toString())); } else { throw new ClassCastException("Value of " + key + " is not a date but a " + value.getClass().getSimpleName()); } } } else { throw new IllegalArgumentException("not a map but a " + jsonObject.getClass().getSimpleName()); } }
@Test public void shouldGetInnerMap() { final Map<String, ?> inner = singletonMap("key", "value"); final Map<String, ?> map = singletonMap("inner", inner); assertThat(jsonMapFrom(map).get("inner"), is(jsonMapFrom(inner))); }
@Test public void shouldRemoveKeyFromInnerObject() { final Map<String, ?> map = singletonMap("key", new HashMap<String, String>() {{ put("removeMe", "hello world"); }}); final JsonMap jsonMap = jsonMapFrom(map); jsonMap.get("key").remove("removeMe"); assertThat(jsonMap, is(jsonMapFrom(singletonMap("key", emptyMap())))); }