Essentials of Javascript - Cultural View
Essentials of Javascript - Cultural View
Essentials of Javascript - Cultural View
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
JSON 103<br />
YAML uses a block-indent syntax to allow formatting <strong>of</strong> structured data without use <strong>of</strong> additional characters<br />
(ie: braces, brackets, quotation marks, etc.). Besides giving YAML a different appearance than JSON, this<br />
block-indent device permits the encapsulation <strong>of</strong> text from other markup languages or even JSON in the other<br />
languages native literal style and without escaping <strong>of</strong> colliding sigils.<br />
Efficiency<br />
JSON is primarily used for communicating data over the Internet, but has certain inherent characteristics that may<br />
limit its efficiency for this purpose. Most <strong>of</strong> the limitations are general limitations <strong>of</strong> textual data formats and also<br />
apply to XML and YAML. For example, despite typically being generated by an algorithm (by machine), parsing<br />
must ironically be accomplished on a character-by-character basis. Additionally, the standard has no provision for<br />
data compression, interning <strong>of</strong> strings, or object references. Compression can, <strong>of</strong> course, be applied to the JSON<br />
formatted data (but the decompressed output typically still requires further full parsing by the browser for<br />
recognizable keywords, tags and delimiters).<br />
In practice performance can be comparable to that <strong>of</strong> similar binary data formats [23] and <strong>of</strong>ten depends more on<br />
implementation quality than on the theoretical limitations <strong>of</strong> formats.<br />
JSONP<br />
JSONP or "JSON with padding" is a complement to the base JSON data format, a usage pattern that allows a page<br />
to request and more meaningfully use JSON from a server other than the primary server. JSONP is an alternative to a<br />
more recent method called Cross-Origin Resource Sharing.<br />
Under the same origin policy, a web page served from server1.example.com cannot normally connect to or<br />
communicate with a server other than server1.example.com. An exception is HTML tags. Taking advantage<br />
<strong>of</strong> the open policy for tags, some pages use them to retrieve JSON from other origins.<br />
To see how that works, let's consider a URL that, when requested, returns a JSON statement. In other words, a<br />
browser requesting the URL would receive something like:<br />
{"Name": "Cheeso", "Rank": 7}<br />
The Basic Idea: Retrieving JSON via Script Tags<br />
It's possible to specify any URL, including a URL that returns JSON, as the src attribute for a tag.<br />
Specifying a URL that returns plain JSON as the src-attribute for a script tag, would embed a data statement into a<br />
browser page. It's just data, and when evaluated within the browser's javascript execution context, it has no<br />
externally detectable effect.<br />
One way to make that script have an effect is to use it as the argument to a function. invoke( {"Name": "Cheeso",<br />
"Rank": 7}) actually does something, if invoke() is a function in <strong>Javascript</strong>.<br />
And that is how JSONP works. With JSONP, the browser provides a JavaScript "prefix" to the server in the src URL<br />
for the script tag; by convention, the browser provides the prefix as a named query string argument in its request to<br />
the server, e.g.,<br />
<br />
The server then wraps its JSON response with this prefix, or "padding", before sending it to the browser. When the<br />
browser receives the wrapped response from the server it is now a script, rather than simply a data declaration. In<br />
this example, what is received is