summaryrefslogtreecommitdiff
path: root/doc/files/lib/json_rb.html
blob: 42c6e28de21280907680aca1d372288163251af1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>json.rb</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/github.css" type="text/css" media="screen" />
<script src="../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
        <h1>
            json.rb
        </h1>
        <ul class="files">
            <li>lib/json.rb</li>
            <li>Last modified: 2012-04-27 14:25:32 +0200</li>
        </ul>
    </div>

    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<h1 id="label-JavaScript+Object+Notation+%28JSON%29">JavaScript Object Notation (<a href="../../classes/JSON.html">JSON</a>)</h1>

<p><a href="../../classes/JSON.html">JSON</a> is a lightweight
data-interchange format. It is easy for us humans to read and write. Plus,
equally simple for machines to generate or parse. <a
href="../../classes/JSON.html">JSON</a> is completely language agnostic,
making it the ideal interchange format.</p>

<p>Built on two universally available structures:</p>

<pre>1. A collection of name/value pairs. Often referred to as an _object_, hash table, record, struct, keyed list, or associative array.
2. An ordered list of values. More commonly called an _array_, vector, sequence or list.</pre>

<p>To read more about <a href="../../classes/JSON.html">JSON</a> visit: <a
href="http://json.org">json.org</a></p>

<h2 id="label-Parsing+JSON">Parsing <a href="../../classes/JSON.html">JSON</a></h2>

<p>To parse a <a href="../../classes/JSON.html">JSON</a> string received by
another application or generated within your existing application:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'json'</span>

<span class="ruby-identifier">my_hash</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-string">'{&quot;hello&quot;: &quot;goodbye&quot;}'</span>)
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">my_hash</span>[<span class="ruby-string">&quot;hello&quot;</span>] =<span class="ruby-operator">&gt;</span> <span class="ruby-string">&quot;goodbye&quot;</span>
</pre>

<p>Notice the extra quotes <code>''</code> around the hash notation. Ruby
expects the argument to be a string and can’t convert objects like a hash
or array.</p>

<p>Ruby converts your string into a hash</p>

<h2 id="label-Generating+JSON">Generating <a href="../../classes/JSON.html">JSON</a></h2>

<p>Creating a <a href="../../classes/JSON.html">JSON</a> string for
communication or serialization is just as simple.</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'json'</span>

<span class="ruby-identifier">my_hash</span> = {:<span class="ruby-identifier">hello</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">&quot;goodbye&quot;</span>}
<span class="ruby-identifier">puts</span> <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">my_hash</span>) =<span class="ruby-operator">&gt;</span> <span class="ruby-string">&quot;{\&quot;hello\&quot;:\&quot;goodbye\&quot;}&quot;</span>
</pre>

<p>Or an alternative way:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'json'</span>
<span class="ruby-identifier">puts</span> {:<span class="ruby-identifier">hello</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">&quot;goodbye&quot;</span>}.<span class="ruby-identifier">to_json</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">&quot;{\&quot;hello\&quot;:\&quot;goodbye\&quot;}&quot;</span>
</pre>

<p><code>JSON.generate</code> only allows objects or arrays to be converted to
<a href="../../classes/JSON.html">JSON</a> syntax. <code>to_json</code>,
however, accepts many Ruby classes even though it acts only as a method for
serialization:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'json'</span>

<span class="ruby-value">1</span>.<span class="ruby-identifier">to_json</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">&quot;1&quot;</span>
</pre>

    </div>
  


  
    <!-- File only: requires -->
    <div class="sectiontitle">Required Files</div>
    <ul>
      
        <li>json/common</li>
      
        <li>json/version</li>
      
        <li>json/ext</li>
      
        <li>json/pure</li>
      
    </ul>
  


  
  


  
    <!-- Namespace -->
    <div class="sectiontitle">Namespace</div>
    <ul>
      
        <li>
          <span class="type">MODULE</span>
          <a href="../../classes/JSON.html">JSON</a>
        </li>
      
    </ul>
  


  

  



  

    

    

    


    


    <!-- Methods -->
              </div>
    </div>
  </body>
</html>