forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodegenImplMap.java
More file actions
44 lines (40 loc) · 1.7 KB
/
CodegenImplMap.java
File metadata and controls
44 lines (40 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.jsoniter;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
class CodegenImplMap {
public static String genMap(Class clazz, Type[] typeArgs) {
Type keyType = String.class;
Type valueType = Object.class;
if (typeArgs.length == 0) {
// default to Map<String, Object>
} else if (typeArgs.length == 2) {
keyType = typeArgs[0];
valueType = typeArgs[1];
} else {
throw new IllegalArgumentException(
"can not bind to generic collection without argument types, " +
"try syntax like TypeLiteral<Map<String, String>>{}");
}
if (keyType != String.class) {
throw new IllegalArgumentException("map key must be String");
}
if (clazz == Map.class) {
clazz = HashMap.class;
}
StringBuilder lines = new StringBuilder();
append(lines, "public static Object decode_(com.jsoniter.JsonIterator iter) {");
append(lines, "{{clazz}} map = ({{clazz}})com.jsoniter.CodegenAccess.resetExistingObject(iter);");
append(lines, "if (map == null) { map = new {{clazz}}(); }");
append(lines, "for (String field = iter.readObject(); field != null; field = iter.readObject()) {");
append(lines, "map.put(field, {{op}});");
append(lines, "}");
append(lines, "return map;");
append(lines, "}");
return lines.toString().replace("{{clazz}}", clazz.getName()).replace("{{op}}", CodegenImplNative.genReadOp(valueType));
}
private static void append(StringBuilder lines, String str) {
lines.append(str);
lines.append("\n");
}
}