Add JSON boolean type

pull/55/head
Richard Körber 2017-02-11 18:30:54 +01:00
parent 7aeb439a62
commit d6d7e04ea5
2 changed files with 24 additions and 1 deletions

View File

@ -326,6 +326,21 @@ public final class JSON implements Serializable {
}
}
/**
* Returns the value as boolean.
*
* @return integer value
*/
public boolean asBoolean() {
required();
try {
return (Boolean) val;
} catch (ClassCastException ex) {
throw new AcmeProtocolException(path + ": bad boolean " + val, ex);
}
}
/**
* Returns the value as {@link URI}.
*

View File

@ -84,7 +84,7 @@ public class JSONTest {
JSON json = TestUtils.getJsonAsObject("json");
assertThat(json.keySet(), containsInAnyOrder(
"text", "number", "uri", "url", "date", "array", "collect"));
"text", "number", "boolean", "uri", "url", "date", "array", "collect"));
assertThat(json.contains("text"), is(true));
assertThat(json.contains("music"), is(false));
assertThat(json.get("text"), is(notNullValue()));
@ -174,6 +174,7 @@ public class JSONTest {
assertThat(json.get("text").asString(), is("lorem ipsum"));
assertThat(json.get("number").asInt(), is(123));
assertThat(json.get("boolean").asBoolean(), is(true));
assertThat(json.get("uri").asURI(), is(URI.create("mailto:foo@example.com")));
assertThat(json.get("url").asURL(), is(new URL("http://example.com")));
assertThat(json.get("date").asInstant(), is(date));
@ -213,6 +214,13 @@ public class JSONTest {
// expected
}
try {
json.get("none").asBoolean();
fail("asBoolean did not fail");
} catch (AcmeProtocolException ex) {
// expected
}
try {
json.get("none").required();
fail("required did not fail");