How to describe tree/recursive structures?
Closed this issue · 1 comments
Hello,
First of all I must say that this library is great and we will be using in our product definitely. Really great work.
For now we're using it for more complex cases for user input, but there was one use case when we tried to return directory-like structure. Each Directory can contain Files, but also a list of other Directories. How this could be expressed as models using jsonobject?
Maybe some kind of foward references as strings? (like in python type annotations - https://www.python.org/dev/peps/pep-0484/#forward-references)
If that's relevant, we're still stuck with python2.7
It sounds like your question is about how recursive structures can be supported, if a class's property must reference itself before the class exists. The way jsonobject handles this is by allowing you to pass, in place of a class, in a function with no arguments that returns a class, like so:
class Directory(JsonObject):
files = ListProperty(unicode)
directories = ListProperty(lambda: Directory) # <-- this line
If you want to dig in for more examples of behavior, check out some examples in tests.py
, e.g. https://github.com/dimagi/jsonobject/blob/master/test/tests.py#L49-L51.
Hope this helps!