json 2.13.2 → 2.14.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 71499860706a6f27871853ec88fe26d5bd6a53f85fba2e9764b9ef0aadc170d9
4
- data.tar.gz: c07cd26190c4f36864490465890c162e1b9ce0ae1f262069e51c94d7cf74b117
3
+ metadata.gz: fcefafe300fa1ffea1f43b8485f01e6203074c961073cd58908043f7cfb8764f
4
+ data.tar.gz: fd1125be522c34646e5e9e790812ab476c6ce3b0535771272756720d2c012188
5
5
  SHA512:
6
- metadata.gz: f1c626d30c67e99c56d9f411b7944b6263261676567c02f6d57ba7566087743b26835d3c6c4c1636a0dc76cc2b8c4e2739a1130f286cd069406c38c66138df97
7
- data.tar.gz: eb4fd62079fc730962359e4128f7abd36ca04c17b580506c3e8e3ebdd0e8e0c54c3cf75d1ae899dc53e13697bfa0c11c6c04277d701cfc83fa9ee611785ac85e
6
+ metadata.gz: c539e410ba97460ee935d56611c6005d70979e9ef87c2af06cd99221ad14e6391851c869a3ffec9f2d08c9a0898ce47589b30f6bfd484d2d9e5183a03bf5dead
7
+ data.tar.gz: 025fb31b8156e809f1bd145f8a8c1ed740fc70ab5a5623f4588df8618e26b243b06e85e7c52756dcbc03c17e8c0ec41ad7ef1ee53761423a570d7cfeb680a26b
data/CHANGES.md CHANGED
@@ -2,6 +2,28 @@
2
2
 
3
3
  ### Unreleased
4
4
 
5
+ ### 2025-09-18 (2.14.1)
6
+
7
+ * Fix `IndexOutOfBoundsException` in the JRuby extension when encoding shared strings.
8
+
9
+ ### 2025-09-18 (2.14.0)
10
+
11
+ * Add new `allow_duplicate_key` generator options. By default a warning is now emitted when a duplicated key is encountered.
12
+ In `json 3.0` an error will be raised.
13
+ ```ruby
14
+ >> Warning[:deprecated] = true
15
+ >> puts JSON.generate({ foo: 1, "foo" => 2 })
16
+ (irb):2: warning: detected duplicate key "foo" in {foo: 1, "foo" => 2}.
17
+ This will raise an error in json 3.0 unless enabled via `allow_duplicate_key: true`
18
+ {"foo":1,"foo":2}
19
+ >> JSON.generate({ foo: 1, "foo" => 2 }, allow_duplicate_key: false)
20
+ detected duplicate key "foo" in {foo: 1, "foo" => 2} (JSON::GeneratorError)
21
+ ```
22
+ * Fix `JSON.generate` `strict: true` mode to also restrict hash keys.
23
+ * Fix `JSON::Coder` to also invoke block for hash keys that aren't strings nor symbols.
24
+ * Fix `JSON.unsafe_load` usage with proc
25
+ * Fix the parser to more consistently reject invalid UTF-16 surogate pairs.
26
+
5
27
  ### 2025-07-28 (2.13.2)
6
28
 
7
29
  * Improve duplicate key warning and errors to include the key name and point to the right caller.
@@ -44,7 +66,7 @@
44
66
  ### 2025-04-24 (2.11.1)
45
67
 
46
68
  * Add back `JSON.restore`, `JSON.unparse`, `JSON.fast_unparse` and `JSON.pretty_unparse`.
47
- These were deprecated 16 years ago, but never emited warnings, only undocumented, so are
69
+ These were deprecated 16 years ago, but never emitted warnings, only undocumented, so are
48
70
  still used by a few gems.
49
71
 
50
72
  ### 2025-04-24 (2.11.0)
@@ -71,7 +93,7 @@
71
93
  ### 2025-03-12 (2.10.2)
72
94
 
73
95
  * Fix a potential crash in the C extension parser.
74
- * Raise a ParserError on all incomplete unicode escape sequence. This was the behavior until `2.10.0` unadvertently changed it.
96
+ * Raise a ParserError on all incomplete unicode escape sequence. This was the behavior until `2.10.0` inadvertently changed it.
75
97
  * Ensure document snippets that are included in parser errors don't include truncated multibyte characters.
76
98
  * Ensure parser error snippets are valid UTF-8.
77
99
  * Fix `JSON::GeneratorError#detailed_message` on Ruby < 3.2
@@ -102,7 +124,7 @@
102
124
 
103
125
  ### 2024-11-14 (2.8.2)
104
126
 
105
- * `JSON.load_file` explictly read the file as UTF-8.
127
+ * `JSON.load_file` explicitly read the file as UTF-8.
106
128
 
107
129
  ### 2024-11-06 (2.8.1)
108
130
 
@@ -110,7 +132,7 @@
110
132
 
111
133
  ### 2024-11-06 (2.8.0)
112
134
 
113
- * Emit a deprecation warning when `JSON.load` create custom types without the `create_additions` option being explictly enabled.
135
+ * Emit a deprecation warning when `JSON.load` create custom types without the `create_additions` option being explicitly enabled.
114
136
  * Prefer to use `JSON.unsafe_load(string)` or `JSON.load(string, create_additions: true)`.
115
137
  * Emit a deprecation warning when serializing valid UTF-8 strings encoded in `ASCII_8BIT` aka `BINARY`.
116
138
  * Bump required Ruby version to 2.7.
@@ -118,7 +140,7 @@
118
140
  pre-existing support for comments, make it suitable to parse `jsonc` documents.
119
141
  * Many performance improvements to `JSON.parse` and `JSON.load`, up to `1.7x` faster on real world documents.
120
142
  * Some minor performance improvements to `JSON.dump` and `JSON.generate`.
121
- * `JSON.pretty_generate` no longer include newline inside empty object and arrays.
143
+ * `JSON.pretty_generate` no longer includes newlines inside empty object and arrays.
122
144
 
123
145
  ### 2024-11-04 (2.7.6)
124
146
 
@@ -135,13 +157,13 @@
135
157
  * Workaround a bug in 3.4.8 and older https://github.com/rubygems/rubygems/pull/6490.
136
158
  This bug would cause some gems with native extension to fail during compilation.
137
159
  * Workaround different versions of `json` and `json_pure` being loaded (not officially supported).
138
- * Make `json_pure` Ractor compatible.
160
+ * Make `json_pure` Ractor compatible.
139
161
 
140
162
  ### 2024-10-24 (2.7.3)
141
163
 
142
164
  * Numerous performance optimizations in `JSON.generate` and `JSON.dump` (up to 2 times faster).
143
- * Limit the size of ParserError exception messages, only include up to 32 bytes of the unparseable source.
144
- * Fix json-pure's `Object#to_json` to accept non state arguments
165
+ * Limit the size of ParserError exception messages, only include up to 32 bytes of the unparsable source.
166
+ * Fix json-pure's `Object#to_json` to accept non-state arguments.
145
167
  * Fix multiline comment support in `json-pure`.
146
168
  * Fix `JSON.parse` to no longer mutate the argument encoding when passed an ASCII-8BIT string.
147
169
  * Fix `String#to_json` to raise on invalid encoding in `json-pure`.
@@ -286,6 +308,7 @@
286
308
  ## 2015-09-11 (2.0.0)
287
309
  * Now complies to newest JSON RFC 7159.
288
310
  * Implements compatibility to ruby 2.4 integer unification.
311
+ * Removed support for `quirks_mode` option.
289
312
  * Drops support for old rubies whose life has ended, that is rubies < 2.0.
290
313
  Also see https://www.ruby-lang.org/en/news/2014/07/01/eol-for-1-8-7-and-1-9-2/
291
314
  * There were still some mentions of dual GPL licensing in the source, but JSON
@@ -24,6 +24,14 @@ typedef unsigned char _Bool;
24
24
  #endif
25
25
  #endif
26
26
 
27
+ #ifndef NOINLINE
28
+ #if defined(__has_attribute) && __has_attribute(noinline)
29
+ #define NOINLINE() __attribute__((noinline))
30
+ #else
31
+ #define NOINLINE()
32
+ #endif
33
+ #endif
34
+
27
35
  #ifndef RB_UNLIKELY
28
36
  #define RB_UNLIKELY(expr) expr
29
37
  #endif
@@ -169,12 +177,17 @@ static inline void fbuffer_inc_capa(FBuffer *fb, unsigned long requested)
169
177
  }
170
178
  }
171
179
 
172
- static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
180
+ static inline void fbuffer_append_reserved(FBuffer *fb, const char *newstr, unsigned long len)
181
+ {
182
+ MEMCPY(fb->ptr + fb->len, newstr, char, len);
183
+ fbuffer_consumed(fb, len);
184
+ }
185
+
186
+ static inline void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
173
187
  {
174
188
  if (len > 0) {
175
189
  fbuffer_inc_capa(fb, len);
176
- MEMCPY(fb->ptr + fb->len, newstr, char, len);
177
- fbuffer_consumed(fb, len);
190
+ fbuffer_append_reserved(fb, newstr, len);
178
191
  }
179
192
  }
180
193
 
@@ -197,11 +210,24 @@ static void fbuffer_append_str(FBuffer *fb, VALUE str)
197
210
  const char *newstr = StringValuePtr(str);
198
211
  unsigned long len = RSTRING_LEN(str);
199
212
 
200
- RB_GC_GUARD(str);
201
-
202
213
  fbuffer_append(fb, newstr, len);
203
214
  }
204
215
 
216
+ static void fbuffer_append_str_repeat(FBuffer *fb, VALUE str, size_t repeat)
217
+ {
218
+ const char *newstr = StringValuePtr(str);
219
+ unsigned long len = RSTRING_LEN(str);
220
+
221
+ fbuffer_inc_capa(fb, repeat * len);
222
+ while (repeat) {
223
+ #ifdef JSON_DEBUG
224
+ fb->requested = len;
225
+ #endif
226
+ fbuffer_append_reserved(fb, newstr, len);
227
+ repeat--;
228
+ }
229
+ }
230
+
205
231
  static inline void fbuffer_append_char(FBuffer *fb, char newchr)
206
232
  {
207
233
  fbuffer_inc_capa(fb, 1);
@@ -9,6 +9,12 @@
9
9
 
10
10
  /* ruby api and some helpers */
11
11
 
12
+ enum duplicate_key_action {
13
+ JSON_DEPRECATED = 0,
14
+ JSON_IGNORE,
15
+ JSON_RAISE,
16
+ };
17
+
12
18
  typedef struct JSON_Generator_StateStruct {
13
19
  VALUE indent;
14
20
  VALUE space;
@@ -21,6 +27,8 @@ typedef struct JSON_Generator_StateStruct {
21
27
  long depth;
22
28
  long buffer_initial_length;
23
29
 
30
+ enum duplicate_key_action on_duplicate_key;
31
+
24
32
  bool allow_nan;
25
33
  bool ascii_only;
26
34
  bool script_safe;
@@ -31,10 +39,10 @@ typedef struct JSON_Generator_StateStruct {
31
39
  #define RB_UNLIKELY(cond) (cond)
32
40
  #endif
33
41
 
34
- static VALUE mJSON, cState, cFragment, mString_Extend, eGeneratorError, eNestingError, Encoding_UTF_8;
42
+ static VALUE mJSON, cState, cFragment, eGeneratorError, eNestingError, Encoding_UTF_8;
35
43
 
36
44
  static ID i_to_s, i_to_json, i_new, i_pack, i_unpack, i_create_id, i_extend, i_encode;
37
- static VALUE sym_indent, sym_space, sym_space_before, sym_object_nl, sym_array_nl, sym_max_nesting, sym_allow_nan,
45
+ 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,
38
46
  sym_ascii_only, sym_depth, sym_buffer_initial_length, sym_script_safe, sym_escape_slash, sym_strict, sym_as_json;
39
47
 
40
48
 
@@ -137,8 +145,8 @@ static inline FORCE_INLINE void search_flush(search_state *search)
137
145
  {
138
146
  // Do not remove this conditional without profiling, specifically escape-heavy text.
139
147
  // escape_UTF8_char_basic will advance search->ptr and search->cursor (effectively a search_flush).
140
- // For back-to-back characters that need to be escaped, specifcally for the SIMD code paths, this method
141
- // will be called just before calling escape_UTF8_char_basic. There will be no characers to append for the
148
+ // For back-to-back characters that need to be escaped, specifically for the SIMD code paths, this method
149
+ // will be called just before calling escape_UTF8_char_basic. There will be no characters to append for the
142
150
  // consecutive characters that need to be escaped. While the fbuffer_append is a no-op if
143
151
  // nothing needs to be flushed, we can save a few memory references with this conditional.
144
152
  if (search->ptr > search->cursor) {
@@ -835,18 +843,6 @@ static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self)
835
843
  return cState_partial_generate(Vstate, self, generate_json_float, Qfalse);
836
844
  }
837
845
 
838
- /*
839
- * call-seq: String.included(modul)
840
- *
841
- * Extends _modul_ with the String::Extend module.
842
- */
843
- static VALUE mString_included_s(VALUE self, VALUE modul)
844
- {
845
- VALUE result = rb_funcall(modul, i_extend, 1, mString_Extend);
846
- rb_call_super(1, &modul);
847
- return result;
848
- }
849
-
850
846
  /*
851
847
  * call-seq: to_json(*)
852
848
  *
@@ -861,51 +857,6 @@ static VALUE mString_to_json(int argc, VALUE *argv, VALUE self)
861
857
  return cState_partial_generate(Vstate, self, generate_json_string, Qfalse);
862
858
  }
863
859
 
864
- /*
865
- * call-seq: to_json_raw_object()
866
- *
867
- * This method creates a raw object hash, that can be nested into
868
- * other data structures and will be generated as a raw string. This
869
- * method should be used, if you want to convert raw strings to JSON
870
- * instead of UTF-8 strings, e. g. binary data.
871
- */
872
- static VALUE mString_to_json_raw_object(VALUE self)
873
- {
874
- VALUE ary;
875
- VALUE result = rb_hash_new();
876
- rb_hash_aset(result, rb_funcall(mJSON, i_create_id, 0), rb_class_name(rb_obj_class(self)));
877
- ary = rb_funcall(self, i_unpack, 1, rb_str_new2("C*"));
878
- rb_hash_aset(result, rb_utf8_str_new_lit("raw"), ary);
879
- return result;
880
- }
881
-
882
- /*
883
- * call-seq: to_json_raw(*args)
884
- *
885
- * This method creates a JSON text from the result of a call to
886
- * to_json_raw_object of this String.
887
- */
888
- static VALUE mString_to_json_raw(int argc, VALUE *argv, VALUE self)
889
- {
890
- VALUE obj = mString_to_json_raw_object(self);
891
- Check_Type(obj, T_HASH);
892
- return mHash_to_json(argc, argv, obj);
893
- }
894
-
895
- /*
896
- * call-seq: json_create(o)
897
- *
898
- * Raw Strings are JSON Objects (the raw bytes are stored in an array for the
899
- * key "raw"). The Ruby String can be created by this module method.
900
- */
901
- static VALUE mString_Extend_json_create(VALUE self, VALUE o)
902
- {
903
- VALUE ary;
904
- Check_Type(o, T_HASH);
905
- ary = rb_hash_aref(o, rb_str_new2("raw"));
906
- return rb_funcall(ary, i_pack, 1, rb_str_new2("C*"));
907
- }
908
-
909
860
  /*
910
861
  * call-seq: to_json(*)
911
862
  *
@@ -1044,8 +995,11 @@ static inline VALUE vstate_get(struct generate_json_data *data)
1044
995
  }
1045
996
 
1046
997
  struct hash_foreach_arg {
998
+ VALUE hash;
1047
999
  struct generate_json_data *data;
1048
- int iter;
1000
+ int first_key_type;
1001
+ bool first;
1002
+ bool mixed_keys_encountered;
1049
1003
  };
1050
1004
 
1051
1005
  static VALUE
@@ -1063,6 +1017,22 @@ convert_string_subclass(VALUE key)
1063
1017
  return key_to_s;
1064
1018
  }
1065
1019
 
1020
+ NOINLINE()
1021
+ static void
1022
+ json_inspect_hash_with_mixed_keys(struct hash_foreach_arg *arg)
1023
+ {
1024
+ if (arg->mixed_keys_encountered) {
1025
+ return;
1026
+ }
1027
+ arg->mixed_keys_encountered = true;
1028
+
1029
+ JSON_Generator_State *state = arg->data->state;
1030
+ if (state->on_duplicate_key != JSON_IGNORE) {
1031
+ VALUE do_raise = state->on_duplicate_key == JSON_RAISE ? Qtrue : Qfalse;
1032
+ rb_funcall(mJSON, rb_intern("on_mixed_keys_hash"), 2, arg->hash, do_raise);
1033
+ }
1034
+ }
1035
+
1066
1036
  static int
1067
1037
  json_object_i(VALUE key, VALUE val, VALUE _arg)
1068
1038
  {
@@ -1073,21 +1043,33 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
1073
1043
  JSON_Generator_State *state = data->state;
1074
1044
 
1075
1045
  long depth = state->depth;
1076
- int j;
1046
+ int key_type = rb_type(key);
1047
+
1048
+ if (arg->first) {
1049
+ arg->first = false;
1050
+ arg->first_key_type = key_type;
1051
+ }
1052
+ else {
1053
+ fbuffer_append_char(buffer, ',');
1054
+ }
1077
1055
 
1078
- if (arg->iter > 0) fbuffer_append_char(buffer, ',');
1079
1056
  if (RB_UNLIKELY(data->state->object_nl)) {
1080
1057
  fbuffer_append_str(buffer, data->state->object_nl);
1081
1058
  }
1082
1059
  if (RB_UNLIKELY(data->state->indent)) {
1083
- for (j = 0; j < depth; j++) {
1084
- fbuffer_append_str(buffer, data->state->indent);
1085
- }
1060
+ fbuffer_append_str_repeat(buffer, data->state->indent, depth);
1086
1061
  }
1087
1062
 
1088
1063
  VALUE key_to_s;
1089
- switch (rb_type(key)) {
1064
+ bool as_json_called = false;
1065
+
1066
+ start:
1067
+ switch (key_type) {
1090
1068
  case T_STRING:
1069
+ if (RB_UNLIKELY(arg->first_key_type != T_STRING)) {
1070
+ json_inspect_hash_with_mixed_keys(arg);
1071
+ }
1072
+
1091
1073
  if (RB_LIKELY(RBASIC_CLASS(key) == rb_cString)) {
1092
1074
  key_to_s = key;
1093
1075
  } else {
@@ -1095,9 +1077,23 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
1095
1077
  }
1096
1078
  break;
1097
1079
  case T_SYMBOL:
1080
+ if (RB_UNLIKELY(arg->first_key_type != T_SYMBOL)) {
1081
+ json_inspect_hash_with_mixed_keys(arg);
1082
+ }
1083
+
1098
1084
  key_to_s = rb_sym2str(key);
1099
1085
  break;
1100
1086
  default:
1087
+ if (data->state->strict) {
1088
+ if (RTEST(data->state->as_json) && !as_json_called) {
1089
+ key = rb_proc_call_with_block(data->state->as_json, 1, &key, Qnil);
1090
+ key_type = rb_type(key);
1091
+ as_json_called = true;
1092
+ goto start;
1093
+ } else {
1094
+ raise_generator_error(key, "%"PRIsVALUE" not allowed as object key in JSON", CLASS_OF(key));
1095
+ }
1096
+ }
1101
1097
  key_to_s = rb_convert_type(key, T_STRING, "String", "to_s");
1102
1098
  break;
1103
1099
  }
@@ -1112,7 +1108,6 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
1112
1108
  if (RB_UNLIKELY(state->space)) fbuffer_append_str(buffer, data->state->space);
1113
1109
  generate_json(buffer, data, val);
1114
1110
 
1115
- arg->iter++;
1116
1111
  return ST_CONTINUE;
1117
1112
  }
1118
1113
 
@@ -1128,7 +1123,6 @@ static inline long increase_depth(struct generate_json_data *data)
1128
1123
 
1129
1124
  static void generate_json_object(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1130
1125
  {
1131
- int j;
1132
1126
  long depth = increase_depth(data);
1133
1127
 
1134
1128
  if (RHASH_SIZE(obj) == 0) {
@@ -1140,8 +1134,9 @@ static void generate_json_object(FBuffer *buffer, struct generate_json_data *dat
1140
1134
  fbuffer_append_char(buffer, '{');
1141
1135
 
1142
1136
  struct hash_foreach_arg arg = {
1137
+ .hash = obj,
1143
1138
  .data = data,
1144
- .iter = 0,
1139
+ .first = true,
1145
1140
  };
1146
1141
  rb_hash_foreach(obj, json_object_i, (VALUE)&arg);
1147
1142
 
@@ -1149,9 +1144,7 @@ static void generate_json_object(FBuffer *buffer, struct generate_json_data *dat
1149
1144
  if (RB_UNLIKELY(data->state->object_nl)) {
1150
1145
  fbuffer_append_str(buffer, data->state->object_nl);
1151
1146
  if (RB_UNLIKELY(data->state->indent)) {
1152
- for (j = 0; j < depth; j++) {
1153
- fbuffer_append_str(buffer, data->state->indent);
1154
- }
1147
+ fbuffer_append_str_repeat(buffer, data->state->indent, depth);
1155
1148
  }
1156
1149
  }
1157
1150
  fbuffer_append_char(buffer, '}');
@@ -1159,7 +1152,6 @@ static void generate_json_object(FBuffer *buffer, struct generate_json_data *dat
1159
1152
 
1160
1153
  static void generate_json_array(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1161
1154
  {
1162
- int i, j;
1163
1155
  long depth = increase_depth(data);
1164
1156
 
1165
1157
  if (RARRAY_LEN(obj) == 0) {
@@ -1170,15 +1162,13 @@ static void generate_json_array(FBuffer *buffer, struct generate_json_data *data
1170
1162
 
1171
1163
  fbuffer_append_char(buffer, '[');
1172
1164
  if (RB_UNLIKELY(data->state->array_nl)) fbuffer_append_str(buffer, data->state->array_nl);
1173
- for (i = 0; i < RARRAY_LEN(obj); i++) {
1165
+ for (int i = 0; i < RARRAY_LEN(obj); i++) {
1174
1166
  if (i > 0) {
1175
1167
  fbuffer_append_char(buffer, ',');
1176
1168
  if (RB_UNLIKELY(data->state->array_nl)) fbuffer_append_str(buffer, data->state->array_nl);
1177
1169
  }
1178
1170
  if (RB_UNLIKELY(data->state->indent)) {
1179
- for (j = 0; j < depth; j++) {
1180
- fbuffer_append_str(buffer, data->state->indent);
1181
- }
1171
+ fbuffer_append_str_repeat(buffer, data->state->indent, depth);
1182
1172
  }
1183
1173
  generate_json(buffer, data, RARRAY_AREF(obj, i));
1184
1174
  }
@@ -1186,9 +1176,7 @@ static void generate_json_array(FBuffer *buffer, struct generate_json_data *data
1186
1176
  if (RB_UNLIKELY(data->state->array_nl)) {
1187
1177
  fbuffer_append_str(buffer, data->state->array_nl);
1188
1178
  if (RB_UNLIKELY(data->state->indent)) {
1189
- for (j = 0; j < depth; j++) {
1190
- fbuffer_append_str(buffer, data->state->indent);
1191
- }
1179
+ fbuffer_append_str_repeat(buffer, data->state->indent, depth);
1192
1180
  }
1193
1181
  }
1194
1182
  fbuffer_append_char(buffer, ']');
@@ -1850,6 +1838,19 @@ static VALUE cState_ascii_only_set(VALUE self, VALUE enable)
1850
1838
  return Qnil;
1851
1839
  }
1852
1840
 
1841
+ static VALUE cState_allow_duplicate_key_p(VALUE self)
1842
+ {
1843
+ GET_STATE(self);
1844
+ switch (state->on_duplicate_key) {
1845
+ case JSON_IGNORE:
1846
+ return Qtrue;
1847
+ case JSON_DEPRECATED:
1848
+ return Qnil;
1849
+ default:
1850
+ return Qfalse;
1851
+ }
1852
+ }
1853
+
1853
1854
  /*
1854
1855
  * call-seq: depth
1855
1856
  *
@@ -1939,6 +1940,7 @@ static int configure_state_i(VALUE key, VALUE val, VALUE _arg)
1939
1940
  else if (key == sym_script_safe) { state->script_safe = RTEST(val); }
1940
1941
  else if (key == sym_escape_slash) { state->script_safe = RTEST(val); }
1941
1942
  else if (key == sym_strict) { state->strict = RTEST(val); }
1943
+ else if (key == sym_allow_duplicate_key) { state->on_duplicate_key = RTEST(val) ? JSON_IGNORE : JSON_RAISE; }
1942
1944
  else if (key == sym_as_json) {
1943
1945
  VALUE proc = RTEST(val) ? rb_convert_type(val, T_DATA, "Proc", "to_proc") : Qfalse;
1944
1946
  state_write_value(data, &state->as_json, proc);
@@ -2064,6 +2066,8 @@ void Init_generator(void)
2064
2066
  rb_define_method(cState, "generate", cState_generate, -1);
2065
2067
  rb_define_alias(cState, "generate_new", "generate"); // :nodoc:
2066
2068
 
2069
+ rb_define_private_method(cState, "allow_duplicate_key?", cState_allow_duplicate_key_p, 0);
2070
+
2067
2071
  rb_define_singleton_method(cState, "generate", cState_m_generate, 3);
2068
2072
 
2069
2073
  VALUE mGeneratorMethods = rb_define_module_under(mGenerator, "GeneratorMethods");
@@ -2091,13 +2095,7 @@ void Init_generator(void)
2091
2095
  rb_define_method(mFloat, "to_json", mFloat_to_json, -1);
2092
2096
 
2093
2097
  VALUE mString = rb_define_module_under(mGeneratorMethods, "String");
2094
- rb_define_singleton_method(mString, "included", mString_included_s, 1);
2095
2098
  rb_define_method(mString, "to_json", mString_to_json, -1);
2096
- rb_define_method(mString, "to_json_raw", mString_to_json_raw, -1);
2097
- rb_define_method(mString, "to_json_raw_object", mString_to_json_raw_object, 0);
2098
-
2099
- mString_Extend = rb_define_module_under(mString, "Extend");
2100
- rb_define_method(mString_Extend, "json_create", mString_Extend_json_create, 1);
2101
2099
 
2102
2100
  VALUE mTrueClass = rb_define_module_under(mGeneratorMethods, "TrueClass");
2103
2101
  rb_define_method(mTrueClass, "to_json", mTrueClass_to_json, -1);
@@ -2134,6 +2132,7 @@ void Init_generator(void)
2134
2132
  sym_escape_slash = ID2SYM(rb_intern("escape_slash"));
2135
2133
  sym_strict = ID2SYM(rb_intern("strict"));
2136
2134
  sym_as_json = ID2SYM(rb_intern("as_json"));
2135
+ sym_allow_duplicate_key = ID2SYM(rb_intern("allow_duplicate_key"));
2137
2136
 
2138
2137
  usascii_encindex = rb_usascii_encindex();
2139
2138
  utf8_encindex = rb_utf8_encindex();
@@ -713,11 +713,16 @@ static VALUE json_string_unescape(JSON_ParserState *state, const char *string, c
713
713
  }
714
714
  if (pe[0] == '\\' && pe[1] == 'u') {
715
715
  uint32_t sur = unescape_unicode(state, (unsigned char *) pe + 2);
716
+
717
+ if ((sur & 0xFC00) != 0xDC00) {
718
+ raise_parse_error_at("invalid surrogate pair at %s", state, p);
719
+ }
720
+
716
721
  ch = (((ch & 0x3F) << 10) | ((((ch >> 6) & 0xF) + 1) << 16)
717
722
  | (sur & 0x3FF));
718
723
  pe += 5;
719
724
  } else {
720
- unescape = (char *) "?";
725
+ raise_parse_error_at("incomplete surrogate pair at %s", state, p);
721
726
  break;
722
727
  }
723
728
  }
@@ -975,7 +980,7 @@ static inline bool FORCE_INLINE string_scan(JSON_ParserState *state)
975
980
  if (RB_UNLIKELY(string_scan_table[(unsigned char)*state->cursor])) {
976
981
  return 1;
977
982
  }
978
- *state->cursor++;
983
+ state->cursor++;
979
984
  }
980
985
  return 0;
981
986
  }
@@ -1265,7 +1270,7 @@ static VALUE json_parse_any(JSON_ParserState *state, JSON_ParserConfig *config)
1265
1270
  break;
1266
1271
  }
1267
1272
 
1268
- raise_parse_error("unreacheable: %s", state);
1273
+ raise_parse_error("unreachable: %s", state);
1269
1274
  }
1270
1275
 
1271
1276
  static void json_ensure_eof(JSON_ParserState *state)
@@ -1314,7 +1319,7 @@ static int parser_config_init_i(VALUE key, VALUE val, VALUE data)
1314
1319
  else if (key == sym_symbolize_names) { config->symbolize_names = RTEST(val); }
1315
1320
  else if (key == sym_freeze) { config->freeze = RTEST(val); }
1316
1321
  else if (key == sym_on_load) { config->on_load_proc = RTEST(val) ? val : Qfalse; }
1317
- else if (key == sym_allow_duplicate_key) { config->on_duplicate_key = RTEST(val) ? JSON_IGNORE : JSON_RAISE; }
1322
+ else if (key == sym_allow_duplicate_key) { config->on_duplicate_key = RTEST(val) ? JSON_IGNORE : JSON_RAISE; }
1318
1323
  else if (key == sym_decimal_class) {
1319
1324
  if (RTEST(val)) {
1320
1325
  if (rb_respond_to(val, i_try_convert)) {
data/lib/json/add/core.rb CHANGED
@@ -7,6 +7,7 @@ require 'json/add/date_time'
7
7
  require 'json/add/exception'
8
8
  require 'json/add/range'
9
9
  require 'json/add/regexp'
10
+ require 'json/add/string'
10
11
  require 'json/add/struct'
11
12
  require 'json/add/symbol'
12
13
  require 'json/add/time'
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+ unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
3
+ require 'json'
4
+ end
5
+
6
+ class String
7
+ # call-seq: json_create(o)
8
+ #
9
+ # Raw Strings are JSON Objects (the raw bytes are stored in an array for the
10
+ # key "raw"). The Ruby String can be created by this class method.
11
+ def self.json_create(object)
12
+ object["raw"].pack("C*")
13
+ end
14
+
15
+ # call-seq: to_json_raw_object()
16
+ #
17
+ # This method creates a raw object hash, that can be nested into
18
+ # other data structures and will be generated as a raw string. This
19
+ # method should be used, if you want to convert raw strings to JSON
20
+ # instead of UTF-8 strings, e. g. binary data.
21
+ def to_json_raw_object
22
+ {
23
+ JSON.create_id => self.class.name,
24
+ "raw" => unpack("C*"),
25
+ }
26
+ end
27
+
28
+ # call-seq: to_json_raw(*args)
29
+ #
30
+ # This method creates a JSON text from the result of a call to
31
+ # to_json_raw_object of this String.
32
+ def to_json_raw(...)
33
+ to_json_raw_object.to_json(...)
34
+ end
35
+ end
data/lib/json/common.rb CHANGED
@@ -73,7 +73,7 @@ module JSON
73
73
  if opts[:create_additions] != false
74
74
  if class_name = object[JSON.create_id]
75
75
  klass = JSON.deep_const_get(class_name)
76
- if (klass.respond_to?(:json_creatable?) && klass.json_creatable?) || klass.respond_to?(:json_create)
76
+ if klass.respond_to?(:json_creatable?) ? klass.json_creatable? : klass.respond_to?(:json_create)
77
77
  create_additions_warning if create_additions.nil?
78
78
  object = klass.json_create(object)
79
79
  end
@@ -97,7 +97,7 @@ module JSON
97
97
 
98
98
  class << self
99
99
  def deprecation_warning(message, uplevel = 3) # :nodoc:
100
- gem_root = File.expand_path("../../../", __FILE__) + "/"
100
+ gem_root = File.expand_path("..", __dir__) + "/"
101
101
  caller_locations(uplevel, 10).each do |frame|
102
102
  if frame.path.nil? || frame.path.start_with?(gem_root) || frame.path.end_with?("/truffle/cext_ruby.rb", ".c")
103
103
  uplevel += 1
@@ -186,6 +186,25 @@ module JSON
186
186
 
187
187
  private
188
188
 
189
+ # Called from the extension when a hash has both string and symbol keys
190
+ def on_mixed_keys_hash(hash, do_raise)
191
+ set = {}
192
+ hash.each_key do |key|
193
+ key_str = key.to_s
194
+
195
+ if set[key_str]
196
+ message = "detected duplicate key #{key_str.inspect} in #{hash.inspect}"
197
+ if do_raise
198
+ raise GeneratorError, message
199
+ else
200
+ deprecation_warning("#{message}.\nThis will raise an error in json 3.0 unless enabled via `allow_duplicate_key: true`")
201
+ end
202
+ else
203
+ set[key_str] = true
204
+ end
205
+ end
206
+ end
207
+
189
208
  def deprecated_singleton_attr_accessor(*attrs)
190
209
  args = RUBY_VERSION >= "3.0" ? ", category: :deprecated" : ""
191
210
  attrs.each do |attr|
@@ -391,7 +410,7 @@ module JSON
391
410
  #
392
411
  # Returns a \String containing the generated \JSON data.
393
412
  #
394
- # See also JSON.fast_generate, JSON.pretty_generate.
413
+ # See also JSON.pretty_generate.
395
414
  #
396
415
  # Argument +obj+ is the Ruby object to be converted to \JSON.
397
416
  #
@@ -643,6 +662,7 @@ module JSON
643
662
  # when Array
644
663
  # obj.map! {|v| deserialize_obj v }
645
664
  # end
665
+ # obj
646
666
  # })
647
667
  # pp ruby
648
668
  # Output:
@@ -684,9 +704,13 @@ module JSON
684
704
  if opts[:allow_blank] && (source.nil? || source.empty?)
685
705
  source = 'null'
686
706
  end
687
- result = parse(source, opts)
688
- recurse_proc(result, &proc) if proc
689
- result
707
+
708
+ if proc
709
+ opts = opts.dup
710
+ opts[:on_load] = proc.to_proc
711
+ end
712
+
713
+ parse(source, opts)
690
714
  end
691
715
 
692
716
  # :call-seq:
@@ -803,6 +827,7 @@ module JSON
803
827
  # when Array
804
828
  # obj.map! {|v| deserialize_obj v }
805
829
  # end
830
+ # obj
806
831
  # })
807
832
  # pp ruby
808
833
  # Output:
@@ -1002,7 +1027,7 @@ module JSON
1002
1027
  # See {Parsing Options}[#module-JSON-label-Parsing+Options], and {Generating Options}[#module-JSON-label-Generating+Options].
1003
1028
  #
1004
1029
  # For generation, the <tt>strict: true</tt> option is always set. When a Ruby object with no native \JSON counterpart is
1005
- # encoutered, the block provided to the initialize method is invoked, and must return a Ruby object that has a native
1030
+ # encountered, the block provided to the initialize method is invoked, and must return a Ruby object that has a native
1006
1031
  # \JSON counterpart:
1007
1032
  #
1008
1033
  # module MyApp
@@ -8,20 +8,8 @@ module JSON
8
8
  #
9
9
  # Instantiates a new State object, configured by _opts_.
10
10
  #
11
- # _opts_ can have the following keys:
12
- #
13
- # * *indent*: a string used to indent levels (default: ''),
14
- # * *space*: a string that is put after, a : or , delimiter (default: ''),
15
- # * *space_before*: a string that is put before a : pair delimiter (default: ''),
16
- # * *object_nl*: a string that is put at the end of a JSON object (default: ''),
17
- # * *array_nl*: a string that is put at the end of a JSON array (default: ''),
18
- # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
19
- # generated, otherwise an exception is thrown, if these values are
20
- # encountered. This options defaults to false.
21
- # * *ascii_only*: true if only ASCII characters should be generated. This
22
- # option defaults to false.
23
- # * *buffer_initial_length*: sets the initial length of the generator's
24
- # internal buffer.
11
+ # Argument +opts+, if given, contains a \Hash of options for the generation.
12
+ # See {Generating Options}[#module-JSON-label-Generating+Options].
25
13
  def initialize(opts = nil)
26
14
  if opts && !opts.empty?
27
15
  configure(opts)
@@ -68,6 +56,11 @@ module JSON
68
56
  buffer_initial_length: buffer_initial_length,
69
57
  }
70
58
 
59
+ allow_duplicate_key = allow_duplicate_key?
60
+ unless allow_duplicate_key.nil?
61
+ result[:allow_duplicate_key] = allow_duplicate_key
62
+ end
63
+
71
64
  instance_variables.each do |iv|
72
65
  iv = iv.to_s[1..-1]
73
66
  result[iv.to_sym] = self[iv]
@@ -52,14 +52,6 @@ module JSON
52
52
  table
53
53
  end
54
54
 
55
- def [](name)
56
- __send__(name)
57
- end unless method_defined?(:[])
58
-
59
- def []=(name, value)
60
- __send__("#{name}=", value)
61
- end unless method_defined?(:[]=)
62
-
63
55
  def |(other)
64
56
  self.class[other.to_hash.merge(to_hash)]
65
57
  end
@@ -271,6 +271,12 @@ module JSON
271
271
  false
272
272
  end
273
273
 
274
+ if opts.key?(:allow_duplicate_key)
275
+ @allow_duplicate_key = !!opts[:allow_duplicate_key]
276
+ else
277
+ @allow_duplicate_key = nil # nil is deprecation
278
+ end
279
+
274
280
  @strict = !!opts[:strict] if opts.key?(:strict)
275
281
 
276
282
  if !opts.key?(:max_nesting) # defaults to 100
@@ -284,6 +290,10 @@ module JSON
284
290
  end
285
291
  alias merge configure
286
292
 
293
+ def allow_duplicate_key? # :nodoc:
294
+ @allow_duplicate_key
295
+ end
296
+
287
297
  # Returns the configuration instance variables as a hash, that can be
288
298
  # passed to the configure method.
289
299
  def to_h
@@ -292,6 +302,11 @@ module JSON
292
302
  iv = iv.to_s[1..-1]
293
303
  result[iv.to_sym] = self[iv]
294
304
  end
305
+
306
+ if result[:allow_duplicate_key].nil?
307
+ result.delete(:allow_duplicate_key)
308
+ end
309
+
295
310
  result
296
311
  end
297
312
 
@@ -330,8 +345,17 @@ module JSON
330
345
  when Hash
331
346
  buf << '{'
332
347
  first = true
348
+ key_type = nil
333
349
  obj.each_pair do |k,v|
334
- buf << ',' unless first
350
+ if first
351
+ key_type = k.class
352
+ else
353
+ if key_type && !@allow_duplicate_key && key_type != k.class
354
+ key_type = nil # stop checking
355
+ JSON.send(:on_mixed_keys_hash, obj, !@allow_duplicate_key.nil?)
356
+ end
357
+ buf << ','
358
+ end
335
359
 
336
360
  key_str = k.to_s
337
361
  if key_str.class == String
@@ -471,11 +495,30 @@ module JSON
471
495
  delim = ",#{state.object_nl}"
472
496
  result = +"{#{state.object_nl}"
473
497
  first = true
498
+ key_type = nil
474
499
  indent = !state.object_nl.empty?
475
500
  each { |key, value|
476
- result << delim unless first
501
+ if first
502
+ key_type = key.class
503
+ else
504
+ if key_type && !state.allow_duplicate_key? && key_type != key.class
505
+ key_type = nil # stop checking
506
+ JSON.send(:on_mixed_keys_hash, self, state.allow_duplicate_key? == false)
507
+ end
508
+ result << delim
509
+ end
477
510
  result << state.indent * depth if indent
478
511
 
512
+ if state.strict? && !(Symbol === key || String === key)
513
+ if state.as_json
514
+ key = state.as_json.call(key)
515
+ end
516
+
517
+ unless Symbol === key || String === key
518
+ raise GeneratorError.new("#{key.class} not allowed as object key in JSON", value)
519
+ end
520
+ end
521
+
479
522
  key_str = key.to_s
480
523
  if key_str.is_a?(String)
481
524
  key_json = key_str.to_json(state)
@@ -635,39 +678,6 @@ module JSON
635
678
  rescue Encoding::UndefinedConversionError => error
636
679
  raise ::JSON::GeneratorError.new(error.message, self)
637
680
  end
638
-
639
- # Module that holds the extending methods if, the String module is
640
- # included.
641
- module Extend
642
- # Raw Strings are JSON Objects (the raw bytes are stored in an
643
- # array for the key "raw"). The Ruby String can be created by this
644
- # module method.
645
- def json_create(o)
646
- o['raw'].pack('C*')
647
- end
648
- end
649
-
650
- # Extends _modul_ with the String::Extend module.
651
- def self.included(modul)
652
- modul.extend Extend
653
- end
654
-
655
- # This method creates a raw object hash, that can be nested into
656
- # other data structures and will be unparsed as a raw string. This
657
- # method should be used, if you want to convert raw strings to JSON
658
- # instead of UTF-8 strings, e. g. binary data.
659
- def to_json_raw_object
660
- {
661
- JSON.create_id => self.class.name,
662
- 'raw' => self.unpack('C*'),
663
- }
664
- end
665
-
666
- # This method creates a JSON text from the result of
667
- # a call to to_json_raw_object of this String.
668
- def to_json_raw(*args)
669
- to_json_raw_object.to_json(*args)
670
- end
671
681
  end
672
682
 
673
683
  module TrueClass
data/lib/json/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JSON
4
- VERSION = '2.13.2'
4
+ VERSION = '2.14.1'
5
5
  end
data/lib/json.rb CHANGED
@@ -133,7 +133,7 @@ require 'json/common'
133
133
  # When not specified:
134
134
  # # The last value is used and a deprecation warning emitted.
135
135
  # JSON.parse('{"a": 1, "a":2}') => {"a" => 2}
136
- # # waring: detected duplicate keys in JSON object.
136
+ # # warning: detected duplicate keys in JSON object.
137
137
  # # This will raise an error in json 3.0 unless enabled via `allow_duplicate_key: true`
138
138
  #
139
139
  # When set to `+true+`
@@ -307,6 +307,25 @@ require 'json/common'
307
307
  #
308
308
  # ---
309
309
  #
310
+ # Option +allow_duplicate_key+ (boolean) specifies whether
311
+ # hashes with duplicate keys should be allowed or produce an error.
312
+ # defaults to emit a deprecation warning.
313
+ #
314
+ # With the default, (not set):
315
+ # Warning[:deprecated] = true
316
+ # JSON.generate({ foo: 1, "foo" => 2 })
317
+ # # warning: detected duplicate key "foo" in {foo: 1, "foo" => 2}.
318
+ # # This will raise an error in json 3.0 unless enabled via `allow_duplicate_key: true`
319
+ # # => '{"foo":1,"foo":2}'
320
+ #
321
+ # With <tt>false</tt>
322
+ # JSON.generate({ foo: 1, "foo" => 2 }, allow_duplicate_key: false)
323
+ # # detected duplicate key "foo" in {foo: 1, "foo" => 2} (JSON::GeneratorError)
324
+ #
325
+ # In version 3.0, <tt>false</tt> will become the default.
326
+ #
327
+ # ---
328
+ #
310
329
  # Option +max_nesting+ (\Integer) specifies the maximum nesting depth
311
330
  # in +obj+; defaults to +100+.
312
331
  #
@@ -384,6 +403,9 @@ require 'json/common'
384
403
  #
385
404
  # == \JSON Additions
386
405
  #
406
+ # Note that JSON Additions must only be used with trusted data, and is
407
+ # deprecated.
408
+ #
387
409
  # When you "round trip" a non-\String object from Ruby to \JSON and back,
388
410
  # you have a new \String, instead of the object you began with:
389
411
  # ruby0 = Range.new(0, 2)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.13.2
4
+ version: 2.14.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-07-28 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: This is a JSON implementation as a Ruby extension in C.
13
13
  email: flori@ping.de
@@ -45,6 +45,7 @@ files:
45
45
  - lib/json/add/rational.rb
46
46
  - lib/json/add/regexp.rb
47
47
  - lib/json/add/set.rb
48
+ - lib/json/add/string.rb
48
49
  - lib/json/add/struct.rb
49
50
  - lib/json/add/symbol.rb
50
51
  - lib/json/add/time.rb
@@ -81,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
82
  - !ruby/object:Gem::Version
82
83
  version: '0'
83
84
  requirements: []
84
- rubygems_version: 3.6.2
85
+ rubygems_version: 3.6.9
85
86
  specification_version: 4
86
87
  summary: JSON Implementation for Ruby
87
88
  test_files: []