private static void generateSerializeOnJackson ( BindTypeContext context , BindEntity entity ) { // @formatter:off MethodSpec . Builder methodBuilder = MethodSpec . methodBuilder ( "serializeOnJackson" ) . addAnnotation ( Override . class ) . addModifiers ( Modifier . PUBLIC ) // .addParameter(typeName(AbstractJacksonContext.class), // "context") . addParameter ( typeName ( entity . getElement ( ) ) , "object" ) . addParameter ( typeName ( JsonGenerator . class ) , "jacksonSerializer" ) . returns ( Integer . TYPE ) . addException ( Exception . class ) ; // @formatter:on // methodBuilder.beginControlFlow("try"); // methodBuilder.addStatement("$T jacksonSerializer = // wrapper.jacksonGenerator", className(JsonGenerator.class)); methodBuilder . addStatement ( "jacksonSerializer.writeStartObject()" ) ; methodBuilder . addStatement ( "int fieldCount=0" ) ; BindTransform bindTransform ; methodBuilder . addCode ( "\n" ) ; // fields methodBuilder . addCode ( "// Serialized Field:\n\n" ) ; for ( BindProperty item : entity . getCollection ( ) ) { bindTransform = BindTransformer . lookup ( item ) ; methodBuilder . addCode ( "// field $L (mapped with $S)\n" , item . getName ( ) , item . label ) ; bindTransform . generateSerializeOnJackson ( context , methodBuilder , "jacksonSerializer" , item . getPropertyType ( ) . getTypeName ( ) , "object" , item ) ; methodBuilder . addCode ( "\n" ) ; } methodBuilder . addStatement ( "jacksonSerializer.writeEndObject()" ) ; methodBuilder . addStatement ( "return fieldCount" ) ; // methodBuilder.nextControlFlow("catch($T e)", // typeName(Exception.class)); // methodBuilder.addStatement("e.printStackTrace()"); // methodBuilder.addStatement("throw (new $T(e))", // typeName(KriptonRuntimeException.class)); // methodBuilder.endControlFlow(); context . builder . addMethod ( methodBuilder . build ( ) ) ; }
Generate serialize on jackson .