json 2.3.0 → 2.6.2

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 (88) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +71 -5
  3. data/README.md +19 -3
  4. data/VERSION +1 -1
  5. data/ext/json/ext/generator/generator.c +132 -23
  6. data/ext/json/ext/generator/generator.h +5 -2
  7. data/ext/json/ext/parser/extconf.rb +26 -0
  8. data/ext/json/ext/parser/parser.c +2979 -1768
  9. data/ext/json/ext/parser/parser.h +6 -1
  10. data/ext/json/ext/parser/parser.rl +128 -38
  11. data/ext/json/extconf.rb +1 -0
  12. data/json.gemspec +0 -0
  13. data/lib/json/add/complex.rb +0 -1
  14. data/lib/json/add/rational.rb +0 -1
  15. data/lib/json/common.rb +370 -123
  16. data/lib/json/pure/generator.rb +29 -9
  17. data/lib/json/pure/parser.rb +23 -5
  18. data/lib/json/version.rb +1 -1
  19. data/lib/json.rb +549 -29
  20. metadata +17 -109
  21. data/.gitignore +0 -18
  22. data/.travis.yml +0 -24
  23. data/Gemfile +0 -14
  24. data/README-json-jruby.md +0 -33
  25. data/Rakefile +0 -413
  26. data/diagrams/.keep +0 -0
  27. data/install.rb +0 -23
  28. data/java/src/json/ext/ByteListTranscoder.java +0 -166
  29. data/java/src/json/ext/Generator.java +0 -466
  30. data/java/src/json/ext/GeneratorMethods.java +0 -231
  31. data/java/src/json/ext/GeneratorService.java +0 -42
  32. data/java/src/json/ext/GeneratorState.java +0 -490
  33. data/java/src/json/ext/OptionsReader.java +0 -113
  34. data/java/src/json/ext/Parser.java +0 -2362
  35. data/java/src/json/ext/Parser.rl +0 -893
  36. data/java/src/json/ext/ParserService.java +0 -34
  37. data/java/src/json/ext/RuntimeInfo.java +0 -116
  38. data/java/src/json/ext/StringDecoder.java +0 -166
  39. data/java/src/json/ext/StringEncoder.java +0 -111
  40. data/java/src/json/ext/Utils.java +0 -88
  41. data/json-java.gemspec +0 -37
  42. data/json_pure.gemspec +0 -38
  43. data/lib/json/ext/.keep +0 -0
  44. data/references/rfc7159.txt +0 -899
  45. data/tests/fixtures/fail10.json +0 -1
  46. data/tests/fixtures/fail11.json +0 -1
  47. data/tests/fixtures/fail12.json +0 -1
  48. data/tests/fixtures/fail13.json +0 -1
  49. data/tests/fixtures/fail14.json +0 -1
  50. data/tests/fixtures/fail18.json +0 -1
  51. data/tests/fixtures/fail19.json +0 -1
  52. data/tests/fixtures/fail2.json +0 -1
  53. data/tests/fixtures/fail20.json +0 -1
  54. data/tests/fixtures/fail21.json +0 -1
  55. data/tests/fixtures/fail22.json +0 -1
  56. data/tests/fixtures/fail23.json +0 -1
  57. data/tests/fixtures/fail24.json +0 -1
  58. data/tests/fixtures/fail25.json +0 -1
  59. data/tests/fixtures/fail27.json +0 -2
  60. data/tests/fixtures/fail28.json +0 -2
  61. data/tests/fixtures/fail3.json +0 -1
  62. data/tests/fixtures/fail4.json +0 -1
  63. data/tests/fixtures/fail5.json +0 -1
  64. data/tests/fixtures/fail6.json +0 -1
  65. data/tests/fixtures/fail7.json +0 -1
  66. data/tests/fixtures/fail8.json +0 -1
  67. data/tests/fixtures/fail9.json +0 -1
  68. data/tests/fixtures/obsolete_fail1.json +0 -1
  69. data/tests/fixtures/pass1.json +0 -56
  70. data/tests/fixtures/pass15.json +0 -1
  71. data/tests/fixtures/pass16.json +0 -1
  72. data/tests/fixtures/pass17.json +0 -1
  73. data/tests/fixtures/pass2.json +0 -1
  74. data/tests/fixtures/pass26.json +0 -1
  75. data/tests/fixtures/pass3.json +0 -6
  76. data/tests/json_addition_test.rb +0 -203
  77. data/tests/json_common_interface_test.rb +0 -126
  78. data/tests/json_encoding_test.rb +0 -107
  79. data/tests/json_ext_parser_test.rb +0 -15
  80. data/tests/json_fixtures_test.rb +0 -32
  81. data/tests/json_generator_test.rb +0 -421
  82. data/tests/json_generic_object_test.rb +0 -82
  83. data/tests/json_parser_test.rb +0 -472
  84. data/tests/json_string_matching_test.rb +0 -38
  85. data/tests/test_helper.rb +0 -17
  86. data/tools/diff.sh +0 -18
  87. data/tools/fuzz.rb +0 -131
  88. data/tools/server.rb +0 -62
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ca534a7e54763534530fcc4d010c6ff3e45ae08959e64d7a54e2ed6b1c4d6cf4
4
- data.tar.gz: 5ed1902c0706bb321b4f4833203c4b6ae3cd47c4303bca04977442965ef9cbe6
3
+ metadata.gz: 21e09bba5bfe89e923363872b46bfb0ee800ff01e039e36b662008c85983983f
4
+ data.tar.gz: 2f319b945dd9f7510ce8dac412ab6d69f289942278cd298728af1cecb3b329c0
5
5
  SHA512:
6
- metadata.gz: 70cefe1d1604c66bff786a1f530acd9fab15670957b84a0efc91d450e130e0b48da6ff0d8858549413fe6beb956b3d19d313f178cbe8f5123ec32ab701635bf9
7
- data.tar.gz: d0b3f6d760d2100b47e4160f372344d057f60ddd7c58e3bc2dceb35b690cbd14e6af75a3028d121099f1586ed134876c16bdf7ad58e68c7295e5c0cc20f52538
6
+ metadata.gz: f7edf79e46c4a5e1b353f1463cad92af3198c8983247e9bb04751dd98ee714040455ebff17f03fe2b1a7928992885914d192e8b509673a26b47c8966822cb450
7
+ data.tar.gz: 5b086c18aac3d99ac1ef2ec5bdf66af841ba5dd6bc3a85e1e8e65ccb086458902b4a49fdb08a8acce978a46c13b82c23dc724450869f02898d6374d79750aca6
data/CHANGES.md CHANGED
@@ -1,5 +1,71 @@
1
1
  # Changes
2
2
 
3
+ ### 2021-10-24 (2.6.1)
4
+
5
+ * Restore version.rb with 2.6.1
6
+
7
+ ### 2021-10-14 (2.6.0)
8
+
9
+ * Use `rb_enc_interned_str` if available to reduce allocations in `freeze: true` mode. #451.
10
+ * Bump required_ruby_version to 2.3.
11
+ * Fix compatibility with `GC.compact`.
12
+ * Fix some compilation warnings. #469
13
+
14
+ ## 2020-12-22 (2.5.1)
15
+
16
+ * Restore the compatibility for constants of JSON class.
17
+
18
+ ## 2020-12-22 (2.5.0)
19
+
20
+ * Ready to Ractor-safe at Ruby 3.0.
21
+
22
+ ## 2020-12-17 (2.4.1)
23
+
24
+ * Restore version.rb with 2.4.1
25
+
26
+ ## 2020-12-15 (2.4.0)
27
+
28
+ * Implement a freeze: parser option #447
29
+ * Fix an issue with generate_pretty and empty objects in the Ruby and Java implementations #449
30
+ * Fix JSON.load_file doc #448
31
+ * Fix pure parser with unclosed arrays / objects #425
32
+ * bundle the LICENSE file in the gem #444
33
+ * Add an option to escape forward slash character #405
34
+ * RDoc for JSON #439 #446 #442 #434 #433 #430
35
+
36
+ ## 2020-06-30 (2.3.1)
37
+
38
+ * Spelling and grammar fixes for comments. Pull request #191 by Josh
39
+ Kline.
40
+ * Enhance generic JSON and #generate docs. Pull request #347 by Victor
41
+ Shepelev.
42
+ * Add :nodoc: for GeneratorMethods. Pull request #349 by Victor Shepelev.
43
+ * Baseline changes to help (JRuby) development. Pull request #371 by Karol
44
+ Bucek.
45
+ * Add metadata for rubygems.org. Pull request #379 by Alexandre ZANNI.
46
+ * Remove invalid JSON.generate description from JSON module rdoc. Pull
47
+ request #384 by Jeremy Evans.
48
+ * Test with TruffleRuby in CI. Pull request #402 by Benoit Daloze.
49
+ * Rdoc enhancements. Pull request #413 by Burdette Lamar.
50
+ * Fixtures/ are not being tested... Pull request #416 by Marc-André
51
+ Lafortune.
52
+ * Use frozen string for hash key. Pull request #420 by Marc-André
53
+ Lafortune.
54
+ * Added :call-seq: to RDoc for some methods. Pull request #422 by Burdette
55
+ Lamar.
56
+ * Small typo fix. Pull request #423 by Marc-André Lafortune.
57
+
58
+ ## 2019-12-11 (2.3.0)
59
+ * Fix default of `create_additions` to always be `false` for `JSON(user_input)`
60
+ and `JSON.parse(user_input, nil)`.
61
+ Note that `JSON.load` remains with default `true` and is meant for internal
62
+ serialization of trusted data. [CVE-2020-10663]
63
+ * Fix passing args all #to_json in json/add/*.
64
+ * Fix encoding issues
65
+ * Fix issues of keyword vs positional parameter
66
+ * Fix JSON::Parser against bigdecimal updates
67
+ * Bug fixes to JRuby port
68
+
3
69
  ## 2019-02-21 (2.2.0)
4
70
  * Adds support for 2.6 BigDecimal and ruby standard library Set datetype.
5
71
 
@@ -28,7 +94,7 @@
28
94
 
29
95
  ## 2015-09-11 (2.0.0)
30
96
  * Now complies to newest JSON RFC 7159.
31
- * Implements compatibiliy to ruby 2.4 integer unification.
97
+ * Implements compatibility to ruby 2.4 integer unification.
32
98
  * Drops support for old rubies whose life has ended, that is rubies < 2.0.
33
99
  Also see https://www.ruby-lang.org/en/news/2014/07/01/eol-for-1-8-7-and-1-9-2/
34
100
  * There were still some mentions of dual GPL licensing in the source, but JSON
@@ -69,9 +135,9 @@
69
135
  ## 2013-02-04 (1.7.7)
70
136
  * Security fix for JSON create_additions default value and
71
137
  `JSON::GenericObject`. It should not be possible to create additions unless
72
- explicitely requested by setting the create_additions argument to true or
138
+ explicitly requested by setting the create_additions argument to true or
73
139
  using the JSON.load/dump interface. If `JSON::GenericObject` is supposed to
74
- be automatically deserialised, this has to be explicitely enabled by
140
+ be automatically deserialised, this has to be explicitly enabled by
75
141
  setting
76
142
  JSON::GenericObject.json_creatable = true
77
143
  as well.
@@ -217,7 +283,7 @@
217
283
  ## 2010-04-23 (1.4.0)
218
284
  * Major speed improvements and building with simplified
219
285
  directory/file-structure.
220
- * Extension should at least be comapatible with MRI, YARV and Rubinius.
286
+ * Extension should at least be compatible with MRI, YARV and Rubinius.
221
287
 
222
288
  ## 2010-04-07 (1.2.4)
223
289
  * Triger const_missing callback to make Rails' dynamic class loading work.
@@ -235,7 +301,7 @@
235
301
  strings in object names/keys.
236
302
 
237
303
  ## 2009-10-01 (1.2.0)
238
- * `fast_generate` now raises an exeception for nan and infinite floats.
304
+ * `fast_generate` now raises an exception for nan and infinite floats.
239
305
  * On Ruby 1.8 json supports parsing of UTF-8, UTF-16BE, UTF-16LE, UTF-32BE,
240
306
  and UTF-32LE JSON documents now. Under Ruby 1.9 the M17n conversion
241
307
  functions are used to convert from all supported encodings. ASCII-8BIT
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # JSON implementation for Ruby
2
2
 
3
- [![Travis Widget](http://travis-ci.org/flori/json.svg?branch=master)](https://travis-ci.org/flori/json)
3
+ [![CI](https://github.com/flori/json/actions/workflows/ci.yml/badge.svg)](https://github.com/flori/json/actions/workflows/ci.yml)
4
4
 
5
5
  ## Description
6
6
 
@@ -267,7 +267,7 @@ the `pp` library's `pp` methods.
267
267
 
268
268
  The script `tools/server.rb` contains a small example if you want to test, how
269
269
  receiving a JSON object from a webrick server in your browser with the
270
- javasript prototype library http://www.prototypejs.org works.
270
+ JavaScript prototype library http://www.prototypejs.org works.
271
271
 
272
272
  ## Speed Comparisons
273
273
 
@@ -294,7 +294,7 @@ extension:
294
294
  ```
295
295
 
296
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
297
+ compatible JSON document, 3 is is `JSON::Pure::Parser`, and 4 is
298
298
  `ActiveSupport::JSON.decode`. The ActiveSupport JSON-decoder converts the
299
299
  input first to YAML and then uses the YAML-parser, the conversion seems to
300
300
  slow it down so much that it is only as fast as the `JSON::Pure::Parser`!
@@ -390,6 +390,22 @@ Here are the median comparisons for completeness' sake:
390
390
  secs/call
391
391
  ```
392
392
 
393
+ ## Development
394
+
395
+ ### Release
396
+
397
+ Update the json.gemspec and json-java.gemspec.
398
+
399
+ ```
400
+ rbenv shell 2.6.5
401
+ rake build
402
+ gem push pkg/json-2.3.0.gem
403
+
404
+ rbenv shell jruby-9.2.9.0
405
+ rake build
406
+ gem push pkg/json-2.3.0-java.gem
407
+ ```
408
+
393
409
  ## Author
394
410
 
395
411
  Florian Frank <mailto:flori@ping.de>
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.3.0
1
+ 2.6.2
@@ -1,11 +1,6 @@
1
1
  #include "../fbuffer/fbuffer.h"
2
2
  #include "generator.h"
3
3
 
4
- #ifdef HAVE_RUBY_ENCODING_H
5
- static VALUE CEncoding_UTF_8;
6
- static ID i_encoding, i_encode;
7
- #endif
8
-
9
4
  static VALUE mJSON, mExt, mGenerator, cState, mGeneratorMethods, mObject,
10
5
  mHash, mArray,
11
6
  #ifdef RUBY_INTEGER_UNIFICATION
@@ -15,14 +10,13 @@ static VALUE mJSON, mExt, mGenerator, cState, mGeneratorMethods, mObject,
15
10
  #endif
16
11
  mFloat, mString, mString_Extend,
17
12
  mTrueClass, mFalseClass, mNilClass, eGeneratorError,
18
- eNestingError,
19
- i_SAFE_STATE_PROTOTYPE;
13
+ eNestingError;
20
14
 
21
15
  static ID i_to_s, i_to_json, i_new, i_indent, i_space, i_space_before,
22
16
  i_object_nl, i_array_nl, i_max_nesting, i_allow_nan, i_ascii_only,
23
17
  i_pack, i_unpack, i_create_id, i_extend, i_key_p,
24
18
  i_aref, i_send, i_respond_to_p, i_match, i_keys, i_depth,
25
- i_buffer_initial_length, i_dup;
19
+ i_buffer_initial_length, i_dup, i_escape_slash;
26
20
 
27
21
  /*
28
22
  * Copyright 2001-2004 Unicode, Inc.
@@ -130,7 +124,7 @@ static void unicode_escape_to_buffer(FBuffer *buffer, char buf[6], UTF16
130
124
 
131
125
  /* Converts string to a JSON string in FBuffer buffer, where all but the ASCII
132
126
  * and control characters are JSON escaped. */
133
- static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string)
127
+ static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string, char escape_slash)
134
128
  {
135
129
  const UTF8 *source = (UTF8 *) RSTRING_PTR(string);
136
130
  const UTF8 *sourceEnd = source + RSTRING_LEN(string);
@@ -180,6 +174,11 @@ static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string)
180
174
  case '"':
181
175
  fbuffer_append(buffer, "\\\"", 2);
182
176
  break;
177
+ case '/':
178
+ if(escape_slash) {
179
+ fbuffer_append(buffer, "\\/", 2);
180
+ break;
181
+ }
183
182
  default:
184
183
  fbuffer_append_char(buffer, (char)ch);
185
184
  break;
@@ -229,7 +228,7 @@ static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string)
229
228
  * characters required by the JSON standard are JSON escaped. The remaining
230
229
  * characters (should be UTF8) are just passed through and appended to the
231
230
  * result. */
232
- static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string)
231
+ static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string, char escape_slash)
233
232
  {
234
233
  const char *ptr = RSTRING_PTR(string), *p;
235
234
  unsigned long len = RSTRING_LEN(string), start = 0, end = 0;
@@ -280,6 +279,12 @@ static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string)
280
279
  escape = "\\\"";
281
280
  escape_len = 2;
282
281
  break;
282
+ case '/':
283
+ if(escape_slash) {
284
+ escape = "\\/";
285
+ escape_len = 2;
286
+ break;
287
+ }
283
288
  default:
284
289
  {
285
290
  unsigned short clen = 1;
@@ -328,6 +333,76 @@ static char *fstrndup(const char *ptr, unsigned long len) {
328
333
  *
329
334
  */
330
335
 
336
+ /* Explanation of the following: that's the only way to not pollute
337
+ * standard library's docs with GeneratorMethods::<ClassName> which
338
+ * are uninformative and take a large place in a list of classes
339
+ */
340
+
341
+ /*
342
+ * Document-module: JSON::Ext::Generator::GeneratorMethods
343
+ * :nodoc:
344
+ */
345
+
346
+ /*
347
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::Array
348
+ * :nodoc:
349
+ */
350
+
351
+ /*
352
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::Bignum
353
+ * :nodoc:
354
+ */
355
+
356
+ /*
357
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::FalseClass
358
+ * :nodoc:
359
+ */
360
+
361
+ /*
362
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::Fixnum
363
+ * :nodoc:
364
+ */
365
+
366
+ /*
367
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::Float
368
+ * :nodoc:
369
+ */
370
+
371
+ /*
372
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::Hash
373
+ * :nodoc:
374
+ */
375
+
376
+ /*
377
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::Integer
378
+ * :nodoc:
379
+ */
380
+
381
+ /*
382
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::NilClass
383
+ * :nodoc:
384
+ */
385
+
386
+ /*
387
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::Object
388
+ * :nodoc:
389
+ */
390
+
391
+ /*
392
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::String
393
+ * :nodoc:
394
+ */
395
+
396
+ /*
397
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::String::Extend
398
+ * :nodoc:
399
+ */
400
+
401
+ /*
402
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::TrueClass
403
+ * :nodoc:
404
+ */
405
+
331
406
  /*
332
407
  * call-seq: to_json(state = nil)
333
408
  *
@@ -539,13 +614,18 @@ static size_t State_memsize(const void *ptr)
539
614
  return size;
540
615
  }
541
616
 
617
+ #ifndef HAVE_RB_EXT_RACTOR_SAFE
618
+ # undef RUBY_TYPED_FROZEN_SHAREABLE
619
+ # define RUBY_TYPED_FROZEN_SHAREABLE 0
620
+ #endif
621
+
542
622
  #ifdef NEW_TYPEDDATA_WRAPPER
543
623
  static const rb_data_type_t JSON_Generator_State_type = {
544
624
  "JSON/Generator/State",
545
625
  {NULL, State_free, State_memsize,},
546
626
  #ifdef RUBY_TYPED_FREE_IMMEDIATELY
547
627
  0, 0,
548
- RUBY_TYPED_FREE_IMMEDIATELY,
628
+ RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_FROZEN_SHAREABLE,
549
629
  #endif
550
630
  };
551
631
  #endif
@@ -646,6 +726,8 @@ static VALUE cState_configure(VALUE self, VALUE opts)
646
726
  state->allow_nan = RTEST(tmp);
647
727
  tmp = rb_hash_aref(opts, ID2SYM(i_ascii_only));
648
728
  state->ascii_only = RTEST(tmp);
729
+ tmp = rb_hash_aref(opts, ID2SYM(i_escape_slash));
730
+ state->escape_slash = RTEST(tmp);
649
731
  return self;
650
732
  }
651
733
 
@@ -680,6 +762,7 @@ static VALUE cState_to_h(VALUE self)
680
762
  rb_hash_aset(result, ID2SYM(i_allow_nan), state->allow_nan ? Qtrue : Qfalse);
681
763
  rb_hash_aset(result, ID2SYM(i_ascii_only), state->ascii_only ? Qtrue : Qfalse);
682
764
  rb_hash_aset(result, ID2SYM(i_max_nesting), LONG2FIX(state->max_nesting));
765
+ rb_hash_aset(result, ID2SYM(i_escape_slash), state->escape_slash ? Qtrue : Qfalse);
683
766
  rb_hash_aset(result, ID2SYM(i_depth), LONG2FIX(state->depth));
684
767
  rb_hash_aset(result, ID2SYM(i_buffer_initial_length), LONG2FIX(state->buffer_initial_length));
685
768
  return result;
@@ -860,13 +943,13 @@ static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_S
860
943
  fbuffer_append_char(buffer, '"');
861
944
  #ifdef HAVE_RUBY_ENCODING_H
862
945
  if (!enc_utf8_compatible_p(rb_enc_get(obj))) {
863
- obj = rb_str_encode(obj, CEncoding_UTF_8, 0, Qnil);
946
+ obj = rb_str_export_to_enc(obj, rb_utf8_encoding());
864
947
  }
865
948
  #endif
866
949
  if (state->ascii_only) {
867
- convert_UTF8_to_JSON_ASCII(buffer, obj);
950
+ convert_UTF8_to_JSON_ASCII(buffer, obj, state->escape_slash);
868
951
  } else {
869
- convert_UTF8_to_JSON(buffer, obj);
952
+ convert_UTF8_to_JSON(buffer, obj, state->escape_slash);
870
953
  }
871
954
  fbuffer_append_char(buffer, '"');
872
955
  }
@@ -1026,7 +1109,7 @@ static VALUE cState_generate(VALUE self, VALUE obj)
1026
1109
  * generated, otherwise an exception is thrown, if these values are
1027
1110
  * encountered. This options defaults to false.
1028
1111
  * * *ascii_only*: true if only ASCII characters should be generated. This
1029
- * ontions defaults to false.
1112
+ * option defaults to false.
1030
1113
  * * *buffer_initial_length*: sets the initial length of the generator's
1031
1114
  * internal buffer.
1032
1115
  */
@@ -1082,8 +1165,7 @@ static VALUE cState_from_state_s(VALUE self, VALUE opts)
1082
1165
  } else if (rb_obj_is_kind_of(opts, rb_cHash)) {
1083
1166
  return rb_funcall(self, i_new, 1, opts);
1084
1167
  } else {
1085
- VALUE prototype = rb_const_get(mJSON, i_SAFE_STATE_PROTOTYPE);
1086
- return rb_funcall(prototype, i_dup, 0);
1168
+ return rb_class_new_instance(0, NULL, cState);
1087
1169
  }
1088
1170
  }
1089
1171
 
@@ -1307,6 +1389,31 @@ static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
1307
1389
  return state->max_nesting = FIX2LONG(depth);
1308
1390
  }
1309
1391
 
1392
+ /*
1393
+ * call-seq: escape_slash
1394
+ *
1395
+ * If this boolean is true, the forward slashes will be escaped in
1396
+ * the json output.
1397
+ */
1398
+ static VALUE cState_escape_slash(VALUE self)
1399
+ {
1400
+ GET_STATE(self);
1401
+ return state->escape_slash ? Qtrue : Qfalse;
1402
+ }
1403
+
1404
+ /*
1405
+ * call-seq: escape_slash=(depth)
1406
+ *
1407
+ * This sets whether or not the forward slashes will be escaped in
1408
+ * the json output.
1409
+ */
1410
+ static VALUE cState_escape_slash_set(VALUE self, VALUE enable)
1411
+ {
1412
+ GET_STATE(self);
1413
+ state->escape_slash = RTEST(enable);
1414
+ return Qnil;
1415
+ }
1416
+
1310
1417
  /*
1311
1418
  * call-seq: allow_nan?
1312
1419
  *
@@ -1390,6 +1497,10 @@ static VALUE cState_buffer_initial_length_set(VALUE self, VALUE buffer_initial_l
1390
1497
  */
1391
1498
  void Init_generator(void)
1392
1499
  {
1500
+ #ifdef HAVE_RB_EXT_RACTOR_SAFE
1501
+ rb_ext_ractor_safe(true);
1502
+ #endif
1503
+
1393
1504
  #undef rb_intern
1394
1505
  rb_require("json/common");
1395
1506
 
@@ -1419,6 +1530,9 @@ void Init_generator(void)
1419
1530
  rb_define_method(cState, "array_nl=", cState_array_nl_set, 1);
1420
1531
  rb_define_method(cState, "max_nesting", cState_max_nesting, 0);
1421
1532
  rb_define_method(cState, "max_nesting=", cState_max_nesting_set, 1);
1533
+ rb_define_method(cState, "escape_slash", cState_escape_slash, 0);
1534
+ rb_define_method(cState, "escape_slash?", cState_escape_slash, 0);
1535
+ rb_define_method(cState, "escape_slash=", cState_escape_slash_set, 1);
1422
1536
  rb_define_method(cState, "check_circular?", cState_check_circular_p, 0);
1423
1537
  rb_define_method(cState, "allow_nan?", cState_allow_nan_p, 0);
1424
1538
  rb_define_method(cState, "ascii_only?", cState_ascii_only_p, 0);
@@ -1475,6 +1589,7 @@ void Init_generator(void)
1475
1589
  i_object_nl = rb_intern("object_nl");
1476
1590
  i_array_nl = rb_intern("array_nl");
1477
1591
  i_max_nesting = rb_intern("max_nesting");
1592
+ i_escape_slash = rb_intern("escape_slash");
1478
1593
  i_allow_nan = rb_intern("allow_nan");
1479
1594
  i_ascii_only = rb_intern("ascii_only");
1480
1595
  i_depth = rb_intern("depth");
@@ -1490,10 +1605,4 @@ void Init_generator(void)
1490
1605
  i_match = rb_intern("match");
1491
1606
  i_keys = rb_intern("keys");
1492
1607
  i_dup = rb_intern("dup");
1493
- #ifdef HAVE_RUBY_ENCODING_H
1494
- CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
1495
- i_encoding = rb_intern("encoding");
1496
- i_encode = rb_intern("encode");
1497
- #endif
1498
- i_SAFE_STATE_PROTOTYPE = rb_intern("SAFE_STATE_PROTOTYPE");
1499
1608
  }
@@ -49,8 +49,8 @@ static const UTF32 halfMask = 0x3FFUL;
49
49
  static unsigned char isLegalUTF8(const UTF8 *source, unsigned long length);
50
50
  static void unicode_escape(char *buf, UTF16 character);
51
51
  static void unicode_escape_to_buffer(FBuffer *buffer, char buf[6], UTF16 character);
52
- static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string);
53
- static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string);
52
+ static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string, char escape_slash);
53
+ static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string, char escape_slash);
54
54
  static char *fstrndup(const char *ptr, unsigned long len);
55
55
 
56
56
  /* ruby api and some helpers */
@@ -72,6 +72,7 @@ typedef struct JSON_Generator_StateStruct {
72
72
  long max_nesting;
73
73
  char allow_nan;
74
74
  char ascii_only;
75
+ char escape_slash;
75
76
  long depth;
76
77
  long buffer_initial_length;
77
78
  } JSON_Generator_State;
@@ -150,6 +151,8 @@ static VALUE cState_allow_nan_p(VALUE self);
150
151
  static VALUE cState_ascii_only_p(VALUE self);
151
152
  static VALUE cState_depth(VALUE self);
152
153
  static VALUE cState_depth_set(VALUE self, VALUE depth);
154
+ static VALUE cState_escape_slash(VALUE self);
155
+ static VALUE cState_escape_slash_set(VALUE self, VALUE depth);
153
156
  static FBuffer *cState_prepare_buffer(VALUE self);
154
157
  #ifndef ZALLOC
155
158
  #define ZALLOC(type) ((type *)ruby_zalloc(sizeof(type)))
@@ -2,5 +2,31 @@
2
2
  require 'mkmf'
3
3
 
4
4
  have_func("rb_enc_raise", "ruby.h")
5
+ have_func("rb_enc_interned_str", "ruby.h")
6
+
7
+ # checking if String#-@ (str_uminus) dedupes... '
8
+ begin
9
+ a = -(%w(t e s t).join)
10
+ b = -(%w(t e s t).join)
11
+ if a.equal?(b)
12
+ $CFLAGS << ' -DSTR_UMINUS_DEDUPE=1 '
13
+ else
14
+ $CFLAGS << ' -DSTR_UMINUS_DEDUPE=0 '
15
+ end
16
+ rescue NoMethodError
17
+ $CFLAGS << ' -DSTR_UMINUS_DEDUPE=0 '
18
+ end
19
+
20
+ # checking if String#-@ (str_uminus) directly interns frozen strings... '
21
+ begin
22
+ s = rand.to_s.freeze
23
+ if (-s).equal?(s) && (-s.dup).equal?(s)
24
+ $CFLAGS << ' -DSTR_UMINUS_DEDUPE_FROZEN=1 '
25
+ else
26
+ $CFLAGS << ' -DSTR_UMINUS_DEDUPE_FROZEN=0 '
27
+ end
28
+ rescue NoMethodError
29
+ $CFLAGS << ' -DSTR_UMINUS_DEDUPE_FROZEN=0 '
30
+ end
5
31
 
6
32
  create_makefile 'json/ext/parser'