~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to ext/json/lib/json.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##
 
2
# = JavaScript Object Notation (JSON)
 
3
#
 
4
# JSON is a lightweight data-interchange format. It is easy for us
 
5
# humans to read and write. Plus, equally simple for machines to generate or parse.
 
6
# JSON is completely language agnostic, making it the ideal interchange format.
 
7
#
 
8
# Built on two universally available structures:
 
9
#   1. A collection of name/value pairs. Often referred to as an _object_, hash table, record, struct, keyed list, or associative array.
 
10
#   2. An ordered list of values. More commonly called an _array_, vector, sequence or list.
 
11
#
 
12
# To read more about JSON visit: http://json.org
 
13
#
 
14
# == Parsing JSON
 
15
#
 
16
# To parse a JSON string received by another application or generated within
 
17
# your existing application:
 
18
#
 
19
#   require 'json'
 
20
#
 
21
#   my_hash = JSON.parse('{"hello": "goodbye"}')
 
22
#   puts my_hash["hello"] => "goodbye"
 
23
#
 
24
# Notice the extra quotes <tt>''</tt> around the hash notation. Ruby expects
 
25
# the argument to be a string and can't convert objects like a hash or array.
 
26
#
 
27
# Ruby converts your string into a hash
 
28
#
 
29
# == Generating JSON
 
30
#
 
31
# Creating a JSON string for communication or serialization is
 
32
# just as simple.
 
33
#
 
34
#   require 'json'
 
35
#
 
36
#   my_hash = {:hello => "goodbye"}
 
37
#   puts JSON.generate(my_hash) => "{\"hello\":\"goodbye\"}"
 
38
#
 
39
# Or an alternative way:
 
40
#
 
41
#   require 'json'
 
42
#   puts {:hello => "goodbye"}.to_json => "{\"hello\":\"goodbye\"}"
 
43
#
 
44
# <tt>JSON.generate</tt> only allows objects or arrays to be converted
 
45
# to JSON syntax. <tt>to_json</tt>, however, accepts many Ruby classes
 
46
# even though it acts only as a method for serialization:
 
47
#
 
48
#   require 'json'
 
49
#
 
50
#   1.to_json => "1"
 
51
#
 
52
 
1
53
require 'json/common'
2
54
module JSON
3
55
  require 'json/version'