The geekery continues!
May. 20th, 2009 08:47 amSo, I've put up my latest geek piece: Fixing an omission from Django's simplejson: iterators, generators, functors and closures, which is exactly what it says it is. Django's "simplejson", which bundles up server-side structures and exports them to the browser for interpretation and display in fat client applications, is okay, but it fails in some common ways.
For example, if I want to render a tree of data stored in a database as a JSON object, first on the server side I have to devolve the database into a massive dictionaries-of-dictionaries or lists-of-lists, and then pass the product to simplejson. The process of devolution usually involves recursing down the tree structure, and the process of rendering involves recursing down the dictionaries-of-dictionaries structure.
Why not write a class or closure that describes the process, and pass that to the JSON renderer, which will build the JSON object with a single recursive pass? A completely great idea as it eliminates this obscure, error-prone, and wasteful interim dictionaries-of-dictionaries, and it describes the rendering process in clear, tight code.
Except, the JSON handler in Django has no idea how to handle a class or closure designed to do that. It does not understand the next recursive or iterative step when presented with one of those as it recurses. My post addresses this issue.
I confess that this was a sidelight on yesterday's research: figuring out how to create a list-of-lists data object in Dojo. That stymied me, and this is a better implementation. Still, I regret not being able to publish a class called "LOLTrees."
For example, if I want to render a tree of data stored in a database as a JSON object, first on the server side I have to devolve the database into a massive dictionaries-of-dictionaries or lists-of-lists, and then pass the product to simplejson. The process of devolution usually involves recursing down the tree structure, and the process of rendering involves recursing down the dictionaries-of-dictionaries structure.
Why not write a class or closure that describes the process, and pass that to the JSON renderer, which will build the JSON object with a single recursive pass? A completely great idea as it eliminates this obscure, error-prone, and wasteful interim dictionaries-of-dictionaries, and it describes the rendering process in clear, tight code.
Except, the JSON handler in Django has no idea how to handle a class or closure designed to do that. It does not understand the next recursive or iterative step when presented with one of those as it recurses. My post addresses this issue.
I confess that this was a sidelight on yesterday's research: figuring out how to create a list-of-lists data object in Dojo. That stymied me, and this is a better implementation. Still, I regret not being able to publish a class called "LOLTrees."