<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>fforw.de &#187; JSON</title>
	<atom:link href="http://fforw.de/tag/json/feed/" rel="self" type="application/rss+xml" />
	<link>http://fforw.de</link>
	<description>skip the boring parts</description>
	<lastBuildDate>Sat, 12 Nov 2011 11:54:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Generating List&lt;Something&gt; from JSON with svenson</title>
		<link>http://fforw.de/post/generating-list-from-json-with-svenson/</link>
		<comments>http://fforw.de/post/generating-list-from-json-with-svenson/#comments</comments>
		<pubDate>Thu, 09 Sep 2010 18:26:19 +0000</pubDate>
		<dc:creator>fforw</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[svenson]]></category>

		<guid isPermaLink="false">http://fforw.de/?p=217</guid>
		<description><![CDATA[Often you&#8217;ll find yourself wanting to parse a JSON into a Java collection but want the values inside the collection to be of a specific type. Nothing easier than that. import org.svenson.JSONParser; … // Getting a list containing your own type Something. // Assume json to be a String containing the JSON dataset. JSONParser parser [...]]]></description>
			<content:encoded><![CDATA[<p>Often you&#8217;ll find yourself wanting to parse a JSON into a Java collection but want the values inside the collection to be of a specific type. Nothing easier than that.</p>
<p><span style="color: #0000ff;"> </span></p>
<pre><span style="color: #0000ff;">import</span> org.svenson.JSONParser;
…
<span style="color: #008000;">// Getting a list containing your own type Something.
// Assume json to be a String containing the JSON dataset.
</span>JSONParser parser = <span style="color: #0000ff;">new</span> JSONParser();
parser.addTypeHint("[]", Something.class);
List&lt;Something&gt; someThings = parser.parse(List.class, json);
<span style="color: #008000;">// someThings will be a ArrayList instance by default. You can change
// that by changing the mappings for interfaces by calling
// org.svenson.JSONParser.setInterfaceMappings(Map&lt;Class, Class&gt;)
</span></pre>
<p>Parsing into a map is not much more complicated either</p>
<pre>JSONParser jsonParser = <span style="color: #0000ff;">new</span> JSONParser();
jsonParser.addTypeHint(new RegExPathMatcher("\\.(f1|f2)"), Something.class);
Map&lt;String,Object&gt; someThings = jsonParser.parse(Map.class, json);</pre>
<p>If we want to have our Something type for more than a single field, we need to setup a matcher. Here you see an example of a RegExPathMatcher that makes sure that both the keys &#8220;f1&#8243; and &#8220;f2&#8243; of the map we receive will be converted to Something, while all other fields are not.</p>
<p>If you want to convert all map properties to Something, the RegExPathMatcher would be like this</p>
<pre>    … new RegExPathMatcher("\\..*") …</pre>
<p>This would match every JSON path that starts with a property. If you don&#8217;t like RegularExpressions, or are on some kind of diet on them, you can also construct a more complex matcher tree from the compositable Matchers like this</p>
<pre>JSONParser jsonParser = <span style="color: #0000ff;">new</span> JSONParser();<span style="color: #000000;">
jsonParser.addTypeHint(<span style="color: #0000ff;">new</span> OrMatcher(
    <span style="color: #0000ff;">new</span> PrefixPathMatcher(".f1"),
    <span style="color: #0000ff;">new</span> PrefixPathMatcher(".f2")), Something<span style="color: #0000ff;">.class</span>);
Map&lt;String,Object&gt; someThings = jsonParser.parse(Map<span style="color: #0000ff;">.class</span>, json);
</span>
</pre>
<p><strong>Update:</strong><span style="color: #000000;"> Due to me fucking up both the Prefix-/Suffix- matchers as well as their tests, the last example will only really work with the current svenson trunk/future svenson 1.3.8</span></p>
<p><span style="color: #000000;"><br />
</span></p>
<p><span style="color: #000000;"></span></p>
]]></content:encoded>
			<wfw:commentRss>http://fforw.de/post/generating-list-from-json-with-svenson/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Annotating DOM nodes with JSON, Part 2</title>
		<link>http://fforw.de/post/annotating-dom-nodes-with-json-part-2/</link>
		<comments>http://fforw.de/post/annotating-dom-nodes-with-json-part-2/#comments</comments>
		<pubDate>Thu, 03 Jun 2010 11:49:51 +0000</pubDate>
		<dc:creator>fforw</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[svenson]]></category>

		<guid isPermaLink="false">http://fforw.de/?p=169</guid>
		<description><![CDATA[It&#8217;s been a while since I wrote Annotating DOM nodes with JSON and in retrospective I can say that I never really used the method described in a real life project. Now I&#8217;d like to present another method of decorating DOM nodes with JSON based on classes. This one I actually implemented in OpenSAGA to [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a while since I wrote <a title="old blog post" href="http://fforw.de/post/annotating-dom-nodes-with-json/">Annotating DOM nodes with JSON</a> and in retrospective I can say that I never really used the method described in a real life project. Now I&#8217;d like to present another method of decorating DOM nodes with JSON based on classes. This one I actually implemented in <a title="OpenSAGA Project website" href="http://opensaga.org/">OpenSAGA</a> to have arbitrary metadata from some of the OpenSAGA Widgets.</p>
<p>I didn&#8217;t really like the idea of misusing onclick for the purpose of meta-data and thought about a better way of doing it. Browsing the w3 HTML specs I came upon the fact that <a title="Link to the HTML spec. Also see 'cdata-list' definition" href="http://www.w3.org/TR/html401/struct/global.html#h-7.5.2">classes can be any character separated by spaces</a>. So for use-cases where I only needed one meta-data value I used classes like</p>
<pre>&lt;div class="refId:id-1234"&gt;
    DIV content
&lt;/div&gt;
</pre>
<p>A use-case specific prefix is used to mark a class as meta-data container containing the string after the prefix. The code to evaluate this in javascript is very easy</p>
<pre>/**
 * Returns the class value with the given prefix using the giving separator
 * @param {DOMElement} elem DOM element to fetch metadata from
 * @param {String} name of the classval value
 * @param {String} separator to use between name and value. Default is ":"
 */
function classval(elem, name, separator)
{
    var match = new RegExp("\\b" + name + (separator || ":") + "([^ ]*)($| )")
                          .exec(elem.className);
    if (match)
    {
        return match[1];
    }
    return null;
}
…
// assume divElement to be DOM element of the div
var refId = classval(divElement, "refId");
</pre>
<p>I thought about going for a more elaborate prefix scheme to support nested metadata but in the end decided against it because I already have a nicely supported format for exchanging data between server and client: JSON. So I tried to come up with a scheme of using arbitrary JSON for the metadata decoration.</p>
<p>Only problem: Spaces are not valid inside classes, so I needed a method to encode and decode JSON into valid classes. The method should not totally mangle the JSON to keep readability and maybe write the encoded variant by hand for simple cases.</p>
<p>Solution:</p>
<ul>
<li>HTML encode the JSON-String</li>
<li>Replace spaces with underlines and underlines with \u005f</li>
</ul>
<p>The replacement of underlines is valid because underlines can only occur inside quoted JSON strings so they can just be replaced by their escaped unicode value \u005f.</p>
<p>Here is the java code to do the escaping. Since it&#8217;s basically a combination of string replacement and HTML encoding this should be easily doable in any server-side language:</p>
<pre>
    public String escapeDecoration(String s)
    {
        String escaped = StringEscapeUtils.escapeHtml(s);

        StringBuilder sb = new StringBuilder(escaped.length());
        sb.append("deco:");
        for (int i = 0; i < escaped.length() ; i++)
        {
            char c = escaped.charAt(i);
            switch(c)
            {
                case '_':
                    sb.append("\\u005F");
                    break;
                case ' ':
                    sb.append('_');
                    break;
                default:
                    sb.append(c);
                    break;
            }
        }

        return sb.toString();
    }
</pre>
<p>The escape method uses the escapeHTML method from Apache commons-lang's StringEscapeUtil. Going the other way in javascript is not that complicated either:</p>
<pre>/**
 * Decodes the given string containing HTML entities.
 */
function htmlDecode(s)
{
    var helper = document.createElement("SPAN");
    helper.innerHTML = s;
    return helper.innerHTML;
}

/**
 * Returns the JSON decoration of the given element.
 * @param {DOMElement} DOM element
 * @param {String} decorator classval name, default is "deco".
 */
function decoration(elem, name)
{
    var value, data, result;

    value = classval(elem, name || "deco");
    if (value)
    {
       // get raw data from DOM element
       data = value.replace(/_/g, " ");
       // replace HTML entities with the original characters
       data = htmlDecode(data);
       // evaluate JSON
       result = eval("("+data+")");
    }
    return result || {};
}
</pre>
<p>In order to achieve a better readability of escaped JSON, I also used <a href="http://code.google.com/p/svenson/">svenson</a>'s ability to deviate from the JSON standard by using single quotes instead of double quotes. Just comparing</p>
<pre>&lt;div id="tst2" class="deco:{'foo':'xxx\u005f_yyy','baz':[1,3,5,7,9]}"&gt;
JSON annotation
&lt;/div&gt;
</pre>
<p>to</p>
<pre>&lt;div id="tst2" class="deco:{&amp;quot;foo&amp;quot;:&amp;quot;xxx\u005f_yyy&amp;quot;,&amp;quot;baz&amp;quot;:[1,3,5,7,9]}"&gt;
JSON annotation
&lt;/div&gt;
</pre>
<p>should demonstrate that single quotes are not only much better readable, but also shorter. If you use eval() evaluate the JSON string, the single quotes are no problem at all. If you want json2.js / native JSON-parsing, you might have to replace the quote chars before parsing.</p>
<p><strong>Links:</strong></p>
<p><a href="http://fforw.de/static/files/meta.html">HTML test page with both metadata strategies</a></p>
]]></content:encoded>
			<wfw:commentRss>http://fforw.de/post/annotating-dom-nodes-with-json-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scripting JSON</title>
		<link>http://fforw.de/post/scripting-json/</link>
		<comments>http://fforw.de/post/scripting-json/#comments</comments>
		<pubDate>Sun, 16 May 2010 15:51:14 +0000</pubDate>
		<dc:creator>fforw</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[NodeJS]]></category>

		<guid isPermaLink="false">http://fforw.de/?p=158</guid>
		<description><![CDATA[Doing a lot of web stuff and fiddling around with CouchDB, I really got to like JSON as versatile format for things. Installing the JSONView extension for firefox really helps with working with JSON in the browser, but what I&#8217;ve been missing so far is an easy way to deal with JSON from bash scripts. [...]]]></description>
			<content:encoded><![CDATA[<p>Doing a lot of web stuff and fiddling around with CouchDB, I really got to like JSON as versatile format for things. Installing the<a href="https://addons.mozilla.org/en-US/firefox/addon/10869/"> JSONView extension for firefox</a> really helps with working with JSON in the browser, but what I&#8217;ve been missing so far is an easy way to deal with JSON from bash scripts. Fiddling around with the very interesting <a href="http://nodejs.org/">NodeJS</a>, I came up with a small node js script that makes JSON handling much easier, the JSON command: It reads a JSON object from stdin and feeds it to a javascript function body with &#8220;v&#8221; and NodeJS&#8217; &#8220;sys&#8221; as parameters. The return value of the function is written to stdout. If it was a string, it is written as-is, if it is another object it will be pretty-JSONified.</p>
<h2>Simple Example</h2>
<pre>$ curl -s http://localhost:5984/test | json "return v;"
{
 "db_name": "test",
 "doc_count": 0,
 "doc_del_count": 0,
 "update_seq": 0,
 "purge_seq": 0,
 "compact_running": false,
 "disk_size": 79,
 "instance_start_time": "1274021449672284",
 "disk_format_version": 5
}</pre>
<p>Use curl to fetch the status of CouchDB database &#8220;test&#8221; from the local CouchDB node and then just pretty print it by returning the implicit value v.</p>
<pre>$ curl -s http://localhost:5984/test | json "return v.disk_size;"
79
</pre>
<p>Just print the disk_size of CouchDB database &#8220;test&#8221;. You can use all the modern JavaScript functions v8 offers plus the implicit &#8220;sys&#8221; object that lets you log stuff to stderr or inspect objects. A little script that I find highly useful:</p>
<pre>#!/bin/bash
# Delete all jcouchdb test databases
DBS=$(curl -s http://localhost:5984/_all_dbs | \
json 'return v.filter( function(db) { return db.indexOf("jcouchdb") == 0; }).join("\n");')

for i in $DBS
do
 curl -X DELETE http://localhost:5984/$i
done
</pre>
<p>Filter the list of database to only contain those that start with &#8220;jcouchdb&#8221;, then loop over them to delete.</p>
<p><strong>Links:</strong></p>
<ul>
<li><a href="http://fforw.de/static/files/json">&#8220;json&#8221; command line script</a>. Requires NodeJS to be installed in /usr/local. If it&#8217;s installed somewhere else, you need to change the script</li>
</ul>
<p><small><strong>Update: </strong>Added &#8220;return v;&#8221; as default function. now also supports &#8220;-h&#8221; and &#8220;&#8211;help&#8221;.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://fforw.de/post/scripting-json/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Memory consumption changes in svenson 1.3</title>
		<link>http://fforw.de/post/memory-consumption-changes-in-svenson-13/</link>
		<comments>http://fforw.de/post/memory-consumption-changes-in-svenson-13/#comments</comments>
		<pubDate>Sun, 19 Apr 2009 15:00:24 +0000</pubDate>
		<dc:creator>fforw</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[jcouchdb]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[stats]]></category>
		<category><![CDATA[svenson]]></category>

		<guid isPermaLink="false">http://blog.willie/?p=4</guid>
		<description><![CDATA[Implementing a streaming attachment feature for jcouchdb, I started to wonder whether it would be a good idea for svenson to support JSON parsing from a stream, too, as I don&#8217;t really need the complete stream to start constructing the java object graph. Implementing stream parsing was really nice and easy thanks to the units [...]]]></description>
			<content:encoded><![CDATA[<p>Implementing a streaming attachment feature for <a title="jcouchdb project page at google code" href="http://code.google.com/p/jcouchdb/">jcouchdb</a>, I started to wonder whether it would be a good idea for svenson to support JSON parsing from a stream, too, as I don&#8217;t really need the complete stream to start constructing the java object graph.</p>
<p>Implementing stream parsing was really nice and easy thanks to the units test present in svenson. After that, I came upon two ways to generally cut down on memory use. All tokens with fixed values could just have a single instance. The recording of tokens to provide token based look ahead was not really needed in all cases. But how much does that save?</p>
<p>As a test case I wrote a small tool class to generate random, nested JSON datasets, generated two test files of 65kb and 4.5mb size and parsed these with svenson 1.2.8 and what now is svenson 1.3.</p>
<p>Measuring the actual memory usage for these two test files proved to be difficult. Somehow none of the programs I tried seemed to give me the data I wanted. Eclipse TPTP just ignored Strings that were no member of any class but just parameters, making stream and string parsing look exactly the same memory-wise. tijmp and others did not provide the data I wanted at all.</p>
<p>So in the end I wrote a little python script that parses a hprof ASCII output to</p>
<ul>
<li>sum up all memory use</li>
<li>group allocations by class type, but only if the stack trace of it touches svenson</li>
<li>output the top 10 of those classes and the sums</li>
</ul>
<p>This provided meaningful data and also showed some points for further improvement. There was a huge number of java.lang.reflect.Method allocations which turned out to be caused by svenson inspecting the target classes for annotations and appropriate methods which was done on a per target basis instead of the better per target class basis.</p>
<p>All in all the memory usage went down quite a bit:</p>
<div class="wp-caption aligncenter" style="width: 507px"><img title="memory usage diagram" src="http://fforw.de/static/image/svenson-combined.png" alt="memory usage for different svenson versions, with and without streaming" width="497" height="296" /><p class="wp-caption-text">memory usage for different svenson versions, with and without streaming</p></div>
<p>45% less memory for the small file and 62% for the large file for all allocations. I think that is really good..</p>
<p>Below are some links to the files needed to repeat the benchmarking. The transform hprof script might also prove to be useful for other projects if changed appropriately.</p>
<p>The new jcouchdb release will also use stream parsing.</p>
<p><strong>Links:</strong></p>
<ul>
<li><a href="/static/files/svenson-memory-report.txt">Detailed data: Top 10 memory use by class and sums for all 6 variants</a></li>
<li><a href="/static/files/transform_hprof.py">Python script to transform hprof ASCII output</a></li>
<li><a href="http://svenson.googlecode.com/svn/trunk/test/org/svenson/RandomJSON.java">Random JSON Generator</a></li>
<li><a href="/static/files/small.json">Small JSON test file</a></li>
<li><a href="/static/files/big.json">Big JSON test file</a></li>
</ul>
<p><strong>edit:</strong><br />
The command to generate the hprof file was something like</p>
<blockquote><p>java -agentlib:hprof=heap=sites,depth=100,cutoff=0 -cp .. svensonperf.ReadJSONOld big.json</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://fforw.de/post/memory-consumption-changes-in-svenson-13/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Two new projects: svenson and jcouchdb</title>
		<link>http://fforw.de/post/two-new-projects-svenson-and-jcouchdb/</link>
		<comments>http://fforw.de/post/two-new-projects-svenson-and-jcouchdb/#comments</comments>
		<pubDate>Sat, 01 Nov 2008 15:04:02 +0000</pubDate>
		<dc:creator>fforw</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[couchdb]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://blog.willie/?p=8</guid>
		<description><![CDATA[In my never ending fight against teh wind-mills, I have produced two new open source projects that are somehow connected to each other. First there is svenson which is a release of my own personal JSON generator / parser. The name is a result of a joke. When my boss asked me what was the [...]]]></description>
			<content:encoded><![CDATA[<p>In my never ending fight against teh wind-mills, I have produced two new open source projects that are somehow connected to each other.</p>
<p>First there is svenson which is a release of my own personal JSON generator / parser. The name is a result of a joke. When my boss asked me what was the unique characteristic of it, my first answer was: &#8220;It&#8217;s written by <strong>me</strong>!&#8221;. So the name comes from &#8220;Sven&#8217;s JSON&#8221;. </p>
<p>The answer was of course not totally serious. I wrote svenson because none of the JSON parsers out there seemed to have the right combination of being not too complicated yet flexible enough to work well in different scenarios. Being able to use a healthy mix of concrete typed java beans and dynamic map / list constructs seemed to be the best way to go, yet none of the JSON libraries out there seemed to go even near that direction. </p>
<p>See the svenson wiki at google code for an explanation of how svenson works.</p>
<p>The other project is called jcouchdb and is my attempt at writing a Java driver for <a href="http://incubator.apache.org/couchdb/" target="_blank" title="couchdb project homepage at apache.org">couchdb</a>. It is very much connected to svenson as it was the driving force for the parser part of svenson. it offers an API that lets you use all those nice svenson features with couchdb documents. </p>
<p><strong>Links:</strong> </p>
<ul>
<li><a href="http://code.google.com/p/svenson/">svenson homepage at google code</a> </li>
<li><a href="http://code.google.com/p/jcouchdb/">jcouchdb homepage at google code</a> </li>
<li><a href="http://couchdb.apache.org/">couchdb homepage at apache.org</a>  </li>
</ul>
<p style="font-size: x-small"><strong>update:</strong>  link to couchdb</p>
]]></content:encoded>
			<wfw:commentRss>http://fforw.de/post/two-new-projects-svenson-and-jcouchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Annotating DOM nodes with JSON</title>
		<link>http://fforw.de/post/annotating-dom-nodes-with-json/</link>
		<comments>http://fforw.de/post/annotating-dom-nodes-with-json/#comments</comments>
		<pubDate>Mon, 03 Jul 2006 20:54:29 +0000</pubDate>
		<dc:creator>fforw</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://blog.willie/?p=22</guid>
		<description><![CDATA[One problem when trying to do &#8220;The Right Thing &#8482;&#8221; and separate your webdesign into different layers is what to do when you need additional information to transform your nicely id and class annotated DOM nodes into fancy, javascript-enabled goodness: Where do you store that information? How do you associate it to the DOM nodes? [...]]]></description>
			<content:encoded><![CDATA[<p>One problem when trying to do &#8220;The Right Thing &#8482;&#8221; and <a href="Separate_it%21/">separate your webdesign into different layers </a>is what to do when you need additional information to transform your nicely id and class annotated DOM nodes into fancy, javascript-enabled goodness: </p>
<p>Where do you store that information? How do you associate it to the DOM nodes? I will present three approaches to this problem..</p>
<hr width="100%" size="2" />
<h5>Method 1: The squeaky clean  </h5>
<p>The first method is relatively straight-forward: You add ids or classes to your DOM nodes. Then you include a dynamically generated javascript library  in your document. </p>
<p>The problem with this is that the generated javascript library is requested in a new request which makes it nescessary to provide this request with the nescessary context to generate it. You also need to write code which finds your DOM nodes and finds the data from the included javascript library and associates both. You also walk over the server side data twice: Once to generate the DOM nodes and once to create the javascript library. </p>
<h5>Method 2: Compromising </h5>
<p>The second approach is to compromise a little on the separation ideal and render the javascript data from the first method right into the page source. </p>
<p>You don&#8217;t need to carry the context for the data into a second request for the javascript library, but you still need to wak the data twice on the server side, you still need to associate the DOM nodes to the data, and you have a big block of js data in the head section of your document. </p>
<h5>Method 3: Annotating DOM nodes with JSON </h5>
<p>I thought about a way to directly associate the DOM with additional data. <a href="http://json.org" title="JSON specification / tools">JSON</a>  seemed to be a good format to store the data. At first I thought about using a custom attribute but I did not like the idea of invalid HTML markup. Then I got another idea:</p>
<p>Why not use the event handlers? Something like</p>
<blockquote><pre>&lt;a href="/no_js" onclick="return { foo: 'extra', bar: 1};">Link&lt;/a></pre>
</blockquote>
<p>works pretty well. It is totally valid HTML, the onclick contains valid javascript code which can also be easily parsed by other tools. (It is basically just a JSON string wrapped with <em>a &#8220;return [...] ;&#8221; ) </em>When javascript is disabled, the link just executes normally and can provide non-javascript functionality. The data can be retrieved by executing &#8220;var data=linkNode.onclick();&#8221;. &#8220;But what about the fake event handler?&#8221; you might ask. Well.. if someone clicks on the unmodified link in a javascript enabled browser: <em>nothing happens</em>. The script just returns some data and does nothing else. The return code will be ignored by the browser environment due to this age-old, pre-DOM standard of canceling the event if the handler returns <em>false </em>and just going on if it returns <em>true</em> &#8212; since the data is something it evaluates to <em>true</em> so it&#8217;s just ignored.</p>
<p>On the server side things get much easier. Not only does no context need to be carried into another request but you also only need to walk over the data once.  You can ouput the HTML and the additional javascript data into the same part of the document.</p>
<p>I admit that this approach bends the <a href="../Separate_it%21/">rules of separation</a> a little, but in my opinion that&#8217;s ok.</p>
<ul>
<li>It uses onclick=&#8221;"  but   does not really put any code in there, just some data</li>
<li>The semantics of the original markup are kept as they are. The method only allows to annotate this &#8220;<em>base data</em>&#8221; with additional information.</li>
<li>The pros vastly outweigh the cons</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://fforw.de/post/annotating-dom-nodes-with-json/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

