Skip to content

Commit

Permalink
continuing work on #3447
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 3, 2022
1 parent d2ba1ad commit ada34dc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
11 changes: 5 additions & 6 deletions src/main/java/com/fasterxml/jackson/databind/node/ArrayNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,16 @@ public boolean equals(Comparator<JsonNode> comparator, JsonNode o)
*/

@Override
public void serialize(JsonGenerator f, SerializerProvider provider) throws IOException
public void serialize(JsonGenerator g, SerializerProvider provider) throws IOException
{
final List<JsonNode> c = _children;
final int size = c.size();
f.writeStartArray(this, size);
g.writeStartArray(this, size);
for (int i = 0; i < size; ++i) { // we'll typically have array list
// For now, assuming it's either BaseJsonNode, JsonSerializable
JsonNode n = c.get(i);
((BaseJsonNode) n).serialize(f, provider);
JsonNode value = c.get(i);
value.serialize(g, provider);
}
f.writeEndArray();
g.writeEndArray();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.fasterxml.jackson.databind.node;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import com.fasterxml.jackson.core.JsonGenerator;

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

/**
* Intermediate serializer we need to implement non-recursive serialization of
* {@link BaseJsonNode}
* {@link BaseJsonNode}.
*<p>
* NOTE: not designed as thread-safe; instances must NOT be shared or reused.
*
* @since 2.14
*/
Expand All @@ -67,22 +71,52 @@ protected static class WrapperForSerializer
{
protected final BaseJsonNode _root;

// Non-final as passed when `serialize()` is called
protected SerializerProvider _context;

public WrapperForSerializer(BaseJsonNode root) {
_root = root;
}

@Override
public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException {
// !!! TODO: placeholder
_root.serialize(gen, serializers);
public void serialize(JsonGenerator g, SerializerProvider ctxt) throws IOException {
_context = ctxt;
_serializeNonRecursive(g, _root);
}

@Override
public void serializeWithType(JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer)
public void serializeWithType(JsonGenerator g, SerializerProvider ctxt, TypeSerializer typeSer)
throws IOException
{
// Should not really be called given usage, so
serialize(gen, serializers);
serialize(g, ctxt);
}


protected void _serializeNonRecursive(JsonGenerator g, JsonNode node) throws IOException
{
if (node instanceof ObjectNode) {
g.writeStartObject(this);
Iterator<Map.Entry<String, JsonNode>> it = node.fields();
while (it.hasNext()) {
Map.Entry<String, JsonNode> en = it.next();
JsonNode value = en.getValue();
g.writeFieldName(en.getKey());
value.serialize(g, _context);
}
g.writeEndObject();
} else if (node instanceof ArrayNode) {
g.writeStartArray(this, node.size());
Iterator<JsonNode> it = node.elements();
while (it.hasNext()) {
// For now, assuming it's either BaseJsonNode, JsonSerializable
JsonNode value = it.next();
value.serialize(g, _context);
}
g.writeEndArray();
} else {
node.serialize(g, _context);
}
}
}
}

0 comments on commit ada34dc

Please sign in to comment.