mirror of https://github.com/shred/acme4j
Set default value for optional JSON keys
parent
2ceffa42e3
commit
173aa14e40
|
@ -277,6 +277,16 @@ public final class JSON implements Serializable {
|
|||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the value is present. If not, the default value is used instead.
|
||||
*
|
||||
* @param def Default value
|
||||
* @return itself
|
||||
*/
|
||||
public Value orElse(Object def) {
|
||||
return val != null ? this : new Value(path, def);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value as {@link String}.
|
||||
*
|
||||
|
|
|
@ -237,6 +237,10 @@ public class JSONTest {
|
|||
assertThat(json.get("none").asBinary(), is(nullValue()));
|
||||
assertThat(json.get("none").asProblem(BASE_URL), is(nullValue()));
|
||||
|
||||
assertThat(json.get("none").orElse("foo").asString(), is("foo"));
|
||||
assertThat(json.get("none").orElse(42).asInt(), is(42));
|
||||
assertThat(json.get("none").orElse(true).asBoolean(), is(true));
|
||||
|
||||
try {
|
||||
json.get("none").asInt();
|
||||
fail("asInt did not fail");
|
||||
|
|
Loading…
Reference in New Issue