[Chicago] Help wanted: Python/JS Reflection library

Ian Bicking ianb at colorstudy.com
Thu Apr 5 01:40:21 CEST 2007


Pete wrote:
> This came up on IRC today...
> 
> I've got some really neat code to use simplejson for general object 
> serialization (a la pickle). I sorta have this pipe dream to use it w/ some 
> JS magic to do full object reflection b/w JS & Python, if that makes any 
> sense at all.
> 
> Basically, the idea is you'd be able to dump some python object to JSON and 
> have JS load up the JSON and create a corresponding object.  And vice versa.
> Unfortunately, I don't have the JS skills to pull it off. If anyone out there 
> with better JS skills than I would like to spend an afternoon hacking on such 
> a beastie, I'd be all for it.

Well, the object models of JSON, Javascript, and Python don't actually 
match up.  Of course you can't actually share objects anyway, since JSON 
is a document and Javascript and Python will each have their own 
distinct copies of the object.  And "class" can't really be shared 
either, since class implies methods, and methods imply code, and there 
is no shared code.  You can use RPC to make a Javascript object that 
actually proxies to a Python object or vice versa (a little harder on 
the Javascript side), but there's a lot of complexities there.  It fails 
in ways normal objects don't; you never get Connection Refused on a 
normal object, for instance.  And Javascript really needs asynchronous 
methods if you are doing network access.  And you have to think about 
whether you want to pass references or concrete data structures over the 
wire.  I think these questions make RPC systems hard to work with.

Messages can work, which is what JSON is typically used for.  Messages 
are just data, not objects, and there's no shared state.  As a common 
message format it's quite useful.  A potentially interesting/useful 
binary version might be bencode (http://en.wikipedia.org/wiki/Bencode), 
though it's data model is slightly different -- but perhaps just because 
bencode has byte strings, where JSON uses unicode strings.  Though I 
find *only* unicode strings a bit awkward, while assuming a UTF8 
convention for bencode seems reasonable.  But that's bit twiddling 
either way, not that much of a concern really.


-- 
Ian Bicking | ianb at colorstudy.com | http://blog.ianbicking.org
             | Write code, do good | http://topp.openplans.org/careers


More information about the Chicago mailing list