json 3.0.2 → 3.2.0

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.
@@ -27,14 +27,16 @@ typedef struct JSON_Generator_StateStruct {
27
27
  bool ascii_only;
28
28
  bool script_safe;
29
29
  bool strict;
30
+ bool rfc8785;
30
31
  VALUE sort_keys;
31
32
  } JSON_Generator_State;
32
33
 
33
- static VALUE mJSON, cState, cFragment, eGeneratorError, eNestingError, Encoding_UTF_8, default_sort_keys_proc;
34
+ static VALUE mJSON, cState, cFragment, eGeneratorError, eNestingError, Encoding_UTF_8, default_sort_keys_proc,
35
+ rfc8785_number_formatter_proc, rfc8785_sort_keys_proc;
34
36
 
35
37
  static ID i_to_s, i_to_json, i_new, i_encode;
36
38
  static VALUE sym_indent, sym_space, sym_space_before, sym_object_nl, sym_array_nl, sym_max_nesting, sym_allow_nan, sym_allow_duplicate_key,
37
- sym_ascii_only, sym_depth, sym_buffer_initial_length, sym_script_safe, sym_strict, sym_as_json, sym_sort_keys;
39
+ sym_ascii_only, sym_depth, sym_buffer_initial_length, sym_script_safe, sym_strict, sym_as_json, sym_sort_keys, sym_rfc8785;
38
40
 
39
41
 
40
42
  #define GET_STATE_TO(self, state) \
@@ -57,7 +59,12 @@ struct generate_json_data {
57
59
  long depth;
58
60
  };
59
61
 
62
+ #if defined(HAVE_SIMD) && defined(HAVE_SIMD_SSE2)
60
63
  static SIMD_Implementation simd_impl;
64
+ # define init_simd_impl() (simd_impl = find_simd_implementation())
65
+ #else
66
+ # define init_simd_impl() (void)0
67
+ #endif
61
68
 
62
69
  static VALUE cState_from_state_s(VALUE self, VALUE opts);
63
70
  static VALUE cState_partial_generate(VALUE self, VALUE obj, generator_func, VALUE io);
@@ -73,7 +80,7 @@ static void generate_json_bignum(FBuffer *buffer, struct generate_json_data *dat
73
80
  static void generate_json_float(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
74
81
  static void generate_json_fragment(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
75
82
 
76
- static int usascii_encindex, utf8_encindex, binary_encindex;
83
+ static int usascii_encindex, utf8_encindex;
77
84
 
78
85
  NORETURN(static void) raise_generator_error_str(VALUE invalid_object, VALUE str)
79
86
  {
@@ -97,8 +104,11 @@ NORETURN(static void) raise_generator_error(VALUE invalid_object, const char *fm
97
104
 
98
105
  // 0 - single byte char that don't need to be escaped.
99
106
  // (x | 8) - char that needs to be escaped.
100
- static const unsigned char CHAR_LENGTH_MASK = 7;
101
- static const unsigned char ESCAPE_MASK = 8;
107
+ #define CHAR_LENGTH_MASK 7
108
+ #define ESCAPE_MASK 8
109
+ #define HTML_ESCAPE_MASK 16
110
+ #define ESCAPE_LEN_1 9 // ESCAPE_MASK | 1
111
+ #define HTML_ESCAPE_LEN_1 25 // HTML_ESCAPE_MASK | ESCAPE_LEN_1
102
112
 
103
113
  typedef struct _search_state {
104
114
  const char *ptr;
@@ -148,6 +158,84 @@ static const unsigned char escape_table_basic[256] = {
148
158
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
149
159
  };
150
160
 
161
+ static const unsigned char script_safe_escape_table[256] = {
162
+ // ASCII Control Characters
163
+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
164
+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
165
+ // ASCII Characters
166
+ 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // '"'
167
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,25, 0, 0, 0, // '<'
168
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
169
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, // '\\'
170
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172
+ // Continuation byte
173
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
174
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
175
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
176
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
177
+ // First byte of a 2-byte code point
178
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
179
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
180
+ // First byte of a 3-byte code point
181
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
182
+ //First byte of a 4+ byte code point
183
+ 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 9, 9,
184
+ };
185
+
186
+ static const unsigned char ascii_only_escape_table[256] = {
187
+ // ASCII Control Characters
188
+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
189
+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
190
+ // ASCII Characters
191
+ 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // '"'
192
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
193
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
194
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, // '\\'
195
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
196
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
197
+ // Continuation byte
198
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
199
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
200
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
201
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
202
+ // First byte of a 2-byte code point
203
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
204
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
205
+ // First byte of a 3-byte code point
206
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
207
+ //First byte of a 4+ byte code point
208
+ 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 9, 9,
209
+ };
210
+
211
+ static inline bool unsafe_html_tag_p(const char *ptr, const char *end)
212
+ {
213
+ size_t remaining = end - ptr;
214
+
215
+ if (remaining < 4) return false;
216
+ if (memcmp(ptr, "<!--", 4) == 0) return true;
217
+
218
+ union { uint64_t num; char bytes[8]; } chunk;
219
+ if (remaining >= 8) {
220
+ memcpy(chunk.bytes, ptr, 8);
221
+ }
222
+ else if (remaining == 7) {
223
+ chunk.num = 0;
224
+ memcpy(chunk.bytes, ptr, 7);
225
+ }
226
+ else {
227
+ return false;
228
+ }
229
+
230
+ chunk.num |= 0x2020202020202020;
231
+
232
+ if (memcmp(chunk.bytes, "</script", 8) == 0) return true;
233
+
234
+ chunk.bytes[7] = 0;
235
+
236
+ return memcmp(chunk.bytes, "<script\0", 8) == 0;
237
+ }
238
+
151
239
  static inline unsigned char search_escape_basic(search_state *search)
152
240
  {
153
241
  while (search->ptr < search->end) {
@@ -197,8 +285,7 @@ ALWAYS_INLINE(static) void escape_UTF8_char_basic(search_state *search)
197
285
  *
198
286
  * - If out_ascii_only: non-ASCII characters (>0x7F)
199
287
  *
200
- * - If script_safe: forwardslash (/), line separator (U+2028), and
201
- * paragraph separator (U+2029)
288
+ * - If script_safe: script tags (`<script`, `</script`) and HTML comments (`<!--`).
202
289
  *
203
290
  * Everything else (should be UTF-8) is just passed through and
204
291
  * appended to the result.
@@ -242,7 +329,9 @@ static inline void escape_UTF8_char(search_state *search, unsigned char ch_len)
242
329
  {
243
330
  const unsigned char ch = (unsigned char)*search->ptr;
244
331
  switch (ch_len) {
245
- case 1: {
332
+ case ESCAPE_LEN_1: {
333
+ search_flush(search);
334
+
246
335
  switch (ch) {
247
336
  case '"': fbuffer_append(search->buffer, "\\\"", 2); break;
248
337
  case '\\': fbuffer_append(search->buffer, "\\\\", 2); break;
@@ -263,16 +352,18 @@ static inline void escape_UTF8_char(search_state *search, unsigned char ch_len)
263
352
  }
264
353
  break;
265
354
  }
266
- case 3: {
267
- if (search->ptr[2] & 1) {
268
- fbuffer_append(search->buffer, "\\u2029", 6);
355
+ case HTML_ESCAPE_LEN_1: {
356
+ if (unsafe_html_tag_p(search->ptr, search->end)) {
357
+ search_flush(search);
358
+ fbuffer_append(search->buffer, "\\u003c", 6);
269
359
  } else {
270
- fbuffer_append(search->buffer, "\\u2028", 6);
360
+ search->ptr++;
361
+ return; // no escape needed, resume search without updating cursor.
271
362
  }
272
363
  break;
273
364
  }
274
365
  }
275
- search->cursor = (search->ptr += ch_len);
366
+ search->cursor = (search->ptr += (ch_len & CHAR_LENGTH_MASK));
276
367
  }
277
368
 
278
369
  #ifdef HAVE_SIMD
@@ -505,31 +596,6 @@ ALWAYS_INLINE(static) TARGET_SSE2 unsigned char search_escape_basic_sse2(search_
505
596
 
506
597
  #endif /* HAVE_SIMD */
507
598
 
508
- static const unsigned char script_safe_escape_table[256] = {
509
- // ASCII Control Characters
510
- 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
511
- 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
512
- // ASCII Characters
513
- 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, // '"' and '/'
514
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
515
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
516
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, // '\\'
517
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
518
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
519
- // Continuation byte
520
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
521
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
522
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
523
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
524
- // First byte of a 2-byte code point
525
- 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
526
- 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
527
- // First byte of a 3-byte code point
528
- 3, 3,11, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0xE2 is the start of \u2028 and \u2029
529
- //First byte of a 4+ byte code point
530
- 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 9, 9,
531
- };
532
-
533
599
  static inline unsigned char search_script_safe_escape(search_state *search)
534
600
  {
535
601
  while (search->ptr < search->end) {
@@ -538,16 +604,9 @@ static inline unsigned char search_script_safe_escape(search_state *search)
538
604
 
539
605
  if (RB_UNLIKELY(ch_len)) {
540
606
  if (ch_len & ESCAPE_MASK) {
541
- if (RB_UNLIKELY(ch_len == 11)) {
542
- const unsigned char *uptr = (const unsigned char *)search->ptr;
543
- if (!(uptr[1] == 0x80 && (uptr[2] >> 1) == 0x54)) {
544
- search->ptr += 3;
545
- continue;
546
- }
547
- }
548
- search_flush(search);
549
- return ch_len & CHAR_LENGTH_MASK;
550
- } else {
607
+ return ch_len;
608
+ }
609
+ else {
551
610
  search->ptr += ch_len;
552
611
  }
553
612
  } else {
@@ -566,31 +625,6 @@ static void convert_UTF8_to_script_safe_JSON(search_state *search)
566
625
  }
567
626
  }
568
627
 
569
- static const unsigned char ascii_only_escape_table[256] = {
570
- // ASCII Control Characters
571
- 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
572
- 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
573
- // ASCII Characters
574
- 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // '"'
575
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
576
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
577
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, // '\\'
578
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
579
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
580
- // Continuation byte
581
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
582
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
583
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
584
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
585
- // First byte of a 2-byte code point
586
- 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
587
- 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
588
- // First byte of a 3-byte code point
589
- 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
590
- //First byte of a 4+ byte code point
591
- 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 9, 9,
592
- };
593
-
594
628
  static inline unsigned char search_ascii_only_escape(search_state *search, const unsigned char escape_table[256])
595
629
  {
596
630
  while (search->ptr < search->end) {
@@ -598,8 +632,7 @@ static inline unsigned char search_ascii_only_escape(search_state *search, const
598
632
  unsigned char ch_len = escape_table[ch];
599
633
 
600
634
  if (RB_UNLIKELY(ch_len)) {
601
- search_flush(search);
602
- return ch_len & CHAR_LENGTH_MASK;
635
+ return ch_len;
603
636
  } else {
604
637
  search->ptr++;
605
638
  }
@@ -612,7 +645,9 @@ static inline void full_escape_UTF8_char(search_state *search, unsigned char ch_
612
645
  {
613
646
  const unsigned char ch = (unsigned char)*search->ptr;
614
647
  switch (ch_len) {
615
- case 1: {
648
+ case ESCAPE_LEN_1: {
649
+ search_flush(search);
650
+
616
651
  switch (ch) {
617
652
  case '"': fbuffer_append(search->buffer, "\\\"", 2); break;
618
653
  case '\\': fbuffer_append(search->buffer, "\\\\", 2); break;
@@ -633,7 +668,19 @@ static inline void full_escape_UTF8_char(search_state *search, unsigned char ch_
633
668
  }
634
669
  break;
635
670
  }
671
+ case HTML_ESCAPE_LEN_1: {
672
+ if (unsafe_html_tag_p(search->ptr, search->end)) {
673
+ search_flush(search);
674
+ fbuffer_append(search->buffer, "\\u003c", 6);
675
+ } else {
676
+ search->ptr++;
677
+ return; // no escape needed, resume search without updating cursor.
678
+ }
679
+ break;
680
+ }
636
681
  default: {
682
+ search_flush(search);
683
+
637
684
  const char *hexdig = "0123456789abcdef";
638
685
  char scratch[12] = { '\\', 'u', 0, 0, 0, 0, '\\', 'u' };
639
686
 
@@ -683,7 +730,7 @@ static inline void full_escape_UTF8_char(search_state *search, unsigned char ch_
683
730
  break;
684
731
  }
685
732
  }
686
- search->cursor = (search->ptr += ch_len);
733
+ search->cursor = (search->ptr += (ch_len & CHAR_LENGTH_MASK));
687
734
  }
688
735
 
689
736
  static void convert_UTF8_to_ASCII_only_JSON(search_state *search, const unsigned char escape_table[256])
@@ -768,6 +815,12 @@ static void vstate_spill(struct generate_json_data *data)
768
815
  RB_OBJ_WRITTEN(vstate, Qundef, state->sort_keys);
769
816
  }
770
817
 
818
+ static inline VALUE json_to_s(VALUE obj)
819
+ {
820
+ VALUE tmp = rb_funcall(obj, i_to_s, 0);
821
+ return StringValue(tmp);
822
+ }
823
+
771
824
  static inline VALUE json_call_to_json(struct generate_json_data *data, VALUE obj)
772
825
  {
773
826
  if (RB_UNLIKELY(!data->vstate)) {
@@ -863,21 +916,12 @@ NOINLINE(static) VALUE convert_invalid_encoding(struct generate_json_data *data,
863
916
  }
864
917
  }
865
918
 
866
- if (RB_ENCODING_GET_INLINED(str) == binary_encindex) {
867
- VALUE utf8_string = rb_enc_associate_index(rb_str_dup(str), utf8_encindex);
868
- switch (rb_enc_str_coderange(utf8_string)) {
869
- case ENC_CODERANGE_7BIT:
870
- return utf8_string;
871
- case ENC_CODERANGE_VALID:
872
- // For historical reason, we silently reinterpret binary strings as UTF-8 if it would work.
873
- // TODO: Raise in 3.0.0
874
- rb_warn("JSON.generate: UTF-8 string passed as BINARY, this will raise an encoding error in json 3.0");
875
- return utf8_string;
876
- break;
877
- }
919
+ str = rb_rescue(encode_json_string_try, str, encode_json_string_rescue, str);
920
+ Check_Type(str, T_STRING);
921
+ if (!valid_json_string_p(str)) {
922
+ raise_generator_error(str, "source sequence is illegal/malformed utf-8");
878
923
  }
879
-
880
- return rb_rescue(encode_json_string_try, str, encode_json_string_rescue, str);
924
+ return str;
881
925
  }
882
926
 
883
927
  ALWAYS_INLINE(static) VALUE ensure_valid_encoding(struct generate_json_data *data, VALUE str, bool as_json_called, bool is_key)
@@ -892,12 +936,17 @@ ALWAYS_INLINE(static) VALUE ensure_valid_encoding(struct generate_json_data *dat
892
936
 
893
937
  static void raw_generate_json_string(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
894
938
  {
939
+ VALUE str = obj;
940
+ if (RB_UNLIKELY(buffer->io)) {
941
+ // IO writes can mutate the original string while we are reading it.
942
+ str = rb_str_new_frozen(str);
943
+ }
895
944
  fbuffer_append_char(buffer, '"');
896
945
 
897
- long len;
946
+ rb_len_t len;
898
947
  search_state search;
899
948
  search.buffer = buffer;
900
- RSTRING_GETMEM(obj, search.ptr, len);
949
+ RSTRING_GETMEM(str, search.ptr, len);
901
950
  search.cursor = search.ptr;
902
951
  search.end = search.ptr + len;
903
952
 
@@ -908,7 +957,7 @@ static void raw_generate_json_string(FBuffer *buffer, struct generate_json_data
908
957
  search.chunk_end = NULL;
909
958
  #endif /* HAVE_SIMD */
910
959
 
911
- switch (json_str_coderange(obj)) {
960
+ switch (json_str_coderange(str)) {
912
961
  case ENC_CODERANGE_7BIT:
913
962
  case ENC_CODERANGE_VALID:
914
963
  if (RB_UNLIKELY(data->state->ascii_only)) {
@@ -924,6 +973,7 @@ static void raw_generate_json_string(FBuffer *buffer, struct generate_json_data
924
973
  break;
925
974
  }
926
975
  fbuffer_append_char(buffer, '"');
976
+ RB_GC_GUARD(str);
927
977
  }
928
978
 
929
979
  static void generate_json_string(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
@@ -1015,6 +1065,9 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
1015
1065
  raise_generator_error(key, "%"PRIsVALUE" not allowed as object key in JSON", CLASS_OF(key));
1016
1066
  }
1017
1067
  }
1068
+
1069
+ json_inspect_hash_with_mixed_keys(arg);
1070
+
1018
1071
  key_to_s = rb_convert_type(key, T_STRING, "String", "to_s");
1019
1072
  break;
1020
1073
  }
@@ -1037,16 +1090,19 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
1037
1090
  static inline long increase_depth(struct generate_json_data *data)
1038
1091
  {
1039
1092
  JSON_Generator_State *state = data->state;
1040
- long depth = ++data->depth;
1041
- if (RB_UNLIKELY(depth > state->max_nesting && state->max_nesting)) {
1042
- rb_raise(eNestingError, "nesting of %ld is too deep. Did you try to serialize objects with circular references?", --data->depth);
1093
+ if (RB_UNLIKELY(data->depth >= state->max_nesting && state->max_nesting)) {
1094
+ rb_raise(eNestingError, "nesting of %ld is too deep. Did you try to serialize objects with circular references?", data->depth + 1);
1043
1095
  }
1044
- return depth;
1096
+ return ++data->depth;
1045
1097
  }
1046
1098
 
1047
1099
  static void generate_json_object(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1048
1100
  {
1049
- if (RB_UNLIKELY(data->state->sort_keys)) {
1101
+ if (RB_UNLIKELY(data->state->rfc8785)) {
1102
+ VALUE args[2] = {obj, data->state->strict ? data->state->as_json : Qnil};
1103
+ obj = rb_proc_call_with_block(rfc8785_sort_keys_proc, 2, args, Qnil);
1104
+ Check_Type(obj, T_HASH);
1105
+ } else if (RB_UNLIKELY(data->state->sort_keys)) {
1050
1106
  obj = rb_proc_call_with_block(data->state->sort_keys, 1, &obj, Qnil);
1051
1107
  Check_Type(obj, T_HASH);
1052
1108
  }
@@ -1090,7 +1146,7 @@ static void generate_json_array(FBuffer *buffer, struct generate_json_data *data
1090
1146
 
1091
1147
  fbuffer_append_char(buffer, '[');
1092
1148
  if (RB_UNLIKELY(data->state->array_nl)) fbuffer_append_str(buffer, data->state->array_nl);
1093
- for (int i = 0; i < RARRAY_LEN(obj); i++) {
1149
+ for (rb_len_t i = 0; i < RARRAY_LEN(obj); i++) {
1094
1150
  if (i > 0) {
1095
1151
  fbuffer_append_char(buffer, ',');
1096
1152
  if (RB_UNLIKELY(data->state->array_nl)) fbuffer_append_str(buffer, data->state->array_nl);
@@ -1118,9 +1174,7 @@ static void generate_json_fallback(FBuffer *buffer, struct generate_json_data *d
1118
1174
  Check_Type(tmp, T_STRING);
1119
1175
  fbuffer_append_str(buffer, tmp);
1120
1176
  } else {
1121
- tmp = rb_funcall(obj, i_to_s, 0);
1122
- Check_Type(tmp, T_STRING);
1123
- generate_json_string(buffer, data, tmp);
1177
+ generate_json_string(buffer, data, json_to_s(obj));
1124
1178
  }
1125
1179
  }
1126
1180
 
@@ -1139,15 +1193,29 @@ static void generate_json_true(FBuffer *buffer, struct generate_json_data *data,
1139
1193
  fbuffer_append(buffer, "true", 4);
1140
1194
  }
1141
1195
 
1196
+ static void generate_json_rfc8785_number(FBuffer *buffer, VALUE obj)
1197
+ {
1198
+ VALUE str = rb_proc_call_with_block(rfc8785_number_formatter_proc, 1, &obj, Qnil);
1199
+ Check_Type(str, T_STRING);
1200
+ fbuffer_append_str(buffer, str);
1201
+ }
1202
+
1142
1203
  static void generate_json_fixnum(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1143
1204
  {
1144
- fbuffer_append_long(buffer, FIX2LONG(obj));
1205
+ if (RB_UNLIKELY(data->state->rfc8785)) {
1206
+ generate_json_rfc8785_number(buffer, obj);
1207
+ return;
1208
+ }
1209
+ fbuffer_append_len(buffer, FIX2LEN(obj));
1145
1210
  }
1146
1211
 
1147
1212
  static void generate_json_bignum(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1148
1213
  {
1149
- VALUE tmp = rb_funcall(obj, i_to_s, 0);
1150
- fbuffer_append_str(buffer, StringValue(tmp));
1214
+ if (RB_UNLIKELY(data->state->rfc8785)) {
1215
+ generate_json_rfc8785_number(buffer, obj);
1216
+ return;
1217
+ }
1218
+ fbuffer_append_str(buffer, json_to_s(obj));
1151
1219
  }
1152
1220
 
1153
1221
  static void generate_json_float(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
@@ -1166,11 +1234,15 @@ static void generate_json_float(FBuffer *buffer, struct generate_json_data *data
1166
1234
  return;
1167
1235
  }
1168
1236
  }
1169
- raise_generator_error(obj, "%"PRIsVALUE" not allowed in JSON", rb_funcall(obj, i_to_s, 0));
1237
+ raise_generator_error(obj, "%"PRIsVALUE" not allowed in JSON", json_to_s(obj));
1170
1238
  }
1171
1239
 
1172
- VALUE tmp = rb_funcall(obj, i_to_s, 0);
1173
- fbuffer_append_str(buffer, tmp);
1240
+ fbuffer_append_str(buffer, json_to_s(obj));
1241
+ return;
1242
+ }
1243
+
1244
+ if (RB_UNLIKELY(data->state->rfc8785)) {
1245
+ generate_json_rfc8785_number(buffer, obj);
1174
1246
  return;
1175
1247
  }
1176
1248
 
@@ -1291,10 +1363,28 @@ static void generate_json_no_fallback(FBuffer *buffer, struct generate_json_data
1291
1363
  generate_json_general(buffer, data, obj, false);
1292
1364
  }
1293
1365
 
1366
+ static void validate_rfc8785(JSON_Generator_State *state)
1367
+ {
1368
+ if (!state->rfc8785) return;
1369
+
1370
+ const char *option = state->indent ? "indent" :
1371
+ state->space ? "space" :
1372
+ state->space_before ? "space_before" :
1373
+ state->object_nl ? "object_nl" :
1374
+ state->array_nl ? "array_nl" :
1375
+ state->ascii_only ? "ascii_only" :
1376
+ state->script_safe ? "script_safe" :
1377
+ state->allow_nan ? "allow_nan" : NULL;
1378
+ if (option) {
1379
+ rb_raise(rb_eArgError, "%s cannot be used with rfc8785", option);
1380
+ }
1381
+ }
1382
+
1294
1383
  static VALUE generate_json_try(VALUE d)
1295
1384
  {
1296
1385
  struct generate_json_data *data = (struct generate_json_data *)d;
1297
1386
 
1387
+ validate_rfc8785(data->state);
1298
1388
  data->func(data->buffer, data, data->obj);
1299
1389
 
1300
1390
  return fbuffer_finalize(data->buffer);
@@ -1542,7 +1632,7 @@ static VALUE cState_array_nl_set(VALUE self, VALUE array_nl)
1542
1632
  /*
1543
1633
  * call-seq: as_json()
1544
1634
  *
1545
- * This string is put at the end of a line that holds a JSON array.
1635
+ * Returns the callback used to convert unsupported objects in strict mode.
1546
1636
  */
1547
1637
  static VALUE cState_as_json(VALUE self)
1548
1638
  {
@@ -1550,16 +1640,28 @@ static VALUE cState_as_json(VALUE self)
1550
1640
  return state->as_json;
1551
1641
  }
1552
1642
 
1643
+ static VALUE as_json_config(VALUE value)
1644
+ {
1645
+ if (rb_obj_is_proc(value)) {
1646
+ return value;
1647
+ }
1648
+ VALUE proc = rb_check_funcall(value, rb_intern("to_proc"), 0, NULL);
1649
+ if (proc == Qundef || !rb_obj_is_proc(proc)) {
1650
+ rb_raise(rb_eTypeError, "as_json must be a Proc");
1651
+ }
1652
+ return proc;
1653
+ }
1654
+
1553
1655
  /*
1554
1656
  * call-seq: as_json=(as_json)
1555
1657
  *
1556
- * This string is put at the end of a line that holds a JSON array.
1658
+ * Sets the Proc used to convert unsupported objects in strict mode.
1557
1659
  */
1558
1660
  static VALUE cState_as_json_set(VALUE self, VALUE as_json)
1559
1661
  {
1560
1662
  rb_check_frozen(self);
1561
1663
  GET_STATE(self);
1562
- RB_OBJ_WRITE(self, &state->as_json, rb_convert_type(as_json, T_DATA, "Proc", "to_proc"));
1664
+ RB_OBJ_WRITE(self, &state->as_json, as_json_config(as_json));
1563
1665
  return Qnil;
1564
1666
  }
1565
1667
 
@@ -1623,7 +1725,7 @@ static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
1623
1725
  /*
1624
1726
  * call-seq: script_safe
1625
1727
  *
1626
- * If this boolean is true, the forward slashes will be escaped in
1728
+ * If this boolean is true, script tags and HTML comments will be escaped in
1627
1729
  * the json output.
1628
1730
  */
1629
1731
  static VALUE cState_script_safe(VALUE self)
@@ -1635,7 +1737,7 @@ static VALUE cState_script_safe(VALUE self)
1635
1737
  /*
1636
1738
  * call-seq: script_safe=(enable)
1637
1739
  *
1638
- * This sets whether or not the forward slashes will be escaped in
1740
+ * This sets whether or not script tags and HTML comments will be escaped in
1639
1741
  * the json output.
1640
1742
  */
1641
1743
  static VALUE cState_script_safe_set(VALUE self, VALUE enable)
@@ -1733,9 +1835,34 @@ static VALUE cState_set_default_sort_keys_proc(VALUE self, VALUE proc)
1733
1835
  if (!rb_obj_is_proc(proc)) {
1734
1836
  rb_raise(rb_eTypeError, "sort_key_proc must be a Proc");
1735
1837
  }
1838
+ if (default_sort_keys_proc) {
1839
+ rb_raise(rb_eArgError, "sort_key_proc can only be set once");
1840
+ }
1736
1841
  return default_sort_keys_proc = proc;
1737
1842
  }
1738
1843
 
1844
+ static VALUE cState_set_rfc8785_number_formatter_proc(VALUE self, VALUE proc)
1845
+ {
1846
+ if (!rb_obj_is_proc(proc)) {
1847
+ rb_raise(rb_eTypeError, "rfc8785_number_formatter_proc must be a Proc");
1848
+ }
1849
+ if (rfc8785_number_formatter_proc) {
1850
+ rb_raise(rb_eArgError, "rfc8785_number_formatter_proc can only be set once");
1851
+ }
1852
+ return rfc8785_number_formatter_proc = proc;
1853
+ }
1854
+
1855
+ static VALUE cState_set_rfc8785_sort_keys_proc(VALUE self, VALUE proc)
1856
+ {
1857
+ if (!rb_obj_is_proc(proc)) {
1858
+ rb_raise(rb_eTypeError, "rfc8785_sort_keys_proc must be a Proc");
1859
+ }
1860
+ if (rfc8785_sort_keys_proc) {
1861
+ rb_raise(rb_eArgError, "rfc8785_sort_keys_proc can only be set once");
1862
+ }
1863
+ return rfc8785_sort_keys_proc = proc;
1864
+ }
1865
+
1739
1866
  static VALUE normalize_sort_keys(VALUE value)
1740
1867
  {
1741
1868
  if (rb_obj_is_proc(value)) {
@@ -1754,7 +1881,7 @@ static VALUE normalize_sort_keys(VALUE value)
1754
1881
  *
1755
1882
  * Get the value of sort_keys.
1756
1883
  */
1757
- static VALUE cState_sort_keys_p(VALUE self)
1884
+ static VALUE cState_sort_keys(VALUE self)
1758
1885
  {
1759
1886
  GET_STATE(self);
1760
1887
  return state->sort_keys;
@@ -1774,7 +1901,33 @@ static VALUE cState_sort_keys_set(VALUE self, VALUE value)
1774
1901
  rb_check_frozen(self);
1775
1902
  GET_STATE(self);
1776
1903
  RB_OBJ_WRITE(self, &state->sort_keys, normalize_sort_keys(value));
1777
- return Qnil;
1904
+ return state->sort_keys;
1905
+ }
1906
+
1907
+
1908
+ /*
1909
+ * call-seq: rfc8785?
1910
+ *
1911
+ * Get whether RFC8785 (canonicalization) is enabled.
1912
+ */
1913
+ static VALUE cState_rfc8785_p(VALUE self)
1914
+ {
1915
+ GET_STATE(self);
1916
+ return state->rfc8785 ? Qtrue : Qfalse;
1917
+ }
1918
+
1919
+ /*
1920
+ * call-seq: rfc8785=
1921
+ *
1922
+ * Sets whether RFC8785 (canonicalization) is enabled.
1923
+ */
1924
+
1925
+ static VALUE cState_rfc8785_set(VALUE self, VALUE value)
1926
+ {
1927
+ rb_check_frozen(self);
1928
+ GET_STATE(self);
1929
+ state->rfc8785 = RTEST(value);
1930
+ return state->rfc8785 ? Qtrue : Qfalse;
1778
1931
  }
1779
1932
 
1780
1933
  static VALUE cState_allow_duplicate_key_p(VALUE self)
@@ -1797,8 +1950,7 @@ static VALUE cState_depth(VALUE self)
1797
1950
  /*
1798
1951
  * call-seq: depth=(depth)
1799
1952
  *
1800
- * This sets the maximum level of data structure nesting in the generated JSON
1801
- * to the integer depth, max_nesting = 0 if no maximum should be checked.
1953
+ * Sets the current depth of data structure nesting to the integer +depth+.
1802
1954
  */
1803
1955
  static VALUE cState_depth_set(VALUE self, VALUE depth)
1804
1956
  {
@@ -1875,8 +2027,9 @@ static int configure_state_i(VALUE key, VALUE val, VALUE _arg)
1875
2027
  else if (key == sym_script_safe) { state->script_safe = RTEST(val); }
1876
2028
  else if (key == sym_strict) { state->strict = RTEST(val); }
1877
2029
  else if (key == sym_allow_duplicate_key) { state->allow_duplicate_key = RTEST(val); }
2030
+ else if (key == sym_rfc8785) { state->rfc8785 = RTEST(val); }
1878
2031
  else if (key == sym_as_json) {
1879
- VALUE proc = RTEST(val) ? rb_convert_type(val, T_DATA, "Proc", "to_proc") : Qfalse;
2032
+ VALUE proc = RTEST(val) ? as_json_config(val) : Qfalse;
1880
2033
  state->as_json_single_arg = proc && rb_proc_arity(proc) == 1;
1881
2034
  state_write_value(data, &state->as_json, proc);
1882
2035
  }
@@ -1911,6 +2064,7 @@ static void configure_state(JSON_Generator_State *state, VALUE vstate, VALUE con
1911
2064
  rb_hash_foreach(config, configure_state_i, (VALUE)&data);
1912
2065
 
1913
2066
  raise_argument_error_on_unknown_keywords(data.unknown_keywords);
2067
+ validate_rfc8785(state);
1914
2068
  }
1915
2069
 
1916
2070
  static VALUE cState_configure(VALUE self, VALUE opts)
@@ -1970,6 +2124,8 @@ void Init_generator(void)
1970
2124
  VALUE mGenerator = rb_define_module_under(mExt, "Generator");
1971
2125
 
1972
2126
  rb_global_variable(&default_sort_keys_proc);
2127
+ rb_global_variable(&rfc8785_number_formatter_proc);
2128
+ rb_global_variable(&rfc8785_sort_keys_proc);
1973
2129
 
1974
2130
  rb_global_variable(&eGeneratorError);
1975
2131
  eGeneratorError = rb_path2class("JSON::GeneratorError");
@@ -1981,6 +2137,8 @@ void Init_generator(void)
1981
2137
  rb_define_alloc_func(cState, cState_s_allocate);
1982
2138
  rb_define_singleton_method(cState, "from_state", cState_from_state_s, 1);
1983
2139
  rb_define_singleton_method(cState, "default_sort_keys_proc=", cState_set_default_sort_keys_proc, 1);
2140
+ rb_define_singleton_method(cState, "rfc8785_number_formatter_proc=", cState_set_rfc8785_number_formatter_proc, 1);
2141
+ rb_define_singleton_method(cState, "rfc8785_sort_keys_proc=", cState_set_rfc8785_sort_keys_proc, 1);
1984
2142
 
1985
2143
  rb_define_method(cState, "initialize", cState_initialize, -1);
1986
2144
  rb_define_alias(cState, "initialize", "initialize"); // avoid method redefinition warnings
@@ -2018,8 +2176,10 @@ void Init_generator(void)
2018
2176
  rb_define_method(cState, "buffer_initial_length=", cState_buffer_initial_length_set, 1);
2019
2177
  rb_define_method(cState, "generate", cState_generate, -1);
2020
2178
  rb_define_method(cState, "_generate_no_fallback", cState_generate_no_fallback, -1);
2021
- rb_define_method(cState, "sort_keys", cState_sort_keys_p, 0);
2179
+ rb_define_method(cState, "sort_keys", cState_sort_keys, 0);
2022
2180
  rb_define_method(cState, "sort_keys=", cState_sort_keys_set, 1);
2181
+ rb_define_method(cState, "rfc8785?", cState_rfc8785_p, 0);
2182
+ rb_define_method(cState, "rfc8785=", cState_rfc8785_set, 1);
2023
2183
 
2024
2184
  rb_define_private_method(cState, "allow_duplicate_key?", cState_allow_duplicate_key_p, 0);
2025
2185
 
@@ -2049,12 +2209,12 @@ void Init_generator(void)
2049
2209
  sym_as_json = ID2SYM(rb_intern("as_json"));
2050
2210
  sym_allow_duplicate_key = ID2SYM(rb_intern("allow_duplicate_key"));
2051
2211
  sym_sort_keys = ID2SYM(rb_intern("sort_keys"));
2212
+ sym_rfc8785 = ID2SYM(rb_intern("rfc8785"));
2052
2213
 
2053
2214
  usascii_encindex = rb_usascii_encindex();
2054
2215
  utf8_encindex = rb_utf8_encindex();
2055
- binary_encindex = rb_ascii8bit_encindex();
2056
2216
 
2057
2217
  rb_require("json/ext/generator/state");
2058
2218
 
2059
- simd_impl = find_simd_implementation();
2219
+ init_simd_impl();
2060
2220
  }