From ada34dcf6676aa5c5869791c23143a65a7bf56c2 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Tue, 3 May 2022 13:49:50 -0700 Subject: [PATCH] continuing work on #3447 --- .../jackson/databind/node/ArrayNode.java | 11 ++--- .../databind/node/InternalNodeMapper.java | 46 ++++++++++++++++--- 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/databind/node/ArrayNode.java b/src/main/java/com/fasterxml/jackson/databind/node/ArrayNode.java index d0950f13cf..e72b9e592a 100644 --- a/src/main/java/com/fasterxml/jackson/databind/node/ArrayNode.java +++ b/src/main/java/com/fasterxml/jackson/databind/node/ArrayNode.java @@ -169,17 +169,16 @@ public boolean equals(Comparator comparator, JsonNode o) */ @Override - public void serialize(JsonGenerator f, SerializerProvider provider) throws IOException + public void serialize(JsonGenerator g, SerializerProvider provider) throws IOException { final List 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 diff --git a/src/main/java/com/fasterxml/jackson/databind/node/InternalNodeMapper.java b/src/main/java/com/fasterxml/jackson/databind/node/InternalNodeMapper.java index 3c30d61c3a..6173ee8e79 100644 --- a/src/main/java/com/fasterxml/jackson/databind/node/InternalNodeMapper.java +++ b/src/main/java/com/fasterxml/jackson/databind/node/InternalNodeMapper.java @@ -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; @@ -58,7 +60,9 @@ private static JsonSerializable _wrapper(BaseJsonNode root) { /** * Intermediate serializer we need to implement non-recursive serialization of - * {@link BaseJsonNode} + * {@link BaseJsonNode}. + *

+ * NOTE: not designed as thread-safe; instances must NOT be shared or reused. * * @since 2.14 */ @@ -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> it = node.fields(); + while (it.hasNext()) { + Map.Entry 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 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); + } } } }