Skip to content

Serialize and Deserialze Sqlalchemy Nested Objects with JSON

At first we build a relationship :

and we want to dump a county instance to json like this:

extending JSONEncoder:

if you want to decode your json file, simply supply a function for object_hood attribute:

object_hook is an optional function that will be called with the result of any object literal decoded (a dict). The return value of object_hook will be used instead of the dict. This feature can be used to implement custom decoders (e.g. JSON-RPC class hinting).

when a json file loads, we can print the parameter “dict” which pass in to object_hood function, the whole process will loads from the most inner object (a dict) to the outer objects look like this:

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *