Skip to content

Commit 3d055f1

Browse files
committed
reflection array encoder
1 parent 3a3d7e5 commit 3d055f1

File tree

10 files changed

+123
-21
lines changed

10 files changed

+123
-21
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ public static String genArray(Class clazz) {
1515
append(lines, "if (obj == null) { stream.writeNull(); return; }");
1616
append(lines, "{{comp}}[] arr = ({{comp}}[])obj;");
1717
append(lines, "if (arr.length == 0) { stream.writeEmptyArray(); return; }");
18-
append(lines, "stream.startArray();");
18+
append(lines, "stream.writeArrayStart();");
1919
append(lines, "int i = 0;");
2020
append(lines, "{{op}}");
2121
append(lines, "while (i < arr.length) {");
2222
append(lines, "stream.writeMore();");
2323
append(lines, "{{op}}");
2424
append(lines, "}");
25-
append(lines, "stream.endArray();");
25+
append(lines, "stream.writeArrayEnd();");
2626
append(lines, "}");
2727
return lines.toString()
2828
.replace("{{comp}}", compType.getCanonicalName())
@@ -59,13 +59,13 @@ private static String genCollection(Class clazz, Type compType) {
5959
append(lines, "if (obj == null) { stream.writeNull(); return; }");
6060
append(lines, "java.util.Iterator iter = ((java.util.Collection)obj).iterator();");
6161
append(lines, "if (!iter.hasNext()) { stream.writeEmptyArray(); return; }");
62-
append(lines, "stream.startArray();");
62+
append(lines, "stream.writeArrayStart();");
6363
append(lines, "{{op}}");
6464
append(lines, "while (iter.hasNext()) {");
6565
append(lines, "stream.writeMore();");
6666
append(lines, "{{op}}");
6767
append(lines, "}");
68-
append(lines, "stream.endArray();");
68+
append(lines, "stream.writeArrayEnd();");
6969
append(lines, "}");
7070
return lines.toString()
7171
.replace("{{comp}}", CodegenImplNative.getTypeName(compType))

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ public static String genMap(Class clazz, Type[] typeArgs) {
3131
append(lines, "java.util.Iterator iter = map.entrySet().iterator();");
3232
append(lines, "if(!iter.hasNext()) { stream.writeEmptyObject(); return; }");
3333
append(lines, "java.util.Map.Entry entry = (java.util.Map.Entry)iter.next();");
34-
append(lines, "stream.startObject();");
35-
append(lines, "stream.writeField((String)entry.getKey());");
34+
append(lines, "stream.writeObjectStart();");
35+
append(lines, "stream.writeObjectField((String)entry.getKey());");
3636
append(lines, "{{op}}");
3737
append(lines, "while(iter.hasNext()) {");
3838
append(lines, "entry = (java.util.Map.Entry)iter.next();");
3939
append(lines, "stream.writeMore();");
40-
append(lines, "stream.writeField((String)entry.getKey());");
40+
append(lines, "stream.writeObjectField((String)entry.getKey());");
4141
append(lines, "{{op}}");
4242
append(lines, "}");
43-
append(lines, "stream.endObject();");
43+
append(lines, "stream.writeObjectEnd();");
4444
append(lines, "}");
4545
return lines.toString()
4646
.replace("{{clazz}}", clazz.getName())

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ public static String genObject(Class clazz) {
1313
append(lines, "if (obj == null) { stream.writeNull(); return; }");
1414
if (hasFieldOutput(desc)) {
1515
boolean notFirst = false;
16-
append(lines, "stream.startObject();");
16+
append(lines, "stream.writeObjectStart();");
1717
for (Binding field : desc.allEncoderBindings()) {
1818
for (String toName : field.toNames) {
1919
if (notFirst) {
2020
append(lines, "stream.writeMore();");
2121
} else {
2222
notFirst = true;
2323
}
24-
append(lines, String.format("stream.writeField(\"%s\");", toName));
24+
append(lines, String.format("stream.writeObjectField(\"%s\");", toName));
2525
append(lines, genField(field));
2626
}
2727
}
28-
append(lines, "stream.endObject();");
28+
append(lines, "stream.writeObjectEnd();");
2929
} else {
3030
append(lines, "stream.writeEmptyObject();");
3131
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public final void writeEmptyArray() throws IOException {
184184
write(']');
185185
}
186186

187-
public final void startArray() throws IOException {
187+
public final void writeArrayStart() throws IOException {
188188
indention += indentionStep;
189189
write('[');
190190
writeIndention();
@@ -218,24 +218,24 @@ private void writeIndention(int delta) throws IOException {
218218
}
219219
}
220220

221-
public final void endArray() throws IOException {
221+
public final void writeArrayEnd() throws IOException {
222222
writeIndention(indentionStep);
223223
indention -= indentionStep;
224224
write(']');
225225
}
226226

227-
public final void startObject() throws IOException {
227+
public final void writeObjectStart() throws IOException {
228228
indention += indentionStep;
229229
write('{');
230230
writeIndention();
231231
}
232232

233-
public final void writeField(String field) throws IOException {
233+
public final void writeObjectField(String field) throws IOException {
234234
writeVal(field);
235235
write(':');
236236
}
237237

238-
public final void endObject() throws IOException {
238+
public final void writeObjectEnd() throws IOException {
239239
writeIndention(indentionStep);
240240
indention -= indentionStep;
241241
write('}');
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.jsoniter.output;
2+
3+
import com.jsoniter.spi.Encoder;
4+
import com.jsoniter.spi.TypeLiteral;
5+
6+
import java.io.IOException;
7+
import java.lang.reflect.Array;
8+
import java.lang.reflect.Type;
9+
10+
class ReflectionArrayEncoder implements Encoder {
11+
12+
private final TypeLiteral compTypeLiteral;
13+
14+
public ReflectionArrayEncoder(Class clazz, Type[] typeArgs) {
15+
compTypeLiteral = TypeLiteral.create(clazz.getComponentType());
16+
}
17+
18+
@Override
19+
public void encode(Object obj, JsonStream stream) throws IOException {
20+
if (null == obj) {
21+
stream.writeNull();
22+
return;
23+
}
24+
int len = Array.getLength(obj);
25+
if (len == 0) {
26+
stream.writeEmptyArray();
27+
return;
28+
}
29+
stream.writeArrayStart();
30+
stream.writeVal(compTypeLiteral, Array.get(obj, 0));
31+
for (int i = 1; i < len; i++) {
32+
stream.writeMore();
33+
stream.writeVal(compTypeLiteral, Array.get(obj, i));
34+
}
35+
stream.writeArrayEnd();
36+
}
37+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.jsoniter.output;
2+
3+
import com.jsoniter.spi.Encoder;
4+
import com.jsoniter.spi.TypeLiteral;
5+
6+
import java.io.IOException;
7+
import java.lang.reflect.Type;
8+
import java.util.Collection;
9+
import java.util.Iterator;
10+
11+
class ReflectionCollectionEncoder implements Encoder {
12+
13+
private final TypeLiteral compTypeLiteral;
14+
15+
public ReflectionCollectionEncoder(Class clazz, Type[] typeArgs) {
16+
if (typeArgs.length > 0) {
17+
compTypeLiteral = TypeLiteral.create(typeArgs[0]);
18+
} else {
19+
compTypeLiteral = TypeLiteral.create(Object.class);
20+
}
21+
}
22+
23+
@Override
24+
public void encode(Object obj, JsonStream stream) throws IOException {
25+
if (null == obj) {
26+
stream.writeNull();
27+
return;
28+
}
29+
Collection col = (Collection) obj;
30+
Iterator iter = col.iterator();
31+
if (!iter.hasNext()) {
32+
stream.writeEmptyArray();
33+
return;
34+
}
35+
stream.writeArrayStart();
36+
stream.writeVal(compTypeLiteral, iter.next());
37+
while (iter.hasNext()) {
38+
stream.writeMore();
39+
stream.writeVal(compTypeLiteral, iter.next());
40+
}
41+
stream.writeArrayEnd();
42+
}
43+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@
33
import com.jsoniter.spi.Encoder;
44

55
import java.lang.reflect.Type;
6+
import java.util.Collection;
67

78
public class ReflectionEncoderFactory {
89

910
public static Encoder create(Class clazz, Type... typeArgs) {
11+
if (clazz.isArray()) {
12+
return new ReflectionArrayEncoder(clazz, typeArgs);
13+
}
14+
if (Collection.class.isAssignableFrom(clazz)) {
15+
return new ReflectionCollectionEncoder(clazz, typeArgs);
16+
}
1017
return new ReflectionObjectEncoder(clazz);
1118
}
1219
}

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,32 @@ private void enocde_(Object obj, JsonStream stream) throws Exception {
3030
stream.writeNull();
3131
return;
3232
}
33-
stream.startObject();
33+
stream.writeObjectStart();
34+
boolean notFirst = false;
3435
for (Binding field : desc.fields) {
3536
Object val = field.field.get(obj);
3637
for (String toName : field.toNames) {
37-
stream.writeField(toName);
38+
if (notFirst) {
39+
stream.writeMore();
40+
} else {
41+
notFirst = true;
42+
}
43+
stream.writeObjectField(toName);
3844
stream.writeVal(val);
3945
}
4046
}
4147
for (Binding getter : desc.getters) {
4248
Object val = getter.method.invoke(obj);
4349
for (String toName : getter.toNames) {
44-
stream.writeField(toName);
50+
if (notFirst) {
51+
stream.writeMore();
52+
} else {
53+
notFirst = true;
54+
}
55+
stream.writeObjectField(toName);
4556
stream.writeVal(val);
4657
}
4758
}
48-
stream.endObject();
59+
stream.writeObjectEnd();
4960
}
5061
}

src/test/java/com/jsoniter/output/TestArray.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
public class TestArray extends TestCase {
1212

13+
static {
14+
// JsonStream.setMode(EncodingMode.REFLECTION_MODE);
15+
}
16+
1317
private ByteArrayOutputStream baos;
1418
private JsonStream stream;
1519

src/test/java/com/jsoniter/output/TestObject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class TestObject extends TestCase {
1212

1313
static {
1414
JsoniterAnnotationSupport.enable();
15-
JsonStream.setMode(EncodingMode.REFLECTION_MODE);
15+
// JsonStream.setMode(EncodingMode.REFLECTION_MODE);
1616
}
1717

1818
private ByteArrayOutputStream baos;

0 commit comments

Comments
 (0)