Return empty JSON array for easier processing

pull/55/head
Richard Körber 2017-04-16 16:26:41 +02:00
parent 4426a4a210
commit d1b313a149
5 changed files with 30 additions and 17 deletions

View File

@ -272,7 +272,7 @@ public class Authorization extends AcmeResource {
*/ */
private List<List<Challenge>> fetchCombinations(JSON json, List<Challenge> challenges) { private List<List<Challenge>> fetchCombinations(JSON json, List<Challenge> challenges) {
JSON.Array jsonCombinations = json.get("combinations").asArray(); JSON.Array jsonCombinations = json.get("combinations").asArray();
if (jsonCombinations == null) { if (jsonCombinations.isEmpty()) {
return Arrays.asList(challenges); return Arrays.asList(challenges);
} }

View File

@ -17,10 +17,8 @@ import static java.util.stream.Collectors.toList;
import java.net.URI; import java.net.URI;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import org.shredzone.acme4j.util.JSON; import org.shredzone.acme4j.util.JSON;
import org.shredzone.acme4j.util.JSON.Array;
import org.shredzone.acme4j.util.JSON.Value; import org.shredzone.acme4j.util.JSON.Value;
/** /**
@ -61,12 +59,9 @@ public class Metadata {
* itself for the purposes of CAA record validation. Empty if not available. * itself for the purposes of CAA record validation. Empty if not available.
*/ */
public Collection<String> getCaaIdentities() { public Collection<String> getCaaIdentities() {
Array array = meta.get("caa-identities").asArray(); return meta.get("caa-identities").asArray().stream()
if (array == null) { .map(Value::asString)
return Collections.emptyList(); .collect(toList());
}
return array.stream().map(Value::asString).collect(toList());
} }
/** /**

View File

@ -157,12 +157,9 @@ public class ResourceIterator<T extends AcmeResource> implements Iterator<T> {
* JSON map to read from * JSON map to read from
*/ */
private void fillUriList(JSON json) { private void fillUriList(JSON json) {
JSON.Array array = json.get(field).asArray(); json.get(field).asArray().stream()
if (array == null) { .map(JSON.Value::asURI)
return; .forEach(uriList::add);
}
array.stream().map(JSON.Value::asURI).forEach(uriList::add);
} }
} }

View File

@ -207,6 +207,13 @@ public final class JSON implements Serializable {
return data.size(); return data.size();
} }
/**
* Returns {@code true} if the array is empty.
*/
public boolean isEmpty() {
return data.isEmpty();
}
/** /**
* Gets the {@link Value} at the given index. * Gets the {@link Value} at the given index.
* *
@ -298,11 +305,11 @@ public final class JSON implements Serializable {
/** /**
* Returns the value as JSON array. * Returns the value as JSON array.
* *
* @return {@link JSON.Array}, or {@code null} if the value was not set. * @return {@link JSON.Array}, which is empty if the value was not set.
*/ */
public Array asArray() { public Array asArray() {
if (val == null) { if (val == null) {
return null; return new Array(path, Collections.emptyList());
} }
try { try {

View File

@ -102,12 +102,26 @@ public class JSONTest {
JSON.Array array = json.get("array").asArray(); JSON.Array array = json.get("array").asArray();
assertThat(array.size(), is(4)); assertThat(array.size(), is(4));
assertThat(array.isEmpty(), is(false));
assertThat(array.get(0), is(notNullValue())); assertThat(array.get(0), is(notNullValue()));
assertThat(array.get(1), is(notNullValue())); assertThat(array.get(1), is(notNullValue()));
assertThat(array.get(2), is(notNullValue())); assertThat(array.get(2), is(notNullValue()));
assertThat(array.get(3), is(notNullValue())); assertThat(array.get(3), is(notNullValue()));
} }
/**
* Test empty array.
*/
@Test
public void testEmptyArray() {
JSON json = TestUtils.getJsonAsObject("json");
JSON.Array array = json.get("missingArray").asArray();
assertThat(array.size(), is(0));
assertThat(array.isEmpty(), is(true));
assertThat(array.stream().count(), is(0L));
}
/** /**
* Test all array iterator related methods. * Test all array iterator related methods.
*/ */