使用棉花糖自动嵌套嵌套模式的字典键解析

cdi*_*ins 5 python schema serialization nested marshmallow

我有一个棉花糖架构,其中对象使用键来引用结构另一部分中的字典中定义的对象。我想在反序列化对象时自动解析键。如何以惯用的方式在棉花糖中实现此效果?

现在的解决方法是手动解析所有引用,但这似乎很笨拙,因为棉花糖的声明性性质应该能够为我们自动完成。

请注意,当将列声明为“关系 ” 时,Marshmallow-SQLAlchemy支持这种(反序列化),这对我们自动完成,但是我想对JSON数据进行此处理。

这是我要实现的示例fields.Relationship,但尚不存在:

class UserSchema(Schema):
    name = fields.String()
    email = fields.Email()
    friends = fields.Relationship('self', path="AddressBook.contacts", many=True)

class AddressBookSchema(Schema):
    contacts = nested.Dict(keys=fields.String(), values=fields.Nested(UserSchema))

# ... create ``user`` ...
serialized_data = AddressBookSchema().dump(user)
pprint(serialized_data)
# "contacts": {
#   "Steve": {
#     "name": "Steve",
#     "email": "steve@example.com",
#     "friends": ["Mike"]
#   },
#   "Mike": {
#     "name": "Mike",
#     "email": "mike@example.com",
#     "friends": []
# }


deserialized_data = UserSchema().load(result)
pprint(deserialized_data)
# "contacts": {
#   "Steve": {
#     "name": "Steve",
#     "email": "steve@example.com",
#     "friends": [ {"name": "Mike", "email": "mike@example.com"]
#   },
#   "Mike": {
#     "name": "Mike",
#     "email": "mike@example.com",
#     "friends": []
# }
Run Code Online (Sandbox Code Playgroud)

我还在棉花糖Github存储库中提出了一个问题

geo*_*xsh 7

为什么不简单地使用post_load钩子传输中间数据:

class UserSchema(Schema):
    name = fields.String()
    email = fields.Email()
    friends = fields.List(fields.String())

class AddressBookSchema(Schema):
    contacts = fields.Dict(keys=fields.String(), values=fields.Nested(UserSchema))

    @post_load
    def trans_friends(self, item):
        for name in item['contacts']:
            item['contacts'][name]['friends'] = [item['contacts'][n] for n in item['contacts'][name]['friends']]


data = """
{
 "contacts": {
  "Steve": {
    "name": "Steve",
    "email": "steve@example.com",
    "friends": ["Mike"]
  },
  "Mike": {
    "name": "Mike",
    "email": "mike@example.com",
    "friends": []
  }
 }
}
"""

deserialized_data = AddressBookSchema().loads(data)
pprint(deserialized_data)
Run Code Online (Sandbox Code Playgroud)

产量:

UnmarshalResult(data={'contacts': {'Steve': {'name': 'Steve', 'email': 'steve@example.com', 'friends': [{'name': 'Mike', 'email': 'mike@example.com', 'friends': []}]}, 'Mike': {'name': 'Mike', 'email': 'mike@example.com', 'friends': []}}}, errors={})
Run Code Online (Sandbox Code Playgroud)