Skip to content

Commit ada34dc

Browse files
committed
continuing work on #3447
1 parent d2ba1ad commit ada34dc

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

Diff for: src/main/java/com/fasterxml/jackson/databind/node/ArrayNode.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,16 @@ public boolean equals(Comparator<JsonNode> comparator, JsonNode o)
169169
*/
170170

171171
@Override
172-
public void serialize(JsonGenerator f, SerializerProvider provider) throws IOException
172+
public void serialize(JsonGenerator g, SerializerProvider provider) throws IOException
173173
{
174174
final List<JsonNode> c = _children;
175175
final int size = c.size();
176-
f.writeStartArray(this, size);
176+
g.writeStartArray(this, size);
177177
for (int i = 0; i < size; ++i) { // we'll typically have array list
178-
// For now, assuming it's either BaseJsonNode, JsonSerializable
179-
JsonNode n = c.get(i);
180-
((BaseJsonNode) n).serialize(f, provider);
178+
JsonNode value = c.get(i);
179+
value.serialize(g, provider);
181180
}
182-
f.writeEndArray();
181+
g.writeEndArray();
183182
}
184183

185184
@Override

Diff for: src/main/java/com/fasterxml/jackson/databind/node/InternalNodeMapper.java

+40-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.fasterxml.jackson.databind.node;
22

33
import java.io.IOException;
4+
import java.util.Iterator;
5+
import java.util.Map;
46

57
import com.fasterxml.jackson.core.JsonGenerator;
68

@@ -58,7 +60,9 @@ private static JsonSerializable _wrapper(BaseJsonNode root) {
5860

5961
/**
6062
* Intermediate serializer we need to implement non-recursive serialization of
61-
* {@link BaseJsonNode}
63+
* {@link BaseJsonNode}.
64+
*<p>
65+
* NOTE: not designed as thread-safe; instances must NOT be shared or reused.
6266
*
6367
* @since 2.14
6468
*/
@@ -67,22 +71,52 @@ protected static class WrapperForSerializer
6771
{
6872
protected final BaseJsonNode _root;
6973

74+
// Non-final as passed when `serialize()` is called
75+
protected SerializerProvider _context;
76+
7077
public WrapperForSerializer(BaseJsonNode root) {
7178
_root = root;
7279
}
7380

7481
@Override
75-
public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException {
76-
// !!! TODO: placeholder
77-
_root.serialize(gen, serializers);
82+
public void serialize(JsonGenerator g, SerializerProvider ctxt) throws IOException {
83+
_context = ctxt;
84+
_serializeNonRecursive(g, _root);
7885
}
7986

8087
@Override
81-
public void serializeWithType(JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer)
88+
public void serializeWithType(JsonGenerator g, SerializerProvider ctxt, TypeSerializer typeSer)
8289
throws IOException
8390
{
8491
// Should not really be called given usage, so
85-
serialize(gen, serializers);
92+
serialize(g, ctxt);
93+
}
94+
95+
96+
protected void _serializeNonRecursive(JsonGenerator g, JsonNode node) throws IOException
97+
{
98+
if (node instanceof ObjectNode) {
99+
g.writeStartObject(this);
100+
Iterator<Map.Entry<String, JsonNode>> it = node.fields();
101+
while (it.hasNext()) {
102+
Map.Entry<String, JsonNode> en = it.next();
103+
JsonNode value = en.getValue();
104+
g.writeFieldName(en.getKey());
105+
value.serialize(g, _context);
106+
}
107+
g.writeEndObject();
108+
} else if (node instanceof ArrayNode) {
109+
g.writeStartArray(this, node.size());
110+
Iterator<JsonNode> it = node.elements();
111+
while (it.hasNext()) {
112+
// For now, assuming it's either BaseJsonNode, JsonSerializable
113+
JsonNode value = it.next();
114+
value.serialize(g, _context);
115+
}
116+
g.writeEndArray();
117+
} else {
118+
node.serialize(g, _context);
119+
}
86120
}
87121
}
88122
}

0 commit comments

Comments
 (0)