SnTT: JSON in Domino

Wednesday 14th February, 2007
show_and_tell Listen up, a whole new technique of how to do AJAX with Domino is coming.

Lotus Domino 8.0 will come out with the support for JSON.

Let start from the beginning - what is JSON? For the definition I recommend you go and read wikipedia article, and than come back.I will put it in my own words - JSON is a way of get some data from the server which can be processed right away. For example, you are doing a XMLHttpRequest and get an JavaScript object which contains the response text. what do you do next - you have to parse the XML, creating the DOM tree, doing XPath, whatever. It is doable, but not really easy, and what worse, it is not even that fast. (XML processing in Firefox is way slower than in IE, unless you use E4X, which is not working in IE, so you would end up maintaining two versions of code, yada yada...)

In JASON the server send you JavaScript chunks instead of an XML document. The first advantage - it is slightly less text to transfer (especially if you get only a small response set, since it does not need XML header and such).
The second - you can call eval( ‘(‘+ retrieved JSON data+ ‘)’); to evaluate the text you have got into an object and then use it like every other JavaScript object. (I will come to an code example as soon as I show the way how to get JSON out of Domino).

A sidestep - At the Lotusphere I went to the session AD401 "Leveraging AJAX Frameworks To Build IBM Lotus Domino Web Applications" by Vinod Seraphin and Akira Sudoh, because I have submitted a very similar titled abstract and have met Vinod in the Cambridge in the RedBooks Lab as I was there last year. They tallked about JSON Support in Domino 8.0 and how you can use it with Dojo Toolkit.

How it will work? With ease - every time you would get XML response with an URL command like ?ReadViewEntries or ?ReadDesign just append &OutputFormat=JSON and you will get a JSON output instead of XML.

Now listen up, here is the SnTT extra value, I came back from Lotusphere and tried it at home. I do not have a Domino 8.0 beta server, but surprise!
JSON is already built in into current 7.0.2 release!


It is undocumented, so there may be something broken with it, but it is enough to start playing.

So here is the example of 'classical' ReadViewEntries:

 <?xml version="1.0" encoding="UTF-8" ?>
<viewentries toplevelentries="90">
<
viewentry position="1" unid="4E34CDE6D4FBF40AC1257182000BB1A3" noteid="2B36" siblings="90">
<
entrydata columnnumber="0" name="res_title">
 <text>03.06.2006040743DEF4HV.htm</text>
</
entrydata>
<entrydata columnnumber="1" name="subject">
 <text>Thanks!</text>
</
entrydata>
<
entrydata columnnumber="2" name="categories">
 <text>travel</text>
</
entrydata>
</
viewentry>
</
viewentries>


... And here is the same output with JSON:

{
"@toplevelentries": 90,
viewentry: [
{
"@position": '1',
"@unid": '4E34CDE6D4FBF40AC1257182000BB1A3',
"@noteid": '2B36',
"@siblings": 90,
entrydata: [
{
"@columnnumber": 0,
"@name": 'res_title',
text: {
0: '03.06.2006040743DEF4HV.htm'
}
},
{
"@columnnumber": 1,
"@name": 'subject',
text: {
0: 'Thanks!'
}
},
{
"@columnnumber": 2,
"@name": 'categories',
text: {
0: 'travel'
}
}
]
}
]
}



So this is how the mapping works:
  • Top level root element name is omitted
  • Attribute names are represented as double quoted property name prefaced with ‘@’
  • All attribute values are represented as a double quoted JavaScript strings - no numbers, no booleans
  • A list of elements are represented as a JavaScript array
  • Any element that might have a list is represented as an array
  • Element values are represented as a value to a “0” attribute

So, to get the number of documents in a view in XML I use:

totalDocs = xmlHttp.responseXML.getElementsByTagName("viewentries")[0].getAttribute("toplevelentries")


in JSON I only need (provided that responseJSON is my variable where i dit the eval() to)

totalDocs = responseJSON[“@toplevelentries”]


So, which one is faster, what would you guess?

    No Comments Found

Discussion for this entry is now closed.