def _generate_union_serializer(self, union):
union_name = fmt_class_prefix(union)<EOL>with self.block_func(<EOL>func='<STR_LIT>',<EOL>args=fmt_func_args_declaration([('<STR_LIT>',<EOL>'<STR_LIT>'.format(union_name))]),<EOL>return_type='<STR_LIT>',<EOL>class_func=True):<EOL><INDENT>if not union.all_fields:<EOL><INDENT>self.emit('<STR_LIT>')<EOL><DEDENT>self.emit(<EOL>'<STR_LIT>'<EOL>)<EOL>self.emit()<EOL>first_block = True<EOL>for field in union.all_fields:<EOL><INDENT>with self.block('<STR_LIT>'.format(<EOL>'<STR_LIT>' if first_block else '<STR_LIT>',<EOL>fmt_camel_upper(field.name))):<EOL><INDENT>data_type, nullable = unwrap_nullable(field.data_type)<EOL>input_value = '<STR_LIT>'.format(fmt_var(field.name))<EOL>serialize_call = self._fmt_serialization_call(<EOL>field.data_type, input_value, True)<EOL>def emit_serializer():<EOL><INDENT>if is_user_defined_type(data_type):<EOL><INDENT>if is_struct_type(data_type) andnot data_type.has_enumerated_subtypes():<EOL><INDENT>self.emit('<STR_LIT>'.<EOL>format(serialize_call))<EOL><DEDENT>else:<EOL><INDENT>self.emit(<EOL>'<STR_LIT>'.<EOL>format(field.name, serialize_call))<EOL><DEDENT><DEDENT>elif is_primitive_type(data_type):<EOL><INDENT>self.emit('<STR_LIT>'.format(<EOL>field.name, input_value))<EOL><DEDENT>else:<EOL><INDENT>self.emit('<STR_LIT>'.format(<EOL>field.name, serialize_call))<EOL><DEDENT><DEDENT>if not is_void_type(data_type):<EOL><INDENT>if not nullable:<EOL><INDENT>emit_serializer()<EOL><DEDENT>else:<EOL><INDENT>with self.block('<STR_LIT>'.format(<EOL>fmt_var(field.name))):<EOL><INDENT>emit_serializer()<EOL><DEDENT><DEDENT><DEDENT>self.emit('<STR_LIT>'.format(field.name))<EOL><DEDENT>if first_block:<EOL><INDENT>first_block = False<EOL><DEDENT><DEDENT>with self.block('<STR_LIT>'):<EOL><INDENT>if not union.closed:<EOL><INDENT>self.emit('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>self._generate_throw_error(<EOL>'<STR_LIT>',<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT><DEDENT>self.emit()<EOL>self.emit('<STR_LIT>')<EOL><DEDENT>self.emit()<EOL>
Emits the serialize method for the serialization object for the given union.