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.io.StringWriter;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class Json implements DataStructure {
|
public class Json implements DataStructure {
|
||||||
|
@ -37,11 +38,14 @@ public class Json implements DataStructure {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String toString(Map<String, Object> map) throws IOException {
|
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")
|
@SuppressWarnings("unchecked")
|
||||||
public static Map<String, Object> toMap(String json) {
|
public static Map<String, Object> toMap(String json) {
|
||||||
|
if (json == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
PlainParser parser = new PlainParser();
|
PlainParser parser = new PlainParser();
|
||||||
parser.parse(json);
|
parser.parse(json);
|
||||||
Object object = parser.getResult();
|
Object object = parser.getResult();
|
||||||
|
@ -52,6 +56,7 @@ public class Json implements DataStructure {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Map<String, Object> toMap(Reader reader) throws IOException {
|
public static Map<String, Object> toMap(Reader reader) throws IOException {
|
||||||
|
Objects.requireNonNull(reader);
|
||||||
// buffered reader is required for mark() support
|
// buffered reader is required for mark() support
|
||||||
try (BufferedReader bufferedReader = new BufferedReader(reader)){
|
try (BufferedReader bufferedReader = new BufferedReader(reader)){
|
||||||
return JsonGenerator.toMap(INSTANCE.createParser().parse(bufferedReader));
|
return JsonGenerator.toMap(INSTANCE.createParser().parse(bufferedReader));
|
||||||
|
|
|
@ -112,6 +112,13 @@ public class JsonBuilder implements Builder {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public Builder buildValue(Object object) throws IOException {
|
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) {
|
if (state.structure == Structure.MAP || state.structure == Structure.KEY) {
|
||||||
beginValue(object);
|
beginValue(object);
|
||||||
} else if (state.structure == Structure.COLLECTION) {
|
} else if (state.structure == Structure.COLLECTION) {
|
||||||
|
@ -119,11 +126,6 @@ public class JsonBuilder implements Builder {
|
||||||
}
|
}
|
||||||
if (object == null) {
|
if (object == null) {
|
||||||
buildNull();
|
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) {
|
} else if (object instanceof CharSequence) {
|
||||||
buildString((CharSequence) object, true);
|
buildString((CharSequence) object, true);
|
||||||
} else if (object instanceof Boolean) {
|
} else if (object instanceof Boolean) {
|
||||||
|
@ -161,8 +163,7 @@ public class JsonBuilder implements Builder {
|
||||||
public Builder buildKey(CharSequence string) throws IOException {
|
public Builder buildKey(CharSequence string) throws IOException {
|
||||||
if (state.structure == Structure.COLLECTION) {
|
if (state.structure == Structure.COLLECTION) {
|
||||||
beginArrayValue(string);
|
beginArrayValue(string);
|
||||||
}
|
} else if (state.structure == Structure.MAP || state.structure == Structure.KEY) {
|
||||||
if (state.structure == Structure.MAP || state.structure == Structure.KEY) {
|
|
||||||
beginKey(string != null ? string.toString() : null);
|
beginKey(string != null ? string.toString() : null);
|
||||||
}
|
}
|
||||||
buildString(string, true);
|
buildString(string, true);
|
||||||
|
|
|
@ -194,6 +194,15 @@ public class JsonBuilderTest {
|
||||||
assertEquals("{\"collection\":[{\"a\":\"b\"},{\"c\":\"d\"}]}", jsonBuilder.build());
|
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
|
@Test
|
||||||
public void testCollectionOfEmptyMaps() throws Exception {
|
public void testCollectionOfEmptyMaps() throws Exception {
|
||||||
JsonBuilder jsonBuilder = new JsonBuilder();
|
JsonBuilder jsonBuilder = new JsonBuilder();
|
||||||
|
|
Loading…
Reference in a new issue