Skip to content

Commit d312340

Browse files
committed
add todo
1 parent 0890724 commit d312340

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

demo/src/test/java/com/jsoniter/demo/LazyAny.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.jsoniter.any.Any;
44
import com.jsoniter.JsonIterator;
55
import com.jsoniter.Slice;
6+
import com.jsoniter.output.JsonStream;
67
import org.junit.Test;
78
import org.openjdk.jmh.Main;
89
import org.openjdk.jmh.annotations.*;
@@ -12,6 +13,7 @@
1213
import java.io.IOException;
1314
import java.io.InputStream;
1415
import java.util.Arrays;
16+
import java.util.HashMap;
1517
import java.util.List;
1618
import java.util.Map;
1719

@@ -40,15 +42,23 @@ public static void main(String[] args) throws Exception {
4042
});
4143
}
4244

45+
public static class User {
46+
public int index;
47+
public String name;
48+
}
49+
4350
@Test
4451
public void test() throws IOException {
4552
benchSetup(null);
4653
System.out.println(jsoniter());
4754
System.out.println(jsoniter_object());
4855

49-
String input = "{'numbers': ['1', '2', ['3', '4']]}".replace('\'', '"');
50-
String[] array = JsonIterator.deserialize(input).get("numbers", 2).as(String[].class);
51-
System.out.println(Arrays.toString(array));
56+
User tom = new User();
57+
tom.index = 1;
58+
tom.name = "tom";
59+
Map<String, Any> tomAsMap = Any.wrap(tom).asMap();
60+
tomAsMap.put("age", Any.wrap(17));
61+
System.out.println(JsonStream.serialize(tomAsMap));
5262
}
5363

5464
@Benchmark

src/main/java/com/jsoniter/IterImplForStreaming.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ final static Slice readSlice(JsonIterator iter) throws IOException {
180180
iter.head = end;
181181
return iter.reusableSlice;
182182
}
183+
// TODO: avoid small memory allocation
183184
byte[] part1 = new byte[iter.tail - iter.head];
184185
System.arraycopy(iter.buf, iter.head, part1, 0, part1.length);
185186
for (; ; ) {
@@ -284,6 +285,7 @@ final static byte readByte(JsonIterator iter) throws IOException {
284285
}
285286

286287
public static Any readAny(JsonIterator iter) throws IOException {
288+
// TODO: avoid small memory allocation
287289
iter.skipStartedAt = iter.head;
288290
byte c = IterImpl.nextToken(iter);
289291
switch (c) {

src/main/java/com/jsoniter/JsonIterator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class JsonIterator implements Closeable {
2323
int tail;
2424
int skipStartedAt = -1; // skip should keep bytes starting at this pos
2525

26-
Map<String, Object> tempObjects = new HashMap<String, Object>();
26+
Map<String, Object> tempObjects = null; // used in reflection object decoder
2727
final Slice reusableSlice = new Slice(null, 0, 0);
2828
char[] reusableChars = new char[32];
2929
Object existingObject = null; // the set should be bind to next

src/main/java/com/jsoniter/ReflectionObjectDecoder.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ private Object decode_(JsonIterator iter) throws Exception {
179179
CodegenAccess.resetExistingObject(iter);
180180
return null;
181181
}
182+
if (iter.tempObjects == null) {
183+
iter.tempObjects = new HashMap<String, Object>();
184+
}
182185
Object[] temp = (Object[]) iter.tempObjects.get(tempCacheKey);
183186
if (temp == null) {
184187
temp = new Object[tempCount];
@@ -266,6 +269,9 @@ private Object decode_(JsonIterator iter) throws Exception {
266269
}
267270
Map<String, Object> extra = null;
268271
long tracker = 0L;
272+
if (iter.tempObjects == null) {
273+
iter.tempObjects = new HashMap<String, Object>();
274+
}
269275
Object[] temp = (Object[]) iter.tempObjects.get(tempCacheKey);
270276
if (temp == null) {
271277
temp = new Object[tempCount];
@@ -388,6 +394,9 @@ private void applyWrappers(Object[] temp, Object obj) throws Exception {
388394
}
389395

390396
private Object createNewObject(JsonIterator iter, Object[] temp) throws Exception {
397+
if (iter.tempObjects == null) {
398+
iter.tempObjects = new HashMap<String, Object>();
399+
}
391400
Object[] ctorArgs = (Object[]) iter.tempObjects.get(ctorArgsCacheKey);
392401
if (ctorArgs == null) {
393402
ctorArgs = new Object[desc.ctor.parameters.size()];

src/main/java/com/jsoniter/any/Any.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ public Object object(Object... keys) {
9696

9797
public abstract Object object();
9898

99+
public Map<String, Any> asMap() {
100+
return (Map<String, Any>) object();
101+
}
102+
103+
public List<Any> asList() {
104+
return (List<Any>) object();
105+
}
106+
99107
public <T> T as(Class<T> clazz, Object... keys) {
100108
Any found = get(keys);
101109
if (found == null) {

0 commit comments

Comments
 (0)