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
1011 public static CodegenResult genArray (String cacheKey , ClassInfo classInfo ) {
12+ boolean noIndention = JsoniterSpi .getCurrentConfig ().indentionStep () == 0 ;
1113 Class clazz = classInfo .clazz ;
1214 Class compType = clazz .getComponentType ();
1315 if (compType .isArray ()) {
@@ -23,8 +25,16 @@ public static CodegenResult genArray(String cacheKey, ClassInfo classInfo) {
2325 CodegenResult ctx = new CodegenResult ();
2426 ctx .append ("public static void encode_(java.lang.Object obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {" );
2527 ctx .append (String .format ("%s[] arr = (%s[])obj;" , compType .getCanonicalName (), compType .getCanonicalName ()));
26- ctx .append ("if (arr.length == 0) { return; }" );
27- ctx .buffer ('[' );
28+ if (noIndention ) {
29+ ctx .append ("if (arr.length == 0) { return; }" );
30+ } else {
31+ ctx .append ("if (arr.length == 0) { stream.write((byte)'[', (byte)']'); return; }" );
32+ }
33+ if (noIndention ) {
34+ ctx .buffer ('[' );
35+ } else {
36+ ctx .append ("stream.writeArrayStart(); stream.writeIndention();" );
37+ }
2838 ctx .append ("int i = 0;" );
2939 ctx .append (String .format ("%s e = arr[i++];" , compType .getCanonicalName ()));
3040 if (isCollectionValueNullable ) {
@@ -35,7 +45,11 @@ public static CodegenResult genArray(String cacheKey, ClassInfo classInfo) {
3545 CodegenImplNative .genWriteOp (ctx , "e" , compType , false );
3646 }
3747 ctx .append ("while (i < arr.length) {" );
38- ctx .append ("stream.write(',');" );
48+ if (noIndention ) {
49+ ctx .append ("stream.write(',');" );
50+ } else {
51+ ctx .append ("stream.writeMore();" );
52+ }
3953 ctx .append ("e = arr[i++];" );
4054 if (isCollectionValueNullable ) {
4155 ctx .append ("if (e == null) { stream.writeNull(); } else {" );
@@ -45,7 +59,11 @@ public static CodegenResult genArray(String cacheKey, ClassInfo classInfo) {
4559 CodegenImplNative .genWriteOp (ctx , "e" , compType , false );
4660 }
4761 ctx .append ("}" ); // while
48- ctx .buffer (']' );
62+ if (noIndention ) {
63+ ctx .buffer (']' );
64+ } else {
65+ ctx .append ("stream.writeArrayEnd();" );
66+ }
4967 ctx .append ("}" ); // public static void encode_
5068 return ctx ;
5169 }
0 commit comments