Json
Json
The x in Ajax
Douglas Crockford
Yahoo! Inc.
YAHOO IS HIRING
DEVELOPERS
Ajax, PHP, DHTML/XHTML,
Javascript, CSS, Actionscript / Flash
Josie Aguada
[email protected]
Data Interchange
• The key idea in Ajax.
• An alternative to page
replacement.
• Database Model
• Document Model
• Minimal
• Textual
• Subset of JavaScript
JSON
• A Subset of ECMA-262 Third Edition.
• Language Independent.
• Text-based.
• Light-weight.
• Easy to parse.
JSON Is Not...
• JSON is not a document format.
• JSON is not a markup language.
• JSON is not a general serialization
format.
No cyclical/recurring structures.
No invisible structures.
No functions.
History
• 1999 ECMAScript Third Edition
• 2002 JSON.org
• 2005 Ajax
• Python
• NewtonScript
Values
• Strings
• Numbers
• Booleans
• Objects
• Arrays
• null
Value
value
string
number
object
array
true
false
null
Strings
• Sequence of 0 or more Unicode
characters
• No separate character type
A character is represented as a
string with a length of 1
• Wrapped in "double quotes"
• Backslash escapement
String
string
Any UNICODE character except
" "
" or \ or control character
quotation mark
\ "
reverse solidus
\
solidus
/
backspace
b
formfeed
f
newline
n
carriage return
r
horizontal tab
t
u 4 hexadecimal digits
Numbers
• Integer
• Real
• Scientific
• No octal or hex
• No NaN or Infinity
Use null instead
Number
number
0 . digit
-
digit
e
1 - 9
digit E
+
digit
-
Booleans
• true
• false
null
• A value that isn't anything
Object
• Objects are unordered containers
of key/value pairs
• Objects are wrapped in { }
• , separates key/value pairs
• : separates keys and values
• Keys are strings
• Values are JSON values
object
{ string : value }
,
Object
{"name":"Jack B. Nimble","at large":
true,"grade":"A","level":3,
"format":{"type":"rect","width":1920,
"height":1080,"interlace":false,
"framerate":24}}
Object
{
"name": "Jack B. Nimble",
"at large": true,
"grade": "A",
"format": {
"type": "rect",
"width": 1920,
"height": 1080,
"interlace": false,
"framerate": 24
}
}
Array
• Arrays are ordered sequences of
values
• Arrays are wrapped in []
• , separates values
• JSON does not talk about
indexing.
An implementation can start array
indexing at 0 or 1.
Array
array
[ value ]
,
Array
["Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday",
"Friday", "Saturday"]
[
[0, -1, 0],
[1, 0, 0],
[0, 0, 1]
]
Arrays vs Objects
• Use objects when the key names
are arbitrary strings.
application/json
Character Encoding
• Strictly UNICODE.
• Default: UTF-8.
responseData = eval(
'(' + responseText + ')');
responseData =
responseText.parseJSON();
JSON in Ajax
• Is it safe to use eval with
XMLHttpRequest?
var request =
JSONRequest.post(url, data, done);
var request =
JSONRequest.get(url, done);
I want JSONRequest!
http://www.JSON.org/JSONRequest.html
ECMAScript Fourth Ed.
• New Methods:
Object.prototype.toJSONString
String.prototype.parseJSON
var data = {
"first": "Carl",
"last": "Hollywood",
"border": 2
};
mydiv.innerHTML = template.supplant(data);
supplant
String.prototype.supplant = function (o) {
return this.replace(/{([^{}]*)}/g,
function (a, b) {
var r = o[b];
return typeof r === 'string' ?
r : a;
}
);
};
JSONT
var rules = {
self:
'<svg><{closed} stroke="{color}" points="{points}" /></svg>',
closed: function (x) {return x ? 'polygon' : 'polyline';},
'points[*][*]': '{$} '
};
var data = {
"color": "blue",
"closed": true,
"points": [[10,10], [20,10], [20,20], [10,20]]
};
jsonT(data, rules)
<svg><polygon stroke="blue"
points="10 10 20 10 20 20 10 20 " /></svg>
http://goessner.net/articles/jsont/
<section>
<title>Book-Signing Event</title>
<signing>
<author title="Mr" name="Vikram Seth" />
<book title="A Suitable Boy" price="$22.95" />
</signing>
<signing>
<author title="Dr" name="Oliver Sacks" />
<book title="The Island of the Color-Blind"
price="$12.95" />
</signing>
</section>
Namespace
{"section":
"title": "Book-Signing Event",
"signing": [
{
"author": { "title": "Mr", "name": "Vikram Seth" },
"book": { "title": "A Suitable Boy",
"price": "$22.95" }
}, {
"author": { "title": "Dr", "name": "Oliver Sacks" },
"book": { "title": "The Island of the Color-Blind",
"price": "$12.95" }
}
]
}}
• section.title
• section.signing[0].author.title
• section.signing[1].book.title
JSON Has No Validator
• Being well-formed and valid is not
the same as being correct and
relevant.
{
"$META$": meta-type,
"value": meta-value
}
Going Meta
• Possible meta-types:
Document
Module
$(Module).receive(json) send(json)
$(Module).send(json) receive(json)
The Mashup Security
Solution
<module id="NAME" href="URL" style="STYLE" />