json 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of json might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +23 -0
- data/CHANGES.md +391 -0
- data/Gemfile +14 -0
- data/README-json-jruby.md +33 -0
- data/README.md +409 -0
- data/Rakefile +408 -0
- data/VERSION +1 -0
- data/diagrams/.keep +0 -0
- data/ext/json/ext/fbuffer/fbuffer.h +187 -0
- data/ext/json/ext/generator/depend +1 -0
- data/ext/json/ext/generator/extconf.rb +4 -0
- data/ext/json/ext/generator/generator.c +1444 -0
- data/ext/json/ext/generator/generator.h +171 -0
- data/ext/json/ext/parser/depend +1 -0
- data/ext/json/ext/parser/extconf.rb +6 -0
- data/ext/json/ext/parser/parser.c +2131 -0
- data/ext/json/ext/parser/parser.h +91 -0
- data/ext/json/ext/parser/parser.rl +891 -0
- data/ext/json/extconf.rb +2 -0
- data/install.rb +23 -0
- data/java/src/json/ext/ByteListTranscoder.java +166 -0
- data/java/src/json/ext/Generator.java +443 -0
- data/java/src/json/ext/GeneratorMethods.java +231 -0
- data/java/src/json/ext/GeneratorService.java +42 -0
- data/java/src/json/ext/GeneratorState.java +490 -0
- data/java/src/json/ext/OptionsReader.java +113 -0
- data/java/src/json/ext/Parser.java +2362 -0
- data/java/src/json/ext/Parser.rl +893 -0
- data/java/src/json/ext/ParserService.java +34 -0
- data/java/src/json/ext/RuntimeInfo.java +116 -0
- data/java/src/json/ext/StringDecoder.java +166 -0
- data/java/src/json/ext/StringEncoder.java +111 -0
- data/java/src/json/ext/Utils.java +88 -0
- data/json-java.gemspec +38 -0
- data/json.gemspec +0 -0
- data/json_pure.gemspec +38 -0
- data/lib/json.rb +63 -0
- data/lib/json/add/bigdecimal.rb +29 -0
- data/lib/json/add/complex.rb +29 -0
- data/lib/json/add/core.rb +12 -0
- data/lib/json/add/date.rb +34 -0
- data/lib/json/add/date_time.rb +50 -0
- data/lib/json/add/exception.rb +31 -0
- data/lib/json/add/ostruct.rb +31 -0
- data/lib/json/add/range.rb +29 -0
- data/lib/json/add/rational.rb +28 -0
- data/lib/json/add/regexp.rb +30 -0
- data/lib/json/add/set.rb +29 -0
- data/lib/json/add/struct.rb +30 -0
- data/lib/json/add/symbol.rb +25 -0
- data/lib/json/add/time.rb +38 -0
- data/lib/json/common.rb +456 -0
- data/lib/json/ext.rb +15 -0
- data/lib/json/ext/.keep +0 -0
- data/lib/json/generic_object.rb +71 -0
- data/lib/json/pure.rb +15 -0
- data/lib/json/pure/generator.rb +458 -0
- data/lib/json/pure/parser.rb +311 -0
- data/lib/json/version.rb +9 -0
- data/references/rfc7159.txt +899 -0
- data/tests/fixtures/fail10.json +1 -0
- data/tests/fixtures/fail11.json +1 -0
- data/tests/fixtures/fail12.json +1 -0
- data/tests/fixtures/fail13.json +1 -0
- data/tests/fixtures/fail14.json +1 -0
- data/tests/fixtures/fail18.json +1 -0
- data/tests/fixtures/fail19.json +1 -0
- data/tests/fixtures/fail2.json +1 -0
- data/tests/fixtures/fail20.json +1 -0
- data/tests/fixtures/fail21.json +1 -0
- data/tests/fixtures/fail22.json +1 -0
- data/tests/fixtures/fail23.json +1 -0
- data/tests/fixtures/fail24.json +1 -0
- data/tests/fixtures/fail25.json +1 -0
- data/tests/fixtures/fail27.json +2 -0
- data/tests/fixtures/fail28.json +2 -0
- data/tests/fixtures/fail3.json +1 -0
- data/tests/fixtures/fail4.json +1 -0
- data/tests/fixtures/fail5.json +1 -0
- data/tests/fixtures/fail6.json +1 -0
- data/tests/fixtures/fail7.json +1 -0
- data/tests/fixtures/fail8.json +1 -0
- data/tests/fixtures/fail9.json +1 -0
- data/tests/fixtures/obsolete_fail1.json +1 -0
- data/tests/fixtures/pass1.json +56 -0
- data/tests/fixtures/pass15.json +1 -0
- data/tests/fixtures/pass16.json +1 -0
- data/tests/fixtures/pass17.json +1 -0
- data/tests/fixtures/pass2.json +1 -0
- data/tests/fixtures/pass26.json +1 -0
- data/tests/fixtures/pass3.json +6 -0
- data/tests/json_addition_test.rb +203 -0
- data/tests/json_common_interface_test.rb +126 -0
- data/tests/json_encoding_test.rb +107 -0
- data/tests/json_ext_parser_test.rb +15 -0
- data/tests/json_fixtures_test.rb +32 -0
- data/tests/json_generator_test.rb +377 -0
- data/tests/json_generic_object_test.rb +82 -0
- data/tests/json_parser_test.rb +472 -0
- data/tests/json_string_matching_test.rb +38 -0
- data/tests/test_helper.rb +17 -0
- data/tools/diff.sh +18 -0
- data/tools/fuzz.rb +131 -0
- data/tools/server.rb +62 -0
- metadata +185 -0
@@ -0,0 +1,33 @@
|
|
1
|
+
JSON-JRuby
|
2
|
+
==========
|
3
|
+
|
4
|
+
JSON-JRuby is a port of Florian Frank's native
|
5
|
+
[`json` library](http://json.rubyforge.org/) to JRuby.
|
6
|
+
It aims to be a perfect drop-in replacement for `json_pure`.
|
7
|
+
|
8
|
+
|
9
|
+
Development version
|
10
|
+
===================
|
11
|
+
|
12
|
+
The latest version is available from the
|
13
|
+
[Git repository](http://github.com/mernen/json-jruby/tree):
|
14
|
+
|
15
|
+
git clone git://github.com/mernen/json-jruby.git
|
16
|
+
|
17
|
+
|
18
|
+
Compiling
|
19
|
+
=========
|
20
|
+
|
21
|
+
You'll need JRuby version 1.2 or greater to build JSON-JRuby.
|
22
|
+
Its path must be set on the `jruby.dir` property of
|
23
|
+
`nbproject/project.properties` (defaults to `../jruby`).
|
24
|
+
|
25
|
+
Additionally, you'll need [Ant](http://ant.apache.org/), and
|
26
|
+
[Ragel](http://www.cs.queensu.ca/~thurston/ragel/) 6.4 or greater.
|
27
|
+
|
28
|
+
Then, from the folder where the sources are located, type:
|
29
|
+
|
30
|
+
ant clean jar
|
31
|
+
|
32
|
+
to clean any leftovers from previous builds and generate the `.jar` files.
|
33
|
+
To generate a RubyGem, specify the `gem` action rather than `jar`.
|
data/README.md
ADDED
@@ -0,0 +1,409 @@
|
|
1
|
+
# JSON implementation for Ruby
|
2
|
+
|
3
|
+
[![Travis Widget](http://travis-ci.org/flori/json.svg?branch=master)](https://travis-ci.org/flori/json)
|
4
|
+
|
5
|
+
## Description
|
6
|
+
|
7
|
+
This is a implementation of the JSON specification according to RFC 7159
|
8
|
+
http://www.ietf.org/rfc/rfc7159.txt . Starting from version 1.0.0 on there
|
9
|
+
will be two variants available:
|
10
|
+
|
11
|
+
* A pure ruby variant, that relies on the iconv and the stringscan
|
12
|
+
extensions, which are both part of the ruby standard library.
|
13
|
+
* The quite a bit faster native extension variant, which is in parts
|
14
|
+
implemented in C or Java and comes with its own unicode conversion
|
15
|
+
functions and a parser generated by the ragel state machine compiler
|
16
|
+
http://www.complang.org/ragel/ .
|
17
|
+
|
18
|
+
Both variants of the JSON generator generate UTF-8 character sequences by
|
19
|
+
default. If an :ascii\_only option with a true value is given, they escape all
|
20
|
+
non-ASCII and control characters with \uXXXX escape sequences, and support
|
21
|
+
UTF-16 surrogate pairs in order to be able to generate the whole range of
|
22
|
+
unicode code points.
|
23
|
+
|
24
|
+
All strings, that are to be encoded as JSON strings, should be UTF-8 byte
|
25
|
+
sequences on the Ruby side. To encode raw binary strings, that aren't UTF-8
|
26
|
+
encoded, please use the to\_json\_raw\_object method of String (which produces
|
27
|
+
an object, that contains a byte array) and decode the result on the receiving
|
28
|
+
endpoint.
|
29
|
+
|
30
|
+
## Installation
|
31
|
+
|
32
|
+
It's recommended to use the extension variant of JSON, because it's faster than
|
33
|
+
the pure ruby variant. If you cannot build it on your system, you can settle
|
34
|
+
for the latter.
|
35
|
+
|
36
|
+
Just type into the command line as root:
|
37
|
+
|
38
|
+
```
|
39
|
+
# rake install
|
40
|
+
```
|
41
|
+
|
42
|
+
The above command will build the extensions and install them on your system.
|
43
|
+
|
44
|
+
```
|
45
|
+
# rake install_pure
|
46
|
+
```
|
47
|
+
|
48
|
+
or
|
49
|
+
|
50
|
+
```
|
51
|
+
# ruby install.rb
|
52
|
+
```
|
53
|
+
|
54
|
+
will just install the pure ruby implementation of JSON.
|
55
|
+
|
56
|
+
If you use Rubygems you can type
|
57
|
+
|
58
|
+
```
|
59
|
+
# gem install json
|
60
|
+
```
|
61
|
+
|
62
|
+
instead, to install the newest JSON version.
|
63
|
+
|
64
|
+
There is also a pure ruby json only variant of the gem, that can be installed
|
65
|
+
with:
|
66
|
+
|
67
|
+
```
|
68
|
+
# gem install json_pure
|
69
|
+
```
|
70
|
+
|
71
|
+
## Compiling the extensions yourself
|
72
|
+
|
73
|
+
If you want to create the `parser.c` file from its `parser.rl` file or draw nice
|
74
|
+
graphviz images of the state machines, you need ragel from:
|
75
|
+
http://www.complang.org/ragel/
|
76
|
+
|
77
|
+
## Usage
|
78
|
+
|
79
|
+
To use JSON you can
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
require 'json'
|
83
|
+
```
|
84
|
+
|
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
|
+
|
89
|
+
```ruby
|
90
|
+
require 'json/ext'
|
91
|
+
```
|
92
|
+
|
93
|
+
or
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
require 'json/pure'
|
97
|
+
```
|
98
|
+
|
99
|
+
Now you can parse a JSON document into a ruby data structure by calling
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
JSON.parse(document)
|
103
|
+
```
|
104
|
+
|
105
|
+
If you want to generate a JSON document from a ruby data structure call
|
106
|
+
```ruby
|
107
|
+
JSON.generate(data)
|
108
|
+
```
|
109
|
+
|
110
|
+
You can also use the `pretty_generate` method (which formats the output more
|
111
|
+
verbosely and nicely) or `fast_generate` (which doesn't do any of the security
|
112
|
+
checks generate performs, e. g. nesting deepness checks).
|
113
|
+
|
114
|
+
There are also the JSON and JSON[] methods which use parse on a String or
|
115
|
+
generate a JSON document from an array or hash:
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
document = JSON 'test' => 23 # => "{\"test\":23}"
|
119
|
+
document = JSON['test' => 23] # => "{\"test\":23}"
|
120
|
+
```
|
121
|
+
|
122
|
+
and
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
data = JSON '{"test":23}' # => {"test"=>23}
|
126
|
+
data = JSON['{"test":23}'] # => {"test"=>23}
|
127
|
+
```
|
128
|
+
|
129
|
+
You can choose to load a set of common additions to ruby core's objects if
|
130
|
+
you
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
require 'json/add/core'
|
134
|
+
```
|
135
|
+
|
136
|
+
After requiring this you can, e. g., serialise/deserialise Ruby ranges:
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
JSON JSON(1..10) # => 1..10
|
140
|
+
```
|
141
|
+
|
142
|
+
To find out how to add JSON support to other or your own classes, read the
|
143
|
+
section "More Examples" below.
|
144
|
+
|
145
|
+
To get the best compatibility to rails' JSON implementation, you can
|
146
|
+
|
147
|
+
```ruby
|
148
|
+
require 'json/add/rails'
|
149
|
+
```
|
150
|
+
|
151
|
+
Both of the additions attempt to require `'json'` (like above) first, if it has
|
152
|
+
not been required yet.
|
153
|
+
|
154
|
+
## Serializing exceptions
|
155
|
+
|
156
|
+
The JSON module doesn't extend `Exception` by default. If you convert an `Exception`
|
157
|
+
object to JSON, it will by default only include the exception message.
|
158
|
+
|
159
|
+
To include the full details, you must either load the `json/add/core` mentioned
|
160
|
+
above, or specifically load the exception addition:
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
require 'json/add/exception'
|
164
|
+
```
|
165
|
+
|
166
|
+
## More Examples
|
167
|
+
|
168
|
+
To create a JSON document from a ruby data structure, you can call
|
169
|
+
`JSON.generate` like that:
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
173
|
+
# => "[1,2,{\"a\":3.141},false,true,null,\"4..10\"]"
|
174
|
+
```
|
175
|
+
|
176
|
+
To get back a ruby data structure from a JSON document, you have to call
|
177
|
+
JSON.parse on it:
|
178
|
+
|
179
|
+
```ruby
|
180
|
+
JSON.parse json
|
181
|
+
# => [1, 2, {"a"=>3.141}, false, true, nil, "4..10"]
|
182
|
+
```
|
183
|
+
|
184
|
+
Note, that the range from the original data structure is a simple
|
185
|
+
string now. The reason for this is, that JSON doesn't support ranges
|
186
|
+
or arbitrary classes. In this case the json library falls back to call
|
187
|
+
`Object#to_json`, which is the same as `#to_s.to_json`.
|
188
|
+
|
189
|
+
It's possible to add JSON support serialization to arbitrary classes by
|
190
|
+
simply implementing a more specialized version of the `#to_json method`, that
|
191
|
+
should return a JSON object (a hash converted to JSON with `#to_json`) like
|
192
|
+
this (don't forget the `*a` for all the arguments):
|
193
|
+
|
194
|
+
```ruby
|
195
|
+
class Range
|
196
|
+
def to_json(*a)
|
197
|
+
{
|
198
|
+
'json_class' => self.class.name, # = 'Range'
|
199
|
+
'data' => [ first, last, exclude_end? ]
|
200
|
+
}.to_json(*a)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
```
|
204
|
+
|
205
|
+
The hash key `json_class` is the class, that will be asked to deserialise the
|
206
|
+
JSON representation later. In this case it's `Range`, but any namespace of
|
207
|
+
the form `A::B` or `::A::B` will do. All other keys are arbitrary and can be
|
208
|
+
used to store the necessary data to configure the object to be deserialised.
|
209
|
+
|
210
|
+
If the key `json_class` is found in a JSON object, the JSON parser checks
|
211
|
+
if the given class responds to the `json_create` class method. If so, it is
|
212
|
+
called with the JSON object converted to a Ruby hash. So a range can
|
213
|
+
be deserialised by implementing `Range.json_create` like this:
|
214
|
+
|
215
|
+
```ruby
|
216
|
+
class Range
|
217
|
+
def self.json_create(o)
|
218
|
+
new(*o['data'])
|
219
|
+
end
|
220
|
+
end
|
221
|
+
```
|
222
|
+
|
223
|
+
Now it possible to serialise/deserialise ranges as well:
|
224
|
+
|
225
|
+
```ruby
|
226
|
+
json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
227
|
+
# => "[1,2,{\"a\":3.141},false,true,null,{\"json_class\":\"Range\",\"data\":[4,10,false]}]"
|
228
|
+
JSON.parse json
|
229
|
+
# => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
230
|
+
json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
231
|
+
# => "[1,2,{\"a\":3.141},false,true,null,{\"json_class\":\"Range\",\"data\":[4,10,false]}]"
|
232
|
+
JSON.parse json, :create_additions => true
|
233
|
+
# => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
234
|
+
```
|
235
|
+
|
236
|
+
`JSON.generate` always creates the shortest possible string representation of a
|
237
|
+
ruby data structure in one line. This is good for data storage or network
|
238
|
+
protocols, but not so good for humans to read. Fortunately there's also
|
239
|
+
`JSON.pretty_generate` (or `JSON.pretty_generate`) that creates a more readable
|
240
|
+
output:
|
241
|
+
|
242
|
+
```ruby
|
243
|
+
puts JSON.pretty_generate([1, 2, {"a"=>3.141}, false, true, nil, 4..10])
|
244
|
+
[
|
245
|
+
1,
|
246
|
+
2,
|
247
|
+
{
|
248
|
+
"a": 3.141
|
249
|
+
},
|
250
|
+
false,
|
251
|
+
true,
|
252
|
+
null,
|
253
|
+
{
|
254
|
+
"json_class": "Range",
|
255
|
+
"data": [
|
256
|
+
4,
|
257
|
+
10,
|
258
|
+
false
|
259
|
+
]
|
260
|
+
}
|
261
|
+
]
|
262
|
+
```
|
263
|
+
|
264
|
+
There are also the methods `Kernel#j` for generate, and `Kernel#jj` for
|
265
|
+
`pretty_generate` output to the console, that work analogous to Core Ruby's `p` and
|
266
|
+
the `pp` library's `pp` methods.
|
267
|
+
|
268
|
+
The script `tools/server.rb` contains a small example if you want to test, how
|
269
|
+
receiving a JSON object from a webrick server in your browser with the
|
270
|
+
javasript prototype library http://www.prototypejs.org works.
|
271
|
+
|
272
|
+
## Speed Comparisons
|
273
|
+
|
274
|
+
I have created some benchmark results (see the benchmarks/data-p4-3Ghz
|
275
|
+
subdir of the package) for the JSON-parser to estimate the speed up in the C
|
276
|
+
extension:
|
277
|
+
|
278
|
+
```
|
279
|
+
Comparing times (call_time_mean):
|
280
|
+
1 ParserBenchmarkExt#parser 900 repeats:
|
281
|
+
553.922304770 ( real) -> 21.500x
|
282
|
+
0.001805307
|
283
|
+
2 ParserBenchmarkYAML#parser 1000 repeats:
|
284
|
+
224.513358139 ( real) -> 8.714x
|
285
|
+
0.004454078
|
286
|
+
3 ParserBenchmarkPure#parser 1000 repeats:
|
287
|
+
26.755020642 ( real) -> 1.038x
|
288
|
+
0.037376163
|
289
|
+
4 ParserBenchmarkRails#parser 1000 repeats:
|
290
|
+
25.763381731 ( real) -> 1.000x
|
291
|
+
0.038814780
|
292
|
+
calls/sec ( time) -> speed covers
|
293
|
+
secs/call
|
294
|
+
```
|
295
|
+
|
296
|
+
In the table above 1 is `JSON::Ext::Parser`, 2 is `YAML.load` with YAML
|
297
|
+
compatbile JSON document, 3 is is `JSON::Pure::Parser`, and 4 is
|
298
|
+
`ActiveSupport::JSON.decode`. The ActiveSupport JSON-decoder converts the
|
299
|
+
input first to YAML and then uses the YAML-parser, the conversion seems to
|
300
|
+
slow it down so much that it is only as fast as the `JSON::Pure::Parser`!
|
301
|
+
|
302
|
+
If you look at the benchmark data you can see that this is mostly caused by
|
303
|
+
the frequent high outliers - the median of the Rails-parser runs is still
|
304
|
+
overall smaller than the median of the `JSON::Pure::Parser` runs:
|
305
|
+
|
306
|
+
```
|
307
|
+
Comparing times (call_time_median):
|
308
|
+
1 ParserBenchmarkExt#parser 900 repeats:
|
309
|
+
800.592479481 ( real) -> 26.936x
|
310
|
+
0.001249075
|
311
|
+
2 ParserBenchmarkYAML#parser 1000 repeats:
|
312
|
+
271.002390644 ( real) -> 9.118x
|
313
|
+
0.003690004
|
314
|
+
3 ParserBenchmarkRails#parser 1000 repeats:
|
315
|
+
30.227910865 ( real) -> 1.017x
|
316
|
+
0.033082008
|
317
|
+
4 ParserBenchmarkPure#parser 1000 repeats:
|
318
|
+
29.722384421 ( real) -> 1.000x
|
319
|
+
0.033644676
|
320
|
+
calls/sec ( time) -> speed covers
|
321
|
+
secs/call
|
322
|
+
```
|
323
|
+
|
324
|
+
I have benchmarked the `JSON-Generator` as well. This generated a few more
|
325
|
+
values, because there are different modes that also influence the achieved
|
326
|
+
speed:
|
327
|
+
|
328
|
+
```
|
329
|
+
Comparing times (call_time_mean):
|
330
|
+
1 GeneratorBenchmarkExt#generator_fast 1000 repeats:
|
331
|
+
547.354332608 ( real) -> 15.090x
|
332
|
+
0.001826970
|
333
|
+
2 GeneratorBenchmarkExt#generator_safe 1000 repeats:
|
334
|
+
443.968212317 ( real) -> 12.240x
|
335
|
+
0.002252414
|
336
|
+
3 GeneratorBenchmarkExt#generator_pretty 900 repeats:
|
337
|
+
375.104545883 ( real) -> 10.341x
|
338
|
+
0.002665923
|
339
|
+
4 GeneratorBenchmarkPure#generator_fast 1000 repeats:
|
340
|
+
49.978706968 ( real) -> 1.378x
|
341
|
+
0.020008521
|
342
|
+
5 GeneratorBenchmarkRails#generator 1000 repeats:
|
343
|
+
38.531868759 ( real) -> 1.062x
|
344
|
+
0.025952543
|
345
|
+
6 GeneratorBenchmarkPure#generator_safe 1000 repeats:
|
346
|
+
36.927649925 ( real) -> 1.018x 7 (>=3859)
|
347
|
+
0.027079979
|
348
|
+
7 GeneratorBenchmarkPure#generator_pretty 1000 repeats:
|
349
|
+
36.272134441 ( real) -> 1.000x 6 (>=3859)
|
350
|
+
0.027569373
|
351
|
+
calls/sec ( time) -> speed covers
|
352
|
+
secs/call
|
353
|
+
```
|
354
|
+
|
355
|
+
In the table above 1-3 are `JSON::Ext::Generator` methods. 4, 6, and 7 are
|
356
|
+
`JSON::Pure::Generator` methods and 5 is the Rails JSON generator. It is now a
|
357
|
+
bit faster than the `generator_safe` and `generator_pretty` methods of the pure
|
358
|
+
variant but slower than the others.
|
359
|
+
|
360
|
+
To achieve the fastest JSON document output, you can use the `fast_generate`
|
361
|
+
method. Beware, that this will disable the checking for circular Ruby data
|
362
|
+
structures, which may cause JSON to go into an infinite loop.
|
363
|
+
|
364
|
+
Here are the median comparisons for completeness' sake:
|
365
|
+
|
366
|
+
```
|
367
|
+
Comparing times (call_time_median):
|
368
|
+
1 GeneratorBenchmarkExt#generator_fast 1000 repeats:
|
369
|
+
708.258020939 ( real) -> 16.547x
|
370
|
+
0.001411915
|
371
|
+
2 GeneratorBenchmarkExt#generator_safe 1000 repeats:
|
372
|
+
569.105020353 ( real) -> 13.296x
|
373
|
+
0.001757145
|
374
|
+
3 GeneratorBenchmarkExt#generator_pretty 900 repeats:
|
375
|
+
482.825371244 ( real) -> 11.280x
|
376
|
+
0.002071142
|
377
|
+
4 GeneratorBenchmarkPure#generator_fast 1000 repeats:
|
378
|
+
62.717626652 ( real) -> 1.465x
|
379
|
+
0.015944481
|
380
|
+
5 GeneratorBenchmarkRails#generator 1000 repeats:
|
381
|
+
43.965681162 ( real) -> 1.027x
|
382
|
+
0.022745013
|
383
|
+
6 GeneratorBenchmarkPure#generator_safe 1000 repeats:
|
384
|
+
43.929073409 ( real) -> 1.026x 7 (>=3859)
|
385
|
+
0.022763968
|
386
|
+
7 GeneratorBenchmarkPure#generator_pretty 1000 repeats:
|
387
|
+
42.802514491 ( real) -> 1.000x 6 (>=3859)
|
388
|
+
0.023363113
|
389
|
+
calls/sec ( time) -> speed covers
|
390
|
+
secs/call
|
391
|
+
```
|
392
|
+
|
393
|
+
## Author
|
394
|
+
|
395
|
+
Florian Frank <mailto:flori@ping.de>
|
396
|
+
|
397
|
+
## License
|
398
|
+
|
399
|
+
Ruby License, see https://www.ruby-lang.org/en/about/license.txt.
|
400
|
+
|
401
|
+
## Download
|
402
|
+
|
403
|
+
The latest version of this library can be downloaded at
|
404
|
+
|
405
|
+
* https://rubygems.org/gems/json
|
406
|
+
|
407
|
+
Online Documentation should be located at
|
408
|
+
|
409
|
+
* http://json.rubyforge.org
|
data/Rakefile
ADDED
@@ -0,0 +1,408 @@
|
|
1
|
+
begin
|
2
|
+
require 'rubygems/package_task'
|
3
|
+
rescue LoadError
|
4
|
+
end
|
5
|
+
|
6
|
+
require 'rbconfig'
|
7
|
+
include\
|
8
|
+
begin
|
9
|
+
RbConfig
|
10
|
+
rescue NameError
|
11
|
+
Config
|
12
|
+
end
|
13
|
+
|
14
|
+
require 'rake/clean'
|
15
|
+
CLOBBER.include 'doc', 'Gemfile.lock'
|
16
|
+
CLEAN.include FileList['diagrams/*.*'], 'doc', 'coverage', 'tmp',
|
17
|
+
FileList["ext/**/{Makefile,mkmf.log}"], 'build', 'dist', FileList['**/*.rbc'],
|
18
|
+
FileList["{ext,lib}/**/*.{so,bundle,#{CONFIG['DLEXT']},o,obj,pdb,lib,manifest,exp,def,jar,class,dSYM}"],
|
19
|
+
FileList['java/src/**/*.class']
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
class UndocumentedTestTask < Rake::TestTask
|
23
|
+
def desc(*) end
|
24
|
+
end
|
25
|
+
|
26
|
+
MAKE = ENV['MAKE'] || %w[gmake make].find { |c| system(c, '-v') }
|
27
|
+
BUNDLE = ENV['BUNDLE'] || %w[bundle].find { |c| system(c, '-v') }
|
28
|
+
PKG_NAME = 'json'
|
29
|
+
PKG_TITLE = 'JSON Implementation for Ruby'
|
30
|
+
PKG_VERSION = File.read('VERSION').chomp
|
31
|
+
PKG_FILES = FileList[`git ls-files`.split(/\n/)]
|
32
|
+
|
33
|
+
EXT_ROOT_DIR = 'ext/json/ext'
|
34
|
+
EXT_PARSER_DIR = "#{EXT_ROOT_DIR}/parser"
|
35
|
+
EXT_PARSER_DL = "#{EXT_PARSER_DIR}/parser.#{CONFIG['DLEXT']}"
|
36
|
+
RAGEL_PATH = "#{EXT_PARSER_DIR}/parser.rl"
|
37
|
+
EXT_PARSER_SRC = "#{EXT_PARSER_DIR}/parser.c"
|
38
|
+
EXT_GENERATOR_DIR = "#{EXT_ROOT_DIR}/generator"
|
39
|
+
EXT_GENERATOR_DL = "#{EXT_GENERATOR_DIR}/generator.#{CONFIG['DLEXT']}"
|
40
|
+
EXT_GENERATOR_SRC = "#{EXT_GENERATOR_DIR}/generator.c"
|
41
|
+
|
42
|
+
JAVA_DIR = "java/src/json/ext"
|
43
|
+
JAVA_RAGEL_PATH = "#{JAVA_DIR}/Parser.rl"
|
44
|
+
JAVA_PARSER_SRC = "#{JAVA_DIR}/Parser.java"
|
45
|
+
JAVA_SOURCES = FileList["#{JAVA_DIR}/*.java"]
|
46
|
+
JAVA_CLASSES = []
|
47
|
+
JRUBY_PARSER_JAR = File.expand_path("lib/json/ext/parser.jar")
|
48
|
+
JRUBY_GENERATOR_JAR = File.expand_path("lib/json/ext/generator.jar")
|
49
|
+
|
50
|
+
RAGEL_CODEGEN = %w[rlcodegen rlgen-cd ragel].find { |c| system(c, '-v') }
|
51
|
+
RAGEL_DOTGEN = %w[rlgen-dot rlgen-cd ragel].find { |c| system(c, '-v') }
|
52
|
+
|
53
|
+
desc "Installing library (pure)"
|
54
|
+
task :install_pure => :version do
|
55
|
+
ruby 'install.rb'
|
56
|
+
end
|
57
|
+
|
58
|
+
task :install_ext_really do
|
59
|
+
sitearchdir = CONFIG["sitearchdir"]
|
60
|
+
cd 'ext' do
|
61
|
+
for file in Dir["json/ext/*.#{CONFIG['DLEXT']}"]
|
62
|
+
d = File.join(sitearchdir, file)
|
63
|
+
mkdir_p File.dirname(d)
|
64
|
+
install(file, d)
|
65
|
+
end
|
66
|
+
warn " *** Installed EXT ruby library."
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
desc "Installing library (extension)"
|
71
|
+
task :install_ext => [ :compile, :install_pure, :install_ext_really ]
|
72
|
+
|
73
|
+
desc "Installing library (extension)"
|
74
|
+
task :install => :install_ext
|
75
|
+
|
76
|
+
if defined?(Gem) and defined?(Gem::PackageTask)
|
77
|
+
spec_pure = Gem::Specification.new do |s|
|
78
|
+
s.name = 'json_pure'
|
79
|
+
s.version = PKG_VERSION
|
80
|
+
s.summary = PKG_TITLE
|
81
|
+
s.description = "This is a JSON implementation in pure Ruby."
|
82
|
+
|
83
|
+
s.files = PKG_FILES
|
84
|
+
|
85
|
+
s.require_path = 'lib'
|
86
|
+
s.add_development_dependency 'rake'
|
87
|
+
s.add_development_dependency 'test-unit', '~> 2.0'
|
88
|
+
|
89
|
+
s.extra_rdoc_files << 'README.md'
|
90
|
+
s.rdoc_options <<
|
91
|
+
'--title' << 'JSON implemention for ruby' << '--main' << 'README.md'
|
92
|
+
s.test_files.concat Dir['./tests/test_*.rb']
|
93
|
+
|
94
|
+
s.author = "Florian Frank"
|
95
|
+
s.email = "flori@ping.de"
|
96
|
+
s.homepage = "http://flori.github.com/#{PKG_NAME}"
|
97
|
+
s.license = 'Ruby'
|
98
|
+
s.required_ruby_version = '>= 1.9'
|
99
|
+
end
|
100
|
+
|
101
|
+
desc 'Creates a json_pure.gemspec file'
|
102
|
+
task :gemspec_pure => :version do
|
103
|
+
File.open('json_pure.gemspec', 'w') do |gemspec|
|
104
|
+
gemspec.write spec_pure.to_ruby
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
Gem::PackageTask.new(spec_pure) do |pkg|
|
109
|
+
pkg.need_tar = true
|
110
|
+
pkg.package_files = PKG_FILES
|
111
|
+
end
|
112
|
+
|
113
|
+
spec_ext = Gem::Specification.new do |s|
|
114
|
+
s.name = 'json'
|
115
|
+
s.version = PKG_VERSION
|
116
|
+
s.summary = PKG_TITLE
|
117
|
+
s.description = "This is a JSON implementation as a Ruby extension in C."
|
118
|
+
|
119
|
+
s.files = PKG_FILES
|
120
|
+
|
121
|
+
s.extensions = FileList['ext/**/extconf.rb']
|
122
|
+
|
123
|
+
s.require_path = 'lib'
|
124
|
+
s.add_development_dependency 'rake'
|
125
|
+
s.add_development_dependency 'test-unit', '~> 2.0'
|
126
|
+
|
127
|
+
s.extra_rdoc_files << 'README.md'
|
128
|
+
s.rdoc_options <<
|
129
|
+
'--title' << 'JSON implemention for Ruby' << '--main' << 'README.md'
|
130
|
+
s.test_files.concat Dir['./tests/test_*.rb']
|
131
|
+
|
132
|
+
s.author = "Florian Frank"
|
133
|
+
s.email = "flori@ping.de"
|
134
|
+
s.homepage = "http://flori.github.com/#{PKG_NAME}"
|
135
|
+
s.license = 'Ruby'
|
136
|
+
s.required_ruby_version = '>= 1.9'
|
137
|
+
end
|
138
|
+
|
139
|
+
desc 'Creates a json.gemspec file'
|
140
|
+
task :gemspec_ext => :version do
|
141
|
+
File.open('json.gemspec', 'w') do |gemspec|
|
142
|
+
gemspec.write spec_ext.to_ruby
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
Gem::PackageTask.new(spec_ext) do |pkg|
|
147
|
+
pkg.need_tar = true
|
148
|
+
pkg.package_files = PKG_FILES
|
149
|
+
end
|
150
|
+
|
151
|
+
|
152
|
+
desc 'Create all gemspec files'
|
153
|
+
task :gemspec => [ :gemspec_pure, :gemspec_ext ]
|
154
|
+
end
|
155
|
+
|
156
|
+
desc m = "Writing version information for #{PKG_VERSION}"
|
157
|
+
task :version do
|
158
|
+
puts m
|
159
|
+
File.open(File.join('lib', 'json', 'version.rb'), 'w') do |v|
|
160
|
+
v.puts <<EOT
|
161
|
+
# frozen_string_literal: false
|
162
|
+
module JSON
|
163
|
+
# JSON version
|
164
|
+
VERSION = '#{PKG_VERSION}'
|
165
|
+
VERSION_ARRAY = VERSION.split(/\\./).map { |x| x.to_i } # :nodoc:
|
166
|
+
VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
|
167
|
+
VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
|
168
|
+
VERSION_BUILD = VERSION_ARRAY[2] # :nodoc:
|
169
|
+
end
|
170
|
+
EOT
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
task :check_env do
|
175
|
+
ENV.key?('JSON') or fail "JSON env var is required"
|
176
|
+
end
|
177
|
+
|
178
|
+
desc "Testing library (pure ruby)"
|
179
|
+
task :test_pure => [ :clean, :check_env, :do_test_pure ]
|
180
|
+
|
181
|
+
UndocumentedTestTask.new do |t|
|
182
|
+
t.name = 'do_test_pure'
|
183
|
+
t.libs << 'lib' << 'tests'
|
184
|
+
t.test_files = FileList['tests/*_test.rb']
|
185
|
+
t.verbose = true
|
186
|
+
t.options = '-v'
|
187
|
+
end
|
188
|
+
|
189
|
+
desc "Testing library (pure ruby and extension)"
|
190
|
+
task :test do
|
191
|
+
sh "env JSON=pure #{BUNDLE} exec rake test_pure" or exit 1
|
192
|
+
sh "env JSON=ext #{BUNDLE} exec rake test_ext" or exit 1
|
193
|
+
end
|
194
|
+
|
195
|
+
namespace :gems do
|
196
|
+
desc 'Install all development gems'
|
197
|
+
task :install do
|
198
|
+
sh "#{BUNDLE}"
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
if defined?(RUBY_ENGINE) and RUBY_ENGINE == 'jruby'
|
203
|
+
ENV['JAVA_HOME'] ||= [
|
204
|
+
'/usr/local/java/jdk',
|
205
|
+
'/usr/lib/jvm/java-6-openjdk',
|
206
|
+
'/Library/Java/Home',
|
207
|
+
].find { |c| File.directory?(c) }
|
208
|
+
if ENV['JAVA_HOME']
|
209
|
+
warn " *** JAVA_HOME is set to #{ENV['JAVA_HOME'].inspect}"
|
210
|
+
ENV['PATH'] = ENV['PATH'].split(/:/).unshift(java_path = "#{ENV['JAVA_HOME']}/bin") * ':'
|
211
|
+
warn " *** java binaries are assumed to be in #{java_path.inspect}"
|
212
|
+
else
|
213
|
+
warn " *** JAVA_HOME was not set or could not be guessed!"
|
214
|
+
exit 1
|
215
|
+
end
|
216
|
+
|
217
|
+
file JAVA_PARSER_SRC => JAVA_RAGEL_PATH do
|
218
|
+
cd JAVA_DIR do
|
219
|
+
if RAGEL_CODEGEN == 'ragel'
|
220
|
+
sh "ragel Parser.rl -J -o Parser.java"
|
221
|
+
else
|
222
|
+
sh "ragel -x Parser.rl | #{RAGEL_CODEGEN} -J"
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
desc "Generate parser for java with ragel"
|
228
|
+
task :ragel => JAVA_PARSER_SRC
|
229
|
+
|
230
|
+
desc "Delete the ragel generated Java source"
|
231
|
+
task :ragel_clean do
|
232
|
+
rm_rf JAVA_PARSER_SRC
|
233
|
+
end
|
234
|
+
|
235
|
+
JRUBY_JAR = File.join(CONFIG["libdir"], "jruby.jar")
|
236
|
+
if File.exist?(JRUBY_JAR)
|
237
|
+
JAVA_SOURCES.each do |src|
|
238
|
+
classpath = (Dir['java/lib/*.jar'] << 'java/src' << JRUBY_JAR) * ':'
|
239
|
+
obj = src.sub(/\.java\Z/, '.class')
|
240
|
+
file obj => src do
|
241
|
+
sh 'javac', '-classpath', classpath, '-source', '1.6', '-target', '1.6', src
|
242
|
+
end
|
243
|
+
JAVA_CLASSES << obj
|
244
|
+
end
|
245
|
+
else
|
246
|
+
warn "WARNING: Cannot find jruby in path => Cannot build jruby extension!"
|
247
|
+
end
|
248
|
+
|
249
|
+
desc "Compiling jruby extension"
|
250
|
+
task :compile => JAVA_CLASSES
|
251
|
+
|
252
|
+
desc "Package the jruby gem"
|
253
|
+
task :jruby_gem => :create_jar do
|
254
|
+
sh 'gem build json-java.gemspec'
|
255
|
+
mkdir_p 'pkg'
|
256
|
+
mv "json-#{PKG_VERSION}-java.gem", 'pkg'
|
257
|
+
end
|
258
|
+
|
259
|
+
desc "Testing library (jruby)"
|
260
|
+
task :test_ext => [ :check_env, :create_jar, :do_test_ext ]
|
261
|
+
|
262
|
+
UndocumentedTestTask.new do |t|
|
263
|
+
t.name = 'do_test_ext'
|
264
|
+
t.libs << 'lib' << 'tests'
|
265
|
+
t.test_files = FileList['tests/*_test.rb']
|
266
|
+
t.verbose = true
|
267
|
+
t.options = '-v'
|
268
|
+
end
|
269
|
+
|
270
|
+
file JRUBY_PARSER_JAR => :compile do
|
271
|
+
cd 'java/src' do
|
272
|
+
parser_classes = FileList[
|
273
|
+
"json/ext/ByteListTranscoder*.class",
|
274
|
+
"json/ext/OptionsReader*.class",
|
275
|
+
"json/ext/Parser*.class",
|
276
|
+
"json/ext/RuntimeInfo*.class",
|
277
|
+
"json/ext/StringDecoder*.class",
|
278
|
+
"json/ext/Utils*.class"
|
279
|
+
]
|
280
|
+
sh 'jar', 'cf', File.basename(JRUBY_PARSER_JAR), *parser_classes
|
281
|
+
mv File.basename(JRUBY_PARSER_JAR), File.dirname(JRUBY_PARSER_JAR)
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
desc "Create parser jar"
|
286
|
+
task :create_parser_jar => JRUBY_PARSER_JAR
|
287
|
+
|
288
|
+
file JRUBY_GENERATOR_JAR => :compile do
|
289
|
+
cd 'java/src' do
|
290
|
+
generator_classes = FileList[
|
291
|
+
"json/ext/ByteListTranscoder*.class",
|
292
|
+
"json/ext/OptionsReader*.class",
|
293
|
+
"json/ext/Generator*.class",
|
294
|
+
"json/ext/RuntimeInfo*.class",
|
295
|
+
"json/ext/StringEncoder*.class",
|
296
|
+
"json/ext/Utils*.class"
|
297
|
+
]
|
298
|
+
sh 'jar', 'cf', File.basename(JRUBY_GENERATOR_JAR), *generator_classes
|
299
|
+
mv File.basename(JRUBY_GENERATOR_JAR), File.dirname(JRUBY_GENERATOR_JAR)
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
303
|
+
desc "Create generator jar"
|
304
|
+
task :create_generator_jar => JRUBY_GENERATOR_JAR
|
305
|
+
|
306
|
+
desc "Create parser and generator jars"
|
307
|
+
task :create_jar => [ :create_parser_jar, :create_generator_jar ]
|
308
|
+
|
309
|
+
desc "Build all gems and archives for a new release of the jruby extension."
|
310
|
+
task :build => [ :clean, :version, :jruby_gem ]
|
311
|
+
|
312
|
+
task :release => :build
|
313
|
+
else
|
314
|
+
desc "Compiling extension"
|
315
|
+
task :compile => [ EXT_PARSER_DL, EXT_GENERATOR_DL ]
|
316
|
+
|
317
|
+
file EXT_PARSER_DL => EXT_PARSER_SRC do
|
318
|
+
cd EXT_PARSER_DIR do
|
319
|
+
ruby 'extconf.rb'
|
320
|
+
sh MAKE
|
321
|
+
end
|
322
|
+
cp "#{EXT_PARSER_DIR}/parser.#{CONFIG['DLEXT']}", EXT_ROOT_DIR
|
323
|
+
end
|
324
|
+
|
325
|
+
file EXT_GENERATOR_DL => EXT_GENERATOR_SRC do
|
326
|
+
cd EXT_GENERATOR_DIR do
|
327
|
+
ruby 'extconf.rb'
|
328
|
+
sh MAKE
|
329
|
+
end
|
330
|
+
cp "#{EXT_GENERATOR_DIR}/generator.#{CONFIG['DLEXT']}", EXT_ROOT_DIR
|
331
|
+
end
|
332
|
+
|
333
|
+
desc "Testing library (extension)"
|
334
|
+
task :test_ext => [ :check_env, :compile, :do_test_ext ]
|
335
|
+
|
336
|
+
UndocumentedTestTask.new do |t|
|
337
|
+
t.name = 'do_test_ext'
|
338
|
+
t.libs << 'ext' << 'lib' << 'tests'
|
339
|
+
t.test_files = FileList['tests/*_test.rb']
|
340
|
+
t.verbose = true
|
341
|
+
t.options = '-v'
|
342
|
+
end
|
343
|
+
|
344
|
+
desc "Generate parser with ragel"
|
345
|
+
task :ragel => EXT_PARSER_SRC
|
346
|
+
|
347
|
+
desc "Delete the ragel generated C source"
|
348
|
+
task :ragel_clean do
|
349
|
+
rm_rf EXT_PARSER_SRC
|
350
|
+
end
|
351
|
+
|
352
|
+
desc "Update the tags file"
|
353
|
+
task :tags do
|
354
|
+
system 'ctags', *Dir['**/*.{rb,c,h,java}']
|
355
|
+
end
|
356
|
+
|
357
|
+
file EXT_PARSER_SRC => RAGEL_PATH do
|
358
|
+
cd EXT_PARSER_DIR do
|
359
|
+
if RAGEL_CODEGEN == 'ragel'
|
360
|
+
sh "ragel parser.rl -G2 -o parser.c"
|
361
|
+
else
|
362
|
+
sh "ragel -x parser.rl | #{RAGEL_CODEGEN} -G2"
|
363
|
+
end
|
364
|
+
src = File.read("parser.c").gsub(/[ \t]+$/, '')
|
365
|
+
src.gsub!(/^static const int (JSON_.*=.*);$/, 'enum {\1};')
|
366
|
+
File.open("parser.c", "w") {|f| f.print src}
|
367
|
+
end
|
368
|
+
end
|
369
|
+
|
370
|
+
desc "Generate diagrams of ragel parser (ps)"
|
371
|
+
task :ragel_dot_ps do
|
372
|
+
root = 'diagrams'
|
373
|
+
specs = []
|
374
|
+
File.new(RAGEL_PATH).grep(/^\s*machine\s*(\S+);\s*$/) { specs << $1 }
|
375
|
+
for s in specs
|
376
|
+
if RAGEL_DOTGEN == 'ragel'
|
377
|
+
sh "ragel #{RAGEL_PATH} -S#{s} -p -V | dot -Tps -o#{root}/#{s}.ps"
|
378
|
+
else
|
379
|
+
sh "ragel -x #{RAGEL_PATH} -S#{s} | #{RAGEL_DOTGEN} -p|dot -Tps -o#{root}/#{s}.ps"
|
380
|
+
end
|
381
|
+
end
|
382
|
+
end
|
383
|
+
|
384
|
+
desc "Generate diagrams of ragel parser (png)"
|
385
|
+
task :ragel_dot_png do
|
386
|
+
root = 'diagrams'
|
387
|
+
specs = []
|
388
|
+
File.new(RAGEL_PATH).grep(/^\s*machine\s*(\S+);\s*$/) { specs << $1 }
|
389
|
+
for s in specs
|
390
|
+
if RAGEL_DOTGEN == 'ragel'
|
391
|
+
sh "ragel #{RAGEL_PATH} -S#{s} -p -V | dot -Tpng -o#{root}/#{s}.png"
|
392
|
+
else
|
393
|
+
sh "ragel -x #{RAGEL_PATH} -S#{s} | #{RAGEL_DOTGEN} -p|dot -Tpng -o#{root}/#{s}.png"
|
394
|
+
end
|
395
|
+
end
|
396
|
+
end
|
397
|
+
|
398
|
+
desc "Generate diagrams of ragel parser"
|
399
|
+
task :ragel_dot => [ :ragel_dot_png, :ragel_dot_ps ]
|
400
|
+
|
401
|
+
desc "Build all gems and archives for a new release of json and json_pure."
|
402
|
+
task :build => [ :clean, :gemspec, :package ]
|
403
|
+
|
404
|
+
task :release => :build
|
405
|
+
end
|
406
|
+
|
407
|
+
desc "Compile in the the source directory"
|
408
|
+
task :default => [ :clean, :gemspec, :test ]
|