<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Finally, a JSON validator</title>
	<atom:link href="http://blog.stringtree.org/2007/02/02/finally-a-json-validator/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.stringtree.org/2007/02/02/finally-a-json-validator/</link>
	<description>Stringtree Development News</description>
	<pubDate>Wed, 19 Nov 2008 21:57:27 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
		<item>
		<title>By: Stringtree</title>
		<link>http://blog.stringtree.org/2007/02/02/finally-a-json-validator/#comment-3891</link>
		<dc:creator>Stringtree</dc:creator>
		<pubDate>Fri, 29 Jun 2007 22:02:23 +0000</pubDate>
		<guid isPermaLink="false">http://blog.stringtree.org/?p=6#comment-3891</guid>
		<description>I'm puzzled by this. I didn't think an empty comma was valid JSON.

Checking &lt;a href='http://www.json.org/' rel="nofollow"&gt;json.org&lt;/a&gt; (always the definitive place to look) I see:

&lt;pre&gt;array
    []
    [ elements ]
elements
    value
    value , elements
value
    string
    number
    object
    array
    true
    false
    null
&lt;/pre&gt;

i.e. an array may have a single value or a value followed by a comma followed by more values separated by commas.

So an array starting or ending with a comma is not valid, and my JSONReader makes no claims about what it might produce for invalid JSON. Did you try running it through my JSONValidator to see what that says about it?

Is this being generated by software which you can control or change to ensure it produces valid JSON? maybe there should be a null before the comma in this case?

If you don't have control over the source, then something like

  s = s.replace("[,", "[null,"));

or

  s = s.replace("[,", "["));

should produce valid JSON for processing.

Let me know if you need any more help, or if I have missed your point.</description>
		<content:encoded><![CDATA[<p>I&#8217;m puzzled by this. I didn&#8217;t think an empty comma was valid JSON.</p>
<p>Checking <a href='http://www.json.org/' rel="nofollow">json.org</a> (always the definitive place to look) I see:</p>
<pre>array
    []
    [ elements ]
elements
    value
    value , elements
value
    string
    number
    object
    array
    true
    false
    null
</pre>
<p>i.e. an array may have a single value or a value followed by a comma followed by more values separated by commas.</p>
<p>So an array starting or ending with a comma is not valid, and my JSONReader makes no claims about what it might produce for invalid JSON. Did you try running it through my JSONValidator to see what that says about it?</p>
<p>Is this being generated by software which you can control or change to ensure it produces valid JSON? maybe there should be a null before the comma in this case?</p>
<p>If you don&#8217;t have control over the source, then something like</p>
<p>  s = s.replace(&#8221;[,&#8221;, &#8220;[null,&#8221;));</p>
<p>or</p>
<p>  s = s.replace(&#8221;[,&#8221;, &#8220;[&#8221;));</p>
<p>should produce valid JSON for processing.</p>
<p>Let me know if you need any more help, or if I have missed your point.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mendy</title>
		<link>http://blog.stringtree.org/2007/02/02/finally-a-json-validator/#comment-3832</link>
		<dc:creator>Mendy</dc:creator>
		<pubDate>Tue, 26 Jun 2007 18:59:21 +0000</pubDate>
		<guid isPermaLink="false">http://blog.stringtree.org/?p=6#comment-3832</guid>
		<description>Great program! I Use the stand alone JSONReader class

One problem I noticed the below valid json gives back bad results.

&lt;code&gt;
{"people":[,{"birthdate": 198940304037,"class": "flexjson.test.mock.Person"}]}
&lt;/code&gt;

I tried but couldn't fix it, basically the empty comma is given an empty object literal in its place.

&lt;code&gt;
{people=[java.lang.Object@82ba41, java.lang.Object@82ba41]}
&lt;/code&gt;

I wonder if the an empty comma in other places would have similar affects.

regards</description>
		<content:encoded><![CDATA[<p>Great program! I Use the stand alone JSONReader class</p>
<p>One problem I noticed the below valid json gives back bad results.</p>
<p><code><br />
{"people":[,{"birthdate": 198940304037,"class": "flexjson.test.mock.Person"}]}<br />
</code></p>
<p>I tried but couldn&#8217;t fix it, basically the empty comma is given an empty object literal in its place.</p>
<p><code><br />
{people=[java.lang.Object@82ba41, java.lang.Object@82ba41]}<br />
</code></p>
<p>I wonder if the an empty comma in other places would have similar affects.</p>
<p>regards</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bryan</title>
		<link>http://blog.stringtree.org/2007/02/02/finally-a-json-validator/#comment-2036</link>
		<dc:creator>Bryan</dc:creator>
		<pubDate>Fri, 06 Apr 2007 05:14:33 +0000</pubDate>
		<guid isPermaLink="false">http://blog.stringtree.org/?p=6#comment-2036</guid>
		<description>You can use java.util.IdentityHashMap.

You should also make sure that the same reference can appear more than once, just as long as its not reachable going up or down the object graph.</description>
		<content:encoded><![CDATA[<p>You can use java.util.IdentityHashMap.</p>
<p>You should also make sure that the same reference can appear more than once, just as long as its not reachable going up or down the object graph.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stringtree</title>
		<link>http://blog.stringtree.org/2007/02/02/finally-a-json-validator/#comment-1800</link>
		<dc:creator>Stringtree</dc:creator>
		<pubDate>Fri, 23 Mar 2007 16:52:10 +0000</pubDate>
		<guid isPermaLink="false">http://blog.stringtree.org/?p=6#comment-1800</guid>
		<description>OK, I've updated the code to fix the problem Chris reported: JSONWriter and JSONWriterMapTest updated.

The bit that took the longest was that the "contains" method from the collections API which I was using overflowed if asked about a Map with cyclic references, so I had to implement my own scan through the collection of called objects comparing with '=='.

Does anyone know of a standard API which allows a "contains" check using '==' rather than 'equals' ?

Thanks again, Chris.</description>
		<content:encoded><![CDATA[<p>OK, I&#8217;ve updated the code to fix the problem Chris reported: JSONWriter and JSONWriterMapTest updated.</p>
<p>The bit that took the longest was that the &#8220;contains&#8221; method from the collections API which I was using overflowed if asked about a Map with cyclic references, so I had to implement my own scan through the collection of called objects comparing with &#8216;==&#8217;.</p>
<p>Does anyone know of a standard API which allows a &#8220;contains&#8221; check using &#8216;==&#8217; rather than &#8216;equals&#8217; ?</p>
<p>Thanks again, Chris.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stringtree</title>
		<link>http://blog.stringtree.org/2007/02/02/finally-a-json-validator/#comment-1799</link>
		<dc:creator>Stringtree</dc:creator>
		<pubDate>Fri, 23 Mar 2007 16:09:28 +0000</pubDate>
		<guid isPermaLink="false">http://blog.stringtree.org/?p=6#comment-1799</guid>
		<description>Thanks for the comment, Chris. 

I'l take a look at that failure case and see if I can generalize the cycle-checking a bit.</description>
		<content:encoded><![CDATA[<p>Thanks for the comment, Chris. </p>
<p>I&#8217;l take a look at that failure case and see if I can generalize the cycle-checking a bit.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://blog.stringtree.org/2007/02/02/finally-a-json-validator/#comment-1796</link>
		<dc:creator>Chris</dc:creator>
		<pubDate>Fri, 23 Mar 2007 14:11:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.stringtree.org/?p=6#comment-1796</guid>
		<description>Compliments on a thorough, yet simple implementation.  We embarked recently on an internal implementation as well, and I was amazed at how similar the two implementations were when I came across yours.

One thing I found while testing our implementation was the cyclic StackOverFlow errors you encountered.  We employed a similar method of keeping track of objects we've visited to catch the cyclic references.   One of our test cases was a Collection containing a cyclic reference to another Collection (Maps work also).  I tried this test case on your implemenation and found it still induced a StackOverFlow error.  We check for cycles at a higher level, before passing the object to the "value" methods and therefore can catch the Collection/Map cyclic references.

Just thought I'd pass along those thoughts.</description>
		<content:encoded><![CDATA[<p>Compliments on a thorough, yet simple implementation.  We embarked recently on an internal implementation as well, and I was amazed at how similar the two implementations were when I came across yours.</p>
<p>One thing I found while testing our implementation was the cyclic StackOverFlow errors you encountered.  We employed a similar method of keeping track of objects we&#8217;ve visited to catch the cyclic references.   One of our test cases was a Collection containing a cyclic reference to another Collection (Maps work also).  I tried this test case on your implemenation and found it still induced a StackOverFlow error.  We check for cycles at a higher level, before passing the object to the &#8220;value&#8221; methods and therefore can catch the Collection/Map cyclic references.</p>
<p>Just thought I&#8217;d pass along those thoughts.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stringtree</title>
		<link>http://blog.stringtree.org/2007/02/02/finally-a-json-validator/#comment-1268</link>
		<dc:creator>Stringtree</dc:creator>
		<pubDate>Thu, 01 Mar 2007 23:38:23 +0000</pubDate>
		<guid isPermaLink="false">http://blog.stringtree.org/?p=6#comment-1268</guid>
		<description>OK Folks. I've had a look at the JSONWriter code and added the following fixes based on suggestions from Clint Pells:

1. Track object context to prevent stack overflow on cyclic references such as enums. If/when a previously processed bean is encountered, just emit null.

2. Changed field access to prevent exceptions on private fields. The suggested use if isAccessible did not actually solve the problem, as it also prevented access to public fields :( However, changing &lt;b&gt;getDeclaredFields&lt;/b&gt; to just &lt;b&gt;getFields&lt;/b&gt; seems to have solved it.

3. Only emit commas in beans when really necessary. This was based on Clint's code, but tweaked a little.

4. Made emitting the class name in a bean map optional. I noticed that Clint had hard-coded not to emit the classname as a map entry. After thinking about this I could come up with scenarios where it might be desired or not. So it is now switchable with a constructor parameter to both JSONWriter and JSONValidatingWriter.

Subversion is now updated, the links at the top should go to the new versions.

Thanks again for taking the time to try out and comment on my software!</description>
		<content:encoded><![CDATA[<p>OK Folks. I&#8217;ve had a look at the JSONWriter code and added the following fixes based on suggestions from Clint Pells:</p>
<p>1. Track object context to prevent stack overflow on cyclic references such as enums. If/when a previously processed bean is encountered, just emit null.</p>
<p>2. Changed field access to prevent exceptions on private fields. The suggested use if isAccessible did not actually solve the problem, as it also prevented access to public fields <img src='http://blog.stringtree.org/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> However, changing <b>getDeclaredFields</b> to just <b>getFields</b> seems to have solved it.</p>
<p>3. Only emit commas in beans when really necessary. This was based on Clint&#8217;s code, but tweaked a little.</p>
<p>4. Made emitting the class name in a bean map optional. I noticed that Clint had hard-coded not to emit the classname as a map entry. After thinking about this I could come up with scenarios where it might be desired or not. So it is now switchable with a constructor parameter to both JSONWriter and JSONValidatingWriter.</p>
<p>Subversion is now updated, the links at the top should go to the new versions.</p>
<p>Thanks again for taking the time to try out and comment on my software!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stringtree</title>
		<link>http://blog.stringtree.org/2007/02/02/finally-a-json-validator/#comment-1265</link>
		<dc:creator>Stringtree</dc:creator>
		<pubDate>Thu, 01 Mar 2007 21:49:39 +0000</pubDate>
		<guid isPermaLink="false">http://blog.stringtree.org/?p=6#comment-1265</guid>
		<description>Thanks for the suggestions, and sorry about the problems with html escaping in your comment :(

I definately see the value of the addition to the bean method, and I'll add that. 

As for the enum one, I'm not so sure. It appears that the reason enums give a stack overflow is that each enum value exposes itself (and all its fellows) as a public property. When JSONWriter tries to expand it, follows the tree indefinately.

Even if we apply a quick fix to catch enums, there's nothing to stop any other class from doing the same.

I'll think about this, but I reckon we might well need to track visited objects and stop if we encounter one we've written.

Hmm..</description>
		<content:encoded><![CDATA[<p>Thanks for the suggestions, and sorry about the problems with html escaping in your comment <img src='http://blog.stringtree.org/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>I definately see the value of the addition to the bean method, and I&#8217;ll add that. </p>
<p>As for the enum one, I&#8217;m not so sure. It appears that the reason enums give a stack overflow is that each enum value exposes itself (and all its fellows) as a public property. When JSONWriter tries to expand it, follows the tree indefinately.</p>
<p>Even if we apply a quick fix to catch enums, there&#8217;s nothing to stop any other class from doing the same.</p>
<p>I&#8217;ll think about this, but I reckon we might well need to track visited objects and stop if we encounter one we&#8217;ve written.</p>
<p>Hmm..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Clint Pells</title>
		<link>http://blog.stringtree.org/2007/02/02/finally-a-json-validator/#comment-1260</link>
		<dc:creator>Clint Pells</dc:creator>
		<pubDate>Thu, 01 Mar 2007 17:26:22 +0000</pubDate>
		<guid isPermaLink="false">http://blog.stringtree.org/?p=6#comment-1260</guid>
		<description>Actually!:
&lt;code&gt;
private void bean(Object object) {
    add("{");
    BeanInfo info;
    boolean addedSomething = false;
    try {
        info = Introspector.getBeanInfo(object.getClass());
        PropertyDescriptor[] props = info.getPropertyDescriptors();
        for (int i = 0; i &#60; props.length; ++i) {
          PropertyDescriptor prop = props[i];
          String name = prop.getName();
          Method accessor = prop.getReadMethod();
          if ((!"class".equals(name) ) &#38;&#38; accessor != null) {
            if ( addedSomething ) { add(','); } else { addedSomething = true; }
              Object value = accessor.invoke(object, (Object[])null);
              add(name, value);
              
          }

        }
        Field[] ff = object.getClass().getDeclaredFields();
        for (int i = 0; i &#60; ff.length; ++i) {
            Field field = ff[i];
            if ( field.isAccessible() ) {
              if ( addedSomething ) { add(','); } else { addedSomething = true; }
              add(field.getName(), field.get(object));
            }
        }

    } catch (Exception e) { 
        e.printStackTrace(); 
    }
    add("}");
}
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Actually!:<br />
<code><br />
private void bean(Object object) {<br />
    add("{");<br />
    BeanInfo info;<br />
    boolean addedSomething = false;<br />
    try {<br />
        info = Introspector.getBeanInfo(object.getClass());<br />
        PropertyDescriptor[] props = info.getPropertyDescriptors();<br />
        for (int i = 0; i &lt; props.length; ++i) {<br />
          PropertyDescriptor prop = props[i];<br />
          String name = prop.getName();<br />
          Method accessor = prop.getReadMethod();<br />
          if ((!&#8221;class&#8221;.equals(name) ) &amp;&amp; accessor != null) {<br />
            if ( addedSomething ) { add(&#8217;,'); } else { addedSomething = true; }<br />
              Object value = accessor.invoke(object, (Object[])null);<br />
              add(name, value);</p>
<p>          }</p>
<p>        }<br />
        Field[] ff = object.getClass().getDeclaredFields();<br />
        for (int i = 0; i &lt; ff.length; ++i) {<br />
            Field field = ff[i];<br />
            if ( field.isAccessible() ) {<br />
              if ( addedSomething ) { add(&#8217;,'); } else { addedSomething = true; }<br />
              add(field.getName(), field.get(object));<br />
            }<br />
        }</p>
<p>    } catch (Exception e) {<br />
        e.printStackTrace();<br />
    }<br />
    add(&#8221;}&#8221;);<br />
}<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Clint Pells</title>
		<link>http://blog.stringtree.org/2007/02/02/finally-a-json-validator/#comment-1259</link>
		<dc:creator>Clint Pells</dc:creator>
		<pubDate>Thu, 01 Mar 2007 17:24:37 +0000</pubDate>
		<guid isPermaLink="false">http://blog.stringtree.org/?p=6#comment-1259</guid>
		<description>Actually:
private void bean(Object object) {
    add("{");
    BeanInfo info;
    boolean addedSomething = false;
    try {
        info = Introspector.getBeanInfo(object.getClass());
        PropertyDescriptor[] props = info.getPropertyDescriptors();
        for (int i = 0; i </description>
		<content:encoded><![CDATA[<p>Actually:<br />
private void bean(Object object) {<br />
    add(&#8221;{&#8221;);<br />
    BeanInfo info;<br />
    boolean addedSomething = false;<br />
    try {<br />
        info = Introspector.getBeanInfo(object.getClass());<br />
        PropertyDescriptor[] props = info.getPropertyDescriptors();<br />
        for (int i = 0; i</p>
]]></content:encoded>
	</item>
</channel>
</rss>
