@@ -24,7 +24,8 @@ class CodegenImplObject {
2424
2525 public static String genObjectUsingSlice (Class clazz , String cacheKey , ClassDescriptor desc ) {
2626 // TODO: when setter is single argument, decode like field
27- Map <Integer , Object > trieTree = buildTriTree (desc .allDecoderBindings ());
27+ List <Binding > allBindings = desc .allDecoderBindings ();
28+ Map <Integer , Object > trieTree = buildTriTree (allBindings );
2829 StringBuilder lines = new StringBuilder ();
2930 append (lines , "public static Object decode_(com.jsoniter.JsonIterator iter) {" );
3031 // if null, return null
@@ -51,7 +52,6 @@ public static String genObjectUsingSlice(Class clazz, String cacheKey, ClassDesc
5152 append (lines , "boolean once = true;" );
5253 append (lines , "while (once) {" );
5354 append (lines , "once = false;" );
54- append (lines , "switch (field.len) {" );
5555 String rendered = renderTriTree (cacheKey , trieTree );
5656 for (Binding field : desc .fields ) {
5757 if (desc .ctor .parameters .isEmpty () && desc .fields .contains (field )) {
@@ -63,16 +63,21 @@ public static String genObjectUsingSlice(Class clazz, String cacheKey, ClassDesc
6363 }
6464 }
6565 }
66- append (lines , rendered );
67- append (lines , "}" ); // end of switch
68- append (lines , "iter.skip();" );
66+ if (!desc .ctor .parameters .isEmpty ()) {
67+ append (lines , "switch (field.len) {" );
68+ append (lines , rendered );
69+ append (lines , "}" ); // end of switch
70+ }
71+ appendOnUnknownField (lines , desc );
6972 append (lines , "}" ); // end of while
7073 append (lines , "while (com.jsoniter.CodegenAccess.nextToken(iter) == ',') {" );
7174 append (lines , "field = com.jsoniter.CodegenAccess.readObjectFieldAsSlice(iter);" );
72- append (lines , "switch (field.len) {" );
73- append (lines , rendered );
74- append (lines , "}" ); // end of switch
75- append (lines , "iter.skip();" );
75+ if (!allBindings .isEmpty ()) {
76+ append (lines , "switch (field.len) {" );
77+ append (lines , rendered );
78+ append (lines , "}" ); // end of switch
79+ }
80+ appendOnUnknownField (lines , desc );
7681 append (lines , "}" ); // end of while
7782 if (!desc .ctor .parameters .isEmpty ()) {
7883 append (lines , String .format ("%s obj = {{newInst}};" , CodegenImplNative .getTypeName (clazz )));
@@ -88,6 +93,14 @@ public static String genObjectUsingSlice(Class clazz, String cacheKey, ClassDesc
8893 .replace ("{{newInst}}" , genNewInstCode (clazz , desc .ctor ));
8994 }
9095
96+ private static void appendOnUnknownField (StringBuilder lines , ClassDescriptor desc ) {
97+ if (desc .forbidUnknownFields ) {
98+ append (lines , "throw new com.jsoniter.JsonException('unknown field: ' + field.toString());" .replace ('\'' , '"' ));
99+ } else {
100+ append (lines , "iter.skip();" );
101+ }
102+ }
103+
91104 private static String renderTriTree (String cacheKey , Map <Integer , Object > trieTree ) {
92105 StringBuilder switchBody = new StringBuilder ();
93106 for (Map .Entry <Integer , Object > entry : trieTree .entrySet ()) {
0 commit comments