JSON

From Vexi

Jump to: navigation, search

Introduction

JSON is short for 'JavaScript Object Notation' and is a data interchange format. Inevitably it gets compared to XML its chief competitor. More properly it should be compared to one of the types of XML used to represent dynamic types (maps, lists, primitives), such as the type system in XML-RPC. JSON's advantages come from its simplicity, as it does not carry the baggage of being a subset of something more general like xml:

  • less overhead, shorter
  • less confusing to the uninitiated, no references to namespaces, schemas
  • when used with javascript its direct mapping with javascript types means less headaches (for instance xmlrpc does not have a null).
  • arguably a larger mindshare than any of the individual types of XML that do the same job.

The last point here, more true as the example given - XMLRPC - whilst describing dynamic types only correctly does so when they are wrapped in a response/request element. So it is not really appropriate when they are in different contexts.

The disadvantages, come from it not being XML:

  • JSON needs specific support on a platform (not really a problem as it is very simple and is quite widely supported)
  • no standard querying tool (XML has XPath)
  • not extensible
  • does not scale to huge files (no SAX-like parser)

Ultimately if the task is data interchange, where the data is dynamic then a flavour of XML could work, but JSON is a dedicated solution which has advantages. If things are more complicated than this and you need to do things such as querying, representing attributes & namespaces in an ad-hoc manor then JSON ceases to be appropriate.

JSON and Vexi

JSON is supported in Vexi using by the functions eval and stringify.

vexi.js.eval(a_string);
vexi.js.stringify(a_js_obj);

Currently there is no known implementation for safe parsing a json object (Vexi's incomplete regexp support currently makes it non-trivial to port the standard Javascript implementation from json.org). As in a browser calling eval on an untrusted string runs the risk of executing code. However it is often the case that this not a problem on the client.

A very non-visual Vexi program that demonstrates how to translate in and out of JSON:

<vexi xmlns:ui="vexi://ui">
     // JSON sample
     var json = 
     '{'+
     '  "name": "Reverand Green",'+
     '  "age": 38,'+
     '  "height": 1.68,'+
     '  "urls": ['+
     '    "http:\/\/www.bbc.co.uk\/",'+
     '    "http:\/\/www.bishopsonline.com\/"'+
     '  ]'+
     '}';

     // Wrap json in a 'return' and a ';' so that the execution
     // returns the object which is evaluated.
     var obj = vexi.js.eval("return "+json +";");

     // and back again (we may lose some formatting!) 
     var json_ = vexi.js.stringify(obj);
     vexi.log.info(json_);
    
     <ui:box />
</vexi>

Save the above as 'main.t' in the same directory as the Vexi jar .

Run from the directory ($DIR) using the following command, adapting vexi3 to be the exact name of the jar:

cd $DIR
java -jar vexi.jar -t main .
Personal tools
wiki information