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) {
JSON.Array jsonCombinations = json.get("combinations").asArray();
if (jsonCombinations == null) {
if (jsonCombinations.isEmpty()) {
return Arrays.asList(challenges);
}

View File

@ -17,10 +17,8 @@ import static java.util.stream.Collectors.toList;
import java.net.URI;
import java.util.Collection;
import java.util.Collections;
import org.shredzone.acme4j.util.JSON;
import org.shredzone.acme4j.util.JSON.Array;
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.
*/
public Collection<String> getCaaIdentities() {
Array array = meta.get("caa-identities").asArray();
if (array == null) {
return Collections.emptyList();
}
return array.stream().map(Value::asString).collect(toList());
return meta.get("caa-identities").asArray().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
*/
private void fillUriList(JSON json) {
JSON.Array array = json.get(field).asArray();
if (array == null) {
return;
}
array.stream().map(JSON.Value::asURI).forEach(uriList::add);
json.get(field).asArray().stream()
.map(JSON.Value::asURI)
.forEach(uriList::add);
}
}

View File

@ -207,6 +207,13 @@ public final class JSON implements Serializable {
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.
*
@ -298,11 +305,11 @@ public final class JSON implements Serializable {
/**
* 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() {
if (val == null) {
return null;
return new Array(path, Collections.emptyList());
}
try {

View File

@ -102,12 +102,26 @@ public class JSONTest {
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 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.
*/