@@ -38,9 +38,9 @@ public static String genObjectUsingSlice(Class clazz, String cacheKey, ClassDesc
3838 Map <Integer , Object > trieTree = buildTriTree (allBindings );
3939 StringBuilder lines = new StringBuilder ();
4040 append (lines , "public static Object decode_(com.jsoniter.JsonIterator iter) {" );
41- // if null, return null
41+ // === if null, return null
4242 append (lines , "if (iter.readNull()) { com.jsoniter.CodegenAccess.resetExistingObject(iter); return null; }" );
43- // if input is empty object, return empty object
43+ // === if input is empty object, return empty object
4444 append (lines , "long tracker = 0;" );
4545 if (desc .ctor .parameters .isEmpty ()) {
4646 append (lines , "{{clazz}} obj = {{newInst}};" );
@@ -71,6 +71,7 @@ public static String genObjectUsingSlice(Class clazz, String cacheKey, ClassDesc
7171 appendVarDef (lines , param );
7272 }
7373 }
74+ // === bind first field
7475 append (lines , "com.jsoniter.Slice field = com.jsoniter.CodegenAccess.readObjectFieldAsSlice(iter);" );
7576 append (lines , "boolean once = true;" );
7677 append (lines , "while (once) {" );
@@ -93,6 +94,7 @@ public static String genObjectUsingSlice(Class clazz, String cacheKey, ClassDesc
9394 }
9495 appendOnUnknownField (lines , desc );
9596 append (lines , "}" ); // end of while
97+ // === bind all fields
9698 append (lines , "while (com.jsoniter.CodegenAccess.nextToken(iter) == ',') {" );
9799 append (lines , "field = com.jsoniter.CodegenAccess.readObjectFieldAsSlice(iter);" );
98100 if (!allBindings .isEmpty ()) {
@@ -181,12 +183,18 @@ private static void addFieldDispatch(
181183 append (lines , String .format ("field.at(%d)==%s" , i , b ));
182184 append (lines , ") {" );
183185 Binding field = (Binding ) entry .getValue ();
184- append (lines , String .format ("_%s_ = %s;" , field .name , genField (field , cacheKey )));
185- if (field .failOnMissing ) {
186- long mask = 1L << field .idx ;
187- append (lines , "tracker = tracker | " + mask + "L;" );
186+ if (field .failOnPresent ) {
187+ append (lines , String .format (
188+ "throw new com.jsoniter.JsonException('found should not present field: %s');" .replace ('\'' , '"' ),
189+ field .name ));
190+ } else {
191+ append (lines , String .format ("_%s_ = %s;" , field .name , genField (field , cacheKey )));
192+ if (field .failOnMissing ) {
193+ long mask = 1L << field .idx ;
194+ append (lines , "tracker = tracker | " + mask + "L;" );
195+ }
196+ append (lines , "continue;" );
188197 }
189- append (lines , "continue;" );
190198 append (lines , "}" );
191199 continue ;
192200 }
0 commit comments