:P<div><br></div><div>The app I&#39;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&#39;s no explicit documentation for the structure you&#39;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&#39;s difficult to know what data-structure you&#39;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&#39;d get some validation for free (it&#39;s a string and it&#39;s not empty and not null), but you could implement extra validation functions if you needed them (it&#39;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&#39;m not about to go ahead and implement this - there&#39;s no particular drive to do so, and I haven&#39;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">&lt;<a href="mailto:dougal85@gmail.com">dougal85@gmail.com</a>&gt;</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&#39;t say that I know of any. </span></div><div><span><br></span></div><div><span>However, I can&#39;t help but think, what the hell are you doing? :) You</span> want JSON to have a schema. Isn&#39;t this exactly what XML was intended for any why many people hate it?</div>

<div><span><br></span></div><div><span>I&#39;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&#39;m envisioning something a bit like an ORM, although obviously without the &#39;R&#39;...</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({&#39;name&#39;: StringField(),</div><div>            &#39;type&#39;: PetTypeField()})</div><div><br></div><div>json:</div><div>{</div><div>    &#39;name&#39;: &#39;Mark Smith&#39;,</div><div>    &#39;roles&#39;: [&#39;user&#39;, &#39;developer&#39;],</div>



<div>    pet = {&#39;name&#39;: &#39;Fido&#39;, &#39;type&#39;: &#39;dog&#39;}</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>