11package com .jsoniter .output ;
22
3- import java .lang .reflect .ParameterizedType ;
43import java .lang .reflect .Type ;
54import java .util .*;
65
@@ -18,12 +17,14 @@ public static String genArray(Class clazz) {
1817 append (lines , "if (arr.length == 0) { stream.writeEmptyArray(); return; }" );
1918 append (lines , "stream.startArray();" );
2019 append (lines , "for (int i = 0; i < arr.length; i++) {" );
21- append (lines , "stream.writeVal(({{comp}})arr[i]); " );
20+ append (lines , "{{op}} " );
2221 append (lines , "stream.writeMore();" );
2322 append (lines , "}" );
2423 append (lines , "stream.endArray();" );
2524 append (lines , "}" );
26- return lines .toString ().replace ("{{comp}}" , compType .getCanonicalName ());
25+ return lines .toString ()
26+ .replace ("{{comp}}" , compType .getCanonicalName ())
27+ .replace ("{{op}}" , CodegenImplNative .genWriteOp ("arr[i]" , compType ));
2728 }
2829
2930 private static void append (StringBuilder lines , String str ) {
@@ -57,27 +58,17 @@ private static String genCollection(Class clazz, Type compType) {
5758 append (lines , "java.util.Iterator iter = ((java.util.Collection)obj).iterator();" );
5859 append (lines , "if (!iter.hasNext()) { stream.writeEmptyArray(); return; }" );
5960 append (lines , "stream.startArray();" );
60- append (lines , "stream.writeVal(({{comp}})iter.next()); " );
61+ append (lines , "{{op}} " );
6162 append (lines , "stream.writeMore();" );
6263 append (lines , "while (iter.hasNext()) {" );
63- append (lines , "stream.writeVal(({{comp}})iter.next()); " );
64+ append (lines , "{{op}} " );
6465 append (lines , "stream.writeMore();" );
6566 append (lines , "}" );
6667 append (lines , "stream.endArray();" );
6768 append (lines , "}" );
68- return lines .toString ().replace ("{{comp}}" , getTypeName (compType ));
69+ return lines .toString ()
70+ .replace ("{{comp}}" , CodegenImplNative .getTypeName (compType ))
71+ .replace ("{{op}}" , CodegenImplNative .genWriteOp ("iter.next()" , compType ));
6972 }
7073
71- private static String getTypeName (Type fieldType ) {
72- if (fieldType instanceof Class ) {
73- Class clazz = (Class ) fieldType ;
74- return clazz .getCanonicalName ();
75- } else if (fieldType instanceof ParameterizedType ) {
76- ParameterizedType pType = (ParameterizedType ) fieldType ;
77- Class clazz = (Class ) pType .getRawType ();
78- return clazz .getCanonicalName ();
79- } else {
80- throw new RuntimeException ("unsupported type: " + fieldType );
81- }
82- }
8374}
0 commit comments