1- = JSON implementation for Ruby {<img src="https://secure.travis-ci.org/flori/json.png" />}[http://travis-ci.org/flori/json]
1+ # JSON implementation for Ruby {<img src =" https://secure.travis-ci.org/flori/json.png " />}[ http://travis-ci.org/flori/json ]
22
3- == Description
3+ ## Description
44
55This is a implementation of the JSON specification according to RFC 4627
66http://www.ietf.org/rfc/rfc4627.txt . Starting from version 1.0.0 on there
@@ -14,14 +14,14 @@ will be two variants available:
1414 http://www.complang.org/ragel/ .
1515
1616Both variants of the JSON generator generate UTF-8 character sequences by
17- default. If an :ascii_only option with a true value is given, they escape all
17+ default. If an : ascii \_ only option with a true value is given, they escape all
1818non-ASCII and control characters with \uXXXX escape sequences, and support
1919UTF-16 surrogate pairs in order to be able to generate the whole range of
2020unicode code points.
2121
2222All strings, that are to be encoded as JSON strings, should be UTF-8 byte
2323sequences on the Ruby side. To encode raw binary strings, that aren't UTF-8
24- encoded, please use the to_json_raw_object method of String (which produces
24+ encoded, please use the to \_ json \_ raw \_ object method of String (which produces
2525an object, that contains a byte array) and decode the result on the receiving
2626endpoint.
2727
@@ -32,7 +32,7 @@ String#encoding set. If a document string has ASCII-8BIT as an encoding the
3232parser attempts to figure out which of the UTF encodings from above it is and
3333trys to parse it.
3434
35- == Installation
35+ ## Installation
3636
3737It's recommended to use the extension variant of JSON, because it's faster than
3838the pure ruby variant. If you cannot build it on your system, you can settle
@@ -63,18 +63,18 @@ with:
6363
6464 # gem install json_pure
6565
66- == Compiling the extensions yourself
66+ ## Compiling the extensions yourself
6767
6868If you want to create the parser.c file from its parser.rl file or draw nice
6969graphviz images of the state machines, you need ragel from:
7070http://www.complang.org/ragel/
7171
72- == Usage
72+ ## Usage
7373
7474To use JSON you can
7575 require 'json'
7676to load the installed variant (either the extension 'json' or the pure
77- variant 'json_pure '). If you have installed the extension variant, you can
77+ variant 'json \_ pure '). If you have installed the extension variant, you can
7878pick either the extension variant or the pure variant by typing
7979 require 'json/ext'
8080or
@@ -87,8 +87,8 @@ Now you can parse a JSON document into a ruby data structure by calling
8787If you want to generate a JSON document from a ruby data structure call
8888 JSON.generate(data)
8989
90- You can also use the pretty_generate method (which formats the output more
91- verbosely and nicely) or fast_generate (which doesn't do any of the security
90+ You can also use the pretty \_ generate method (which formats the output more
91+ verbosely and nicely) or fast \_ generate (which doesn't do any of the security
9292checks generate performs, e. g. nesting deepness checks).
9393
9494To create a valid JSON document you have to make sure, that the output is
@@ -123,7 +123,7 @@ To get the best compatibility to rails' JSON implementation, you can
123123Both of the additions attempt to require 'json' (like above) first, if it has
124124not been required yet.
125125
126- == More Examples
126+ ## More Examples
127127
128128To create a JSON document from a ruby data structure, you can call
129129JSON.generate like that:
@@ -140,11 +140,11 @@ JSON.parse on it:
140140Note, that the range from the original data structure is a simple
141141string now. The reason for this is, that JSON doesn't support ranges
142142or arbitrary classes. In this case the json library falls back to call
143- Object#to_json , which is the same as #to_s.to_json .
143+ Object#to \_ json , which is the same as #to \_ s.to \_ json .
144144
145145It's possible to add JSON support serialization to arbitrary classes by
146- simply implementing a more specialized version of the #to_json method, that
147- should return a JSON object (a hash converted to JSON with #to_json ) like
146+ simply implementing a more specialized version of the #to \_ json method, that
147+ should return a JSON object (a hash converted to JSON with #to \_ json ) like
148148this (don't forget the * a for all the arguments):
149149
150150 class Range
@@ -156,15 +156,15 @@ this (don't forget the *a for all the arguments):
156156 end
157157 end
158158
159- The hash key 'json_class ' is the class, that will be asked to deserialise the
159+ The hash key 'json \_ class ' is the class, that will be asked to deserialise the
160160JSON representation later. In this case it's 'Range', but any namespace of
161161the form 'A::B' or '::A::B' will do. All other keys are arbitrary and can be
162162used to store the necessary data to configure the object to be deserialised.
163163
164- If a the key 'json_class ' is found in a JSON object, the JSON parser checks
165- if the given class responds to the json_create class method. If so, it is
164+ If a the key 'json \_ class ' is found in a JSON object, the JSON parser checks
165+ if the given class responds to the json \_ create class method. If so, it is
166166called with the JSON object converted to a Ruby hash. So a range can
167- be deserialised by implementing Range.json_create like this:
167+ be deserialised by implementing Range.json \_ create like this:
168168
169169 class Range
170170 def self.json_create(o)
@@ -182,7 +182,7 @@ Now it possible to serialise/deserialise ranges as well:
182182JSON.generate always creates the shortest possible string representation of a
183183ruby data structure in one line. This is good for data storage or network
184184protocols, but not so good for humans to read. Fortunately there's also
185- JSON.pretty_generate (or JSON.pretty_generate ) that creates a more readable
185+ JSON.pretty \_ generate (or JSON.pretty \_ generate ) that creates a more readable
186186output:
187187
188188 puts JSON.pretty_generate([ 1, 2, {"a"=>3.141}, false, true, nil, 4..10] )
@@ -206,14 +206,14 @@ output:
206206 ]
207207
208208There are also the methods Kernel#j for generate, and Kernel#jj for
209- pretty_generate output to the console, that work analogous to Core Ruby's p and
209+ pretty \_ generate output to the console, that work analogous to Core Ruby's p and
210210the pp library's pp methods.
211211
212212The script tools/server.rb contains a small example if you want to test, how
213213receiving a JSON object from a webrick server in your browser with the
214214javasript prototype library http://www.prototypejs.org works.
215215
216- == Speed Comparisons
216+ ## Speed Comparisons
217217
218218I have created some benchmark results (see the benchmarks/data-p4-3Ghz
219219subdir of the package) for the JSON-parser to estimate the speed up in the C
@@ -292,10 +292,10 @@ speed:
292292
293293In the table above 1-3 are JSON::Ext::Generator methods. 4, 6, and 7 are
294294JSON::Pure::Generator methods and 5 is the Rails JSON generator. It is now a
295- bit faster than the generator_safe and generator_pretty methods of the pure
295+ bit faster than the generator \_ safe and generator \_ pretty methods of the pure
296296variant but slower than the others.
297297
298- To achieve the fastest JSON document output, you can use the fast_generate
298+ To achieve the fastest JSON document output, you can use the fast \_ generate
299299method. Beware, that this will disable the checking for circular Ruby data
300300structures, which may cause JSON to go into an infinite loop.
301301
@@ -326,15 +326,15 @@ Here are the median comparisons for completeness' sake:
326326 calls/sec ( time) -> speed covers
327327 secs/call
328328
329- == Author
329+ ## Author
330330
331331Florian Frank
< mailto:[email protected] > 332332
333- == License
333+ ## License
334334
335335Ruby License, see https://www.ruby-lang.org/en/about/license.txt .
336336
337- == Download
337+ ## Download
338338
339339The latest version of this library can be downloaded at
340340
0 commit comments