Storing collections of objects in various containers is an everyday job. However, currently there is no obvious way to have a field that is just a top-level container by itself.
Let's say we have the following structure:
{
"Hello": {
"name": "World",
"id": 123,
},
"Foo": {
"name": "Bar",
"id": 42,
}
}
In this case one could write the User type as the following:
class User(Model):
name: fields.Str()
id: fields.Int()
but representing the outer dictionary is hard and cumbersome.
Of course it is possible to wrap it in a new type that has a dict field, but that will create a new layer in the output and the user has to dispatch the functions being used (or use the field directly, also not very nice).
The other solution I found is to serialize the User type into a dict and serialize the whole data structure using the json module, but that also feels cumbersome.
I could imagine something like this (but probably now the brightest solution):
MyUsersDict = serde.collections.Dict(serde.fields.Str(), User)
users = MyUsersDict()
users["Hello"] = User(...)
Storing collections of objects in various containers is an everyday job. However, currently there is no obvious way to have a field that is just a top-level container by itself.
Let's say we have the following structure:
In this case one could write the
Usertype as the following:but representing the outer dictionary is hard and cumbersome.
Of course it is possible to wrap it in a new type that has a
dictfield, but that will create a new layer in the output and the user has to dispatch the functions being used (or use the field directly, also not very nice).The other solution I found is to serialize the
Usertype into adictand serialize the whole data structure using thejsonmodule, but that also feels cumbersome.I could imagine something like this (but probably now the brightest solution):