4
4
5
5
## Description
6
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:
7
+ This is an implementation of the JSON specification according to RFC 7159
8
+ http://www.ietf.org/rfc/rfc7159.txt . There is two variants available:
10
9
11
- * A pure ruby variant, that relies on the iconv and the stringscan
12
- extensions, which are both part of the ruby standard library.
10
+ * A pure ruby variant, that relies on the ` strscan ` extensions, which is
11
+ part of the ruby standard library.
13
12
* 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.
13
+ implemented in C or Java and comes with a parser generated by the [ Ragel ]
14
+ state machine compiler.
16
15
17
16
Both variants of the JSON generator generate UTF-8 character sequences by
18
17
default. If an : ascii \_ only option with a true value is given, they escape all
@@ -32,45 +31,19 @@ It's recommended to use the extension variant of JSON, because it's faster than
32
31
the pure ruby variant. If you cannot build it on your system, you can settle
33
32
for the latter.
34
33
35
- Just type into the command line as root :
34
+ Install the gem and add to the application's Gemfile by executing :
36
35
37
- ```
38
- # rake install
39
- ```
40
-
41
- The above command will build the extensions and install them on your system.
42
-
43
- ```
44
- # rake install_pure
45
- ```
46
-
47
- or
48
-
49
- ```
50
- # ruby install.rb
51
- ```
36
+ $ bundle add json
52
37
53
- will just install the pure ruby implementation of JSON.
38
+ If bundler is not being used to manage dependencies, install the gem by executing:
54
39
55
- If you use Rubygems you can type
40
+ $ gem install json
56
41
57
- ```
58
- # gem install json
59
- ```
60
-
61
- instead, to install the newest JSON version.
62
42
63
43
There is also a pure ruby json only variant of the gem, that can be installed
64
44
with:
65
45
66
- ```
67
- # gem install json_pure
68
- ```
69
-
70
- ## Compiling the extensions yourself
71
-
72
- If you want to create the ` parser.c ` file from its ` parser.rl ` file or draw nice
73
- graphviz images of the state machines, you need [ Ragel] .
46
+ $ gem install json_pure
74
47
75
48
## Usage
76
49
@@ -254,131 +227,6 @@ There are also the methods `Kernel#j` for generate, and `Kernel#jj` for
254
227
` pretty_generate ` output to the console, that work analogous to Core Ruby's ` p ` and
255
228
the ` pp ` library's ` pp ` methods.
256
229
257
- The script ` tools/server.rb ` contains a small example if you want to test, how
258
- receiving a JSON object from a webrick server in your browser with the
259
- JavaScript prototype library http://www.prototypejs.org works.
260
-
261
- ## Speed Comparisons
262
-
263
- I have created some benchmark results (see the benchmarks/data-p4-3Ghz
264
- subdir of the package) for the JSON-parser to estimate the speed up in the C
265
- extension:
266
-
267
- ```
268
- Comparing times (call_time_mean):
269
- 1 ParserBenchmarkExt#parser 900 repeats:
270
- 553.922304770 ( real) -> 21.500x
271
- 0.001805307
272
- 2 ParserBenchmarkYAML#parser 1000 repeats:
273
- 224.513358139 ( real) -> 8.714x
274
- 0.004454078
275
- 3 ParserBenchmarkPure#parser 1000 repeats:
276
- 26.755020642 ( real) -> 1.038x
277
- 0.037376163
278
- 4 ParserBenchmarkRails#parser 1000 repeats:
279
- 25.763381731 ( real) -> 1.000x
280
- 0.038814780
281
- calls/sec ( time) -> speed covers
282
- secs/call
283
- ```
284
-
285
- In the table above 1 is ` JSON::Ext::Parser ` , 2 is ` YAML.load ` with YAML
286
- compatible JSON document, 3 is is ` JSON::Pure::Parser ` , and 4 is
287
- ` ActiveSupport::JSON.decode ` . The ActiveSupport JSON-decoder converts the
288
- input first to YAML and then uses the YAML-parser, the conversion seems to
289
- slow it down so much that it is only as fast as the ` JSON::Pure::Parser ` !
290
-
291
- If you look at the benchmark data you can see that this is mostly caused by
292
- the frequent high outliers - the median of the Rails-parser runs is still
293
- overall smaller than the median of the ` JSON::Pure::Parser ` runs:
294
-
295
- ```
296
- Comparing times (call_time_median):
297
- 1 ParserBenchmarkExt#parser 900 repeats:
298
- 800.592479481 ( real) -> 26.936x
299
- 0.001249075
300
- 2 ParserBenchmarkYAML#parser 1000 repeats:
301
- 271.002390644 ( real) -> 9.118x
302
- 0.003690004
303
- 3 ParserBenchmarkRails#parser 1000 repeats:
304
- 30.227910865 ( real) -> 1.017x
305
- 0.033082008
306
- 4 ParserBenchmarkPure#parser 1000 repeats:
307
- 29.722384421 ( real) -> 1.000x
308
- 0.033644676
309
- calls/sec ( time) -> speed covers
310
- secs/call
311
- ```
312
-
313
- I have benchmarked the ` JSON-Generator ` as well. This generated a few more
314
- values, because there are different modes that also influence the achieved
315
- speed:
316
-
317
- ```
318
- Comparing times (call_time_mean):
319
- 1 GeneratorBenchmarkExt#generator_fast 1000 repeats:
320
- 547.354332608 ( real) -> 15.090x
321
- 0.001826970
322
- 2 GeneratorBenchmarkExt#generator_safe 1000 repeats:
323
- 443.968212317 ( real) -> 12.240x
324
- 0.002252414
325
- 3 GeneratorBenchmarkExt#generator_pretty 900 repeats:
326
- 375.104545883 ( real) -> 10.341x
327
- 0.002665923
328
- 4 GeneratorBenchmarkPure#generator_fast 1000 repeats:
329
- 49.978706968 ( real) -> 1.378x
330
- 0.020008521
331
- 5 GeneratorBenchmarkRails#generator 1000 repeats:
332
- 38.531868759 ( real) -> 1.062x
333
- 0.025952543
334
- 6 GeneratorBenchmarkPure#generator_safe 1000 repeats:
335
- 36.927649925 ( real) -> 1.018x 7 (>=3859)
336
- 0.027079979
337
- 7 GeneratorBenchmarkPure#generator_pretty 1000 repeats:
338
- 36.272134441 ( real) -> 1.000x 6 (>=3859)
339
- 0.027569373
340
- calls/sec ( time) -> speed covers
341
- secs/call
342
- ```
343
-
344
- In the table above 1-3 are ` JSON::Ext::Generator ` methods. 4, 6, and 7 are
345
- ` JSON::Pure::Generator ` methods and 5 is the Rails JSON generator. It is now a
346
- bit faster than the ` generator_safe ` and ` generator_pretty ` methods of the pure
347
- variant but slower than the others.
348
-
349
- To achieve the fastest JSON document output, you can use the ` fast_generate `
350
- method. Beware, that this will disable the checking for circular Ruby data
351
- structures, which may cause JSON to go into an infinite loop.
352
-
353
- Here are the median comparisons for completeness' sake:
354
-
355
- ```
356
- Comparing times (call_time_median):
357
- 1 GeneratorBenchmarkExt#generator_fast 1000 repeats:
358
- 708.258020939 ( real) -> 16.547x
359
- 0.001411915
360
- 2 GeneratorBenchmarkExt#generator_safe 1000 repeats:
361
- 569.105020353 ( real) -> 13.296x
362
- 0.001757145
363
- 3 GeneratorBenchmarkExt#generator_pretty 900 repeats:
364
- 482.825371244 ( real) -> 11.280x
365
- 0.002071142
366
- 4 GeneratorBenchmarkPure#generator_fast 1000 repeats:
367
- 62.717626652 ( real) -> 1.465x
368
- 0.015944481
369
- 5 GeneratorBenchmarkRails#generator 1000 repeats:
370
- 43.965681162 ( real) -> 1.027x
371
- 0.022745013
372
- 6 GeneratorBenchmarkPure#generator_safe 1000 repeats:
373
- 43.929073409 ( real) -> 1.026x 7 (>=3859)
374
- 0.022763968
375
- 7 GeneratorBenchmarkPure#generator_pretty 1000 repeats:
376
- 42.802514491 ( real) -> 1.000x 6 (>=3859)
377
- 0.023363113
378
- calls/sec ( time) -> speed covers
379
- secs/call
380
- ```
381
-
382
230
## Development
383
231
384
232
### Release
0 commit comments