AJAX - The Complete Reference

(avery) #1

136 Part I: Core Ideas


$json = new Services_JSON();
$jsonObject = $json->decode($payloadString);
}

You can run the complete example online at http://ajaxref.com/ch4/jsonrequest.html.
JSON is easy enough to use for requests and, depending on the language and library in
play, it can also be nice to use on the server side, as it automatically creates the variables for
you. Then again, the standard x-www-form-urlencoded format does this too and without
any extra requirements on either side.

Other Request Forms: YAML, Text, and Beyond


The sky’s the limit as far as transmitting data. We present a few more examples here to
prove that point, starting first with a format called YAML.

YAML
YAML (YAML Ain’t Markup Language) is a data serialization language designed to be
expressive, human friendly, portable between programming environments, easy to
implement, and efficient. A complete discussion of YAML can be found at http://yaml.org.
We present just a short discussion of it here to illustrate its potential use in the example.
YAML is quite simple and, interestingly, assigns meaning to whitespace to improve the
format’s readability. For example, list items are indicated with a leading dash (-) and are
presented one per line, like so:


  • Larry

  • Curly

  • Moe


Alternatively, these values could be presented in a more familiar less whitespace
focused format by enclosing the list in brackets.

[ Larry, Curly, Moe ]

To create simple relationships in YAML, use a colon character ( : ) to separate the values
placed individually on each line, like so:

Name: Moe
Hair : True
Hair Color : Black
Stooge Number : 1

To avoid whitespace styling, it is also possible to use curly braces ({}) to enclose the
content:

{ Name: Moe, Hair : True, Hair Color : Black, Stooge Number : 1 }
Free download pdf