:P<div><br></div><div>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.</div>
<div><br></div><div>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 ;-)</div>
<div><br></div><div>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.</div>
<div><br></div><div>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.</div><div>
<br>
</div><div>--Mark</div><div><br><br><div class="gmail_quote">On 10 January 2011 19:13, Dougal Matthews <span dir="ltr"><<a href="mailto:dougal85@gmail.com">dougal85@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div>
<div>
<span>I can't say that I know of any. </span></div><div><span><br></span></div><div><span>However, I can't help but think, what the hell are you doing? :) You</span> want JSON to have a schema. Isn't this exactly what XML was intended for any why many people hate it?</div>
<div><span><br></span></div><div><span>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? </span></div><div><span><br></span></div>
<div><span>Dougal<br>
</span>
<span><br><br><div><span style="font-family:arial;font-size:small">-- <br><b>Dougal Matthews</b><br><a href="http://www.dougalmatthews.com/" target="_blank">www.dougalmatthews.com</a><br><a href="http://www.twitter.com/d0ugal" target="_blank">www.twitter.com/d0ugal</a></span></div>
<br></span><div><div></div><div class="h5">
<p style="color:#a0a0a0">On Monday, 10 January 2011 at 15:59, Mark Smith wrote:</p>
</div></div><blockquote type="cite" style="border-left-style:solid;border-width:1px;margin-left:0px;padding-left:10px">
<span><div><div><div></div><div class="h5"><div>Hi All,<div><br></div><div>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'...</div>
<div><br></div><div>The following is a bit inconsistent, but should illustrate the idea:</div><div><br></div><div>python:</div><div>class MyObject(JSONObject):</div><div> name = StringField()</div><div> roles = ListField(StringField(), min=0, max=10)</div>
<div> pet = ObjectField({'name': StringField(),</div><div> 'type': PetTypeField()})</div><div><br></div><div>json:</div><div>{</div><div> 'name': 'Mark Smith',</div><div> 'roles': ['user', 'developer'],</div>
<div> pet = {'name': 'Fido', 'type': 'dog'}</div><div>}</div><div><br></div><div>To be honest, the validation is more important than the mapping at this stage.</div><div><br></div><div>
--Mark</div>
</div></div></div><div>_______________________________________________<br>Edinburgh mailing list<br><a href="mailto:Edinburgh@python.org" target="_blank">Edinburgh@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/edinburgh" target="_blank">http://mail.python.org/mailman/listinfo/edinburgh</a><br>
</div></div></span>
</blockquote>
<div>
<br>
</div>
</div>
</div><br>_______________________________________________<br>
Edinburgh mailing list<br>
<a href="mailto:Edinburgh@python.org">Edinburgh@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/edinburgh" target="_blank">http://mail.python.org/mailman/listinfo/edinburgh</a><br>
<br></blockquote></div><br></div>