|
9 | 9 | # JSON is completely language agnostic, making it the ideal interchange format.
|
10 | 10 | #
|
11 | 11 | # Built on two universally available structures:
|
12 |
| -# 1. A collection of name/value pairs. Often referred to as an _object_, hash table, record, struct, keyed list, or associative array. |
13 |
| -# 2. An ordered list of values. More commonly called an _array_, vector, sequence or list. |
| 12 | +# |
| 13 | +# 1. A collection of name/value pairs. Often referred to as an _object_, hash table, |
| 14 | +# record, struct, keyed list, or associative array. |
| 15 | +# 2. An ordered list of values. More commonly called an _array_, vector, sequence or |
| 16 | +# list. |
14 | 17 | #
|
15 | 18 | # To read more about JSON visit: http://json.org
|
16 | 19 | #
|
|
22 | 25 | # require 'json'
|
23 | 26 | #
|
24 | 27 | # my_hash = JSON.parse('{"hello": "goodbye"}')
|
25 |
| -# puts my_hash["hello"] => "goodbye" |
| 28 | +# puts my_hash["hello"] # => "goodbye" |
26 | 29 | #
|
27 | 30 | # Notice the extra quotes <tt>''</tt> around the hash notation. Ruby expects
|
28 | 31 | # the argument to be a string and can't convert objects like a hash or array.
|
|
37 | 40 | # require 'json'
|
38 | 41 | #
|
39 | 42 | # my_hash = {:hello => "goodbye"}
|
40 |
| -# puts JSON.generate(my_hash) => "{\"hello\":\"goodbye\"}" |
| 43 | +# puts JSON.generate(my_hash) # => "{\"hello\":\"goodbye\"}" |
41 | 44 | #
|
42 | 45 | # Or an alternative way:
|
43 | 46 | #
|
44 | 47 | # require 'json'
|
45 |
| -# puts {:hello => "goodbye"}.to_json => "{\"hello\":\"goodbye\"}" |
| 48 | +# puts({:hello => "goodbye"}.to_json) # => "{\"hello\":\"goodbye\"}" |
| 49 | +# |
| 50 | +# <tt>JSON.generate</tt> only allows objects or arrays to be converted |
| 51 | +# to JSON syntax. <tt>to_json</tt>, however, accepts many Ruby classes |
| 52 | +# even though it acts only as a method for serialization: |
| 53 | +# |
| 54 | +# require 'json' |
46 | 55 | #
|
| 56 | +# 1.to_json # => "1" |
| 57 | +# |
| 58 | +# The {#generate}[rdoc-ref:JSON#generate] method accepts a variety of options |
| 59 | +# to set the formatting of string output and defining what input is accepteable. |
| 60 | +# There are also shortcut methods pretty_generate (with a set of options to |
| 61 | +# generate human-readable multiline JSON) and fast_generate (with a set of |
| 62 | +# options to generate JSON faster at the price of disabling some checks). |
| 63 | +# |
| 64 | +# == Extended rendering and loading of Ruby objects |
| 65 | +# |
| 66 | +# JSON library provides optional _additions_ allowing to serialize and |
| 67 | +# deserialize Ruby classes without loosing their type. |
| 68 | +# |
| 69 | +# # without additions |
| 70 | +# require "json" |
| 71 | +# json = JSON.generate({range: 1..3, regex: /test/}) |
| 72 | +# # => '{"range":"1..3","regex":"(?-mix:test)"}' |
| 73 | +# JSON.parse(json) |
| 74 | +# # => {"range"=>"1..3", "regex"=>"(?-mix:test)"} |
| 75 | +# |
| 76 | +# # with additions |
| 77 | +# require "json/add/range" |
| 78 | +# require "json/add/regexp" |
| 79 | +# json = JSON.generate({range: 1..3, regex: /test/}) |
| 80 | +# # => '{"range":{"json_class":"Range","a":[1,3,false]},"regex":{"json_class":"Regexp","o":0,"s":"test"}}' |
| 81 | +# JSON.parse(json) |
| 82 | +# # => {"range"=>{"json_class"=>"Range", "a"=>[1, 3, false]}, "regex"=>{"json_class"=>"Regexp", "o"=>0, "s"=>"test"}} |
| 83 | +# JSON.load(json) |
| 84 | +# # => {"range"=>1..3, "regex"=>/test/} |
| 85 | +# |
| 86 | +# See JSON.load for details. |
47 | 87 | module JSON
|
48 | 88 | require 'json/version'
|
49 | 89 |
|
|
0 commit comments