|
1 | 1 | package com.jsoniter.any; |
2 | 2 |
|
3 | 3 | import com.jsoniter.*; |
| 4 | +import com.jsoniter.output.JsonStream; |
4 | 5 | import com.jsoniter.spi.JsonException; |
| 6 | +import com.jsoniter.spi.TypeLiteral; |
5 | 7 |
|
6 | 8 | import java.io.IOException; |
7 | 9 | import java.util.HashMap; |
|
11 | 13 |
|
12 | 14 | class ObjectLazyAny extends LazyAny { |
13 | 15 |
|
| 16 | + private final static TypeLiteral<Map<String, Any>> typeLiteral = new TypeLiteral<Map<String, Any>>(){}; |
14 | 17 | private Map<Object, Any> cache; |
15 | 18 | private int lastParsedPos; |
16 | 19 |
|
@@ -134,14 +137,16 @@ private void fillCache() { |
134 | 137 | try { |
135 | 138 | JsonIterator iter = JsonIterator.tlsIter.get(); |
136 | 139 | iter.reset(data, lastParsedPos, tail); |
137 | | - if (!CodegenAccess.readObjectStart(iter)) { |
138 | | - lastParsedPos = tail; |
139 | | - return; |
| 140 | + if (lastParsedPos == head) { |
| 141 | + if (!CodegenAccess.readObjectStart(iter)) { |
| 142 | + lastParsedPos = tail; |
| 143 | + return; |
| 144 | + } |
| 145 | + String field = CodegenAccess.readObjectFieldAsString(iter); |
| 146 | + cache.put(field, iter.readAny()); |
140 | 147 | } |
141 | | - String field = CodegenAccess.readObjectFieldAsString(iter); |
142 | | - cache.put(field, iter.readAny()); |
143 | 148 | while (CodegenAccess.nextToken(iter) == ',') { |
144 | | - field = CodegenAccess.readObjectFieldAsString(iter); |
| 149 | + String field = CodegenAccess.readObjectFieldAsString(iter); |
145 | 150 | cache.put(field, iter.readAny()); |
146 | 151 | } |
147 | 152 | lastParsedPos = tail; |
@@ -223,4 +228,15 @@ public Any value() { |
223 | 228 | return value; |
224 | 229 | } |
225 | 230 | } |
| 231 | + |
| 232 | + @Override |
| 233 | + public void writeTo(JsonStream stream) throws IOException { |
| 234 | + if (lastParsedPos == head) { |
| 235 | + super.writeTo(stream); |
| 236 | + } else { |
| 237 | + // there might be modification |
| 238 | + fillCache(); |
| 239 | + stream.writeVal(typeLiteral, (Map) cache); |
| 240 | + } |
| 241 | + } |
226 | 242 | } |
0 commit comments