amfast文档翻译——encode and decode(编码与解码)

编码与解码

自定义的类型映射

  • amfast支持用户自定义的对象类型。
  • 使用 amfast.class_def.ClassDef对象,以达到用户自定义对象的序列化与反序列化。
  • 使用ClassDefMapper将一个amfast.class_def.ClassDef与一个客户端的对象映射起来。
from amfast import class_def

# ClassDefMapper objects keep track of ClassDefs
class_mapper = class_def.ClassDefMapper()

# Map a custom class with static attributes
class_mapper.mapClass(class_def.ClassDef(MyCustomClass, 'class.alias', ('tuple', 'of', 'static', 'attribute', 'names')))

# Object attributes can be automatically converted
# to/from a specific type with the
# encode_types and decode_types attributes.
#
# encode_types and decode_types are dictionaries where
# the keys are the names of the attributes to convert,
# and the values are functions that will perform the conversion.
mapped_class = class_def.ClassDef(MyCustomClass, 'class.alias'....
mapped_class.encode_types = {'attribute_to_convert_to_int_before_encoding': int}
mapped_class.decode_types = {'attribute_to_convert_to_int_after_decoding': int}

# Map a custom class with dynamic attributes
class_mapper.mapClass(class_def.DynamicClassDef(MyCustomClass, 'class.alias', ()))

# Map a custom class implementing IExternalizable
# ExternClassDef must be sub-classed, and the
# methods readExternal and writeExternal must be implemented.
class_mapper.mapClass(class_def.ExternClassDef(MyCustomClass,'class.alias'))

# Map a custom class that is also mapped with SQLAlchemy.
# Attributes mapped with SQLAlchemy will automatically be
# added to the list of static attributes.
class_mapper.mapClass(class_def.sa_class_def.SaClassDef(MyCustomClass, 'class.alias')

# Attach ClassDefMapper to Encoder and Decoder
# objects to use the ClassDefMapper for encoding
# and decoding
encoder.class_def_mapper = class_mapper
decoder.class_def_mapper = class_mapper

# Use custom class mappings with a Channel
channel = channel_set.getChannel('channel_name')
channel.endpoint.encoder.class_def_mapper = class_mapper
channel.endpoint.decoder.class_def_mapper = class_mapper
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值