private static void generateParseOnJackson ( BindTypeContext context , BindEntity entity ) { // @formatter:off MethodSpec . Builder methodBuilder = MethodSpec . methodBuilder ( "parseOnJackson" ) . addJavadoc ( "parse with jackson\n" ) . addAnnotation ( Override . class ) . addModifiers ( Modifier . PUBLIC ) // .addParameter(typeName(AbstractJacksonContext.class), // "context") . addParameter ( typeName ( JsonParser . class ) , "jacksonParser" ) . returns ( typeName ( entity . getElement ( ) ) ) . addException ( Exception . class ) ; // @formatter:on boolean mutableObject = entity . isMutablePojo ( ) ; if ( mutableObject ) { methodBuilder . addStatement ( "$T instance = new $T()" , entity . getElement ( ) , entity . getElement ( ) ) ; } else { ImmutableUtility . generateImmutableVariableInit ( entity , methodBuilder ) ; } methodBuilder . addStatement ( "String fieldName" ) ; methodBuilder . beginControlFlow ( "if (jacksonParser.currentToken() == null)" ) ; methodBuilder . addStatement ( "jacksonParser.nextToken()" ) ; methodBuilder . endControlFlow ( ) ; methodBuilder . beginControlFlow ( "if (jacksonParser.currentToken() != $T.START_OBJECT)" , JsonToken . class ) ; methodBuilder . addStatement ( "jacksonParser.skipChildren()" ) ; if ( ! mutableObject ) { ImmutableUtility . generateImmutableEntityCreation ( entity , methodBuilder , "instance" , true ) ; } methodBuilder . addStatement ( "return instance" ) ; methodBuilder . endControlFlow ( ) ; methodBuilder . beginControlFlow ( "while (jacksonParser.nextToken() != $T.END_OBJECT)" , JsonToken . class ) ; methodBuilder . addStatement ( "fieldName = jacksonParser.getCurrentName()" ) ; methodBuilder . addStatement ( "jacksonParser.nextToken()" ) ; if ( entity . getCollection ( ) . size ( ) > 0 ) { methodBuilder . addCode ( "\n// Parse fields:\n" ) ; methodBuilder . beginControlFlow ( "switch (fieldName)$>" ) ; BindTransform bindTransform ; for ( BindProperty item : entity . getCollection ( ) ) { bindTransform = BindTransformer . lookup ( item ) ; methodBuilder . addCode ( "case $S:\n$>" , item . label ) ; methodBuilder . addCode ( "// field $L (mapped with $S)\n" , item . getName ( ) , item . label ) ; bindTransform . generateParseOnJackson ( context , methodBuilder , "jacksonParser" , item . getPropertyType ( ) . getTypeName ( ) , "instance" , item ) ; methodBuilder . addCode ( "$<break;\n" ) ; } methodBuilder . addCode ( "default:$>\n" ) ; methodBuilder . addStatement ( "jacksonParser.skipChildren()" ) ; methodBuilder . addCode ( "$<break;" ) ; methodBuilder . addCode ( "$<" ) ; methodBuilder . endControlFlow ( ) ; } else { methodBuilder . addCode ( "\n// No field to parse\n" ) ; } methodBuilder . endControlFlow ( ) ; if ( ! mutableObject ) { ImmutableUtility . generateImmutableEntityCreation ( entity , methodBuilder , "instance" , true ) ; } methodBuilder . addStatement ( "return instance" ) ; context . builder . addMethod ( methodBuilder . build ( ) ) ; }
Generate parse on jackson .