README.md in jsonify-0.1.1 vs README.md in jsonify-0.1.2
- old
+ new
@@ -31,13 +31,13 @@
## Usage
In the examples that follow, the JSON output is usually shown "prettified". Is this only
for illustration purposes, as the default behavior for Jsonify is not to prettify the output.
-You can enable prettification by passing `:pretty => true` to the Jsonify::Builder constructor; however,
+You can enable prettification by passing `:format => :pretty` to the Jsonify::Builder constructor; however,
pretty printing is a relatively costly operation and should not be used in production (unless, of course, you explicitly
-want to show this format).
+want to show this format). The default format, `plain`, dictates no special formatting: the result will be rendered as a compact string without any newlines.
### Standalone
# Create some objects that represent a person and associated hyperlinks
@person = Struct.new(:first_name,:last_name).new('George','Burdell')
@links = [
@@ -45,44 +45,43 @@
['school', 'http://gatech.edu'],
]
# Build this information as JSON
require 'jsonify'
- json = Jsonify::Builder.new(:pretty => true)
+ json = Jsonify::Builder.new(:format => :pretty)
- json.result do
- json.alumnus do
- json.fname @person.first_name
- json.lname @person.last_name
- end
- json.links(@links) do |link|
- {:rel => link.first, :href => link.last}
- end
+ # Representation of the person
+ json.alumnus do
+ json.fname @person.first_name
+ json.lname @person.last_name
end
+ # Relevant links
+ json.links(@links) do |link|
+ {:rel => link.first, :href => link.last}
+ end
+
# Evaluate the result to a string
json.compile!
Results in ...
{
- "result": {
- "alumnus": {
- "fname": "George",
- "lname": "Burdell"
+ "alumnus": {
+ "fname": "George",
+ "lname": "Burdell"
+ },
+ "links": [
+ {
+ "rel": "self",
+ "href": "http://example.com/people/123"
},
- "links": [
- {
- "rel": "self",
- "href": "http://example.com/people/123"
- },
- {
- "rel": "school",
- "href": "http://gatech.edu"
- }
- ]
- }
+ {
+ "rel": "school",
+ "href": "http://gatech.edu"
+ }
+ ]
}
### View Templates
Jsonify includes a Rails 3 template handler. Any template with a `.jsonify` will be handled by Rails.
@@ -270,10 +269,10 @@
That JSON array is then set as the value of the name-value pair, where the name comes from the method name (for `method_missing`)
or symbol (for `tag!`).
So this construct is really doing two things -- creating a JSON pair, and creating a JSON array as the value of the pair.
- json = Jsonify::Builder.new(:pretty => true)
+ json = Jsonify::Builder.new(:format => :pretty)
json.letters('a'..'c') do |letter|
letter.upcase
end
compiles to ...