Skip to content

Commit e94d89a

Browse files
committed
add array wrapper any
1 parent a912eaa commit e94d89a

File tree

11 files changed

+271
-55
lines changed

11 files changed

+271
-55
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public Any wrap(Object obj) {
4242
JsonStream.registerNativeEncoder(ArrayAny.class, anyEncoder);
4343
JsonStream.registerNativeEncoder(ObjectAny.class, anyEncoder);
4444
JsonStream.registerNativeEncoder(ListWrapperAny.class, anyEncoder);
45+
JsonStream.registerNativeEncoder(ArrayWrapperAny.class, anyEncoder);
46+
}
47+
48+
public static Any wrapArray(Object val) {
49+
return new ArrayWrapperAny(val);
4550
}
4651

4752
public interface EntryIterator {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,21 @@ public boolean toBoolean() {
9898

9999
@Override
100100
public int toInt() {
101-
return val.isEmpty() ? 0 : 1;
101+
return val.size();
102102
}
103103

104104
@Override
105105
public long toLong() {
106-
return val.isEmpty() ? 0 : 1;
106+
return val.size();
107107
}
108108

109109
@Override
110110
public float toFloat() {
111-
return val.isEmpty() ? 0 : 1;
111+
return val.size();
112112
}
113113

114114
@Override
115115
public double toDouble() {
116-
return val.isEmpty() ? 0 : 1;
116+
return val.size();
117117
}
118118
}

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,22 @@ public boolean toBoolean() {
4343

4444
@Override
4545
public int toInt() {
46-
if (cache == null) {
47-
iterator().next();
48-
}
49-
return cache.isEmpty() ? 0 : 1;
46+
return size();
5047
}
5148

5249
@Override
5350
public long toLong() {
54-
if (cache == null) {
55-
iterator().next();
56-
}
57-
return cache.isEmpty() ? 0 : 1;
51+
return size();
5852
}
5953

6054
@Override
6155
public float toFloat() {
62-
if (cache == null) {
63-
iterator().next();
64-
}
65-
return cache.isEmpty() ? 0 : 1;
56+
return size();
6657
}
6758

6859
@Override
6960
public double toDouble() {
70-
if (cache == null) {
71-
iterator().next();
72-
}
73-
return cache.isEmpty() ? 0 : 1;
61+
return size();
7462
}
7563

7664
@Override
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package com.jsoniter.any;
2+
3+
import com.jsoniter.ValueType;
4+
import com.jsoniter.output.JsonStream;
5+
6+
import java.io.IOException;
7+
import java.lang.reflect.Array;
8+
import java.util.ArrayList;
9+
import java.util.Iterator;
10+
import java.util.List;
11+
12+
class ArrayWrapperAny extends Any {
13+
14+
private final Object val;
15+
private List<Any> cache;
16+
17+
public ArrayWrapperAny(Object val) {
18+
this.val = val;
19+
}
20+
21+
@Override
22+
public ValueType valueType() {
23+
return ValueType.ARRAY;
24+
}
25+
26+
@Override
27+
public Object object() {
28+
fillCache();
29+
return cache;
30+
}
31+
32+
@Override
33+
public boolean toBoolean() {
34+
return size() != 0;
35+
}
36+
37+
@Override
38+
public int toInt() {
39+
return size();
40+
}
41+
42+
@Override
43+
public long toLong() {
44+
return size();
45+
}
46+
47+
@Override
48+
public float toFloat() {
49+
return size();
50+
}
51+
52+
@Override
53+
public double toDouble() {
54+
return size();
55+
}
56+
57+
@Override
58+
public String toString() {
59+
if (cache == null) {
60+
return JsonStream.serialize(val);
61+
} else {
62+
fillCache();
63+
return JsonStream.serialize(cache);
64+
}
65+
}
66+
67+
@Override
68+
public void writeTo(JsonStream stream) throws IOException {
69+
if (cache == null) {
70+
stream.writeVal(val);
71+
} else {
72+
fillCache();
73+
stream.writeVal(cache);
74+
}
75+
}
76+
77+
@Override
78+
public int size() {
79+
return Array.getLength(val);
80+
}
81+
82+
@Override
83+
public Any get(int index) {
84+
return fillCacheUntil(index);
85+
}
86+
87+
@Override
88+
public Any get(Object[] keys, int idx) {
89+
if (idx == keys.length) {
90+
return this;
91+
}
92+
Object key = keys[idx];
93+
if (isWildcard(key)) {
94+
fillCache();
95+
ArrayList<Any> result = new ArrayList<Any>();
96+
for (Any element : cache) {
97+
Any mapped = element.get(keys, idx + 1);
98+
if (mapped.valueType() != ValueType.INVALID) {
99+
result.add(mapped);
100+
}
101+
}
102+
return Any.wrapAnyList(result);
103+
}
104+
try {
105+
return fillCacheUntil((Integer) key).get(keys, idx + 1);
106+
} catch (IndexOutOfBoundsException e) {
107+
return new NotFoundAny(keys, idx, object());
108+
} catch (ClassCastException e) {
109+
return new NotFoundAny(keys, idx, object());
110+
}
111+
}
112+
113+
@Override
114+
public Iterator<Any> iterator() {
115+
return new WrapperIterator();
116+
}
117+
118+
private Any fillCacheUntil(int index) {
119+
if (cache == null) {
120+
cache = new ArrayList<Any>();
121+
}
122+
if (index < cache.size()) {
123+
return cache.get(index);
124+
}
125+
for (int i = cache.size(); i < size(); i++) {
126+
Any element = Any.wrap(Array.get(val, i));
127+
cache.add(element);
128+
if (index == i) {
129+
return element;
130+
}
131+
}
132+
return new NotFoundAny(index, val);
133+
}
134+
135+
private void fillCache() {
136+
if (cache == null) {
137+
cache = new ArrayList<Any>();
138+
}
139+
int size = size();
140+
if (cache.size() == size) {
141+
return;
142+
}
143+
for (int i = cache.size(); i < size; i++) {
144+
Any element = Any.wrap(Array.get(val, i));
145+
cache.add(element);
146+
}
147+
}
148+
149+
private class WrapperIterator implements Iterator<Any> {
150+
151+
private int index;
152+
private final int size;
153+
154+
private WrapperIterator() {
155+
size = size();
156+
}
157+
158+
@Override
159+
public boolean hasNext() {
160+
return index < size;
161+
}
162+
163+
@Override
164+
public Any next() {
165+
if (cache == null) {
166+
cache = new ArrayList<Any>();
167+
}
168+
if (index == cache.size()) {
169+
cache.add(Any.wrap(Array.get(val, index)));
170+
}
171+
return cache.get(index++);
172+
}
173+
174+
@Override
175+
public void remove() {
176+
throw new UnsupportedOperationException();
177+
}
178+
}
179+
}

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.util.Iterator;
99
import java.util.List;
1010

11-
public class ListWrapperAny extends Any {
11+
class ListWrapperAny extends Any {
1212

1313
private final List val;
1414
private List<Any> cache;
@@ -35,22 +35,22 @@ public boolean toBoolean() {
3535

3636
@Override
3737
public int toInt() {
38-
return val.isEmpty() ? 0 : 1;
38+
return size();
3939
}
4040

4141
@Override
4242
public long toLong() {
43-
return val.isEmpty() ? 0 : 1;
43+
return size();
4444
}
4545

4646
@Override
4747
public float toFloat() {
48-
return val.isEmpty() ? 0 : 1;
48+
return size();
4949
}
5050

5151
@Override
5252
public double toDouble() {
53-
return val.isEmpty() ? 0 : 1;
53+
return size();
5454
}
5555

5656
@Override
@@ -65,7 +65,12 @@ public String toString() {
6565

6666
@Override
6767
public void writeTo(JsonStream stream) throws IOException {
68-
stream.writeVal(val);
68+
if (cache == null) {
69+
stream.writeVal(val);
70+
} else {
71+
fillCache();
72+
stream.writeVal(cache);
73+
}
6974
}
7075

7176
@Override

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,22 @@ public boolean toBoolean() {
4949

5050
@Override
5151
public int toInt() {
52-
return val.isEmpty() ? 0 : 1;
52+
return size();
5353
}
5454

5555
@Override
5656
public long toLong() {
57-
return val.isEmpty() ? 0 : 1;
57+
return size();
5858
}
5959

6060
@Override
6161
public float toFloat() {
62-
return val.isEmpty() ? 0 : 1;
62+
return size();
6363
}
6464

6565
@Override
6666
public double toDouble() {
67-
return val.isEmpty() ? 0 : 1;
67+
return size();
6868
}
6969

7070
@Override

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,22 @@ public boolean toBoolean() {
4444

4545
@Override
4646
public int toInt() {
47-
if (cache == null) {
48-
entries().next();
49-
}
50-
return cache.isEmpty() ? 0 : 1;
47+
return size();
5148
}
5249

5350
@Override
5451
public long toLong() {
55-
if (cache == null) {
56-
entries().next();
57-
}
58-
return cache.isEmpty() ? 0 : 1;
52+
return size();
5953
}
6054

6155
@Override
6256
public float toFloat() {
63-
if (cache == null) {
64-
entries().next();
65-
}
66-
return cache.isEmpty() ? 0 : 1;
57+
return size();
6758
}
6859

6960
@Override
7061
public double toDouble() {
71-
if (cache == null) {
72-
entries().next();
73-
}
74-
return cache.isEmpty() ? 0 : 1;
62+
return size();
7563
}
7664

7765
@Override

src/main/java/com/jsoniter/output/ReflectionArrayEncoder.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.io.IOException;
88
import java.lang.reflect.Array;
99
import java.lang.reflect.Type;
10-
import java.util.ArrayList;
1110

1211
class ReflectionArrayEncoder implements Encoder {
1312

@@ -39,11 +38,6 @@ public void encode(Object obj, JsonStream stream) throws IOException {
3938

4039
@Override
4140
public Any wrap(Object obj) {
42-
int len = Array.getLength(obj);
43-
ArrayList<Any> copied = new ArrayList<Any>(len);
44-
for (int i = 0; i < len; i++) {
45-
copied.add(JsonStream.wrap(Array.get(obj, i)));
46-
}
47-
return Any.wrapAnyList(copied);
41+
return Any.wrapArray(obj);
4842
}
4943
}

0 commit comments

Comments
 (0)