@@ -19,7 +19,6 @@ public static CodegenResult genMap(String cacheKey, ClassInfo classInfo) {
1919 keyType = typeArgs [0 ];
2020 valueType = typeArgs [1 ];
2121 }
22- String mapCacheKey = JsoniterSpi .getMapKeyEncoderCacheKey (keyType );
2322 CodegenResult ctx = new CodegenResult ();
2423 ctx .append ("public static void encode_(java.lang.Object obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {" );
2524 ctx .append ("if (obj == null) { stream.writeNull(); return; }" );
@@ -36,16 +35,7 @@ public static CodegenResult genMap(String cacheKey, ClassInfo classInfo) {
3635 } else {
3736 ctx .append ("stream.writeObjectStart(); stream.writeIndention();" );
3837 }
39- if (keyType == String .class ) {
40- ctx .append ("stream.writeVal((java.lang.String)entry.getKey());" );
41- } else {
42- ctx .append (String .format ("com.jsoniter.output.CodegenAccess.writeMapKey(\" %s\" , entry.getKey(), stream);" , mapCacheKey ));
43- }
44- if (noIndention ) {
45- ctx .append ("stream.write(':');" );
46- } else {
47- ctx .append ("stream.write((byte)':', (byte)' ');" );
48- }
38+ genWriteMapKey (ctx , keyType , noIndention );
4939 if (isCollectionValueNullable ) {
5040 ctx .append ("if (entry.getValue() == null) { stream.writeNull(); } else {" );
5141 CodegenImplNative .genWriteOp (ctx , "entry.getValue()" , valueType , true );
@@ -60,11 +50,7 @@ public static CodegenResult genMap(String cacheKey, ClassInfo classInfo) {
6050 } else {
6151 ctx .append ("stream.writeMore();" );
6252 }
63- if (keyType == String .class ) {
64- ctx .append ("stream.writeVal((java.lang.String)entry.getKey());" );
65- } else {
66- ctx .append (String .format ("com.jsoniter.output.CodegenAccess.writeMapKey(\" %s\" , entry.getKey(), stream);" , mapCacheKey ));
67- }
53+ genWriteMapKey (ctx , keyType , noIndention );
6854 if (noIndention ) {
6955 ctx .append ("stream.write(':');" );
7056 } else {
@@ -86,4 +72,24 @@ public static CodegenResult genMap(String cacheKey, ClassInfo classInfo) {
8672 ctx .append ("}" );
8773 return ctx ;
8874 }
75+
76+ private static void genWriteMapKey (CodegenResult ctx , Type keyType , boolean noIndention ) {
77+ if (keyType == Object .class ) {
78+ ctx .append ("stream.writeObjectField(entry.getKey());" );
79+ return ;
80+ }
81+ if (keyType == String .class ) {
82+ ctx .append ("stream.writeVal((java.lang.String)entry.getKey());" );
83+ } else if (CodegenImplNative .NATIVE_ENCODERS .containsKey (keyType )) {
84+ ctx .append (String .format ("stream.writeVal((%s)entry.getKey());" , CodegenImplNative .getTypeName (keyType )));
85+ } else {
86+ String mapCacheKey = JsoniterSpi .getMapKeyEncoderCacheKey (keyType );
87+ ctx .append (String .format ("com.jsoniter.output.CodegenAccess.writeMapKey(\" %s\" , entry.getKey(), stream);" , mapCacheKey ));
88+ }
89+ if (noIndention ) {
90+ ctx .append ("stream.write(':');" );
91+ } else {
92+ ctx .append ("stream.write((byte)':', (byte)' ');" );
93+ }
94+ }
8995}
0 commit comments