|
1 | 1 | package com.fasterxml.jackson.databind.node;
|
2 | 2 |
|
3 | 3 | import java.io.IOException;
|
| 4 | +import java.util.Arrays; |
4 | 5 | import java.util.Iterator;
|
5 | 6 | import java.util.Map;
|
| 7 | +import java.util.NoSuchElementException; |
6 | 8 |
|
7 | 9 | import com.fasterxml.jackson.core.JsonGenerator;
|
8 | 10 |
|
@@ -92,31 +94,100 @@ public void serializeWithType(JsonGenerator g, SerializerProvider ctxt, TypeSeri
|
92 | 94 | serialize(g, ctxt);
|
93 | 95 | }
|
94 | 96 |
|
95 |
| - |
96 | 97 | protected void _serializeNonRecursive(JsonGenerator g, JsonNode node) throws IOException
|
97 | 98 | {
|
98 | 99 | 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(); |
| 100 | + g.writeStartObject(this, node.size()); |
| 101 | + _serializeNonRecursive(g, new IteratorStack(), node.fields()); |
108 | 102 | } else if (node instanceof ArrayNode) {
|
109 | 103 | 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(); |
| 104 | + _serializeNonRecursive(g, new IteratorStack(), node.elements()); |
117 | 105 | } else {
|
118 | 106 | node.serialize(g, _context);
|
119 | 107 | }
|
120 | 108 | }
|
| 109 | + |
| 110 | + protected void _serializeNonRecursive(JsonGenerator g, IteratorStack stack, |
| 111 | + final Iterator<?> rootIterator) |
| 112 | + throws IOException |
| 113 | + { |
| 114 | + Iterator<?> currIt = rootIterator; |
| 115 | + while (true) { |
| 116 | + // First: any more elements from the current iterator? |
| 117 | + while (currIt.hasNext()) { |
| 118 | + JsonNode value; |
| 119 | + |
| 120 | + // Otherwise we do have another Map or Array element to handle |
| 121 | + Object elem = currIt.next(); |
| 122 | + if (elem instanceof Map.Entry<?,?>) { |
| 123 | + @SuppressWarnings("unchecked") |
| 124 | + Map.Entry<String, JsonNode> en = (Map.Entry<String, JsonNode>) elem; |
| 125 | + g.writeFieldName(en.getKey()); |
| 126 | + value = en.getValue(); |
| 127 | + } else { |
| 128 | + value = (JsonNode) elem; |
| 129 | + } |
| 130 | + if (value instanceof ObjectNode) { |
| 131 | + stack.push(currIt); |
| 132 | + currIt = value.fields(); |
| 133 | + g.writeStartObject(value, value.size()); |
| 134 | + } else if (value instanceof ArrayNode) { |
| 135 | + stack.push(currIt); |
| 136 | + currIt = value.elements(); |
| 137 | + g.writeStartArray(value, value.size()); |
| 138 | + } else { |
| 139 | + value.serialize(g, _context); |
| 140 | + } |
| 141 | + } |
| 142 | + if (g.getOutputContext().inArray()) { |
| 143 | + g.writeEndArray(); |
| 144 | + } else { |
| 145 | + g.writeEndObject(); |
| 146 | + } |
| 147 | + currIt = stack.popOrNull(); |
| 148 | + if (currIt == null) { |
| 149 | + return; |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Optimized variant similar in functionality to (a subset of) |
| 157 | + * {@link java.util.ArrayDeque}; used to hold enclosing Array/Object |
| 158 | + * nodes during recursion-as-iteration. |
| 159 | + */ |
| 160 | + final static class IteratorStack |
| 161 | + { |
| 162 | + private Iterator<?>[] _stack; |
| 163 | + private int _top, _end; |
| 164 | + |
| 165 | + public IteratorStack() { } |
| 166 | + |
| 167 | + public void push(Iterator<?> it) |
| 168 | + { |
| 169 | + if (_top < _end) { |
| 170 | + _stack[_top++] = it; // lgtm [java/dereferenced-value-may-be-null] |
| 171 | + return; |
| 172 | + } |
| 173 | + if (_stack == null) { |
| 174 | + _end = 10; |
| 175 | + _stack = new Iterator<?>[_end]; |
| 176 | + } else { |
| 177 | + // grow by 50%, for most part |
| 178 | + _end += Math.min(4000, Math.max(20, _end>>1)); |
| 179 | + _stack = Arrays.copyOf(_stack, _end); |
| 180 | + } |
| 181 | + _stack[_top++] = it; |
| 182 | + } |
| 183 | + |
| 184 | + public Iterator<?> popOrNull() { |
| 185 | + if (_top == 0) { |
| 186 | + return null; |
| 187 | + } |
| 188 | + // note: could clean up stack but due to usage pattern, should not make |
| 189 | + // much difference since the whole stack is discarded after serialization done |
| 190 | + return _stack[--_top]; |
| 191 | + } |
121 | 192 | }
|
122 | 193 | }
|
0 commit comments