public static void generateFieldSerialize ( BindTypeContext context , PersistType persistType , BindProperty property , Modifier ... modifiers ) { Converter < String , String > format = CaseFormat . LOWER_CAMEL . converterTo ( CaseFormat . UPPER_CAMEL ) ; String methodName = "serialize" + format . convert ( property . getName ( ) ) ; MethodSpec . Builder methodBuilder = MethodSpec . methodBuilder ( methodName ) . addJavadoc ( "for attribute $L serialization\n" , property . getName ( ) ) . addParameter ( ParameterSpec . builder ( typeName ( property . getElement ( ) ) , "value" ) . build ( ) ) . addModifiers ( modifiers ) ; switch ( persistType ) { case STRING : methodBuilder . returns ( className ( String . class ) ) ; break ; case BYTE : methodBuilder . returns ( TypeUtility . arrayTypeName ( Byte . TYPE ) ) ; break ; } // if property type is byte[], return directly the value if ( ArrayTypeName . of ( Byte . TYPE ) . equals ( property . getPropertyType ( ) . getTypeName ( ) ) && persistType == PersistType . BYTE ) { methodBuilder . addStatement ( "return value" ) ; } else { methodBuilder . beginControlFlow ( "if (value==null)" ) ; methodBuilder . addStatement ( "return null" ) ; methodBuilder . endControlFlow ( ) ; methodBuilder . addStatement ( "$T context=$T.jsonBind()" , KriptonJsonContext . class , KriptonBinder . class ) ; methodBuilder . beginControlFlow ( "try ($T stream=new $T(); $T wrapper=context.createSerializer(stream))" , KriptonByteArrayOutputStream . class , KriptonByteArrayOutputStream . class , JacksonWrapperSerializer . class ) ; methodBuilder . addStatement ( "$T jacksonSerializer=wrapper.jacksonGenerator" , JsonGenerator . class ) ; if ( ! property . isBindedObject ( ) ) { methodBuilder . addStatement ( "jacksonSerializer.writeStartObject()" ) ; } methodBuilder . addStatement ( "int fieldCount=0" ) ; BindTransform bindTransform = BindTransformer . lookup ( property ) ; String serializerName = "jacksonSerializer" ; bindTransform . generateSerializeOnJackson ( context , methodBuilder , serializerName , null , "value" , property ) ; if ( ! property . isBindedObject ( ) ) { methodBuilder . addStatement ( "jacksonSerializer.writeEndObject()" ) ; } methodBuilder . addStatement ( "jacksonSerializer.flush()" ) ; switch ( persistType ) { case STRING : methodBuilder . addStatement ( "return stream.toString()" ) ; break ; case BYTE : methodBuilder . addStatement ( "return stream.toByteArray()" ) ; break ; } methodBuilder . nextControlFlow ( "catch($T e)" , Exception . class ) ; methodBuilder . addStatement ( "throw(new $T(e.getMessage()))" , KriptonRuntimeException . class ) ; methodBuilder . endControlFlow ( ) ; } context . builder . addMethod ( methodBuilder . build ( ) ) ; }
generates code to manage field serialization .