11package com .jsoniter .output ;
22
33import com .jsoniter .spi .ClassInfo ;
4+ import com .jsoniter .spi .JsoniterSpi ;
45
56import java .lang .reflect .Type ;
67import java .util .*;
78
89class CodegenImplArray {
910
11+ public static CodegenResult genCollection (String cacheKey , ClassInfo classInfo ) {
12+ Type [] typeArgs = classInfo .typeArgs ;
13+ Class clazz = classInfo .clazz ;
14+ Type compType = Object .class ;
15+ if (typeArgs .length == 0 ) {
16+ // default to List<Object>
17+ } else if (typeArgs .length == 1 ) {
18+ compType = typeArgs [0 ];
19+ } else {
20+ throw new IllegalArgumentException (
21+ "can not bind to generic collection without argument types, " +
22+ "try syntax like TypeLiteral<List<Integer>>{}" );
23+ }
24+ if (clazz == List .class ) {
25+ clazz = ArrayList .class ;
26+ } else if (clazz == Set .class ) {
27+ clazz = HashSet .class ;
28+ }
29+ if (List .class .isAssignableFrom (clazz )) {
30+ return genList (cacheKey , clazz , compType );
31+ } else {
32+ return genCollection (cacheKey , clazz , compType );
33+ }
34+ }
35+
1036 public static CodegenResult genArray (String cacheKey , ClassInfo classInfo ) {
37+ boolean noIndention = JsoniterSpi .getCurrentConfig ().indentionStep () == 0 ;
1138 Class clazz = classInfo .clazz ;
1239 Class compType = clazz .getComponentType ();
1340 if (compType .isArray ()) {
@@ -23,8 +50,13 @@ public static CodegenResult genArray(String cacheKey, ClassInfo classInfo) {
2350 CodegenResult ctx = new CodegenResult ();
2451 ctx .append ("public static void encode_(java.lang.Object obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {" );
2552 ctx .append (String .format ("%s[] arr = (%s[])obj;" , compType .getCanonicalName (), compType .getCanonicalName ()));
26- ctx .append ("if (arr.length == 0) { return; }" );
27- ctx .buffer ('[' );
53+ if (noIndention ) {
54+ ctx .append ("if (arr.length == 0) { return; }" );
55+ ctx .buffer ('[' );
56+ } else {
57+ ctx .append ("if (arr.length == 0) { stream.write((byte)'[', (byte)']'); return; }" );
58+ ctx .append ("stream.writeArrayStart(); stream.writeIndention();" );
59+ }
2860 ctx .append ("int i = 0;" );
2961 ctx .append (String .format ("%s e = arr[i++];" , compType .getCanonicalName ()));
3062 if (isCollectionValueNullable ) {
@@ -35,7 +67,11 @@ public static CodegenResult genArray(String cacheKey, ClassInfo classInfo) {
3567 CodegenImplNative .genWriteOp (ctx , "e" , compType , false );
3668 }
3769 ctx .append ("while (i < arr.length) {" );
38- ctx .append ("stream.write(',');" );
70+ if (noIndention ) {
71+ ctx .append ("stream.write(',');" );
72+ } else {
73+ ctx .append ("stream.writeMore();" );
74+ }
3975 ctx .append ("e = arr[i++];" );
4076 if (isCollectionValueNullable ) {
4177 ctx .append ("if (e == null) { stream.writeNull(); } else {" );
@@ -45,37 +81,17 @@ public static CodegenResult genArray(String cacheKey, ClassInfo classInfo) {
4581 CodegenImplNative .genWriteOp (ctx , "e" , compType , false );
4682 }
4783 ctx .append ("}" ); // while
48- ctx .buffer (']' );
49- ctx .append ("}" ); // public static void encode_
50- return ctx ;
51- }
52-
53- public static CodegenResult genCollection (String cacheKey , ClassInfo classInfo ) {
54- Type [] typeArgs = classInfo .typeArgs ;
55- Class clazz = classInfo .clazz ;
56- Type compType = Object .class ;
57- if (typeArgs .length == 0 ) {
58- // default to List<Object>
59- } else if (typeArgs .length == 1 ) {
60- compType = typeArgs [0 ];
61- } else {
62- throw new IllegalArgumentException (
63- "can not bind to generic collection without argument types, " +
64- "try syntax like TypeLiteral<List<Integer>>{}" );
65- }
66- if (clazz == List .class ) {
67- clazz = ArrayList .class ;
68- } else if (clazz == Set .class ) {
69- clazz = HashSet .class ;
70- }
71- if (List .class .isAssignableFrom (clazz )) {
72- return genList (cacheKey , clazz , compType );
84+ if (noIndention ) {
85+ ctx .buffer (']' );
7386 } else {
74- return genCollection ( cacheKey , clazz , compType );
87+ ctx . append ( "stream.writeArrayEnd();" );
7588 }
89+ ctx .append ("}" ); // public static void encode_
90+ return ctx ;
7691 }
7792
7893 private static CodegenResult genList (String cacheKey , Class clazz , Type compType ) {
94+ boolean noIndention = JsoniterSpi .getCurrentConfig ().indentionStep () == 0 ;
7995 boolean isCollectionValueNullable = true ;
8096 if (cacheKey .endsWith ("__value_not_nullable" )) {
8197 isCollectionValueNullable = false ;
@@ -84,8 +100,13 @@ private static CodegenResult genList(String cacheKey, Class clazz, Type compType
84100 ctx .append ("public static void encode_(java.lang.Object obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {" );
85101 ctx .append ("java.util.List list = (java.util.List)obj;" );
86102 ctx .append ("int size = list.size();" );
87- ctx .append ("if (size == 0) { return; }" );
88- ctx .buffer ('[' );
103+ if (noIndention ) {
104+ ctx .append ("if (size == 0) { return; }" );
105+ ctx .buffer ('[' );
106+ } else {
107+ ctx .append ("if (size == 0) { stream.write((byte)'[', (byte)']'); return; }" );
108+ ctx .append ("stream.writeArrayStart(); stream.writeIndention();" );
109+ }
89110 ctx .append ("java.lang.Object e = list.get(0);" );
90111 if (isCollectionValueNullable ) {
91112 ctx .append ("if (e == null) { stream.writeNull(); } else {" );
@@ -95,7 +116,11 @@ private static CodegenResult genList(String cacheKey, Class clazz, Type compType
95116 CodegenImplNative .genWriteOp (ctx , "e" , compType , false );
96117 }
97118 ctx .append ("for (int i = 1; i < size; i++) {" );
98- ctx .append ("stream.write(',');" );
119+ if (noIndention ) {
120+ ctx .append ("stream.write(',');" );
121+ } else {
122+ ctx .append ("stream.writeMore();" );
123+ }
99124 ctx .append ("e = list.get(i);" );
100125 if (isCollectionValueNullable ) {
101126 ctx .append ("if (e == null) { stream.writeNull(); } else {" );
@@ -105,21 +130,31 @@ private static CodegenResult genList(String cacheKey, Class clazz, Type compType
105130 CodegenImplNative .genWriteOp (ctx , "e" , compType , false );
106131 }
107132 ctx .append ("}" ); // for
108- ctx .buffer (']' );
133+ if (noIndention ) {
134+ ctx .buffer (']' );
135+ } else {
136+ ctx .append ("stream.writeArrayEnd();" );
137+ }
109138 ctx .append ("}" ); // public static void encode_
110139 return ctx ;
111140 }
112141
113142 private static CodegenResult genCollection (String cacheKey , Class clazz , Type compType ) {
143+ boolean noIndention = JsoniterSpi .getCurrentConfig ().indentionStep () == 0 ;
114144 boolean isCollectionValueNullable = true ;
115145 if (cacheKey .endsWith ("__value_not_nullable" )) {
116146 isCollectionValueNullable = false ;
117147 }
118148 CodegenResult ctx = new CodegenResult ();
119149 ctx .append ("public static void encode_(java.lang.Object obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {" );
120150 ctx .append ("java.util.Iterator iter = ((java.util.Collection)obj).iterator();" );
121- ctx .append ("if (!iter.hasNext()) { return; }" );
122- ctx .buffer ('[' );
151+ if (noIndention ) {
152+ ctx .append ("if (!iter.hasNext()) { return; }" );
153+ ctx .buffer ('[' );
154+ } else {
155+ ctx .append ("if (!iter.hasNext()) { stream.write((byte)'[', (byte)']'); return; }" );
156+ ctx .append ("stream.writeArrayStart(); stream.writeIndention();" );
157+ }
123158 ctx .append ("java.lang.Object e = iter.next();" );
124159 if (isCollectionValueNullable ) {
125160 ctx .append ("if (e == null) { stream.writeNull(); } else {" );
@@ -129,7 +164,11 @@ private static CodegenResult genCollection(String cacheKey, Class clazz, Type co
129164 CodegenImplNative .genWriteOp (ctx , "e" , compType , false );
130165 }
131166 ctx .append ("while (iter.hasNext()) {" );
132- ctx .append ("stream.write(',');" );
167+ if (noIndention ) {
168+ ctx .append ("stream.write(',');" );
169+ } else {
170+ ctx .append ("stream.writeMore();" );
171+ }
133172 ctx .append ("e = iter.next();" );
134173 if (isCollectionValueNullable ) {
135174 ctx .append ("if (e == null) { stream.writeNull(); } else {" );
@@ -139,7 +178,11 @@ private static CodegenResult genCollection(String cacheKey, Class clazz, Type co
139178 CodegenImplNative .genWriteOp (ctx , "e" , compType , false );
140179 }
141180 ctx .append ("}" ); // while
142- ctx .buffer (']' );
181+ if (noIndention ) {
182+ ctx .buffer (']' );
183+ } else {
184+ ctx .append ("stream.writeArrayEnd();" );
185+ }
143186 ctx .append ("}" ); // public static void encode_
144187 return ctx ;
145188 }
0 commit comments