fix map/list in fields, add null safety in convenience methods
This commit is contained in:
parent
f80c001075
commit
1805b7cd10
3 changed files with 23 additions and 8 deletions
|
@ -13,6 +13,7 @@ import java.io.Reader;
|
|||
import java.io.StringWriter;
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class Json implements DataStructure {
|
||||
|
@ -37,11 +38,14 @@ public class Json implements DataStructure {
|
|||
}
|
||||
|
||||
public static String toString(Map<String, Object> map) throws IOException {
|
||||
return INSTANCE.createBuilder().buildMap(map).build();
|
||||
return map != null ? INSTANCE.createBuilder().buildMap(map).build() : null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Map<String, Object> toMap(String json) {
|
||||
if (json == null) {
|
||||
return null;
|
||||
}
|
||||
PlainParser parser = new PlainParser();
|
||||
parser.parse(json);
|
||||
Object object = parser.getResult();
|
||||
|
@ -52,6 +56,7 @@ public class Json implements DataStructure {
|
|||
}
|
||||
|
||||
public static Map<String, Object> toMap(Reader reader) throws IOException {
|
||||
Objects.requireNonNull(reader);
|
||||
// buffered reader is required for mark() support
|
||||
try (BufferedReader bufferedReader = new BufferedReader(reader)){
|
||||
return JsonGenerator.toMap(INSTANCE.createParser().parse(bufferedReader));
|
||||
|
|
|
@ -112,6 +112,13 @@ public class JsonBuilder implements Builder {
|
|||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Builder buildValue(Object object) throws IOException {
|
||||
if (object instanceof Map) {
|
||||
buildMap((Map<String, Object>) object);
|
||||
return this;
|
||||
} else if (object instanceof Collection) {
|
||||
buildCollection((Collection<Object>) object);
|
||||
return this;
|
||||
}
|
||||
if (state.structure == Structure.MAP || state.structure == Structure.KEY) {
|
||||
beginValue(object);
|
||||
} else if (state.structure == Structure.COLLECTION) {
|
||||
|
@ -119,11 +126,6 @@ public class JsonBuilder implements Builder {
|
|||
}
|
||||
if (object == null) {
|
||||
buildNull();
|
||||
return this;
|
||||
} else if (object instanceof Map) {
|
||||
buildMap((Map<String, Object>) object);
|
||||
} else if (object instanceof Collection) {
|
||||
buildCollection((Collection<Object>) object);
|
||||
} else if (object instanceof CharSequence) {
|
||||
buildString((CharSequence) object, true);
|
||||
} else if (object instanceof Boolean) {
|
||||
|
@ -161,8 +163,7 @@ public class JsonBuilder implements Builder {
|
|||
public Builder buildKey(CharSequence string) throws IOException {
|
||||
if (state.structure == Structure.COLLECTION) {
|
||||
beginArrayValue(string);
|
||||
}
|
||||
if (state.structure == Structure.MAP || state.structure == Structure.KEY) {
|
||||
} else if (state.structure == Structure.MAP || state.structure == Structure.KEY) {
|
||||
beginKey(string != null ? string.toString() : null);
|
||||
}
|
||||
buildString(string, true);
|
||||
|
|
|
@ -194,6 +194,15 @@ public class JsonBuilderTest {
|
|||
assertEquals("{\"collection\":[{\"a\":\"b\"},{\"c\":\"d\"}]}", jsonBuilder.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListOfMapAsField() throws Exception {
|
||||
JsonBuilder jsonBuilder = new JsonBuilder();
|
||||
jsonBuilder.beginMap();
|
||||
jsonBuilder.field("collection", List.of(Map.of("a","b"), Map.of("c", "d")));
|
||||
jsonBuilder.endMap();
|
||||
assertEquals("{\"collection\":[{\"a\":\"b\"},{\"c\":\"d\"}]}", jsonBuilder.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCollectionOfEmptyMaps() throws Exception {
|
||||
JsonBuilder jsonBuilder = new JsonBuilder();
|
||||
|
|
Loading…
Reference in a new issue