info343/lectures/json-web-services/content.txt

Recall: Literal objects, arrays
More complex object/array structures
   Arrays of objects
   Objects with arrays
Extracting data from complex structures
JSON: Complex structures as strings
Saving a data structure to a string
   Serialize: JSON.stringify(objOrAry);
Restoring a data structure from a string
   Deserialize: JSON.parse(str);
Uses of JSON
   "The Fat-Free Alternative to XML" http://www.JSON.org/fatfree.html
   "JSON has become the X in Ajax."
   web services use JSON to communicate in lieu of XML
      (nearly) the same complexity as XML, but with almost zero processing, and immediate data access (no DOM extraction)
   client-side code can use JSON to save configuration settings
   can be used for arbitrarily large/small amounts of free-form data, on client and server
      could be used for storage on server, but most often used to transmit data, not store it
   emerging: JSON Schemas (like XML Schemas), data formats in JSON
      GeoJSON http://en.wikipedia.org/wiki/GeoJSON
      JSON-RPC http://en.wikipedia.org/wiki/JSON-RPC
Advanced uses of JSON
   JSONP: "Padded" JSON, fetching a JavaScript file containing JSON from another domain
      http://en.wikipedia.org/wiki/JSONP
   JsonML: Lossless conversion of XML to JSON
      http://jsonml.org/
Some JSON sources
   
JSON vs XML in Ajax
   XML procedure:
      1. Fetch XML using Ajax request
      2. Browser automatically parses into a DOM object in ajax.responseXML
      3. Use getElementsByTagName, .firstChild.nodeValue to isolate desired pieces of information and extract it (long and convoluted process)
   JSON procedure:
      1. Fetch JSON using Ajax request
      2. Run through JSON.parse() to restore object
      3. Directly access data in newly restored object

JSON vs XML 
   <key>value</key>
   "key" : "value"
   <tag attribute="value">foo</tag>
   "key" : {
      "attribute": "value",
      "content" : "foo"
   }
   
   "key" : {
      "key" : "value",
      "key" : "value"
   }
   
   <key>
      <key>value</key>
      <key>value</key>
   </key>
   
Pros and cons
   JSON:
      more convenient - direct access to data without having to extract using the DOM
      less descriptive/more concise - limited to lists and key/value pairs; some complex XML relations are difficult to represent; tags are more self-describing
      no conversion - if the object was created in JavaScript, it's a pain to convert it to XML (or another format) and back again for storage
   XML:
      more descriptive/less concise - each piece of data is descriptively labeled; elements and attributes can be in more complex structure; some data structures are more intuitively expressed in XML