Python 重命名Colander中的反序列化字段
在基于金字塔/檐口的项目中,我使用Colander将JSON字符串转换为Python对象,反之亦然 是否有任何方法可以序列化/反序列化为不同的名称/键 以下是Colander模式:Python 重命名Colander中的反序列化字段,python,colander,cornice,Python,Colander,Cornice,在基于金字塔/檐口的项目中,我使用Colander将JSON字符串转换为Python对象,反之亦然 是否有任何方法可以序列化/反序列化为不同的名称/键 以下是Colander模式: class CommentSchema(MappingSchema): resource_id = SchemaNode(Int(), name="resourceID", location="body") text = SchemaNode(String(), name="text", locati
class CommentSchema(MappingSchema):
resource_id = SchemaNode(Int(), name="resourceID", location="body")
text = SchemaNode(String(), name="text", location="body")
这里是输入JSON
{"text":"Hello!", "resourceID":12}
它正在转换为:
{u'text': u'Hello!', u'resourceID': 12}
这是我的问题,我可以将相同的输入JSON转换为以下内容吗
{u'full_text': u'Hello!', u'resource_id': 12}
谢谢你的帮助。我最终不得不手动完成。 从JSON接收的任何内容都用于构造数据对象。对象将具有一个自定义函数,用于将数据映射到所需的输出格式,并将输出传递给序列化程序:
data_schema = DataSchema().deserialize(self.request.json)
data_obj = DataObject(data_schema**) // or DataObject(full_text = data_schema['text'], resource_id = data_schema['resourceID'])
#
# ...
#
rbody = DataSchema().serialize(data_obj.map_dump())
return Response(body=rbody, status_code=201)
数据对象将如下所示:
class DataObject(Object):
def __init__(self, text, resourceID): // or __init__(self, full_text, resource_id)
self.text = text
self.resourceID = resourceID
def map_dump(self):
output['full_text'] = self.text
output['resource_id'] = self.resource
return output
也在努力实现同样的目标。有人有主意吗?我刚把我当时想到的东西发出去了。尽管我认为应该有一个更简单/更干净的方法来做。我不确定问题和/或解决方案是否仍然有效,因为我已经有一段时间没有与Colander合作了。