1
1
package com .fasterxml .jackson .databind .node ;
2
2
3
3
import java .io .IOException ;
4
+ import java .util .Iterator ;
5
+ import java .util .Map ;
4
6
5
7
import com .fasterxml .jackson .core .JsonGenerator ;
6
8
@@ -58,7 +60,9 @@ private static JsonSerializable _wrapper(BaseJsonNode root) {
58
60
59
61
/**
60
62
* 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.
62
66
*
63
67
* @since 2.14
64
68
*/
@@ -67,22 +71,52 @@ protected static class WrapperForSerializer
67
71
{
68
72
protected final BaseJsonNode _root ;
69
73
74
+ // Non-final as passed when `serialize()` is called
75
+ protected SerializerProvider _context ;
76
+
70
77
public WrapperForSerializer (BaseJsonNode root ) {
71
78
_root = root ;
72
79
}
73
80
74
81
@ 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 );
78
85
}
79
86
80
87
@ Override
81
- public void serializeWithType (JsonGenerator gen , SerializerProvider serializers , TypeSerializer typeSer )
88
+ public void serializeWithType (JsonGenerator g , SerializerProvider ctxt , TypeSerializer typeSer )
82
89
throws IOException
83
90
{
84
91
// 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
+ }
86
120
}
87
121
}
88
122
}
0 commit comments