这些Colander类型从SchemaType派生并实现实际进行序列化和反序列化的方法.
我能想到的唯一方法是编写自己的SchemaType实现,该实现本质上是一个包装程序,用于测试值并应用Colander中定义的类型之一.
我认为这不会那么难,只是不漂亮.
编辑:这是一个临时示例.我没有测试过,但是它传达了这个想法.
class AnyType(SchemaType):
def serialize(self, node, appstruct):
if appstruct is null:
return null
impl = colander.Mapping() # Or whatever default.
t = type(appstruct)
if t == str:
impl = colander.String()
elif t == int:
impl = colander.Int()
# Test the others, throw if indeterminate etc.
return impl.serialize(node, appstruct)
def deserialize(self, node, cstruct):
if cstruct is null:
return null
# Test and return again.