json_pure 1.0.0 → 1.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. data/CHANGES +155 -1
  2. data/COPYING +58 -0
  3. data/GPL +7 -7
  4. data/README +324 -45
  5. data/Rakefile +166 -124
  6. data/TODO +1 -1
  7. data/VERSION +1 -1
  8. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkComparison.log +52 -0
  9. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast-autocorrelation.dat +1000 -0
  10. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast.dat +1001 -0
  11. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty-autocorrelation.dat +900 -0
  12. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty.dat +901 -0
  13. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe-autocorrelation.dat +1000 -0
  14. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe.dat +1001 -0
  15. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt.log +261 -0
  16. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast-autocorrelation.dat +1000 -0
  17. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast.dat +1001 -0
  18. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty-autocorrelation.dat +1000 -0
  19. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty.dat +1001 -0
  20. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe-autocorrelation.dat +1000 -0
  21. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe.dat +1001 -0
  22. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure.log +262 -0
  23. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator-autocorrelation.dat +1000 -0
  24. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator.dat +1001 -0
  25. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails.log +82 -0
  26. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkComparison.log +34 -0
  27. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser-autocorrelation.dat +900 -0
  28. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser.dat +901 -0
  29. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt.log +81 -0
  30. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser-autocorrelation.dat +1000 -0
  31. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser.dat +1001 -0
  32. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure.log +82 -0
  33. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser-autocorrelation.dat +1000 -0
  34. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser.dat +1001 -0
  35. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails.log +82 -0
  36. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser-autocorrelation.dat +1000 -0
  37. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser.dat +1001 -0
  38. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML.log +82 -0
  39. data/benchmarks/generator2_benchmark.rb +222 -0
  40. data/benchmarks/generator_benchmark.rb +224 -0
  41. data/benchmarks/ohai.json +1216 -0
  42. data/benchmarks/ohai.ruby +1 -0
  43. data/benchmarks/parser2_benchmark.rb +251 -0
  44. data/benchmarks/parser_benchmark.rb +259 -0
  45. data/bin/edit_json.rb +1 -3
  46. data/bin/prettify_json.rb +75 -0
  47. data/data/index.html +5 -4
  48. data/data/prototype.js +2764 -1095
  49. data/ext/json/ext/generator/extconf.rb +14 -3
  50. data/ext/json/ext/generator/generator.c +1022 -334
  51. data/ext/json/ext/generator/generator.h +197 -0
  52. data/ext/json/ext/parser/extconf.rb +9 -3
  53. data/ext/json/ext/parser/parser.c +961 -577
  54. data/ext/json/ext/parser/parser.h +71 -0
  55. data/ext/json/ext/parser/parser.rl +400 -123
  56. data/install.rb +0 -0
  57. data/lib/json/add/core.rb +148 -0
  58. data/lib/json/add/rails.rb +58 -0
  59. data/lib/json/common.rb +254 -47
  60. data/lib/json/editor.rb +236 -72
  61. data/lib/json/ext.rb +2 -0
  62. data/lib/json/pure/generator.rb +235 -117
  63. data/lib/json/pure/parser.rb +124 -25
  64. data/lib/json/pure.rb +5 -3
  65. data/lib/json/version.rb +1 -1
  66. data/lib/json.rb +2 -197
  67. data/tests/fixtures/fail18.json +1 -0
  68. data/tests/test_json.rb +181 -22
  69. data/tests/test_json_addition.rb +84 -16
  70. data/tests/test_json_encoding.rb +68 -0
  71. data/tests/test_json_fixtures.rb +9 -5
  72. data/tests/test_json_generate.rb +114 -14
  73. data/tests/test_json_rails.rb +144 -0
  74. data/tests/test_json_unicode.rb +35 -14
  75. data/tools/fuzz.rb +13 -7
  76. data/tools/server.rb +0 -1
  77. metadata +156 -122
  78. data/benchmarks/benchmark.txt +0 -133
  79. data/benchmarks/benchmark_generator.rb +0 -44
  80. data/benchmarks/benchmark_parser.rb +0 -22
  81. data/benchmarks/benchmark_rails.rb +0 -26
  82. data/ext/json/ext/generator/Makefile +0 -149
  83. data/ext/json/ext/generator/unicode.c +0 -184
  84. data/ext/json/ext/generator/unicode.h +0 -40
  85. data/ext/json/ext/parser/Makefile +0 -149
  86. data/ext/json/ext/parser/unicode.c +0 -156
  87. data/ext/json/ext/parser/unicode.h +0 -44
  88. data/tests/fixtures/pass18.json +0 -1
  89. data/tests/runner.rb +0 -24
  90. /data/tests/fixtures/{fail15.json → pass15.json} +0 -0
  91. /data/tests/fixtures/{fail16.json → pass16.json} +0 -0
  92. /data/tests/fixtures/{fail17.json → pass17.json} +0 -0
  93. /data/tests/fixtures/{fail26.json → pass26.json} +0 -0
data/CHANGES CHANGED
@@ -1,3 +1,157 @@
1
+ 2010-08-09 (1.4.6)
2
+ * Fixed oversight reported in http://github.com/flori/json/issues/closed#issue/23,
3
+ always create a new object from the state prototype.
4
+ * Made pure and ext api more similar again.
5
+ 2010-08-07 (1.4.5)
6
+ * Manage data structure nesting depth in state object during generation. This
7
+ should reduce problems with to_json method definіtions that only have one
8
+ argument.
9
+ * Some fixes in the state objects and additional tests.
10
+ 2010-08-06 (1.4.4)
11
+ * Fixes build problem for rubinius under OS X, http://github.com/flori/json/issues/closed#issue/25
12
+ * Fixes crashes described in http://github.com/flori/json/issues/closed#issue/21 and
13
+ http://github.com/flori/json/issues/closed#issue/23
14
+ 2010-05-05 (1.4.3)
15
+ * Fixed some test assertions, from Ruby r27587 and r27590, patch by nobu.
16
+ * Fixed issue http://github.com/flori/json/issues/#issue/20 reported by
17
+ electronicwhisper@github. Thx!
18
+ 2010-04-26 (1.4.2)
19
+ * Applied patch from naruse Yui NARUSE <naruse@airemix.com> to make building with
20
+ Microsoft Visual C possible again.
21
+ * Applied patch from devrandom <c1.github@niftybox.net> in order to allow building of
22
+ json_pure if extensiontask is not present.
23
+ * Thanks to Dustin Schneider <dustin@stocktwits.com>, who reported a memory
24
+ leak, which is fixed in this release.
25
+ * Applied 993f261ccb8f911d2ae57e9db48ec7acd0187283 patch from josh@github.
26
+ 2010-04-25 (1.4.1)
27
+ * Fix for a bug reported by Dan DeLeo <dan@kallistec.com>, caused by T_FIXNUM
28
+ being different on 32bit/64bit architectures.
29
+ 2010-04-23 (1.4.0)
30
+ * Major speed improvements and building with simplified
31
+ directory/file-structure.
32
+ * Extension should at least be comapatible with MRI, YARV and Rubinius.
33
+ 2010-04-07 (1.2.4)
34
+ * Triger const_missing callback to make Rails' dynamic class loading work.
35
+ 2010-03-11 (1.2.3)
36
+ * Added a State#[] method which returns an attribute's value in order to
37
+ increase duck type compatibility to Hash.
38
+ 2010-02-27 (1.2.2)
39
+ * Made some changes to make the building of the parser/generator compatible
40
+ to Rubinius.
41
+ 2009-11-25 (1.2.1)
42
+ * Added :symbolize_names option to Parser, which returns symbols instead of
43
+ strings in object names/keys.
44
+ 2009-10-01 (1.2.0)
45
+ * fast_generate now raises an exeception for nan and infinite floats.
46
+ * On Ruby 1.8 json supports parsing of UTF-8, UTF-16BE, UTF-16LE, UTF-32BE,
47
+ and UTF-32LE JSON documents now. Under Ruby 1.9 the M17n conversion
48
+ functions are used to convert from all supported encodings. ASCII-8BIT
49
+ encoded strings are handled like all strings under Ruby 1.8 were.
50
+ * Better documentation
51
+ 2009-08-23 (1.1.9)
52
+ * Added forgotten main doc file extra_rdoc_files.
53
+ 2009-08-23 (1.1.8)
54
+ * Applied a patch by OZAWA Sakuro <sakuro@2238club.org> to make json/pure
55
+ work in environments that don't provide iconv.
56
+ * Applied patch by okkez_ in order to fix Ruby Bug #1768:
57
+ http://redmine.ruby-lang.org/issues/show/1768.
58
+ * Finally got around to avoid the rather paranoid escaping of ?/ characters
59
+ in the generator's output. The parsers aren't affected by this change.
60
+ Thanks to Rich Apodaca <rapodaca@metamolecular.com> for the suggestion.
61
+ 2009-06-29 (1.1.7)
62
+ * Security Fix for JSON::Pure::Parser. A specially designed string could
63
+ cause catastrophic backtracking in one of the parser's regular expressions
64
+ in earlier 1.1.x versions. JSON::Ext::Parser isn't affected by this issue.
65
+ Thanks to Bartosz Blimke <bartosz@new-bamboo.co.uk> for reporting this
66
+ problem.
67
+ * This release also uses a less strict ruby version requirement for the
68
+ creation of the mswin32 native gem.
69
+ 2009-05-10 (1.1.6)
70
+ * No changes. І tested native linux gems in the last release and they don't
71
+ play well with different ruby versions other than the one the gem was built
72
+ with. This release is just to bump the version number in order to skip the
73
+ native gem on rubyforge.
74
+ 2009-05-10 (1.1.5)
75
+ * Started to build gems with rake-compiler gem.
76
+ * Applied patch object/array class patch from Brian Candler
77
+ <B.Candler@pobox.com> and fixes.
78
+ 2009-04-01 (1.1.4)
79
+ * Fixed a bug in the creation of serialized generic rails objects reported by
80
+ Friedrich Graeter <graeter@hydrixos.org>.
81
+ * Deleted tests/runner.rb, we're using testrb instead.
82
+ * Editor supports Infinity in numbers now.
83
+ * Made some changes in order to get the library to compile/run under Ruby
84
+ 1.9.
85
+ * Improved speed of the code path for the fast_generate method in the pure
86
+ variant.
87
+ 2008-07-10 (1.1.3)
88
+ * Wesley Beary <monki@geemus.com> reported a bug in json/add/core's DateTime
89
+ handling: If the nominator and denominator of the offset were divisible by
90
+ each other Ruby's Rational#to_s returns them as an integer not a fraction
91
+ with '/'. This caused a ZeroDivisionError during parsing.
92
+ * Use Date#start and DateTime#start instead of sg method, while
93
+ remaining backwards compatible.
94
+ * Supports ragel >= 6.0 now.
95
+ * Corrected some tests.
96
+ * Some minor changes.
97
+ 2007-11-27 (1.1.2)
98
+ * Remember default dir (last used directory) in editor.
99
+ * JSON::Editor.edit method added, the editor can now receive json texts from
100
+ the clipboard via C-v.
101
+ * Load json texts from an URL pasted via middle button press.
102
+ * Added :create_additions option to Parser. This makes it possible to disable
103
+ the creation of additions by force, in order to treat json texts as data
104
+ while having additions loaded.
105
+ * Jacob Maine <jmaine@blurb.com> reported, that JSON(:foo) outputs a JSON
106
+ object if the rails addition is enabled, which is wrong. It now outputs a
107
+ JSON string "foo" instead, like suggested by Jacob Maine.
108
+ * Discovered a bug in the Ruby Bugs Tracker on rubyforge, that was reported
109
+ by John Evans lgastako@gmail.com. He could produce a crash in the JSON
110
+ generator by returning something other than a String instance from a
111
+ to_json method. I now guard against this by doing a rather crude type
112
+ check, which raises an exception instead of crashing.
113
+ 2007-07-06 (1.1.1)
114
+ * Yui NARUSE <naruse@airemix.com> sent some patches to fix tests for Ruby
115
+ 1.9. I applied them and adapted some of them a bit to run both on 1.8 and
116
+ 1.9.
117
+ * Introduced a JSON.parse! method without depth checking for people who like
118
+ danger.
119
+ * Made generate and pretty_generate methods configurable by an options hash.
120
+ * Added :allow_nan option to parser and generator in order to handle NaN,
121
+ Infinity, and -Infinity correctly - if requested. Floats, which aren't numbers,
122
+ aren't valid JSON according to RFC4627, so by default an exception will be
123
+ raised if any of these symbols are encountered. Thanks to Andrea Censi
124
+ <andrea.censi@dis.uniroma1.it> for his hint about this.
125
+ * Fixed some more tests for Ruby 1.9.
126
+ * Implemented dump/load interface of Marshal as suggested in ruby-core:11405
127
+ by murphy <murphy@rubychan.de>.
128
+ * Implemented the max_nesting feature for generate methods, too.
129
+ * Added some implementations for ruby core's custom objects for
130
+ serialisation/deserialisation purposes.
131
+ 2007-05-21 (1.1.0)
132
+ * Implemented max_nesting feature for parser to avoid stack overflows for
133
+ data from untrusted sources. If you trust the source, you can disable it
134
+ with the option max_nesting => false.
135
+ * Piers Cawley <pdcawley@bofh.org.uk> reported a bug, that not every
136
+ character can be escaped by ?\ as required by RFC4627. There's a
137
+ contradiction between David Crockford's JSON checker test vectors (in
138
+ tests/fixtures) and RFC4627, though. I decided to stick to the RFC, because
139
+ the JSON checker seems to be a bit older than the RFC.
140
+ * Extended license to Ruby License, which includes the GPL.
141
+ * Added keyboard shortcuts, and 'Open location' menu item to edit_json.rb.
142
+ 2007-05-09 (1.0.4)
143
+ * Applied a patch from Yui NARUSE <naruse@airemix.com> to make JSON compile
144
+ under Ruby 1.9. Thank you very much for mailing it to me!
145
+ * Made binary variants of JSON fail early, instead of falling back to the
146
+ pure version. This should avoid overshadowing of eventual problems while
147
+ loading of the binary.
148
+ 2007-03-24 (1.0.3)
149
+ * Improved performance of pure variant a bit.
150
+ * The ext variant of this release supports the mswin32 platform. Ugh!
151
+ 2007-03-24 (1.0.2)
152
+ * Ext Parser didn't parse 0e0 correctly into 0.0: Fixed!
153
+ 2007-03-24 (1.0.1)
154
+ * Forgot some object files in the build dir. I really like that - not!
1
155
  2007-03-24 (1.0.0)
2
156
  * Added C implementations for the JSON generator and a ragel based JSON
3
157
  parser in C.
@@ -12,7 +166,7 @@
12
166
  well.
13
167
  * Added aliases generate and pretty_generate of unparse and pretty_unparse.
14
168
  * Fixed a test case.
15
- * Catch an Іconv::InvalidEncoding exception, that seems to occur on some Sun
169
+ * Catch an Iconv::InvalidEncoding exception, that seems to occur on some Sun
16
170
  boxes with SunOS 5.8, if iconv doesn't support utf16 conversions. This was
17
171
  reported by Andrew R Jackson <andrewj@bcm.tmc.edu>, thanks a bunch!
18
172
  2006-08-25 (0.4.2)
data/COPYING ADDED
@@ -0,0 +1,58 @@
1
+ Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.co.jp>.
2
+ You can redistribute it and/or modify it under either the terms of the GPL
3
+ (see GPL file), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a) place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b) use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c) rename any non-standard executables so the names do not conflict
21
+ with standard executables, which must also be provided.
22
+
23
+ d) make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or executable
26
+ form, provided that you do at least ONE of the following:
27
+
28
+ a) distribute the executables and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b) accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c) give non-standard executables non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d) make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial). But some files in the distribution
42
+ are not written by the author, so that they are not under this terms.
43
+
44
+ They are gc.c(partly), utils.c(partly), regex.[ch], st.[ch] and some
45
+ files under the ./missing directory. See each file for the copying
46
+ condition.
47
+
48
+ 5. The scripts and library files supplied as input to or produced as
49
+ output from the software do not automatically fall under the
50
+ copyright of the software, but belong to whomever generated them,
51
+ and may be sold commercially, and may be aggregated with this
52
+ software.
53
+
54
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
55
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
56
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57
+ PURPOSE.
58
+
data/GPL CHANGED
@@ -1,12 +1,12 @@
1
- GNU GENERAL PUBLIC LICENSE
2
- Version 2, June 1991
1
+ GNU GENERAL PUBLIC LICENSE
2
+ Version 2, June 1991
3
3
 
4
4
  Copyright (C) 1989, 1991 Free Software Foundation, Inc.
5
5
  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
6
6
  Everyone is permitted to copy and distribute verbatim copies
7
7
  of this license document, but changing it is not allowed.
8
8
 
9
- Preamble
9
+ Preamble
10
10
 
11
11
  The licenses for most software are designed to take away your
12
12
  freedom to share and change it. By contrast, the GNU General Public
@@ -56,7 +56,7 @@ patent must be licensed for everyone's free use or not licensed at all.
56
56
  The precise terms and conditions for copying, distribution and
57
57
  modification follow.
58
58
 
59
- GNU GENERAL PUBLIC LICENSE
59
+ GNU GENERAL PUBLIC LICENSE
60
60
  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61
61
 
62
62
  0. This License applies to any program or other work which contains
@@ -255,7 +255,7 @@ make exceptions for this. Our decision will be guided by the two goals
255
255
  of preserving the free status of all derivatives of our free software and
256
256
  of promoting the sharing and reuse of software generally.
257
257
 
258
- NO WARRANTY
258
+ NO WARRANTY
259
259
 
260
260
  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261
261
  FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
@@ -277,9 +277,9 @@ YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277
277
  PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278
278
  POSSIBILITY OF SUCH DAMAGES.
279
279
 
280
- END OF TERMS AND CONDITIONS
280
+ END OF TERMS AND CONDITIONS
281
281
 
282
- How to Apply These Terms to Your New Programs
282
+ How to Apply These Terms to Your New Programs
283
283
 
284
284
  If you develop a new program, and you want it to be of the greatest
285
285
  possible use to the public, the best way to achieve this is to make it
data/README CHANGED
@@ -1,77 +1,356 @@
1
- Dependencies for Building
2
- =========================
3
-
4
- - You need rake to build the extensions and install them.
5
-
6
- You can get it from rubyforge:
7
- http://rubyforge.org/projects/rake
8
-
9
- or just type
10
-
11
- # gem install rake
12
-
13
- for the installation via rubygems.
14
-
15
- - If you want to rebuild the parser.c file or draw nice graphviz images of the
16
- state machines, you need ragel from:
17
- http://www.cs.queensu.ca/~thurston/ragel
18
-
19
- Installation
20
- ============
21
-
22
- It's recommended to use the extension variant of JSON, because it's quite a bit
23
- faster than the pure ruby variant. If you cannot build it on your system, you
24
- can settle for the latter.
1
+ == Description
2
+
3
+ This is a implementation of the JSON specification according to RFC 4627
4
+ http://www.ietf.org/rfc/rfc4627.txt . Starting from version 1.0.0 on there
5
+ will be two variants available:
6
+
7
+ * A pure ruby variant, that relies on the iconv and the stringscan
8
+ extensions, which are both part of the ruby standard library.
9
+ * The quite a bit faster C extension variant, which is in parts implemented
10
+ in C and comes with its own unicode conversion functions and a parser
11
+ generated by the ragel state machine compiler
12
+ http://www.cs.queensu.ca/~thurston/ragel .
13
+
14
+ Both variants of the JSON generator generate UTF-8 character sequences by
15
+ default. If an :ascii_only option with a true value is given, they escape all
16
+ non-ASCII and control characters with \uXXXX escape sequences, and support
17
+ UTF-16 surrogate pairs in order to be able to generate the whole range of
18
+ unicode code points.
19
+
20
+ All strings, that are to be encoded as JSON strings, should be UTF-8 byte
21
+ sequences on the Ruby side. To encode raw binary strings, that aren't UTF-8
22
+ encoded, please use the to_json_raw_object method of String (which produces
23
+ an object, that contains a byte array) and decode the result on the receiving
24
+ endpoint.
25
+
26
+ The JSON parsers can parse UTF-8, UTF-16BE, UTF-16LE, UTF-32BE, and UTF-32LE
27
+ JSON documents under Ruby 1.8. Under Ruby 1.9 they take advantage of Ruby's
28
+ M17n features and can parse all documents which have the correct
29
+ String#encoding set. If a document string has ASCII-8BIT as an encoding the
30
+ parser attempts to figure out which of the UTF encodings from above it is and
31
+ trys to parse it.
32
+
33
+ == Installation
34
+
35
+ It's recommended to use the extension variant of JSON, because it's faster than
36
+ the pure ruby variant. If you cannot build it on your system, you can settle
37
+ for the latter.
25
38
 
26
39
  Just type into the command line as root:
27
40
 
28
- # rake install
41
+ # rake install
29
42
 
30
43
  The above command will build the extensions and install them on your system.
31
44
 
32
- # rake install_pure
45
+ # rake install_pure
33
46
 
34
47
  or
35
48
 
36
- # ruby install.rb
49
+ # ruby install.rb
37
50
 
38
51
  will just install the pure ruby implementation of JSON.
39
52
 
40
53
  If you use Rubygems you can type
41
54
 
42
- # gem install json
55
+ # gem install json
43
56
 
44
57
  instead, to install the newest JSON version.
45
58
 
46
59
  There is also a pure ruby json only variant of the gem, that can be installed
47
60
  with:
48
61
 
49
- # gem install json_pure
62
+ # gem install json_pure
50
63
 
51
- Testing and Examples
52
- ====================
64
+ == Compiling the extensions yourself
53
65
 
54
- To run the tests type:
66
+ If you want to build the extensions yourself you need rake:
55
67
 
56
- $ rake test_ext
68
+ You can get it from rubyforge:
69
+ http://rubyforge.org/projects/rake
57
70
 
58
- This will build the extensions first and then test them.
71
+ or just type
59
72
 
60
- $ rake test_pure
73
+ # gem install rake
61
74
 
62
- This will test the pure ruby extensions.
75
+ for the installation via rubygems.
76
+
77
+ If you want to create the parser.c file from its parser.rl file or draw nice
78
+ graphviz images of the state machines, you need ragel from: http://www.cs.queensu.ca/~thurston/ragel
63
79
 
64
- There is also a small example in tools/server.rb if you want to see, how
65
- receiving a JSON object from a webrick server in your browser with the
66
- javasript prototype library (http://www.prototypejs.org) works.
67
80
 
68
- Author
69
- ======
81
+ == Usage
70
82
 
71
- Florian Frank <flori@ping.de>
83
+ To use JSON you can
84
+ require 'json'
85
+ to load the installed variant (either the extension 'json' or the pure
86
+ variant 'json_pure'). If you have installed the extension variant, you can
87
+ pick either the extension variant or the pure variant by typing
88
+ require 'json/ext'
89
+ or
90
+ require 'json/pure'
72
91
 
73
- License
74
- =======
92
+ Now you can parse a JSON document into a ruby data structure by calling
75
93
 
76
- GNU General Public License (GPL), Version 2
94
+ JSON.parse(document)
77
95
 
96
+ If you want to generate a JSON document from a ruby data structure call
97
+ JSON.generate(data)
98
+
99
+ You can also use the pretty_generate method (which formats the output more
100
+ verbosely and nicely) or fast_generate (which doesn't do any of the security
101
+ checks generate performs, e. g. nesting deepness checks).
102
+
103
+ To create a valid JSON document you have to make sure, that the output is
104
+ embedded in either a JSON array [] or a JSON object {}. The easiest way to do
105
+ this, is by putting your values in a Ruby Array or Hash instance.
106
+
107
+ There are also the JSON and JSON[] methods which use parse on a String or
108
+ generate a JSON document from an array or hash:
109
+
110
+ document = JSON 'test' => 23 # => "{\"test\":23}"
111
+ document = JSON['test'] => 23 # => "{\"test\":23}"
112
+
113
+ and
114
+
115
+ data = JSON '{"test":23}' # => {"test"=>23}
116
+ data = JSON['{"test":23}'] # => {"test"=>23}
117
+
118
+ You can choose to load a set of common additions to ruby core's objects if
119
+ you
120
+ require 'json/add/core'
121
+
122
+ After requiring this you can, e. g., serialise/deserialise Ruby ranges:
123
+
124
+ JSON JSON(1..10) # => 1..10
125
+
126
+ To find out how to add JSON support to other or your own classes, read the
127
+ section "More Examples" below.
128
+
129
+ To get the best compatibility to rails' JSON implementation, you can
130
+ require 'json/add/rails'
131
+
132
+ Both of the additions attempt to require 'json' (like above) first, if it has
133
+ not been required yet.
134
+
135
+ == More Examples
136
+
137
+ To create a JSON document from a ruby data structure, you can call
138
+ JSON.generate like that:
139
+
140
+ json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
141
+ # => "[1,2,{\"a\":3.141},false,true,null,\"4..10\"]"
142
+
143
+ To get back a ruby data structure from a JSON document, you have to call
144
+ JSON.parse on it:
145
+
146
+ JSON.parse json
147
+ # => [1, 2, {"a"=>3.141}, false, true, nil, "4..10"]
148
+
149
+ Note, that the range from the original data structure is a simple
150
+ string now. The reason for this is, that JSON doesn't support ranges
151
+ or arbitrary classes. In this case the json library falls back to call
152
+ Object#to_json, which is the same as #to_s.to_json.
153
+
154
+ It's possible to add JSON support serialization to arbitrary classes by
155
+ simply implementing a more specialized version of the #to_json method, that
156
+ should return a JSON object (a hash converted to JSON with #to_json) like
157
+ this (don't forget the *a for all the arguments):
158
+
159
+ class Range
160
+ def to_json(*a)
161
+ {
162
+ 'json_class' => self.class.name, # = 'Range'
163
+ 'data' => [ first, last, exclude_end? ]
164
+ }.to_json(*a)
165
+ end
166
+ end
167
+
168
+ The hash key 'json_class' is the class, that will be asked to deserialise the
169
+ JSON representation later. In this case it's 'Range', but any namespace of
170
+ the form 'A::B' or '::A::B' will do. All other keys are arbitrary and can be
171
+ used to store the necessary data to configure the object to be deserialised.
172
+
173
+ If a the key 'json_class' is found in a JSON object, the JSON parser checks
174
+ if the given class responds to the json_create class method. If so, it is
175
+ called with the JSON object converted to a Ruby hash. So a range can
176
+ be deserialised by implementing Range.json_create like this:
177
+
178
+ class Range
179
+ def self.json_create(o)
180
+ new(*o['data'])
181
+ end
182
+ end
183
+
184
+ Now it possible to serialise/deserialise ranges as well:
185
+
186
+ json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
187
+ # => "[1,2,{\"a\":3.141},false,true,null,{\"json_class\":\"Range\",\"data\":[4,10,false]}]"
188
+ JSON.parse json
189
+ # => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
190
+
191
+ JSON.generate always creates the shortest possible string representation of a
192
+ ruby data structure in one line. This is good for data storage or network
193
+ protocols, but not so good for humans to read. Fortunately there's also
194
+ JSON.pretty_generate (or JSON.pretty_generate) that creates a more readable
195
+ output:
196
+
197
+ puts JSON.pretty_generate([1, 2, {"a"=>3.141}, false, true, nil, 4..10])
198
+ [
199
+ 1,
200
+ 2,
201
+ {
202
+ "a": 3.141
203
+ },
204
+ false,
205
+ true,
206
+ null,
207
+ {
208
+ "json_class": "Range",
209
+ "data": [
210
+ 4,
211
+ 10,
212
+ false
213
+ ]
214
+ }
215
+ ]
216
+
217
+ There are also the methods Kernel#j for generate, and Kernel#jj for
218
+ pretty_generate output to the console, that work analogous to Core Ruby's p and
219
+ the pp library's pp methods.
220
+
221
+ The script tools/server.rb contains a small example if you want to test, how
222
+ receiving a JSON object from a webrick server in your browser with the
223
+ javasript prototype library http://www.prototypejs.org works.
224
+
225
+ == Speed Comparisons
226
+
227
+ I have created some benchmark results (see the benchmarks/data-p4-3Ghz
228
+ subdir of the package) for the JSON-parser to estimate the speed up in the C
229
+ extension:
230
+
231
+ Comparing times (call_time_mean):
232
+ 1 ParserBenchmarkExt#parser 900 repeats:
233
+ 553.922304770 ( real) -> 21.500x
234
+ 0.001805307
235
+ 2 ParserBenchmarkYAML#parser 1000 repeats:
236
+ 224.513358139 ( real) -> 8.714x
237
+ 0.004454078
238
+ 3 ParserBenchmarkPure#parser 1000 repeats:
239
+ 26.755020642 ( real) -> 1.038x
240
+ 0.037376163
241
+ 4 ParserBenchmarkRails#parser 1000 repeats:
242
+ 25.763381731 ( real) -> 1.000x
243
+ 0.038814780
244
+ calls/sec ( time) -> speed covers
245
+ secs/call
246
+
247
+ In the table above 1 is JSON::Ext::Parser, 2 is YAML.load with YAML
248
+ compatbile JSON document, 3 is is JSON::Pure::Parser, and 4 is
249
+ ActiveSupport::JSON.decode. The ActiveSupport JSON-decoder converts the
250
+ input first to YAML and then uses the YAML-parser, the conversion seems to
251
+ slow it down so much that it is only as fast as the JSON::Pure::Parser!
252
+
253
+ If you look at the benchmark data you can see that this is mostly caused by
254
+ the frequent high outliers - the median of the Rails-parser runs is still
255
+ overall smaller than the median of the JSON::Pure::Parser runs:
256
+
257
+ Comparing times (call_time_median):
258
+ 1 ParserBenchmarkExt#parser 900 repeats:
259
+ 800.592479481 ( real) -> 26.936x
260
+ 0.001249075
261
+ 2 ParserBenchmarkYAML#parser 1000 repeats:
262
+ 271.002390644 ( real) -> 9.118x
263
+ 0.003690004
264
+ 3 ParserBenchmarkRails#parser 1000 repeats:
265
+ 30.227910865 ( real) -> 1.017x
266
+ 0.033082008
267
+ 4 ParserBenchmarkPure#parser 1000 repeats:
268
+ 29.722384421 ( real) -> 1.000x
269
+ 0.033644676
270
+ calls/sec ( time) -> speed covers
271
+ secs/call
272
+
273
+ I have benchmarked the JSON-Generator as well. This generated a few more
274
+ values, because there are different modes that also influence the achieved
275
+ speed:
276
+
277
+ Comparing times (call_time_mean):
278
+ 1 GeneratorBenchmarkExt#generator_fast 1000 repeats:
279
+ 547.354332608 ( real) -> 15.090x
280
+ 0.001826970
281
+ 2 GeneratorBenchmarkExt#generator_safe 1000 repeats:
282
+ 443.968212317 ( real) -> 12.240x
283
+ 0.002252414
284
+ 3 GeneratorBenchmarkExt#generator_pretty 900 repeats:
285
+ 375.104545883 ( real) -> 10.341x
286
+ 0.002665923
287
+ 4 GeneratorBenchmarkPure#generator_fast 1000 repeats:
288
+ 49.978706968 ( real) -> 1.378x
289
+ 0.020008521
290
+ 5 GeneratorBenchmarkRails#generator 1000 repeats:
291
+ 38.531868759 ( real) -> 1.062x
292
+ 0.025952543
293
+ 6 GeneratorBenchmarkPure#generator_safe 1000 repeats:
294
+ 36.927649925 ( real) -> 1.018x 7 (>=3859)
295
+ 0.027079979
296
+ 7 GeneratorBenchmarkPure#generator_pretty 1000 repeats:
297
+ 36.272134441 ( real) -> 1.000x 6 (>=3859)
298
+ 0.027569373
299
+ calls/sec ( time) -> speed covers
300
+ secs/call
301
+
302
+ In the table above 1-3 are JSON::Ext::Generator methods. 4, 6, and 7 are
303
+ JSON::Pure::Generator methods and 5 is the Rails JSON generator. It is now a
304
+ bit faster than the generator_safe and generator_pretty methods of the pure
305
+ variant but slower than the others.
306
+
307
+ To achieve the fastest JSON document output, you can use the fast_generate
308
+ method. Beware, that this will disable the checking for circular Ruby data
309
+ structures, which may cause JSON to go into an infinite loop.
310
+
311
+ Here are the median comparisons for completeness' sake:
312
+
313
+ Comparing times (call_time_median):
314
+ 1 GeneratorBenchmarkExt#generator_fast 1000 repeats:
315
+ 708.258020939 ( real) -> 16.547x
316
+ 0.001411915
317
+ 2 GeneratorBenchmarkExt#generator_safe 1000 repeats:
318
+ 569.105020353 ( real) -> 13.296x
319
+ 0.001757145
320
+ 3 GeneratorBenchmarkExt#generator_pretty 900 repeats:
321
+ 482.825371244 ( real) -> 11.280x
322
+ 0.002071142
323
+ 4 GeneratorBenchmarkPure#generator_fast 1000 repeats:
324
+ 62.717626652 ( real) -> 1.465x
325
+ 0.015944481
326
+ 5 GeneratorBenchmarkRails#generator 1000 repeats:
327
+ 43.965681162 ( real) -> 1.027x
328
+ 0.022745013
329
+ 6 GeneratorBenchmarkPure#generator_safe 1000 repeats:
330
+ 43.929073409 ( real) -> 1.026x 7 (>=3859)
331
+ 0.022763968
332
+ 7 GeneratorBenchmarkPure#generator_pretty 1000 repeats:
333
+ 42.802514491 ( real) -> 1.000x 6 (>=3859)
334
+ 0.023363113
335
+ calls/sec ( time) -> speed covers
336
+ secs/call
337
+
338
+ == Author
339
+
340
+ Florian Frank <mailto:flori@ping.de>
341
+
342
+ == License
343
+
344
+ Ruby License, see the COPYING file included in the source distribution. The
345
+ Ruby License includes the GNU General Public License (GPL), Version 2, so see
346
+ the file GPL as well.
347
+
348
+ == Download
349
+
350
+ The latest version of this library can be downloaded at
351
+
352
+ * http://rubyforge.org/frs?group_id=953
353
+
354
+ Online Documentation should be located at
355
+
356
+ * http://json.rubyforge.org