json 2.0.3 → 2.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGES.md +66 -0
  3. data/Gemfile +1 -3
  4. data/LICENSE +56 -0
  5. data/README.md +54 -21
  6. data/VERSION +1 -1
  7. data/ext/json/ext/fbuffer/fbuffer.h +0 -3
  8. data/ext/json/ext/generator/generator.c +229 -54
  9. data/ext/json/ext/generator/generator.h +5 -3
  10. data/ext/json/ext/parser/extconf.rb +25 -0
  11. data/ext/json/ext/parser/parser.c +180 -85
  12. data/ext/json/ext/parser/parser.h +2 -0
  13. data/ext/json/ext/parser/parser.rl +104 -9
  14. data/ext/json/extconf.rb +1 -0
  15. data/json.gemspec +0 -0
  16. data/lib/json/add/bigdecimal.rb +2 -2
  17. data/lib/json/add/complex.rb +2 -3
  18. data/lib/json/add/ostruct.rb +1 -1
  19. data/lib/json/add/rational.rb +2 -3
  20. data/lib/json/add/regexp.rb +2 -2
  21. data/lib/json/add/set.rb +29 -0
  22. data/lib/json/common.rb +372 -125
  23. data/lib/json/pure/generator.rb +31 -10
  24. data/lib/json/pure/parser.rb +35 -5
  25. data/lib/json/version.rb +1 -1
  26. data/lib/json.rb +549 -29
  27. data/tests/fixtures/fail29.json +1 -0
  28. data/tests/fixtures/fail30.json +1 -0
  29. data/tests/fixtures/fail31.json +1 -0
  30. data/tests/fixtures/fail32.json +1 -0
  31. data/tests/json_addition_test.rb +6 -0
  32. data/tests/json_common_interface_test.rb +47 -4
  33. data/tests/json_encoding_test.rb +2 -0
  34. data/tests/json_fixtures_test.rb +9 -1
  35. data/tests/json_generator_test.rb +30 -8
  36. data/tests/json_parser_test.rb +43 -12
  37. data/tests/lib/core_assertions.rb +763 -0
  38. data/tests/lib/envutil.rb +365 -0
  39. data/tests/lib/find_executable.rb +22 -0
  40. data/tests/lib/helper.rb +4 -0
  41. data/tests/ractor_test.rb +30 -0
  42. data/tests/test_helper.rb +3 -7
  43. metadata +31 -44
  44. data/.gitignore +0 -17
  45. data/.travis.yml +0 -19
  46. data/README-json-jruby.md +0 -33
  47. data/Rakefile +0 -408
  48. data/data/example.json +0 -1
  49. data/data/index.html +0 -38
  50. data/data/prototype.js +0 -4184
  51. data/diagrams/.keep +0 -0
  52. data/install.rb +0 -23
  53. data/java/src/json/ext/ByteListTranscoder.java +0 -166
  54. data/java/src/json/ext/Generator.java +0 -443
  55. data/java/src/json/ext/GeneratorMethods.java +0 -231
  56. data/java/src/json/ext/GeneratorService.java +0 -42
  57. data/java/src/json/ext/GeneratorState.java +0 -490
  58. data/java/src/json/ext/OptionsReader.java +0 -113
  59. data/java/src/json/ext/Parser.java +0 -2347
  60. data/java/src/json/ext/Parser.rl +0 -878
  61. data/java/src/json/ext/ParserService.java +0 -34
  62. data/java/src/json/ext/RuntimeInfo.java +0 -116
  63. data/java/src/json/ext/StringDecoder.java +0 -166
  64. data/java/src/json/ext/StringEncoder.java +0 -111
  65. data/java/src/json/ext/Utils.java +0 -88
  66. data/json-java.gemspec +0 -38
  67. data/json_pure.gemspec +0 -38
  68. data/references/rfc7159.txt +0 -899
  69. data/tools/diff.sh +0 -18
  70. data/tools/fuzz.rb +0 -131
  71. data/tools/server.rb +0 -62
@@ -15,14 +15,13 @@ static VALUE mJSON, mExt, mGenerator, cState, mGeneratorMethods, mObject,
15
15
  #endif
16
16
  mFloat, mString, mString_Extend,
17
17
  mTrueClass, mFalseClass, mNilClass, eGeneratorError,
18
- eNestingError, CRegexp_MULTILINE, CJSON_SAFE_STATE_PROTOTYPE,
19
- i_SAFE_STATE_PROTOTYPE;
18
+ eNestingError;
20
19
 
21
20
  static ID i_to_s, i_to_json, i_new, i_indent, i_space, i_space_before,
22
21
  i_object_nl, i_array_nl, i_max_nesting, i_allow_nan, i_ascii_only,
23
22
  i_pack, i_unpack, i_create_id, i_extend, i_key_p,
24
23
  i_aref, i_send, i_respond_to_p, i_match, i_keys, i_depth,
25
- i_buffer_initial_length, i_dup;
24
+ i_buffer_initial_length, i_dup, i_escape_slash;
26
25
 
27
26
  /*
28
27
  * Copyright 2001-2004 Unicode, Inc.
@@ -130,7 +129,7 @@ static void unicode_escape_to_buffer(FBuffer *buffer, char buf[6], UTF16
130
129
 
131
130
  /* Converts string to a JSON string in FBuffer buffer, where all but the ASCII
132
131
  * and control characters are JSON escaped. */
133
- static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string)
132
+ static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string, char escape_slash)
134
133
  {
135
134
  const UTF8 *source = (UTF8 *) RSTRING_PTR(string);
136
135
  const UTF8 *sourceEnd = source + RSTRING_LEN(string);
@@ -180,6 +179,11 @@ static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string)
180
179
  case '"':
181
180
  fbuffer_append(buffer, "\\\"", 2);
182
181
  break;
182
+ case '/':
183
+ if(escape_slash) {
184
+ fbuffer_append(buffer, "\\/", 2);
185
+ break;
186
+ }
183
187
  default:
184
188
  fbuffer_append_char(buffer, (char)ch);
185
189
  break;
@@ -229,7 +233,7 @@ static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string)
229
233
  * characters required by the JSON standard are JSON escaped. The remaining
230
234
  * characters (should be UTF8) are just passed through and appended to the
231
235
  * result. */
232
- static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string)
236
+ static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string, char escape_slash)
233
237
  {
234
238
  const char *ptr = RSTRING_PTR(string), *p;
235
239
  unsigned long len = RSTRING_LEN(string), start = 0, end = 0;
@@ -237,6 +241,7 @@ static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string)
237
241
  int escape_len;
238
242
  unsigned char c;
239
243
  char buf[6] = { '\\', 'u' };
244
+ int ascii_only = rb_enc_str_asciionly_p(string);
240
245
 
241
246
  for (start = 0, end = 0; end < len;) {
242
247
  p = ptr + end;
@@ -279,16 +284,25 @@ static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string)
279
284
  escape = "\\\"";
280
285
  escape_len = 2;
281
286
  break;
287
+ case '/':
288
+ if(escape_slash) {
289
+ escape = "\\/";
290
+ escape_len = 2;
291
+ break;
292
+ }
282
293
  default:
283
294
  {
284
- unsigned short clen = trailingBytesForUTF8[c] + 1;
285
- if (end + clen > len) {
286
- rb_raise(rb_path2class("JSON::GeneratorError"),
287
- "partial character in source, but hit end");
288
- }
289
- if (!isLegalUTF8((UTF8 *) p, clen)) {
290
- rb_raise(rb_path2class("JSON::GeneratorError"),
291
- "source sequence is illegal/malformed utf-8");
295
+ unsigned short clen = 1;
296
+ if (!ascii_only) {
297
+ clen += trailingBytesForUTF8[c];
298
+ if (end + clen > len) {
299
+ rb_raise(rb_path2class("JSON::GeneratorError"),
300
+ "partial character in source, but hit end");
301
+ }
302
+ if (!isLegalUTF8((UTF8 *) p, clen)) {
303
+ rb_raise(rb_path2class("JSON::GeneratorError"),
304
+ "source sequence is illegal/malformed utf-8");
305
+ }
292
306
  }
293
307
  end += clen;
294
308
  }
@@ -308,7 +322,7 @@ static char *fstrndup(const char *ptr, unsigned long len) {
308
322
  char *result;
309
323
  if (len <= 0) return NULL;
310
324
  result = ALLOC_N(char, len);
311
- memccpy(result, ptr, 0, len);
325
+ memcpy(result, ptr, len);
312
326
  return result;
313
327
  }
314
328
 
@@ -324,6 +338,76 @@ static char *fstrndup(const char *ptr, unsigned long len) {
324
338
  *
325
339
  */
326
340
 
341
+ /* Explanation of the following: that's the only way to not pollute
342
+ * standard library's docs with GeneratorMethods::<ClassName> which
343
+ * are uninformative and take a large place in a list of classes
344
+ */
345
+
346
+ /*
347
+ * Document-module: JSON::Ext::Generator::GeneratorMethods
348
+ * :nodoc:
349
+ */
350
+
351
+ /*
352
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::Array
353
+ * :nodoc:
354
+ */
355
+
356
+ /*
357
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::Bignum
358
+ * :nodoc:
359
+ */
360
+
361
+ /*
362
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::FalseClass
363
+ * :nodoc:
364
+ */
365
+
366
+ /*
367
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::Fixnum
368
+ * :nodoc:
369
+ */
370
+
371
+ /*
372
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::Float
373
+ * :nodoc:
374
+ */
375
+
376
+ /*
377
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::Hash
378
+ * :nodoc:
379
+ */
380
+
381
+ /*
382
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::Integer
383
+ * :nodoc:
384
+ */
385
+
386
+ /*
387
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::NilClass
388
+ * :nodoc:
389
+ */
390
+
391
+ /*
392
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::Object
393
+ * :nodoc:
394
+ */
395
+
396
+ /*
397
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::String
398
+ * :nodoc:
399
+ */
400
+
401
+ /*
402
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::String::Extend
403
+ * :nodoc:
404
+ */
405
+
406
+ /*
407
+ * Document-module: JSON::Ext::Generator::GeneratorMethods::TrueClass
408
+ * :nodoc:
409
+ */
410
+
327
411
  /*
328
412
  * call-seq: to_json(state = nil)
329
413
  *
@@ -535,13 +619,18 @@ static size_t State_memsize(const void *ptr)
535
619
  return size;
536
620
  }
537
621
 
622
+ #ifndef HAVE_RB_EXT_RACTOR_SAFE
623
+ # undef RUBY_TYPED_FROZEN_SHAREABLE
624
+ # define RUBY_TYPED_FROZEN_SHAREABLE 0
625
+ #endif
626
+
538
627
  #ifdef NEW_TYPEDDATA_WRAPPER
539
628
  static const rb_data_type_t JSON_Generator_State_type = {
540
629
  "JSON/Generator/State",
541
630
  {NULL, State_free, State_memsize,},
542
631
  #ifdef RUBY_TYPED_FREE_IMMEDIATELY
543
632
  0, 0,
544
- RUBY_TYPED_FREE_IMMEDIATELY,
633
+ RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_FROZEN_SHAREABLE,
545
634
  #endif
546
635
  };
547
636
  #endif
@@ -642,6 +731,8 @@ static VALUE cState_configure(VALUE self, VALUE opts)
642
731
  state->allow_nan = RTEST(tmp);
643
732
  tmp = rb_hash_aref(opts, ID2SYM(i_ascii_only));
644
733
  state->ascii_only = RTEST(tmp);
734
+ tmp = rb_hash_aref(opts, ID2SYM(i_escape_slash));
735
+ state->escape_slash = RTEST(tmp);
645
736
  return self;
646
737
  }
647
738
 
@@ -676,6 +767,7 @@ static VALUE cState_to_h(VALUE self)
676
767
  rb_hash_aset(result, ID2SYM(i_allow_nan), state->allow_nan ? Qtrue : Qfalse);
677
768
  rb_hash_aset(result, ID2SYM(i_ascii_only), state->ascii_only ? Qtrue : Qfalse);
678
769
  rb_hash_aset(result, ID2SYM(i_max_nesting), LONG2FIX(state->max_nesting));
770
+ rb_hash_aset(result, ID2SYM(i_escape_slash), state->escape_slash ? Qtrue : Qfalse);
679
771
  rb_hash_aset(result, ID2SYM(i_depth), LONG2FIX(state->depth));
680
772
  rb_hash_aset(result, ID2SYM(i_buffer_initial_length), LONG2FIX(state->buffer_initial_length));
681
773
  return result;
@@ -692,7 +784,7 @@ static VALUE cState_aref(VALUE self, VALUE name)
692
784
  if (RTEST(rb_funcall(self, i_respond_to_p, 1, name))) {
693
785
  return rb_funcall(self, i_send, 1, name);
694
786
  } else {
695
- return rb_ivar_get(self, rb_intern_str(rb_str_concat(rb_str_new2("@"), name)));
787
+ return rb_attr_get(self, rb_intern_str(rb_str_concat(rb_str_new2("@"), name)));
696
788
  }
697
789
  }
698
790
 
@@ -715,43 +807,83 @@ static VALUE cState_aset(VALUE self, VALUE name, VALUE value)
715
807
  return Qnil;
716
808
  }
717
809
 
718
- static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
810
+ struct hash_foreach_arg {
811
+ FBuffer *buffer;
812
+ JSON_Generator_State *state;
813
+ VALUE Vstate;
814
+ int iter;
815
+ };
816
+
817
+ static int
818
+ json_object_i(VALUE key, VALUE val, VALUE _arg)
719
819
  {
820
+ struct hash_foreach_arg *arg = (struct hash_foreach_arg *)_arg;
821
+ FBuffer *buffer = arg->buffer;
822
+ JSON_Generator_State *state = arg->state;
823
+ VALUE Vstate = arg->Vstate;
824
+
720
825
  char *object_nl = state->object_nl;
721
826
  long object_nl_len = state->object_nl_len;
722
827
  char *indent = state->indent;
723
828
  long indent_len = state->indent_len;
724
- long max_nesting = state->max_nesting;
725
829
  char *delim = FBUFFER_PTR(state->object_delim);
726
830
  long delim_len = FBUFFER_LEN(state->object_delim);
727
831
  char *delim2 = FBUFFER_PTR(state->object_delim2);
728
832
  long delim2_len = FBUFFER_LEN(state->object_delim2);
833
+ long depth = state->depth;
834
+ int j;
835
+ VALUE klass, key_to_s;
836
+
837
+ if (arg->iter > 0) fbuffer_append(buffer, delim, delim_len);
838
+ if (object_nl) {
839
+ fbuffer_append(buffer, object_nl, object_nl_len);
840
+ }
841
+ if (indent) {
842
+ for (j = 0; j < depth; j++) {
843
+ fbuffer_append(buffer, indent, indent_len);
844
+ }
845
+ }
846
+
847
+ klass = CLASS_OF(key);
848
+ if (klass == rb_cString) {
849
+ key_to_s = key;
850
+ } else if (klass == rb_cSymbol) {
851
+ key_to_s = rb_id2str(SYM2ID(key));
852
+ } else {
853
+ key_to_s = rb_funcall(key, i_to_s, 0);
854
+ }
855
+ Check_Type(key_to_s, T_STRING);
856
+ generate_json(buffer, Vstate, state, key_to_s);
857
+ fbuffer_append(buffer, delim2, delim2_len);
858
+ generate_json(buffer, Vstate, state, val);
859
+
860
+ arg->iter++;
861
+ return ST_CONTINUE;
862
+ }
863
+
864
+ static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
865
+ {
866
+ char *object_nl = state->object_nl;
867
+ long object_nl_len = state->object_nl_len;
868
+ char *indent = state->indent;
869
+ long indent_len = state->indent_len;
870
+ long max_nesting = state->max_nesting;
729
871
  long depth = ++state->depth;
730
- int i, j;
731
- VALUE key, key_to_s, keys;
872
+ int j;
873
+ struct hash_foreach_arg arg;
874
+
732
875
  if (max_nesting != 0 && depth > max_nesting) {
733
876
  fbuffer_free(buffer);
734
877
  rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
735
878
  }
736
879
  fbuffer_append_char(buffer, '{');
737
- keys = rb_funcall(obj, i_keys, 0);
738
- for(i = 0; i < RARRAY_LEN(keys); i++) {
739
- if (i > 0) fbuffer_append(buffer, delim, delim_len);
740
- if (object_nl) {
741
- fbuffer_append(buffer, object_nl, object_nl_len);
742
- }
743
- if (indent) {
744
- for (j = 0; j < depth; j++) {
745
- fbuffer_append(buffer, indent, indent_len);
746
- }
747
- }
748
- key = rb_ary_entry(keys, i);
749
- key_to_s = rb_funcall(key, i_to_s, 0);
750
- Check_Type(key_to_s, T_STRING);
751
- generate_json(buffer, Vstate, state, key_to_s);
752
- fbuffer_append(buffer, delim2, delim2_len);
753
- generate_json(buffer, Vstate, state, rb_hash_aref(obj, key));
754
- }
880
+
881
+ arg.buffer = buffer;
882
+ arg.state = state;
883
+ arg.Vstate = Vstate;
884
+ arg.iter = 0;
885
+ rb_hash_foreach(obj, json_object_i, (VALUE)&arg);
886
+
755
887
  depth = --state->depth;
756
888
  if (object_nl) {
757
889
  fbuffer_append(buffer, object_nl, object_nl_len);
@@ -802,16 +934,27 @@ static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_St
802
934
  fbuffer_append_char(buffer, ']');
803
935
  }
804
936
 
937
+ #ifdef HAVE_RUBY_ENCODING_H
938
+ static int enc_utf8_compatible_p(rb_encoding *enc)
939
+ {
940
+ if (enc == rb_usascii_encoding()) return 1;
941
+ if (enc == rb_utf8_encoding()) return 1;
942
+ return 0;
943
+ }
944
+ #endif
945
+
805
946
  static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
806
947
  {
807
948
  fbuffer_append_char(buffer, '"');
808
949
  #ifdef HAVE_RUBY_ENCODING_H
809
- obj = rb_funcall(obj, i_encode, 1, CEncoding_UTF_8);
950
+ if (!enc_utf8_compatible_p(rb_enc_get(obj))) {
951
+ obj = rb_str_encode(obj, CEncoding_UTF_8, 0, Qnil);
952
+ }
810
953
  #endif
811
954
  if (state->ascii_only) {
812
- convert_UTF8_to_JSON_ASCII(buffer, obj);
955
+ convert_UTF8_to_JSON_ASCII(buffer, obj, state->escape_slash);
813
956
  } else {
814
- convert_UTF8_to_JSON(buffer, obj);
957
+ convert_UTF8_to_JSON(buffer, obj, state->escape_slash);
815
958
  }
816
959
  fbuffer_append_char(buffer, '"');
817
960
  }
@@ -970,6 +1113,8 @@ static VALUE cState_generate(VALUE self, VALUE obj)
970
1113
  * * *allow_nan*: true if NaN, Infinity, and -Infinity should be
971
1114
  * generated, otherwise an exception is thrown, if these values are
972
1115
  * encountered. This options defaults to false.
1116
+ * * *ascii_only*: true if only ASCII characters should be generated. This
1117
+ * option defaults to false.
973
1118
  * * *buffer_initial_length*: sets the initial length of the generator's
974
1119
  * internal buffer.
975
1120
  */
@@ -1025,10 +1170,7 @@ static VALUE cState_from_state_s(VALUE self, VALUE opts)
1025
1170
  } else if (rb_obj_is_kind_of(opts, rb_cHash)) {
1026
1171
  return rb_funcall(self, i_new, 1, opts);
1027
1172
  } else {
1028
- if (NIL_P(CJSON_SAFE_STATE_PROTOTYPE)) {
1029
- CJSON_SAFE_STATE_PROTOTYPE = rb_const_get(mJSON, i_SAFE_STATE_PROTOTYPE);
1030
- }
1031
- return rb_funcall(CJSON_SAFE_STATE_PROTOTYPE, i_dup, 0);
1173
+ return rb_class_new_instance(0, NULL, cState);
1032
1174
  }
1033
1175
  }
1034
1176
 
@@ -1062,7 +1204,7 @@ static VALUE cState_indent_set(VALUE self, VALUE indent)
1062
1204
  }
1063
1205
  } else {
1064
1206
  if (state->indent) ruby_xfree(state->indent);
1065
- state->indent = strdup(RSTRING_PTR(indent));
1207
+ state->indent = fstrndup(RSTRING_PTR(indent), len);
1066
1208
  state->indent_len = len;
1067
1209
  }
1068
1210
  return Qnil;
@@ -1100,7 +1242,7 @@ static VALUE cState_space_set(VALUE self, VALUE space)
1100
1242
  }
1101
1243
  } else {
1102
1244
  if (state->space) ruby_xfree(state->space);
1103
- state->space = strdup(RSTRING_PTR(space));
1245
+ state->space = fstrndup(RSTRING_PTR(space), len);
1104
1246
  state->space_len = len;
1105
1247
  }
1106
1248
  return Qnil;
@@ -1136,7 +1278,7 @@ static VALUE cState_space_before_set(VALUE self, VALUE space_before)
1136
1278
  }
1137
1279
  } else {
1138
1280
  if (state->space_before) ruby_xfree(state->space_before);
1139
- state->space_before = strdup(RSTRING_PTR(space_before));
1281
+ state->space_before = fstrndup(RSTRING_PTR(space_before), len);
1140
1282
  state->space_before_len = len;
1141
1283
  }
1142
1284
  return Qnil;
@@ -1173,7 +1315,7 @@ static VALUE cState_object_nl_set(VALUE self, VALUE object_nl)
1173
1315
  }
1174
1316
  } else {
1175
1317
  if (state->object_nl) ruby_xfree(state->object_nl);
1176
- state->object_nl = strdup(RSTRING_PTR(object_nl));
1318
+ state->object_nl = fstrndup(RSTRING_PTR(object_nl), len);
1177
1319
  state->object_nl_len = len;
1178
1320
  }
1179
1321
  return Qnil;
@@ -1208,7 +1350,7 @@ static VALUE cState_array_nl_set(VALUE self, VALUE array_nl)
1208
1350
  }
1209
1351
  } else {
1210
1352
  if (state->array_nl) ruby_xfree(state->array_nl);
1211
- state->array_nl = strdup(RSTRING_PTR(array_nl));
1353
+ state->array_nl = fstrndup(RSTRING_PTR(array_nl), len);
1212
1354
  state->array_nl_len = len;
1213
1355
  }
1214
1356
  return Qnil;
@@ -1252,6 +1394,31 @@ static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
1252
1394
  return state->max_nesting = FIX2LONG(depth);
1253
1395
  }
1254
1396
 
1397
+ /*
1398
+ * call-seq: escape_slash
1399
+ *
1400
+ * If this boolean is true, the forward slashes will be escaped in
1401
+ * the json output.
1402
+ */
1403
+ static VALUE cState_escape_slash(VALUE self)
1404
+ {
1405
+ GET_STATE(self);
1406
+ return state->escape_slash ? Qtrue : Qfalse;
1407
+ }
1408
+
1409
+ /*
1410
+ * call-seq: escape_slash=(depth)
1411
+ *
1412
+ * This sets whether or not the forward slashes will be escaped in
1413
+ * the json output.
1414
+ */
1415
+ static VALUE cState_escape_slash_set(VALUE self, VALUE enable)
1416
+ {
1417
+ GET_STATE(self);
1418
+ state->escape_slash = RTEST(enable);
1419
+ return Qnil;
1420
+ }
1421
+
1255
1422
  /*
1256
1423
  * call-seq: allow_nan?
1257
1424
  *
@@ -1267,7 +1434,7 @@ static VALUE cState_allow_nan_p(VALUE self)
1267
1434
  /*
1268
1435
  * call-seq: ascii_only?
1269
1436
  *
1270
- * Returns true, if NaN, Infinity, and -Infinity should be generated, otherwise
1437
+ * Returns true, if only ASCII characters should be generated. Otherwise
1271
1438
  * returns false.
1272
1439
  */
1273
1440
  static VALUE cState_ascii_only_p(VALUE self)
@@ -1335,6 +1502,11 @@ static VALUE cState_buffer_initial_length_set(VALUE self, VALUE buffer_initial_l
1335
1502
  */
1336
1503
  void Init_generator(void)
1337
1504
  {
1505
+ #ifdef HAVE_RB_EXT_RACTOR_SAFE
1506
+ rb_ext_ractor_safe(true);
1507
+ #endif
1508
+
1509
+ #undef rb_intern
1338
1510
  rb_require("json/common");
1339
1511
 
1340
1512
  mJSON = rb_define_module("JSON");
@@ -1343,6 +1515,8 @@ void Init_generator(void)
1343
1515
 
1344
1516
  eGeneratorError = rb_path2class("JSON::GeneratorError");
1345
1517
  eNestingError = rb_path2class("JSON::NestingError");
1518
+ rb_gc_register_mark_object(eGeneratorError);
1519
+ rb_gc_register_mark_object(eNestingError);
1346
1520
 
1347
1521
  cState = rb_define_class_under(mGenerator, "State", rb_cObject);
1348
1522
  rb_define_alloc_func(cState, cState_s_allocate);
@@ -1361,6 +1535,9 @@ void Init_generator(void)
1361
1535
  rb_define_method(cState, "array_nl=", cState_array_nl_set, 1);
1362
1536
  rb_define_method(cState, "max_nesting", cState_max_nesting, 0);
1363
1537
  rb_define_method(cState, "max_nesting=", cState_max_nesting_set, 1);
1538
+ rb_define_method(cState, "escape_slash", cState_escape_slash, 0);
1539
+ rb_define_method(cState, "escape_slash?", cState_escape_slash, 0);
1540
+ rb_define_method(cState, "escape_slash=", cState_escape_slash_set, 1);
1364
1541
  rb_define_method(cState, "check_circular?", cState_check_circular_p, 0);
1365
1542
  rb_define_method(cState, "allow_nan?", cState_allow_nan_p, 0);
1366
1543
  rb_define_method(cState, "ascii_only?", cState_ascii_only_p, 0);
@@ -1408,7 +1585,6 @@ void Init_generator(void)
1408
1585
  mNilClass = rb_define_module_under(mGeneratorMethods, "NilClass");
1409
1586
  rb_define_method(mNilClass, "to_json", mNilClass_to_json, -1);
1410
1587
 
1411
- CRegexp_MULTILINE = rb_const_get(rb_cRegexp, rb_intern("MULTILINE"));
1412
1588
  i_to_s = rb_intern("to_s");
1413
1589
  i_to_json = rb_intern("to_json");
1414
1590
  i_new = rb_intern("new");
@@ -1418,6 +1594,7 @@ void Init_generator(void)
1418
1594
  i_object_nl = rb_intern("object_nl");
1419
1595
  i_array_nl = rb_intern("array_nl");
1420
1596
  i_max_nesting = rb_intern("max_nesting");
1597
+ i_escape_slash = rb_intern("escape_slash");
1421
1598
  i_allow_nan = rb_intern("allow_nan");
1422
1599
  i_ascii_only = rb_intern("ascii_only");
1423
1600
  i_depth = rb_intern("depth");
@@ -1438,6 +1615,4 @@ void Init_generator(void)
1438
1615
  i_encoding = rb_intern("encoding");
1439
1616
  i_encode = rb_intern("encode");
1440
1617
  #endif
1441
- i_SAFE_STATE_PROTOTYPE = rb_intern("SAFE_STATE_PROTOTYPE");
1442
- CJSON_SAFE_STATE_PROTOTYPE = Qnil;
1443
1618
  }
@@ -1,7 +1,6 @@
1
1
  #ifndef _GENERATOR_H_
2
2
  #define _GENERATOR_H_
3
3
 
4
- #include <string.h>
5
4
  #include <math.h>
6
5
  #include <ctype.h>
7
6
 
@@ -50,8 +49,8 @@ static const UTF32 halfMask = 0x3FFUL;
50
49
  static unsigned char isLegalUTF8(const UTF8 *source, unsigned long length);
51
50
  static void unicode_escape(char *buf, UTF16 character);
52
51
  static void unicode_escape_to_buffer(FBuffer *buffer, char buf[6], UTF16 character);
53
- static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string);
54
- 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);
55
54
  static char *fstrndup(const char *ptr, unsigned long len);
56
55
 
57
56
  /* ruby api and some helpers */
@@ -73,6 +72,7 @@ typedef struct JSON_Generator_StateStruct {
73
72
  long max_nesting;
74
73
  char allow_nan;
75
74
  char ascii_only;
75
+ char escape_slash;
76
76
  long depth;
77
77
  long buffer_initial_length;
78
78
  } JSON_Generator_State;
@@ -151,6 +151,8 @@ static VALUE cState_allow_nan_p(VALUE self);
151
151
  static VALUE cState_ascii_only_p(VALUE self);
152
152
  static VALUE cState_depth(VALUE self);
153
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);
154
156
  static FBuffer *cState_prepare_buffer(VALUE self);
155
157
  #ifndef ZALLOC
156
158
  #define ZALLOC(type) ((type *)ruby_zalloc(sizeof(type)))
@@ -3,4 +3,29 @@ require 'mkmf'
3
3
 
4
4
  have_func("rb_enc_raise", "ruby.h")
5
5
 
6
+ # checking if String#-@ (str_uminus) dedupes... '
7
+ begin
8
+ a = -(%w(t e s t).join)
9
+ b = -(%w(t e s t).join)
10
+ if a.equal?(b)
11
+ $CFLAGS << ' -DSTR_UMINUS_DEDUPE=1 '
12
+ else
13
+ $CFLAGS << ' -DSTR_UMINUS_DEDUPE=0 '
14
+ end
15
+ rescue NoMethodError
16
+ $CFLAGS << ' -DSTR_UMINUS_DEDUPE=0 '
17
+ end
18
+
19
+ # checking if String#-@ (str_uminus) directly interns frozen strings... '
20
+ begin
21
+ s = rand.to_s.freeze
22
+ if (-s).equal?(s) && (-s.dup).equal?(s)
23
+ $CFLAGS << ' -DSTR_UMINUS_DEDUPE_FROZEN=1 '
24
+ else
25
+ $CFLAGS << ' -DSTR_UMINUS_DEDUPE_FROZEN=0 '
26
+ end
27
+ rescue NoMethodError
28
+ $CFLAGS << ' -DSTR_UMINUS_DEDUPE_FROZEN=0 '
29
+ end
30
+
6
31
  create_makefile 'json/ext/parser'