[Python Edinburgh] JSON mapping library - recommendations?

Mark Smith mark.smith at practicalpoetry.co.uk
Tue Jan 11 10:20:07 CET 2011


The app I'm working on has a couple of cobbled-together approaches for
dealing with JSON data, and neither of them is really very good. We work
with a lot of JSON as the app itself is a single-page AJAX app, and the
data-stores we use are CouchDB and Solr, both of which provide JSON data;
and the documents can be quite complicated and deep.

Approach one is to just deserialise the data and then work directly with the
raw data structure, but then you get no helper methods, no validation, and
there's no explicit documentation for the structure you're dealing with.
Approach two is to implement what I described manually, and either wrap the
JSON data-structure, or to extract the data into an object-model. The first
approach turns out to be very opaque - it's difficult to know what
data-structure you're dealing with; the second approach tends to end up with
long functions responsible for serialisation/deserialisation, and as you
know, when you have a long function with lots of repetitive behaviour then
you should be looking for an opportunity to refactor and abstract that
behaviour ;-)

I was thinking a better approach would be a declarative approach, such as
that used by various ORM frameworks, with optional validation points - so
you'd get some validation for free (it's a string and it's not empty and not
null), but you could implement extra validation functions if you needed them
(it's a string that looks like a phone number). Maybe I put too much
emphasis on validation in my original email.

Anybody got any better ideas? I should point out that I'm not about to go
ahead and implement this - there's no particular drive to do so, and I
haven't given it enough thought.


On 10 January 2011 19:13, Dougal Matthews <dougal85 at gmail.com> wrote:

>  I can't say that I know of any.
> However, I can't help but think, what the hell are you doing? :) You want
> JSON to have a schema. Isn't this exactly what XML was intended for any why
> many people hate it?
> I'd be interested to hear the use case for this. I can only imagine its
> because JSON is coming from somewhere externally? Is this for an API?
> Dougal
> --
> *Dougal Matthews*
> www.dougalmatthews.com
> www.twitter.com/d0ugal
> On Monday, 10 January 2011 at 15:59, Mark Smith wrote:
> Hi All,
> Can anyone recommend a library for parsing/validating/serialising objects
> to and from JSON data structures? I'm envisioning something a bit like an
> ORM, although obviously without the 'R'...
> The following is a bit inconsistent, but should illustrate the idea:
> python:
> class MyObject(JSONObject):
>     name = StringField()
>     roles = ListField(StringField(), min=0, max=10)
>     pet = ObjectField({'name': StringField(),
>             'type': PetTypeField()})
> json:
> {
>     'name': 'Mark Smith',
>     'roles': ['user', 'developer'],
>     pet = {'name': 'Fido', 'type': 'dog'}
> }
> To be honest, the validation is more important than the mapping at this
> stage.
> --Mark
> _______________________________________________
> Edinburgh mailing list
> Edinburgh at python.org
> http://mail.python.org/mailman/listinfo/edinburgh
> _______________________________________________
> Edinburgh mailing list
> Edinburgh at python.org
> http://mail.python.org/mailman/listinfo/edinburgh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edinburgh/attachments/20110111/3ed7e85e/attachment.html>

More information about the Edinburgh mailing list