add modfiable variant for Json toMap()
This commit is contained in:
parent
1805b7cd10
commit
9953d28bac
2 changed files with 36 additions and 1 deletions
|
@ -56,10 +56,17 @@ public class Json implements DataStructure {
|
|||
}
|
||||
|
||||
public static Map<String, Object> toMap(Reader reader) throws IOException {
|
||||
Objects.requireNonNull(reader);
|
||||
StringWriter writer = new StringWriter();
|
||||
reader.transferTo(writer);
|
||||
return toMap(writer.toString());
|
||||
}
|
||||
|
||||
public static Map<String, Object> toModifiableMap(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));
|
||||
return JsonGenerator.toModifiableMap(INSTANCE.createParser().parse(bufferedReader));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -34,6 +34,11 @@ public class JsonGenerator implements Generator {
|
|||
return (Map<String, Object>) internalMap(root);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Map<String, Object> toModifiableMap(Node<?> root) {
|
||||
return (Map<String, Object>) internalModifiableMap(root);
|
||||
}
|
||||
|
||||
private void internalWrite(Node<?> curnode) throws IOException {
|
||||
if (curnode instanceof ValueNode) {
|
||||
ValueNode valueNode = (ValueNode) curnode;
|
||||
|
@ -79,4 +84,27 @@ public class JsonGenerator implements Generator {
|
|||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static Object internalModifiableMap(Node<?> curnode) {
|
||||
if (curnode instanceof ValueNode) {
|
||||
ValueNode valueNode = (ValueNode) curnode;
|
||||
return valueNode.get();
|
||||
} else if (curnode instanceof MapNode) {
|
||||
MapNode mapNode = (MapNode) curnode;
|
||||
TinyMap.Builder<String, Object> map = TinyMap.builder();
|
||||
for (Map.Entry<CharSequence, Node<?>> e : mapNode.get().entrySet()) {
|
||||
map.put(e.getKey().toString(), internalMap(e.getValue()));
|
||||
}
|
||||
return map;
|
||||
} else if (curnode instanceof ListNode) {
|
||||
ListNode listNode = (ListNode) curnode;
|
||||
TinyList.Builder<Object> list = TinyList.builder();
|
||||
for (Node<?> node : listNode.get()) {
|
||||
list.add(internalMap(node));
|
||||
}
|
||||
return list;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue