55
66class CodegenImplArray {
77
8- public static String genArray (Class clazz ) {
8+ public static CodegenResult genArray (Class clazz ) {
99 Class compType = clazz .getComponentType ();
1010 if (compType .isArray ()) {
1111 throw new IllegalArgumentException ("nested array not supported: " + clazz .getCanonicalName ());
1212 }
13- StringBuilder lines = new StringBuilder ();
14- append (lines , "public static void encode_(java.lang.Object obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {" );
15- append (lines , "if (obj == null) { stream.writeNull(); return; }" );
16- append (lines , "{{comp}}[] arr = ({{comp}}[])obj;" );
17- append (lines , "if (arr.length == 0) { stream.writeEmptyArray(); return; }" );
18- append (lines , "stream.writeArrayStart();" );
19- append (lines , "int i = 0;" );
20- append (lines , "{{op}}" );
21- append (lines , "while (i < arr.length) {" );
22- append (lines , "stream.writeMore();" );
23- append (lines , "{{op}}" );
24- append (lines , "}" );
25- append (lines , "stream.writeArrayEnd();" );
26- append (lines , "}" );
27- return lines .toString ()
28- .replace ("{{comp}}" , compType .getCanonicalName ())
29- .replace ("{{op}}" , CodegenImplNative .genWriteOp ("arr[i++]" , compType ));
13+ CodegenResult ctx = new CodegenResult ();
14+ ctx .append ("public static void encode_(java.lang.Object obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {" );
15+ ctx .append ("if (obj == null) { stream.writeNull(); return; }" );
16+ ctx .append (String .format ("%s[] arr = (%s[])obj;" , compType .getCanonicalName (), compType .getCanonicalName ()));
17+ ctx .append ("if (arr.length == 0) { return; }" );
18+ ctx .buffer ('[' );
19+ ctx .append ("int i = 0;" );
20+ CodegenImplNative .genWriteOp (ctx , "arr[i++]" , compType );
21+ ctx .append ("while (i < arr.length) {" );
22+ ctx .buffer (',' );
23+ CodegenImplNative .genWriteOp (ctx , "arr[i++]" , compType );
24+ ctx .append ("}" );
25+ ctx .buffer (']' );
26+ ctx .append ("}" );
27+ return ctx ;
3028 }
3129
32- private static void append (StringBuilder lines , String str ) {
33- lines .append (str );
34- lines .append ("\n " );
35- }
36-
37- public static String genCollection (Class clazz , Type [] typeArgs ) {
30+ public static CodegenResult genCollection (Class clazz , Type [] typeArgs ) {
3831 Type compType = Object .class ;
3932 if (typeArgs .length == 0 ) {
4033 // default to List<Object>
@@ -53,23 +46,21 @@ public static String genCollection(Class clazz, Type[] typeArgs) {
5346 return genCollection (clazz , compType );
5447 }
5548
56- private static String genCollection (Class clazz , Type compType ) {
57- StringBuilder lines = new StringBuilder ();
58- append (lines , "public static void encode_(java.lang.Object obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {" );
59- append (lines , "if (obj == null) { stream.writeNull(); return; }" );
60- append (lines , "java.util.Iterator iter = ((java.util.Collection)obj).iterator();" );
61- append (lines , "if (!iter.hasNext()) { stream.writeEmptyArray(); return; }" );
62- append (lines , "stream.writeArrayStart();" );
63- append (lines , "{{op}}" );
64- append (lines , "while (iter.hasNext()) {" );
65- append (lines , "stream.writeMore();" );
66- append (lines , "{{op}}" );
67- append (lines , "}" );
68- append (lines , "stream.writeArrayEnd();" );
69- append (lines , "}" );
70- return lines .toString ()
71- .replace ("{{comp}}" , CodegenImplNative .getTypeName (compType ))
72- .replace ("{{op}}" , CodegenImplNative .genWriteOp ("iter.next()" , compType ));
49+ private static CodegenResult genCollection (Class clazz , Type compType ) {
50+ CodegenResult ctx = new CodegenResult ();
51+ ctx .append ("public static void encode_(java.lang.Object obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {" );
52+ ctx .append ("if (obj == null) { stream.writeNull(); return; }" );
53+ ctx .append ("java.util.Iterator iter = ((java.util.Collection)obj).iterator();" );
54+ ctx .append ("if (!iter.hasNext()) { return; }" );
55+ ctx .buffer ('[' );
56+ CodegenImplNative .genWriteOp (ctx , "iter.next()" , compType );
57+ ctx .append ("while (iter.hasNext()) {" );
58+ ctx .buffer (',' );
59+ CodegenImplNative .genWriteOp (ctx , "iter.next()" , compType );
60+ ctx .append ("}" );
61+ ctx .buffer (']' );
62+ ctx .append ("}" );
63+ return ctx ;
7364 }
7465
7566}
0 commit comments