@Test public void nested_structure() { final String json = toJson(asList("str", 123, map(), true, array(), null, asList(asList(map(), array())))); assertSerializationRoundTrip(json); }
private void toJson(PropertyTree tree, Map<PropertyPath, Object> map, JsonWriter writer) throws IOException { Object value = map.get(tree.path); switch (JsonType.getType(value)) { case NULL: writer.nullValue(); break; case STRING: writer.value((String) value); break; case BOOLEAN: writer.value((Boolean) value); break; case NUMBER: writer.value((Number) value); break; case ARRAY: writer.beginArray(); Map<NodeId, PropertyTree> childrenMap = tree.getChildrenMap(); int nonNullElements = 0; for (int i=0; nonNullElements < childrenMap.size(); i++) { PropertyTree child = childrenMap.get(NodeId.index(i)); if (child != null) { nonNullElements++; toJson(child, map, writer); } else { writer.nullValue(); } } writer.endArray(); break; case OBJECT: writer.beginObject(); String typeAlias = ((Persistent.Object) value).type; if (!Persistent.GENERIC_TYPE.equals(typeAlias)) { writer.name(TYPE_FIELD).value(typeAlias); } for (PropertyTree child : tree.getChildren()) { NodeId nodeId = child.getNodeId(); writer.name(nodeId.toString()); toJson(child, map, writer); } writer.endObject(); break; } }
JsonSerializer { private void toJson(PropertyTree tree, Map<PropertyPath, Object> map, JsonWriter writer) throws IOException { Object value = map.get(tree.path); switch (JsonType.getType(value)) { case NULL: writer.nullValue(); break; case STRING: writer.value((String) value); break; case BOOLEAN: writer.value((Boolean) value); break; case NUMBER: writer.value((Number) value); break; case ARRAY: writer.beginArray(); Map<NodeId, PropertyTree> childrenMap = tree.getChildrenMap(); int nonNullElements = 0; for (int i=0; nonNullElements < childrenMap.size(); i++) { PropertyTree child = childrenMap.get(NodeId.index(i)); if (child != null) { nonNullElements++; toJson(child, map, writer); } else { writer.nullValue(); } } writer.endArray(); break; case OBJECT: writer.beginObject(); String typeAlias = ((Persistent.Object) value).type; if (!Persistent.GENERIC_TYPE.equals(typeAlias)) { writer.name(TYPE_FIELD).value(typeAlias); } for (PropertyTree child : tree.getChildren()) { NodeId nodeId = child.getNodeId(); writer.name(nodeId.toString()); toJson(child, map, writer); } writer.endObject(); break; } } }
JsonSerializer { private void toJson(PropertyTree tree, Map<PropertyPath, Object> map, JsonWriter writer) throws IOException { Object value = map.get(tree.path); switch (JsonType.getType(value)) { case NULL: writer.nullValue(); break; case STRING: writer.value((String) value); break; case BOOLEAN: writer.value((Boolean) value); break; case NUMBER: writer.value((Number) value); break; case ARRAY: writer.beginArray(); Map<NodeId, PropertyTree> childrenMap = tree.getChildrenMap(); int nonNullElements = 0; for (int i=0; nonNullElements < childrenMap.size(); i++) { PropertyTree child = childrenMap.get(NodeId.index(i)); if (child != null) { nonNullElements++; toJson(child, map, writer); } else { writer.nullValue(); } } writer.endArray(); break; case OBJECT: writer.beginObject(); String typeAlias = ((Persistent.Object) value).type; if (!Persistent.GENERIC_TYPE.equals(typeAlias)) { writer.name(TYPE_FIELD).value(typeAlias); } for (PropertyTree child : tree.getChildren()) { NodeId nodeId = child.getNodeId(); writer.name(nodeId.toString()); toJson(child, map, writer); } writer.endObject(); break; } } JsonSerializer();  JsonSerializer(Schema<Persistent.Type> schemaRoot);  JsonSerializer(Config config, Schema<?> schemaRoot); }
JsonSerializer { private void toJson(PropertyTree tree, Map<PropertyPath, Object> map, JsonWriter writer) throws IOException { Object value = map.get(tree.path); switch (JsonType.getType(value)) { case NULL: writer.nullValue(); break; case STRING: writer.value((String) value); break; case BOOLEAN: writer.value((Boolean) value); break; case NUMBER: writer.value((Number) value); break; case ARRAY: writer.beginArray(); Map<NodeId, PropertyTree> childrenMap = tree.getChildrenMap(); int nonNullElements = 0; for (int i=0; nonNullElements < childrenMap.size(); i++) { PropertyTree child = childrenMap.get(NodeId.index(i)); if (child != null) { nonNullElements++; toJson(child, map, writer); } else { writer.nullValue(); } } writer.endArray(); break; case OBJECT: writer.beginObject(); String typeAlias = ((Persistent.Object) value).type; if (!Persistent.GENERIC_TYPE.equals(typeAlias)) { writer.name(TYPE_FIELD).value(typeAlias); } for (PropertyTree child : tree.getChildren()) { NodeId nodeId = child.getNodeId(); writer.name(nodeId.toString()); toJson(child, map, writer); } writer.endObject(); break; } } JsonSerializer();  JsonSerializer(Schema<Persistent.Type> schemaRoot);  JsonSerializer(Config config, Schema<?> schemaRoot); JsonPaths parse(String json); String serialize(Map<PropertyPath, Object> map); }
JsonSerializer { private void toJson(PropertyTree tree, Map<PropertyPath, Object> map, JsonWriter writer) throws IOException { Object value = map.get(tree.path); switch (JsonType.getType(value)) { case NULL: writer.nullValue(); break; case STRING: writer.value((String) value); break; case BOOLEAN: writer.value((Boolean) value); break; case NUMBER: writer.value((Number) value); break; case ARRAY: writer.beginArray(); Map<NodeId, PropertyTree> childrenMap = tree.getChildrenMap(); int nonNullElements = 0; for (int i=0; nonNullElements < childrenMap.size(); i++) { PropertyTree child = childrenMap.get(NodeId.index(i)); if (child != null) { nonNullElements++; toJson(child, map, writer); } else { writer.nullValue(); } } writer.endArray(); break; case OBJECT: writer.beginObject(); String typeAlias = ((Persistent.Object) value).type; if (!Persistent.GENERIC_TYPE.equals(typeAlias)) { writer.name(TYPE_FIELD).value(typeAlias); } for (PropertyTree child : tree.getChildren()) { NodeId nodeId = child.getNodeId(); writer.name(nodeId.toString()); toJson(child, map, writer); } writer.endObject(); break; } } JsonSerializer();  JsonSerializer(Schema<Persistent.Type> schemaRoot);  JsonSerializer(Config config, Schema<?> schemaRoot); JsonPaths parse(String json); String serialize(Map<PropertyPath, Object> map); static final String TYPE_FIELD; static final String META_PREFIX; }