@@ -19,7 +19,6 @@ public static CodegenResult genMap(String cacheKey, ClassInfo classInfo) {
19
19
keyType = typeArgs [0 ];
20
20
valueType = typeArgs [1 ];
21
21
}
22
- String mapCacheKey = JsoniterSpi .getMapKeyEncoderCacheKey (keyType );
23
22
CodegenResult ctx = new CodegenResult ();
24
23
ctx .append ("public static void encode_(java.lang.Object obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {" );
25
24
ctx .append ("if (obj == null) { stream.writeNull(); return; }" );
@@ -36,16 +35,7 @@ public static CodegenResult genMap(String cacheKey, ClassInfo classInfo) {
36
35
} else {
37
36
ctx .append ("stream.writeObjectStart(); stream.writeIndention();" );
38
37
}
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 );
49
39
if (isCollectionValueNullable ) {
50
40
ctx .append ("if (entry.getValue() == null) { stream.writeNull(); } else {" );
51
41
CodegenImplNative .genWriteOp (ctx , "entry.getValue()" , valueType , true );
@@ -60,11 +50,7 @@ public static CodegenResult genMap(String cacheKey, ClassInfo classInfo) {
60
50
} else {
61
51
ctx .append ("stream.writeMore();" );
62
52
}
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 );
68
54
if (noIndention ) {
69
55
ctx .append ("stream.write(':');" );
70
56
} else {
@@ -86,4 +72,24 @@ public static CodegenResult genMap(String cacheKey, ClassInfo classInfo) {
86
72
ctx .append ("}" );
87
73
return ctx ;
88
74
}
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
+ }
89
95
}
0 commit comments