JSON implements Serializable { public Value get(String key) { return new Value( path.isEmpty() ? key : path + '.' + key, data.get(key)); } private  JSON(Map<String, Object> data); private  JSON(String path, Map<String, Object> data); static JSON parse(InputStream in); static JSON parse(String json); static JSON empty(); Set<String> keySet(); boolean contains(String key); Value get(String key); @Override String toString(); Map<String,Object> toMap();  }
@Test public void testArray() { JSON json = TestUtils.getJSON("datatypes"); JSON.Array array = json.get("array").asArray(); assertThat(array.size(), is(4)); assertThat(array.isEmpty(), is(false)); assertThat(array.get(0), is(notNullValue())); assertThat(array.get(1), is(notNullValue())); assertThat(array.get(2), is(notNullValue())); assertThat(array.get(3), is(notNullValue())); }
@Test public void testEmptyArray() { JSON json = TestUtils.getJSON("datatypes"); JSON.Array array = json.get("missingArray").asArray(); assertThat(array.size(), is(0)); assertThat(array.isEmpty(), is(true)); assertThat(array.stream().count(), is(0L)); }
@Test public void testArrayIterator() { JSON json = TestUtils.getJSON("datatypes"); JSON.Array array = json.get("array").asArray(); Iterator<JSON.Value> it = array.iterator(); assertThat(it, is(notNullValue())); assertThat(it.hasNext(), is(true)); assertThat(it.next().asString(), is("foo")); assertThat(it.hasNext(), is(true)); assertThat(it.next().asInt(), is(987)); assertThat(it.hasNext(), is(true)); assertThat(it.next().asArray().size(), is(3)); assertThat(it.hasNext(), is(true)); try { it.remove(); fail("was able to remove from array"); } catch (UnsupportedOperationException ex) { } assertThat(it.next().asObject(), is(notNullValue())); assertThat(it.hasNext(), is(false)); try { it.next(); fail("next past last element"); } catch (NoSuchElementException ex) { } }
@Test public void testNullGetter() throws MalformedURLException { JSON json = TestUtils.getJSON("datatypes"); assertThat(json.get("none"), is(notNullValue())); assertThat(json.get("none").isPresent(), is(false)); assertThat(json.get("none").optional().isPresent(), is(false)); assertThat(json.get("none").map(Value::asString).isPresent(), is(false)); try { json.get("none").asString(); fail("asString did not fail"); } catch (AcmeProtocolException ex) { } try { json.get("none").asURI(); fail("asURI did not fail"); } catch (AcmeProtocolException ex) { } try { json.get("none").asURL(); fail("asURL did not fail"); } catch (AcmeProtocolException ex) { } try { json.get("none").asInstant(); fail("asInstant did not fail"); } catch (AcmeProtocolException ex) { } try { json.get("none").asDuration(); fail("asDuration did not fail"); } catch (AcmeProtocolException ex) { } try { json.get("none").asObject(); fail("asObject did not fail"); } catch (AcmeProtocolException ex) { } try { json.get("none").asEncodedObject(); fail("asEncodedObject did not fail"); } catch (AcmeProtocolException ex) { } try { json.get("none").asStatus(); fail("asStatus did not fail"); } catch (AcmeProtocolException ex) { } try { json.get("none").asBinary(); fail("asBinary did not fail"); } catch (AcmeProtocolException ex) { } try { json.get("none").asProblem(BASE_URL); fail("asProblem did not fail"); } catch (AcmeProtocolException ex) { } try { json.get("none").asInt(); fail("asInt did not fail"); } catch (AcmeProtocolException ex) { } try { json.get("none").asBoolean(); fail("asBoolean did not fail"); } catch (AcmeProtocolException ex) { } }
@Test public void testWrongGetter() throws MalformedURLException { JSON json = TestUtils.getJSON("datatypes"); try { json.get("text").asObject(); fail("no exception was thrown"); } catch (AcmeProtocolException ex) { } try { json.get("text").asEncodedObject(); fail("no exception was thrown"); } catch (AcmeProtocolException ex) { } try { json.get("text").asArray(); fail("no exception was thrown"); } catch (AcmeProtocolException ex) { } try { json.get("text").asInt(); fail("no exception was thrown"); } catch (AcmeProtocolException ex) { } try { json.get("text").asURI(); fail("no exception was thrown"); } catch (AcmeProtocolException ex) { } try { json.get("text").asURL(); fail("no exception was thrown"); } catch (AcmeProtocolException ex) { } try { json.get("text").asInstant(); fail("no exception was thrown"); } catch (AcmeProtocolException ex) { } try { json.get("text").asDuration(); fail("no exception was thrown"); } catch (AcmeProtocolException ex) { } try { json.get("text").asProblem(BASE_URL); fail("no exception was thrown"); } catch (AcmeProtocolException ex) { } }