oj 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +17 -23
  3. data/README.md +74 -425
  4. data/ext/oj/buf.h +103 -0
  5. data/ext/oj/cache8.c +4 -0
  6. data/ext/oj/circarray.c +68 -0
  7. data/ext/oj/circarray.h +23 -0
  8. data/ext/oj/code.c +227 -0
  9. data/ext/oj/code.h +40 -0
  10. data/ext/oj/compat.c +243 -0
  11. data/ext/oj/custom.c +1097 -0
  12. data/ext/oj/dump.c +766 -1534
  13. data/ext/oj/dump.h +92 -0
  14. data/ext/oj/dump_compat.c +937 -0
  15. data/ext/oj/dump_leaf.c +254 -0
  16. data/ext/oj/dump_object.c +810 -0
  17. data/ext/oj/dump_rails.c +329 -0
  18. data/ext/oj/dump_strict.c +416 -0
  19. data/ext/oj/encode.h +51 -0
  20. data/ext/oj/err.c +57 -0
  21. data/ext/oj/err.h +70 -0
  22. data/ext/oj/extconf.rb +17 -7
  23. data/ext/oj/fast.c +213 -180
  24. data/ext/oj/hash.c +163 -0
  25. data/ext/oj/hash.h +46 -0
  26. data/ext/oj/hash_test.c +512 -0
  27. data/ext/oj/mimic_json.c +817 -0
  28. data/ext/oj/mimic_rails.c +806 -0
  29. data/ext/oj/mimic_rails.h +17 -0
  30. data/ext/oj/object.c +752 -0
  31. data/ext/oj/odd.c +230 -0
  32. data/ext/oj/odd.h +44 -0
  33. data/ext/oj/oj.c +1288 -929
  34. data/ext/oj/oj.h +240 -69
  35. data/ext/oj/parse.c +1014 -0
  36. data/ext/oj/parse.h +92 -0
  37. data/ext/oj/reader.c +223 -0
  38. data/ext/oj/reader.h +151 -0
  39. data/ext/oj/resolve.c +127 -0
  40. data/ext/oj/{cache.h → resolve.h} +6 -13
  41. data/ext/oj/rxclass.c +133 -0
  42. data/ext/oj/rxclass.h +27 -0
  43. data/ext/oj/saj.c +77 -175
  44. data/ext/oj/scp.c +224 -0
  45. data/ext/oj/sparse.c +911 -0
  46. data/ext/oj/stream_writer.c +301 -0
  47. data/ext/oj/strict.c +162 -0
  48. data/ext/oj/string_writer.c +480 -0
  49. data/ext/oj/val_stack.c +98 -0
  50. data/ext/oj/val_stack.h +188 -0
  51. data/lib/oj/active_support_helper.rb +41 -0
  52. data/lib/oj/bag.rb +6 -10
  53. data/lib/oj/easy_hash.rb +52 -0
  54. data/lib/oj/json.rb +172 -0
  55. data/lib/oj/mimic.rb +260 -5
  56. data/lib/oj/saj.rb +13 -10
  57. data/lib/oj/schandler.rb +142 -0
  58. data/lib/oj/state.rb +131 -0
  59. data/lib/oj/version.rb +1 -1
  60. data/lib/oj.rb +11 -23
  61. data/pages/Advanced.md +22 -0
  62. data/pages/Compatibility.md +25 -0
  63. data/pages/Custom.md +23 -0
  64. data/pages/Encoding.md +65 -0
  65. data/pages/JsonGem.md +79 -0
  66. data/pages/Modes.md +140 -0
  67. data/pages/Options.md +250 -0
  68. data/pages/Rails.md +60 -0
  69. data/pages/Security.md +20 -0
  70. data/test/_test_active.rb +76 -0
  71. data/test/_test_active_mimic.rb +96 -0
  72. data/test/_test_mimic_rails.rb +126 -0
  73. data/test/activesupport4/decoding_test.rb +105 -0
  74. data/test/activesupport4/encoding_test.rb +531 -0
  75. data/test/activesupport4/test_helper.rb +41 -0
  76. data/test/activesupport5/decoding_test.rb +125 -0
  77. data/test/activesupport5/encoding_test.rb +483 -0
  78. data/test/activesupport5/encoding_test_cases.rb +90 -0
  79. data/test/activesupport5/test_helper.rb +50 -0
  80. data/test/activesupport5/time_zone_test_helpers.rb +24 -0
  81. data/test/helper.rb +27 -0
  82. data/test/isolated/shared.rb +310 -0
  83. data/test/isolated/test_mimic_after.rb +13 -0
  84. data/test/isolated/test_mimic_alone.rb +12 -0
  85. data/test/isolated/test_mimic_as_json.rb +45 -0
  86. data/test/isolated/test_mimic_before.rb +13 -0
  87. data/test/isolated/test_mimic_define.rb +28 -0
  88. data/test/isolated/test_mimic_rails_after.rb +22 -0
  89. data/test/isolated/test_mimic_rails_before.rb +21 -0
  90. data/test/isolated/test_mimic_redefine.rb +15 -0
  91. data/test/json_gem/json_addition_test.rb +216 -0
  92. data/test/json_gem/json_common_interface_test.rb +143 -0
  93. data/test/json_gem/json_encoding_test.rb +109 -0
  94. data/test/json_gem/json_ext_parser_test.rb +20 -0
  95. data/test/json_gem/json_fixtures_test.rb +35 -0
  96. data/test/json_gem/json_generator_test.rb +383 -0
  97. data/test/json_gem/json_generic_object_test.rb +90 -0
  98. data/test/json_gem/json_parser_test.rb +470 -0
  99. data/test/json_gem/json_string_matching_test.rb +42 -0
  100. data/test/json_gem/test_helper.rb +18 -0
  101. data/test/perf_compat.rb +130 -0
  102. data/test/perf_fast.rb +9 -9
  103. data/test/perf_file.rb +64 -0
  104. data/test/{perf_obj.rb → perf_object.rb} +24 -10
  105. data/test/perf_scp.rb +151 -0
  106. data/test/perf_strict.rb +32 -113
  107. data/test/sample.rb +2 -3
  108. data/test/test_compat.rb +474 -0
  109. data/test/test_custom.rb +355 -0
  110. data/test/test_debian.rb +53 -0
  111. data/test/test_fast.rb +66 -16
  112. data/test/test_file.rb +237 -0
  113. data/test/test_gc.rb +49 -0
  114. data/test/test_hash.rb +29 -0
  115. data/test/test_null.rb +376 -0
  116. data/test/test_object.rb +1010 -0
  117. data/test/test_saj.rb +16 -16
  118. data/test/test_scp.rb +417 -0
  119. data/test/test_strict.rb +410 -0
  120. data/test/test_various.rb +815 -0
  121. data/test/test_writer.rb +308 -0
  122. data/test/tests.rb +9 -902
  123. data/test/tests_mimic.rb +14 -0
  124. data/test/tests_mimic_addition.rb +7 -0
  125. metadata +253 -38
  126. data/ext/oj/cache.c +0 -148
  127. data/ext/oj/foo.rb +0 -6
  128. data/ext/oj/load.c +0 -1049
  129. data/test/a.rb +0 -38
  130. data/test/perf1.rb +0 -64
  131. data/test/perf2.rb +0 -76
  132. data/test/perf_obj_old.rb +0 -213
  133. data/test/test_mimic.rb +0 -208
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7b5e26434d67518ead10da0ffb7f97ca0d05a0de
4
+ data.tar.gz: cdd5a57251a116ba7e7f1d16136821a5baa04869
5
+ SHA512:
6
+ metadata.gz: 1a9ab31bbf01cd8794e88a25adcbb989dead17ac17c76ea792d0580211e516f67b9224aaa64e85af62b1ba5a1a230693c6e14415443f33e04f683e2e0cb632f0
7
+ data.tar.gz: 6656a40ec3b1aea8de039062f76f00124c0af96e4679df3975c97e495ced5aa5811f628304ec23c52baa17b18780eee6f2705cd9dedc23e75626e733e3777872
data/LICENSE CHANGED
@@ -1,27 +1,21 @@
1
- Copyright (c) 2012, Peter Ohler
2
- All rights reserved.
1
+ The MIT License (MIT)
3
2
 
4
- Redistribution and use in source and binary forms, with or without
5
- modification, are permitted provided that the following conditions are met:
3
+ Copyright (c) 2012 Peter Ohler
6
4
 
7
- - Redistributions of source code must retain the above copyright notice, this
8
- list of conditions and the following disclaimer.
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
9
11
 
10
- - Redistributions in binary form must reproduce the above copyright notice,
11
- this list of conditions and the following disclaimer in the documentation
12
- and/or other materials provided with the distribution.
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
13
14
 
14
- - Neither the name of Peter Ohler nor the names of its contributors may be
15
- used to endorse or promote products derived from this software without
16
- specific prior written permission.
17
-
18
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22
- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
- OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md CHANGED
@@ -1,445 +1,94 @@
1
- # Oj gem
2
- A fast JSON parser and Object marshaller as a Ruby gem.
1
+ # [![{}j](http://www.ohler.com/dev/images/oj_hel_fire_64.png)](http://www.ohler.com/oj) gem
3
2
 
4
- ## <a name="installation">Installation</a>
5
- gem install oj
3
+ [![Build Status](https://img.shields.io/travis/ohler55/oj/master.svg)](http://travis-ci.org/ohler55/oj?branch=master) ![Gem](https://img.shields.io/gem/v/oj.svg) ![Gem](https://img.shields.io/gem/dt/oj.svg)
6
4
 
7
- ## <a name="documentation">Documentation</a>
5
+ A *fast* JSON parser and Object marshaller as a Ruby gem.
8
6
 
9
- *Documentation*: http://www.ohler.com/oj
7
+ Version 3.0 is out! 3.0 provides better json gem and Rails compatibility. It
8
+ also provides additional optimization options.
10
9
 
11
- ## <a name="source">Source</a>
10
+ Please check out issue #364 and help pick or suggest a image to represent Oj.
12
11
 
13
- *GitHub* *repo*: https://github.com/ohler55/oj
12
+ ## Using
14
13
 
15
- *RubyGems* *repo*: https://rubygems.org/gems/oj
14
+ ```ruby
15
+ require 'oj'
16
16
 
17
- ## <a name="follow">Follow @oxgem on Twitter</a>
17
+ h = { 'one' => 1, 'array' => [ true, false ] }
18
+ json = Oj.dump(h)
18
19
 
19
- [Follow @peterohler on Twitter](http://twitter.com/#!/peterohler) for announcements and news about the Oj gem.
20
+ # json =
21
+ # {
22
+ # "one":1,
23
+ # "array":[
24
+ # true,
25
+ # false
26
+ # ]
27
+ # }
20
28
 
21
- ## <a name="build_status">Build Status</a>
29
+ h2 = Oj.load(json)
30
+ puts "Same? #{h == h2}"
31
+ # true
32
+ ```
22
33
 
23
- [![Build Status](https://secure.travis-ci.org/ohler55/oj.png?branch=master)](http://travis-ci.org/ohler55/oj)
34
+ ## Installation
35
+ ```
36
+ gem install oj
37
+ ```
24
38
 
25
- ## <a name="links">Links of Interest</a>
39
+ or in Bundler:
26
40
 
27
- [Need for Speed](http://www.ohler.com/dev/need_for_speed/need_for_speed.html) for an overview of how Oj::Doc was designed.
41
+ ```
42
+ gem 'oj'
43
+ ```
28
44
 
29
- *Fast XML parser and marshaller on RubyGems*: https://rubygems.org/gems/ox
45
+ ## Further Reading
30
46
 
31
- *Fast XML parser and marshaller on GitHub*: https://github.com/ohler55/ox
47
+ For more details on options, modes, advanced features, and more follow these
48
+ links.
32
49
 
33
- ## <a name="release">Release Notes</a>
50
+ - [{file:Options.md}](pages/Options.md) for parse and dump options.
51
+ - [{file:Modes.md}](pages/Modes.md) for details on modes for strict JSON compliance, mimicing the JSON gem, and mimicing Rails and ActiveSupport behavior.
52
+ - [{file:JsonGem.md}](pages/JsonGem.md) includes more details on json gem compatibility and use.
53
+ - [{file:Rails.md}](pages/Rails.md) includes more details on Rails and ActiveSupport compatibility and use.
54
+ - [{file:Custom.md}](pages/Custom.md) includes more details on Custom mode.
55
+ - [{file:Encoding.md}](pages/Encoding.md) describes the :object encoding format.
56
+ - [{file:Compatibility.md}](pages/Compatibility.md) lists current compatibility with Rubys and Rails.
57
+ - [{file:Advanced.md}](pages/Advanced.md) for fast parser and marshalling features.
58
+ - [{file:Security.md}](pages/Security.md) for security considerations.
34
59
 
35
- ### Release 2.0.0
60
+ ## Releases
36
61
 
37
- - Thanks to yuki24 Floats are now output with a decimal even if they are an integer value.
62
+ See [{file:CHANGELOG.md}](CHANGELOG.md)
38
63
 
39
- - <b>The Simple API for JSON (SAJ) API has been added. Read more about it on the [Oj::Saj page](http://www.ohler.com/oj/Oj/Saj.html).</b>
64
+ ## Links
40
65
 
41
- ## <a name="description">Description</a>
66
+ - *Documentation*: http://www.ohler.com/oj, http://rubydoc.info/gems/oj
42
67
 
43
- Optimized JSON (Oj), as the name implies was written to provide speed
44
- optimized JSON handling. It was designed as a faster alternative to Yajl and
45
- other the common Ruby JSON parsers. So far is has achieved that at about 2
46
- time faster than Yajl for parsing and 3 or more times faster writing JSON.
47
-
48
- Oj has several dump or serialization modes which control how Objects are
49
- converted to JSON. These modes are set with the :mode option in either the
50
- default options or as one of the options to the dump() method.
51
-
52
- - :strict mode will only allow the 7 basic JSON types to be serialized. Any other Object
53
- will raise and Exception.
54
-
55
- - :null mode replaces any Object that is not one of the JSON types is replaced by a JSON null.
56
-
57
- - :object mode will dump any Object as a JSON Object with keys that match the
58
- Ruby Object's variable names without the '@' character. This is the highest
59
- performance mode.
60
-
61
- - :compat mode is is the compatible with other systems. It will serialize any
62
- Object but will check to see if the Object implements a to_hash() or to_json()
63
- method. If either exists that method is used for serializing the Object. The
64
- to_hash() is more flexible and produces more consistent output so it has a
65
- preference over the to_json() method. If neither the to_json() or to_hash()
66
- methods exist then the Oj internal Object variable encoding is used.
67
-
68
- Oj is compatible with Ruby 1.8.7, 1.9.2, 1.9.3, JRuby, RBX, and the latest 2.0dev. Note that JRuby now disables support
69
- for extentions by default and is no longer supporting C extensions. Bugs reported in the JRuby MRI are no longer being
70
- fixed and there is at least one that has caused a test to be commented out for JRuby in the Oj test suite. JRuby can be
71
- build with extensions enabled. Check the documenation for JRuby installs in your environment.
72
-
73
- Oj is also compatible with Rails. Just make sure the Oj gem is installed and
74
- [multi_json](https://github.com/intridea/multi_json) will pick it up and use it.
75
-
76
- Oj offers two alternative APIs for processing JSON. The fastest one is the Oj::Doc API. The Oj::Doc API takes a
77
- completely different approach by opening a JSON document and providing calls to navigate around the JSON while it is
78
- open. With this approach JSON access can be well over 20 times faster than conventional JSON parsing.
79
-
80
- Another API, the Oj::Saj API follows an XML SAX model and walks the JSON document depth first and makes callbacks for
81
- each element. The Oj::Saj API is useful when only portions of the JSON are of interest. Performance up to 20 times
82
- faster than conventional JSON are possible. The API is simple to use but does require a different approach than the
83
- conventional parse followed by access approach used by conventional JSON parsing.
84
-
85
- ## <a name="compare">Comparisons</a>
86
-
87
- ### Fast Oj::Doc parser comparisons
88
-
89
- The fast Oj::Doc parser is compared to the Yajl and JSON::Pure parsers with
90
- strict JSON documents. No object conversions are included, just simple JSON.
91
-
92
- Since the Oj::Doc deviation from the conventional parsers comparisons of not
93
- only parsing but data access is also included. These tests use the
94
- perf_fast.rb test file. The first benchmark is for just parsing. The second is
95
- for doing a get on every leaf value in the JSON data structure. The third
96
- fetchs a value from a specific spot in the document. With Yajl and JSON this
97
- is done with a set of calls to fetch() for each level in the document. For
98
- Oj::Doc a single fetch with a path is used.
99
-
100
- The benchmark results are:
101
-
102
- > perf_fast.rb -g 1 -f
103
- --------------------------------------------------------------------------------
104
- Parse Performance
105
- Oj::Doc.parse 100000 times in 0.164 seconds or 609893.696 parse/sec.
106
- Yajl.parse 100000 times in 3.168 seconds or 31569.902 parse/sec.
107
- JSON::Ext.parse 100000 times in 3.282 seconds or 30464.826 parse/sec.
108
-
109
- Summary:
110
- System time (secs) rate (ops/sec)
111
- --------- ----------- --------------
112
- Oj::Doc 0.164 609893.696
113
- Yajl 3.168 31569.902
114
- JSON::Ext 3.282 30464.826
115
-
116
- Comparison Matrix
117
- (performance factor, 2.0 row is means twice as fast as column)
118
- Oj::Doc Yajl JSON::Ext
119
- --------- --------- --------- ---------
120
- Oj::Doc 1.00 19.32 20.02
121
- Yajl 0.05 1.00 1.04
122
- JSON::Ext 0.05 0.96 1.00
123
-
124
- --------------------------------------------------------------------------------
125
- Parse and get all values Performance
126
- Oj::Doc.parse 100000 times in 0.417 seconds or 240054.540 parse/sec.
127
- Yajl.parse 100000 times in 5.159 seconds or 19384.191 parse/sec.
128
- JSON::Ext.parse 100000 times in 5.269 seconds or 18978.638 parse/sec.
129
-
130
- Summary:
131
- System time (secs) rate (ops/sec)
132
- --------- ----------- --------------
133
- Oj::Doc 0.417 240054.540
134
- Yajl 5.159 19384.191
135
- JSON::Ext 5.269 18978.638
136
-
137
- Comparison Matrix
138
- (performance factor, 2.0 row is means twice as fast as column)
139
- Oj::Doc Yajl JSON::Ext
140
- --------- --------- --------- ---------
141
- Oj::Doc 1.00 12.38 12.65
142
- Yajl 0.08 1.00 1.02
143
- JSON::Ext 0.08 0.98 1.00
144
-
145
- --------------------------------------------------------------------------------
146
- fetch nested Performance
147
- Oj::Doc.fetch 100000 times in 0.094 seconds or 1059995.760 fetch/sec.
148
- Ruby.fetch 100000 times in 0.503 seconds or 198851.434 fetch/sec.
149
-
150
- Summary:
151
- System time (secs) rate (ops/sec)
152
- ------- ----------- --------------
153
- Oj::Doc 0.094 1059995.760
154
- Ruby 0.503 198851.434
155
-
156
- Comparison Matrix
157
- (performance factor, 2.0 row is means twice as fast as column)
158
- Oj::Doc Ruby
159
- ------- ------- -------
160
- Oj::Doc 1.00 5.33
161
- Ruby 0.19 1.00
162
-
163
- What the results mean are that for getting just a few values from a JSON
164
- document Oj::Doc is 20 times faster than any other parser and for accessing
165
- all values it is still over 12 times faster than any other Ruby JSON parser.
166
-
167
- ### Conventional Oj parser comparisons
168
-
169
- The following table shows the difference is speeds between several
170
- serialization packages compared to the more conventional Oj parser. The tests
171
- had to be scaled back due to limitation of some of the gems. I finally gave up
172
- trying to get JSON Pure to serialize without errors with Ruby 1.9.3. It had
173
- internal errors on anything other than a simple JSON structure. The errors
174
- encountered were:
175
-
176
- - MessagePack fails to convert Bignum to JSON
177
-
178
- - JSON Pure fails to serialize any numbers or Objects with the to_json() method
179
-
180
- Options were added to the test/perf_strict.rb test to run the test without
181
- Object encoding and without Bignums.
182
-
183
- None of the packages except Oj were able to serialize Ruby Objects that did
184
- not have a to_json() method or were of the 7 native JSON types.
185
-
186
- A perf_obj.rb file was added for comparing different Object marshalling
187
- packages.
188
-
189
- It is also worth noting that although Oj is slightly behind MessagePack for
190
- parsing, Oj serialization is much faster than MessagePack even though Oj uses
191
- human readable JSON vs the binary MessagePack format.
192
-
193
- Oj supports circular references when in :object mode and when the :circular
194
- flag is true. None of the other gems tested supported circular
195
- references. They failed in the following manners when the input included
196
- circular references.
197
-
198
- - Yajl core dumps Ruby
199
-
200
- - JSON fails and raises an Exception
201
-
202
- - MessagePack fails and raises an Exception
203
-
204
- The benchmark results are:
205
-
206
- with Object and Bignum encoding:
207
-
208
- > perf_strict.rb
209
- --------------------------------------------------------------------------------
210
- Load/Parse Performance
211
- Oj:compat.load 100000 times in 1.481 seconds or 67513.146 load/sec.
212
- Oj.load 100000 times in 1.066 seconds or 93796.400 load/sec.
213
- JSON::Ext.parse 100000 times in 3.023 seconds or 33074.875 parse/sec.
214
- JSON::Pure.parse 100000 times in 18.908 seconds or 5288.799 parse/sec.
215
- Ox.load 100000 times in 1.240 seconds or 80671.900 load/sec.
216
-
217
- Summary:
218
- System time (secs) rate (ops/sec)
219
- ---------- ----------- --------------
220
- Oj 1.066 93796.400
221
- Ox 1.240 80671.900
222
- Oj:compat 1.481 67513.146
223
- JSON::Ext 3.023 33074.875
224
- JSON::Pure 18.908 5288.799
225
-
226
- Comparison Matrix
227
- (performance factor, 2.0 row is means twice as fast as column)
228
- Oj Ox Oj:compat JSON::Ext JSON::Pure
229
- ---------- ---------- ---------- ---------- ---------- ----------
230
- Oj 1.00 1.16 1.39 2.84 17.73
231
- Ox 0.86 1.00 1.19 2.44 15.25
232
- Oj:compat 0.72 0.84 1.00 2.04 12.77
233
- JSON::Ext 0.35 0.41 0.49 1.00 6.25
234
- JSON::Pure 0.06 0.07 0.08 0.16 1.00
235
-
236
-
237
- --------------------------------------------------------------------------------
238
- Dump/Encode/Generate Performance
239
- Oj:compat.dump 100000 times in 0.789 seconds or 126715.249 dump/sec.
240
- Oj.dump 100000 times in 0.457 seconds or 218798.751 dump/sec.
241
- JSON::Ext.generate 100000 times in 4.371 seconds or 22878.630 generate/sec.
242
- Ox.dump 100000 times in 0.501 seconds or 199425.256 dump/sec.
243
-
244
- Summary:
245
- System time (secs) rate (ops/sec)
246
- --------- ----------- --------------
247
- Oj 0.457 218798.751
248
- Ox 0.501 199425.256
249
- Oj:compat 0.789 126715.249
250
- JSON::Ext 4.371 22878.630
251
-
252
- Comparison Matrix
253
- (performance factor, 2.0 row is means twice as fast as column)
254
- Oj Ox Oj:compat JSON::Ext
255
- --------- --------- --------- --------- ---------
256
- Oj 1.00 1.10 1.73 9.56
257
- Ox 0.91 1.00 1.57 8.72
258
- Oj:compat 0.58 0.64 1.00 5.54
259
- JSON::Ext 0.10 0.11 0.18 1.00
260
-
261
-
262
- The following packages were not included for the reason listed
263
- ***** MessagePack: RangeError: bignum too big to convert into `unsigned long long'
264
- ***** Yajl: RuntimeError: Yajl parse and encode did not return the same object as the original.
265
- ***** JSON::Pure: TypeError: wrong argument type JSON::Pure::Generator::State (expected Data)
266
-
267
- without Objects or numbers (for JSON Pure, Yajl, and Messagepack) JSON:
268
-
269
- --------------------------------------------------------------------------------
270
- Load/Parse Performance
271
- Oj:compat.load 100000 times in 0.806 seconds or 124051.164 load/sec.
272
- Oj.load 100000 times in 0.810 seconds or 123384.587 load/sec.
273
- Yajl.parse 100000 times in 1.441 seconds or 69385.996 parse/sec.
274
- JSON::Ext.parse 100000 times in 1.567 seconds or 63797.848 parse/sec.
275
- JSON::Pure.parse 100000 times in 13.500 seconds or 7407.247 parse/sec.
276
- Ox.load 100000 times in 0.954 seconds or 104836.748 load/sec.
277
- MessagePack.unpack 100000 times in 0.651 seconds or 153707.817 unpack/sec.
278
-
279
- Summary:
280
- System time (secs) rate (ops/sec)
281
- ----------- ----------- --------------
282
- MessagePack 0.651 153707.817
283
- Oj:compat 0.806 124051.164
284
- Oj 0.810 123384.587
285
- Ox 0.954 104836.748
286
- Yajl 1.441 69385.996
287
- JSON::Ext 1.567 63797.848
288
- JSON::Pure 13.500 7407.247
289
-
290
- Comparison Matrix
291
- (performance factor, 2.0 row is means twice as fast as column)
292
- MessagePack Oj:compat Oj Ox Yajl JSON::Ext JSON::Pure
293
- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
294
- MessagePack 1.00 1.24 1.25 1.47 2.22 2.41 20.75
295
- Oj:compat 0.81 1.00 1.01 1.18 1.79 1.94 16.75
296
- Oj 0.80 0.99 1.00 1.18 1.78 1.93 16.66
297
- Ox 0.68 0.85 0.85 1.00 1.51 1.64 14.15
298
- Yajl 0.45 0.56 0.56 0.66 1.00 1.09 9.37
299
- JSON::Ext 0.42 0.51 0.52 0.61 0.92 1.00 8.61
300
- JSON::Pure 0.05 0.06 0.06 0.07 0.11 0.12 1.00
301
-
302
-
303
- --------------------------------------------------------------------------------
304
- Dump/Encode/Generate Performance
305
- Oj:compat.dump 100000 times in 0.173 seconds or 578526.262 dump/sec.
306
- Oj.dump 100000 times in 0.179 seconds or 558362.880 dump/sec.
307
- Yajl.encode 100000 times in 0.776 seconds or 128794.279 encode/sec.
308
- JSON::Ext.generate 100000 times in 3.511 seconds or 28483.812 generate/sec.
309
- JSON::Pure.generate 100000 times in 7.389 seconds or 13533.717 generate/sec.
310
- Ox.dump 100000 times in 0.196 seconds or 510589.629 dump/sec.
311
- MessagePack.pack 100000 times in 0.317 seconds or 315307.220 pack/sec.
312
-
313
- Summary:
314
- System time (secs) rate (ops/sec)
315
- ----------- ----------- --------------
316
- Oj:compat 0.173 578526.262
317
- Oj 0.179 558362.880
318
- Ox 0.196 510589.629
319
- MessagePack 0.317 315307.220
320
- Yajl 0.776 128794.279
321
- JSON::Ext 3.511 28483.812
322
- JSON::Pure 7.389 13533.717
323
-
324
- Comparison Matrix
325
- (performance factor, 2.0 row is means twice as fast as column)
326
- Oj:compat Oj Ox MessagePack Yajl JSON::Ext JSON::Pure
327
- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
328
- Oj:compat 1.00 1.04 1.13 1.83 4.49 20.31 42.75
329
- Oj 0.97 1.00 1.09 1.77 4.34 19.60 41.26
330
- Ox 0.88 0.91 1.00 1.62 3.96 17.93 37.73
331
- MessagePack 0.55 0.56 0.62 1.00 2.45 11.07 23.30
332
- Yajl 0.22 0.23 0.25 0.41 1.00 4.52 9.52
333
- JSON::Ext 0.05 0.05 0.06 0.09 0.22 1.00 2.10
334
- JSON::Pure 0.02 0.02 0.03 0.04 0.11 0.48 1.00
335
-
336
- ### Simple JSON Writing and Parsing:
337
-
338
- require 'oj'
339
-
340
- h = { 'one' => 1, 'array' => [ true, false ] }
341
- json = Oj.dump(h)
342
-
343
- # json =
344
- # {
345
- # "one":1,
346
- # "array":[
347
- # true,
348
- # false
349
- # ]
350
- # }
351
-
352
- h2 = Oj.load(json)
353
- puts "Same? #{h == h2}"
354
- # true
355
-
356
- ### Object JSON format:
357
-
358
- In :object mode Oj generates JSON that follows conventions which allow Class
359
- and other information such as Object IDs for circular reference detection. The
360
- formating follows the following rules.
361
-
362
- 1. JSON native types, true, false, nil, String, Hash, Array, and Number are
363
- encoded normally.
364
-
365
- 2. A Symbol is encoded as a JSON string with a preceeding `:` character.
366
-
367
- 3. The `^` character denotes a special key value when in a JSON Object sequence.
368
-
369
- 4. A Ruby String that starts with `:` or the sequence `^i` or `^r` are encoded by
370
- excaping the first character so that it appears as `\u005e` or `\u003a` instead of
371
- `:` or `^`.
372
-
373
- 5. A `"^c"` JSON Object key indicates the value should be converted to a Ruby
374
- class. The sequence `{"^c":"Oj::Bag"}` is read as the Oj::Bag class.
375
-
376
- 6. A `"^t"` JSON Object key indicates the value should be converted to a Ruby
377
- Time. The sequence `{"^t":1325775487.000000}` is read as Jan 5, 2012 at
378
- 23:58:07.
379
-
380
- 7. A `"^o"` JSON Object key indicates the value should be converted to a Ruby
381
- Object. The first entry in the JSON Object must be a class with the `"^o"`
382
- key. After that each entry is treated as a variable of the Object where the
383
- key is the variable name without the preceeding `@`. An example is
384
- `{"^o":"Oj::Bag","x":58,"y":"marbles"}`. `"^O"` is the same except that it is
385
- for built in or odd classes that don't obey the normal Ruby rules. Examples
386
- are Rational, Date, and DateTime.
387
-
388
- 8. A `"^u"` JSON Object key indicates the value should be converted to a Ruby
389
- Struct. The first entry in the JSON Object must be a class with the `"^u"`
390
- key. After that each entry is is given a numeric position in the struct and
391
- that is used as the key in the JSON Object. An example is `{"^u":["Range",1,7,false]}`.
392
-
393
- 9. When encoding an Object, if the variable name does not begin with an `@`
394
- character then the name preceeded by a `~` character. This occurs in the
395
- Exception class. An example is `{"^o":"StandardError","~mesg":"A Message","~bt":[".\/tests.rb:345:in `test_exception'"]}`.
396
-
397
- 10. If a Hash entry has a key that is not a String or Symbol then the entry is
398
- encoded with a key of the form `"^#n"` where n is a hex number. The value that
399
- is an Array where the first element is the key in the Hash and the second is
400
- the value. An example is `{"^#3":[2,5]}`.
401
-
402
- 11. A `"^i"` JSON entry in either an Object or Array is the ID of the Ruby
403
- Object being encoded. It is used when the :circular flag is set. It can appear
404
- in either a JSON Object or in a JSON Array. In an Object the `"^i"` key has a
405
- corresponding reference Fixnum. In an array the sequence will include an
406
- embedded reference number. An example is
407
- `{"^o":"Oj::Bag","^i":1,"x":["^i2",true],"me":"^r1"}`.
408
-
409
- 12. A `"^r"` JSON entry in an Object is a references to a Object or Array that
410
- already appears in the JSON String. It must match up with a previous `"^i"`
411
- ID. An example is `{"^o":"Oj::Bag","^i":1,"x":3,"me":"^r1"}`.
412
-
413
- 13. If an Array element is a String and starts with `"^i"` then the first
414
- character, the `^` is encoded as a hex character sequence. An example is
415
- `["\u005ei37",3]`.
416
-
417
- ### License:
418
-
419
- Copyright (c) 2012, Peter Ohler
420
- All rights reserved.
421
-
422
- Redistribution and use in source and binary forms, with or without
423
- modification, are permitted provided that the following conditions are met:
424
-
425
- - Redistributions of source code must retain the above copyright notice, this
426
- list of conditions and the following disclaimer.
427
-
428
- - Redistributions in binary form must reproduce the above copyright notice,
429
- this list of conditions and the following disclaimer in the documentation
430
- and/or other materials provided with the distribution.
431
-
432
- - Neither the name of Peter Ohler nor the names of its contributors may be
433
- used to endorse or promote products derived from this software without
434
- specific prior written permission.
435
-
436
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
437
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
438
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
439
- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
440
- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
441
- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
442
- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
443
- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
444
- OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
445
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68
+ - *GitHub* *repo*: https://github.com/ohler55/oj
69
+
70
+ - *RubyGems* *repo*: https://rubygems.org/gems/oj
71
+
72
+ Follow [@peterohler on Twitter](http://twitter.com/#!/peterohler) for announcements and news about the Oj gem.
73
+
74
+ #### Performance Comparisons
75
+
76
+ - [Oj Strict Mode Performance](http://www.ohler.com/dev/oj_misc/performance_strict.html) compares Oj strict mode parser performance to other JSON parsers.
77
+
78
+ - [Oj Compat Mode Performance](http://www.ohler.com/dev/oj_misc/performance_compat.html) compares Oj compat mode parser performance to other JSON parsers.
79
+
80
+ - [Oj Object Mode Performance](http://www.ohler.com/dev/oj_misc/performance_object.html) compares Oj object mode parser performance to other marshallers.
81
+
82
+ - [Oj Callback Performance](http://www.ohler.com/dev/oj_misc/performance_callback.html) compares Oj callback parser performance to other JSON parsers.
83
+
84
+ #### Links of Interest
85
+
86
+ - *Fast XML parser and marshaller on RubyGems*: https://rubygems.org/gems/ox
87
+
88
+ - *Fast XML parser and marshaller on GitHub*: https://github.com/ohler55/ox
89
+
90
+ - [Need for Speed](http://www.ohler.com/dev/need_for_speed/need_for_speed.html) for an overview of how Oj::Doc was designed.
91
+
92
+ - *OjC, a C JSON parser*: https://www.ohler.com/ojc also at https://github.com/ohler55/ojc
93
+
94
+ - *Piper Push Cache, push JSON to browsers*: http://www.piperpushcache.com