json 2.6.3 → 2.11.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/BSDL +22 -0
  3. data/CHANGES.md +188 -17
  4. data/LEGAL +8 -0
  5. data/README.md +67 -224
  6. data/ext/json/ext/fbuffer/fbuffer.h +144 -95
  7. data/ext/json/ext/generator/extconf.rb +8 -2
  8. data/ext/json/ext/generator/generator.c +1043 -810
  9. data/ext/json/ext/parser/extconf.rb +6 -27
  10. data/ext/json/ext/parser/parser.c +1227 -3209
  11. data/ext/json/ext/vendor/fpconv.c +479 -0
  12. data/ext/json/ext/vendor/jeaiii-ltoa.h +267 -0
  13. data/json.gemspec +48 -52
  14. data/lib/json/add/bigdecimal.rb +39 -10
  15. data/lib/json/add/complex.rb +29 -6
  16. data/lib/json/add/core.rb +1 -1
  17. data/lib/json/add/date.rb +27 -7
  18. data/lib/json/add/date_time.rb +26 -9
  19. data/lib/json/add/exception.rb +25 -7
  20. data/lib/json/add/ostruct.rb +32 -9
  21. data/lib/json/add/range.rb +33 -8
  22. data/lib/json/add/rational.rb +28 -6
  23. data/lib/json/add/regexp.rb +26 -8
  24. data/lib/json/add/set.rb +25 -6
  25. data/lib/json/add/struct.rb +29 -7
  26. data/lib/json/add/symbol.rb +34 -7
  27. data/lib/json/add/time.rb +29 -15
  28. data/lib/json/common.rb +648 -249
  29. data/lib/json/ext/generator/state.rb +106 -0
  30. data/lib/json/ext.rb +34 -4
  31. data/lib/json/generic_object.rb +7 -3
  32. data/lib/json/truffle_ruby/generator.rb +690 -0
  33. data/lib/json/version.rb +3 -7
  34. data/lib/json.rb +25 -21
  35. metadata +17 -26
  36. data/VERSION +0 -1
  37. data/ext/json/ext/generator/depend +0 -1
  38. data/ext/json/ext/generator/generator.h +0 -174
  39. data/ext/json/ext/parser/depend +0 -1
  40. data/ext/json/ext/parser/parser.h +0 -96
  41. data/ext/json/ext/parser/parser.rl +0 -986
  42. data/ext/json/extconf.rb +0 -3
  43. data/lib/json/pure/generator.rb +0 -479
  44. data/lib/json/pure/parser.rb +0 -337
  45. data/lib/json/pure.rb +0 -15
  46. /data/{LICENSE → COPYING} +0 -0
@@ -1,324 +1,418 @@
1
+ #include "ruby.h"
1
2
  #include "../fbuffer/fbuffer.h"
2
- #include "generator.h"
3
+ #include "../vendor/fpconv.c"
3
4
 
4
- static VALUE mJSON, mExt, mGenerator, cState, mGeneratorMethods, mObject,
5
- mHash, mArray,
6
- #ifdef RUBY_INTEGER_UNIFICATION
7
- mInteger,
8
- #else
9
- mFixnum, mBignum,
5
+ #include <math.h>
6
+ #include <ctype.h>
7
+
8
+ /* ruby api and some helpers */
9
+
10
+ typedef struct JSON_Generator_StateStruct {
11
+ VALUE indent;
12
+ VALUE space;
13
+ VALUE space_before;
14
+ VALUE object_nl;
15
+ VALUE array_nl;
16
+ VALUE as_json;
17
+
18
+ long max_nesting;
19
+ long depth;
20
+ long buffer_initial_length;
21
+
22
+ bool allow_nan;
23
+ bool ascii_only;
24
+ bool script_safe;
25
+ bool strict;
26
+ } JSON_Generator_State;
27
+
28
+ #ifndef RB_UNLIKELY
29
+ #define RB_UNLIKELY(cond) (cond)
10
30
  #endif
11
- mFloat, mString, mString_Extend,
12
- mTrueClass, mFalseClass, mNilClass, eGeneratorError,
13
- eNestingError;
14
31
 
15
- static ID i_to_s, i_to_json, i_new, i_indent, i_space, i_space_before,
16
- i_object_nl, i_array_nl, i_max_nesting, i_allow_nan, i_ascii_only,
17
- i_pack, i_unpack, i_create_id, i_extend, i_key_p,
18
- i_aref, i_send, i_respond_to_p, i_match, i_keys, i_depth,
19
- i_buffer_initial_length, i_dup, i_escape_slash;
32
+ static VALUE mJSON, cState, cFragment, mString_Extend, eGeneratorError, eNestingError, Encoding_UTF_8;
20
33
 
21
- /*
22
- * Copyright 2001-2004 Unicode, Inc.
23
- *
24
- * Disclaimer
25
- *
26
- * This source code is provided as is by Unicode, Inc. No claims are
27
- * made as to fitness for any particular purpose. No warranties of any
28
- * kind are expressed or implied. The recipient agrees to determine
29
- * applicability of information provided. If this file has been
30
- * purchased on magnetic or optical media from Unicode, Inc., the
31
- * sole remedy for any claim will be exchange of defective media
32
- * within 90 days of receipt.
33
- *
34
- * Limitations on Rights to Redistribute This Code
35
- *
36
- * Unicode, Inc. hereby grants the right to freely use the information
37
- * supplied in this file in the creation of products supporting the
38
- * Unicode Standard, and to make copies of this file in any form
39
- * for internal or external distribution as long as this notice
40
- * remains attached.
41
- */
34
+ static ID i_to_s, i_to_json, i_new, i_pack, i_unpack, i_create_id, i_extend, i_encode;
35
+ static VALUE sym_indent, sym_space, sym_space_before, sym_object_nl, sym_array_nl, sym_max_nesting, sym_allow_nan,
36
+ sym_ascii_only, sym_depth, sym_buffer_initial_length, sym_script_safe, sym_escape_slash, sym_strict, sym_as_json;
42
37
 
43
- /*
44
- * Index into the table below with the first byte of a UTF-8 sequence to
45
- * get the number of trailing bytes that are supposed to follow it.
46
- * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
47
- * left as-is for anyone who may want to do such conversion, which was
48
- * allowed in earlier algorithms.
49
- */
50
- static const char trailingBytesForUTF8[256] = {
51
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
52
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
53
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
54
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
55
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
56
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
57
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
58
- 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
38
+
39
+ #define GET_STATE_TO(self, state) \
40
+ TypedData_Get_Struct(self, JSON_Generator_State, &JSON_Generator_State_type, state)
41
+
42
+ #define GET_STATE(self) \
43
+ JSON_Generator_State *state; \
44
+ GET_STATE_TO(self, state)
45
+
46
+ struct generate_json_data;
47
+
48
+ typedef void (*generator_func)(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
49
+
50
+ struct generate_json_data {
51
+ FBuffer *buffer;
52
+ VALUE vstate;
53
+ JSON_Generator_State *state;
54
+ VALUE obj;
55
+ generator_func func;
59
56
  };
60
57
 
61
- /*
62
- * Magic values subtracted from a buffer value during UTF8 conversion.
63
- * This table contains as many values as there might be trailing bytes
64
- * in a UTF-8 sequence.
65
- */
66
- static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
67
- 0x03C82080UL, 0xFA082080UL, 0x82082080UL };
58
+ static VALUE cState_from_state_s(VALUE self, VALUE opts);
59
+ static VALUE cState_partial_generate(VALUE self, VALUE obj, generator_func, VALUE io);
60
+ static void generate_json(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
61
+ static void generate_json_object(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
62
+ static void generate_json_array(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
63
+ static void generate_json_string(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
64
+ static void generate_json_null(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
65
+ static void generate_json_false(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
66
+ static void generate_json_true(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
67
+ #ifdef RUBY_INTEGER_UNIFICATION
68
+ static void generate_json_integer(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
69
+ #endif
70
+ static void generate_json_fixnum(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
71
+ static void generate_json_bignum(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
72
+ static void generate_json_float(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
73
+ static void generate_json_fragment(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
68
74
 
69
- /*
70
- * Utility routine to tell whether a sequence of bytes is legal UTF-8.
71
- * This must be called with the length pre-determined by the first byte.
72
- * If not calling this from ConvertUTF8to*, then the length can be set by:
73
- * length = trailingBytesForUTF8[*source]+1;
74
- * and the sequence is illegal right away if there aren't that many bytes
75
- * available.
76
- * If presented with a length > 4, this returns 0. The Unicode
77
- * definition of UTF-8 goes up to 4-byte sequences.
78
- */
79
- static unsigned char isLegalUTF8(const UTF8 *source, unsigned long length)
80
- {
81
- UTF8 a;
82
- const UTF8 *srcptr = source+length;
83
- switch (length) {
84
- default: return 0;
85
- /* Everything else falls through when "1"... */
86
- case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
87
- case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
88
- case 2: if ((a = (*--srcptr)) > 0xBF) return 0;
89
-
90
- switch (*source) {
91
- /* no fall-through in this inner switch */
92
- case 0xE0: if (a < 0xA0) return 0; break;
93
- case 0xED: if (a > 0x9F) return 0; break;
94
- case 0xF0: if (a < 0x90) return 0; break;
95
- case 0xF4: if (a > 0x8F) return 0; break;
96
- default: if (a < 0x80) return 0;
97
- }
75
+ static int usascii_encindex, utf8_encindex, binary_encindex;
98
76
 
99
- case 1: if (*source >= 0x80 && *source < 0xC2) return 0;
100
- }
101
- if (*source > 0xF4) return 0;
102
- return 1;
77
+ #ifdef RBIMPL_ATTR_NORETURN
78
+ RBIMPL_ATTR_NORETURN()
79
+ #endif
80
+ static void raise_generator_error_str(VALUE invalid_object, VALUE str)
81
+ {
82
+ VALUE exc = rb_exc_new_str(eGeneratorError, str);
83
+ rb_ivar_set(exc, rb_intern("@invalid_object"), invalid_object);
84
+ rb_exc_raise(exc);
103
85
  }
104
86
 
105
- /* Escapes the UTF16 character and stores the result in the buffer buf. */
106
- static void unicode_escape(char *buf, UTF16 character)
87
+ #ifdef RBIMPL_ATTR_NORETURN
88
+ RBIMPL_ATTR_NORETURN()
89
+ #endif
90
+ #ifdef RBIMPL_ATTR_FORMAT
91
+ RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 3)
92
+ #endif
93
+ static void raise_generator_error(VALUE invalid_object, const char *fmt, ...)
107
94
  {
108
- const char *digits = "0123456789abcdef";
109
-
110
- buf[2] = digits[character >> 12];
111
- buf[3] = digits[(character >> 8) & 0xf];
112
- buf[4] = digits[(character >> 4) & 0xf];
113
- buf[5] = digits[character & 0xf];
95
+ va_list args;
96
+ va_start(args, fmt);
97
+ VALUE str = rb_vsprintf(fmt, args);
98
+ va_end(args);
99
+ raise_generator_error_str(invalid_object, str);
114
100
  }
115
101
 
116
- /* Escapes the UTF16 character and stores the result in the buffer buf, then
117
- * the buffer buf is appended to the FBuffer buffer. */
118
- static void unicode_escape_to_buffer(FBuffer *buffer, char buf[6], UTF16
119
- character)
102
+ // 0 - single byte char that don't need to be escaped.
103
+ // (x | 8) - char that needs to be escaped.
104
+ static const unsigned char CHAR_LENGTH_MASK = 7;
105
+ static const unsigned char ESCAPE_MASK = 8;
106
+
107
+ typedef struct _search_state {
108
+ const char *ptr;
109
+ const char *end;
110
+ const char *cursor;
111
+ FBuffer *buffer;
112
+ } search_state;
113
+
114
+ static inline void search_flush(search_state *search)
120
115
  {
121
- unicode_escape(buf, character);
122
- fbuffer_append(buffer, buf, 6);
116
+ fbuffer_append(search->buffer, search->cursor, search->ptr - search->cursor);
117
+ search->cursor = search->ptr;
123
118
  }
124
119
 
125
- /* Converts string to a JSON string in FBuffer buffer, where all but the ASCII
126
- * and control characters are JSON escaped. */
127
- static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string, char escape_slash)
128
- {
129
- const UTF8 *source = (UTF8 *) RSTRING_PTR(string);
130
- const UTF8 *sourceEnd = source + RSTRING_LEN(string);
131
- char buf[6] = { '\\', 'u' };
120
+ static const unsigned char escape_table_basic[256] = {
121
+ // ASCII Control Characters
122
+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
123
+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
124
+ // ASCII Characters
125
+ 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // '"'
126
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
127
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
128
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, // '\\'
129
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
130
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
131
+ };
132
132
 
133
- while (source < sourceEnd) {
134
- UTF32 ch = 0;
135
- unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
136
- if (source + extraBytesToRead >= sourceEnd) {
137
- rb_raise(rb_path2class("JSON::GeneratorError"),
138
- "partial character in source, but hit end");
133
+ static inline unsigned char search_escape_basic(search_state *search)
134
+ {
135
+ while (search->ptr < search->end) {
136
+ if (RB_UNLIKELY(escape_table_basic[(const unsigned char)*search->ptr])) {
137
+ search_flush(search);
138
+ return 1;
139
+ } else {
140
+ search->ptr++;
139
141
  }
140
- if (!isLegalUTF8(source, extraBytesToRead+1)) {
141
- rb_raise(rb_path2class("JSON::GeneratorError"),
142
- "source sequence is illegal/malformed utf-8");
142
+ }
143
+ search_flush(search);
144
+ return 0;
145
+ }
146
+
147
+ static inline void escape_UTF8_char_basic(search_state *search) {
148
+ const unsigned char ch = (unsigned char)*search->ptr;
149
+ switch (ch) {
150
+ case '"': fbuffer_append(search->buffer, "\\\"", 2); break;
151
+ case '\\': fbuffer_append(search->buffer, "\\\\", 2); break;
152
+ case '/': fbuffer_append(search->buffer, "\\/", 2); break;
153
+ case '\b': fbuffer_append(search->buffer, "\\b", 2); break;
154
+ case '\f': fbuffer_append(search->buffer, "\\f", 2); break;
155
+ case '\n': fbuffer_append(search->buffer, "\\n", 2); break;
156
+ case '\r': fbuffer_append(search->buffer, "\\r", 2); break;
157
+ case '\t': fbuffer_append(search->buffer, "\\t", 2); break;
158
+ default: {
159
+ const char *hexdig = "0123456789abcdef";
160
+ char scratch[6] = { '\\', 'u', '0', '0', 0, 0 };
161
+ scratch[4] = hexdig[(ch >> 4) & 0xf];
162
+ scratch[5] = hexdig[ch & 0xf];
163
+ fbuffer_append(search->buffer, scratch, 6);
164
+ break;
143
165
  }
144
- /*
145
- * The cases all fall through. See "Note A" below.
146
- */
147
- switch (extraBytesToRead) {
148
- case 5: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
149
- case 4: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
150
- case 3: ch += *source++; ch <<= 6;
151
- case 2: ch += *source++; ch <<= 6;
152
- case 1: ch += *source++; ch <<= 6;
153
- case 0: ch += *source++;
166
+ }
167
+ search->ptr++;
168
+ search->cursor = search->ptr;
169
+ }
170
+
171
+ /* Converts in_string to a JSON string (without the wrapping '"'
172
+ * characters) in FBuffer out_buffer.
173
+ *
174
+ * Character are JSON-escaped according to:
175
+ *
176
+ * - Always: ASCII control characters (0x00-0x1F), dquote, and
177
+ * backslash.
178
+ *
179
+ * - If out_ascii_only: non-ASCII characters (>0x7F)
180
+ *
181
+ * - If script_safe: forwardslash (/), line separator (U+2028), and
182
+ * paragraph separator (U+2029)
183
+ *
184
+ * Everything else (should be UTF-8) is just passed through and
185
+ * appended to the result.
186
+ */
187
+ static inline void convert_UTF8_to_JSON(search_state *search)
188
+ {
189
+ while (search_escape_basic(search)) {
190
+ escape_UTF8_char_basic(search);
191
+ }
192
+ }
193
+
194
+ static inline void escape_UTF8_char(search_state *search, unsigned char ch_len) {
195
+ const unsigned char ch = (unsigned char)*search->ptr;
196
+ switch (ch_len) {
197
+ case 1: {
198
+ switch (ch) {
199
+ case '"': fbuffer_append(search->buffer, "\\\"", 2); break;
200
+ case '\\': fbuffer_append(search->buffer, "\\\\", 2); break;
201
+ case '/': fbuffer_append(search->buffer, "\\/", 2); break;
202
+ case '\b': fbuffer_append(search->buffer, "\\b", 2); break;
203
+ case '\f': fbuffer_append(search->buffer, "\\f", 2); break;
204
+ case '\n': fbuffer_append(search->buffer, "\\n", 2); break;
205
+ case '\r': fbuffer_append(search->buffer, "\\r", 2); break;
206
+ case '\t': fbuffer_append(search->buffer, "\\t", 2); break;
207
+ default: {
208
+ const char *hexdig = "0123456789abcdef";
209
+ char scratch[6] = { '\\', 'u', '0', '0', 0, 0 };
210
+ scratch[4] = hexdig[(ch >> 4) & 0xf];
211
+ scratch[5] = hexdig[ch & 0xf];
212
+ fbuffer_append(search->buffer, scratch, 6);
213
+ break;
214
+ }
215
+ }
216
+ break;
154
217
  }
155
- ch -= offsetsFromUTF8[extraBytesToRead];
156
-
157
- if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
158
- /* UTF-16 surrogate values are illegal in UTF-32 */
159
- if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
160
- #if UNI_STRICT_CONVERSION
161
- source -= (extraBytesToRead+1); /* return to the illegal value itself */
162
- rb_raise(rb_path2class("JSON::GeneratorError"),
163
- "source sequence is illegal/malformed utf-8");
164
- #else
165
- unicode_escape_to_buffer(buffer, buf, UNI_REPLACEMENT_CHAR);
166
- #endif
218
+ case 3: {
219
+ if (search->ptr[2] & 1) {
220
+ fbuffer_append(search->buffer, "\\u2029", 6);
167
221
  } else {
168
- /* normal case */
169
- if (ch >= 0x20 && ch <= 0x7f) {
170
- switch (ch) {
171
- case '\\':
172
- fbuffer_append(buffer, "\\\\", 2);
173
- break;
174
- case '"':
175
- fbuffer_append(buffer, "\\\"", 2);
176
- break;
177
- case '/':
178
- if(escape_slash) {
179
- fbuffer_append(buffer, "\\/", 2);
180
- break;
181
- }
182
- default:
183
- fbuffer_append_char(buffer, (char)ch);
184
- break;
185
- }
186
- } else {
187
- switch (ch) {
188
- case '\n':
189
- fbuffer_append(buffer, "\\n", 2);
190
- break;
191
- case '\r':
192
- fbuffer_append(buffer, "\\r", 2);
193
- break;
194
- case '\t':
195
- fbuffer_append(buffer, "\\t", 2);
196
- break;
197
- case '\f':
198
- fbuffer_append(buffer, "\\f", 2);
199
- break;
200
- case '\b':
201
- fbuffer_append(buffer, "\\b", 2);
202
- break;
203
- default:
204
- unicode_escape_to_buffer(buffer, buf, (UTF16) ch);
205
- break;
222
+ fbuffer_append(search->buffer, "\\u2028", 6);
223
+ }
224
+ break;
225
+ }
226
+ }
227
+ search->cursor = (search->ptr += ch_len);
228
+ }
229
+
230
+ static const unsigned char script_safe_escape_table[256] = {
231
+ // ASCII Control Characters
232
+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
233
+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
234
+ // ASCII Characters
235
+ 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, // '"' and '/'
236
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
237
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
238
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, // '\\'
239
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
240
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
241
+ // Continuation byte
242
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
243
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
244
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
245
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
246
+ // First byte of a 2-byte code point
247
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
248
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
249
+ // First byte of a 3-byte code point
250
+ 3, 3,11, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0xE2 is the start of \u2028 and \u2029
251
+ //First byte of a 4+ byte code point
252
+ 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 9, 9,
253
+ };
254
+
255
+ static inline unsigned char search_script_safe_escape(search_state *search)
256
+ {
257
+ while (search->ptr < search->end) {
258
+ unsigned char ch = (unsigned char)*search->ptr;
259
+ unsigned char ch_len = script_safe_escape_table[ch];
260
+
261
+ if (RB_UNLIKELY(ch_len)) {
262
+ if (ch_len & ESCAPE_MASK) {
263
+ if (RB_UNLIKELY(ch_len == 11)) {
264
+ const unsigned char *uptr = (const unsigned char *)search->ptr;
265
+ if (!(uptr[1] == 0x80 && (uptr[2] >> 1) == 0x54)) {
266
+ search->ptr += 3;
267
+ continue;
206
268
  }
207
269
  }
270
+ search_flush(search);
271
+ return ch_len & CHAR_LENGTH_MASK;
272
+ } else {
273
+ search->ptr += ch_len;
208
274
  }
209
- } else if (ch > UNI_MAX_UTF16) {
210
- #if UNI_STRICT_CONVERSION
211
- source -= (extraBytesToRead+1); /* return to the start */
212
- rb_raise(rb_path2class("JSON::GeneratorError"),
213
- "source sequence is illegal/malformed utf8");
214
- #else
215
- unicode_escape_to_buffer(buffer, buf, UNI_REPLACEMENT_CHAR);
216
- #endif
217
275
  } else {
218
- /* target is a character in range 0xFFFF - 0x10FFFF. */
219
- ch -= halfBase;
220
- unicode_escape_to_buffer(buffer, buf, (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START));
221
- unicode_escape_to_buffer(buffer, buf, (UTF16)((ch & halfMask) + UNI_SUR_LOW_START));
276
+ search->ptr++;
222
277
  }
223
278
  }
224
- RB_GC_GUARD(string);
225
- }
226
-
227
- /* Converts string to a JSON string in FBuffer buffer, where only the
228
- * characters required by the JSON standard are JSON escaped. The remaining
229
- * characters (should be UTF8) are just passed through and appended to the
230
- * result. */
231
- static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string, char escape_slash)
232
- {
233
- const char *ptr = RSTRING_PTR(string), *p;
234
- unsigned long len = RSTRING_LEN(string), start = 0, end = 0;
235
- const char *escape = NULL;
236
- int escape_len;
237
- unsigned char c;
238
- char buf[6] = { '\\', 'u' };
239
- int ascii_only = rb_enc_str_asciionly_p(string);
240
-
241
- for (start = 0, end = 0; end < len;) {
242
- p = ptr + end;
243
- c = (unsigned char) *p;
244
- if (c < 0x20) {
245
- switch (c) {
246
- case '\n':
247
- escape = "\\n";
248
- escape_len = 2;
249
- break;
250
- case '\r':
251
- escape = "\\r";
252
- escape_len = 2;
253
- break;
254
- case '\t':
255
- escape = "\\t";
256
- escape_len = 2;
257
- break;
258
- case '\f':
259
- escape = "\\f";
260
- escape_len = 2;
261
- break;
262
- case '\b':
263
- escape = "\\b";
264
- escape_len = 2;
265
- break;
266
- default:
267
- unicode_escape(buf, (UTF16) *p);
268
- escape = buf;
269
- escape_len = 6;
279
+ search_flush(search);
280
+ return 0;
281
+ }
282
+
283
+ static void convert_UTF8_to_script_safe_JSON(search_state *search)
284
+ {
285
+ unsigned char ch_len;
286
+ while ((ch_len = search_script_safe_escape(search))) {
287
+ escape_UTF8_char(search, ch_len);
288
+ }
289
+ }
290
+
291
+ static const unsigned char ascii_only_escape_table[256] = {
292
+ // ASCII Control Characters
293
+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
294
+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
295
+ // ASCII Characters
296
+ 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // '"'
297
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
298
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
299
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, // '\\'
300
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
301
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
302
+ // Continuation byte
303
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
304
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
305
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
306
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
307
+ // First byte of a 2-byte code point
308
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
309
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
310
+ // First byte of a 3-byte code point
311
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
312
+ //First byte of a 4+ byte code point
313
+ 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 9, 9,
314
+ };
315
+
316
+ static inline unsigned char search_ascii_only_escape(search_state *search, const unsigned char escape_table[256])
317
+ {
318
+ while (search->ptr < search->end) {
319
+ unsigned char ch = (unsigned char)*search->ptr;
320
+ unsigned char ch_len = escape_table[ch];
321
+
322
+ if (RB_UNLIKELY(ch_len)) {
323
+ search_flush(search);
324
+ return ch_len & CHAR_LENGTH_MASK;
325
+ } else {
326
+ search->ptr++;
327
+ }
328
+ }
329
+ search_flush(search);
330
+ return 0;
331
+ }
332
+
333
+ static inline void full_escape_UTF8_char(search_state *search, unsigned char ch_len) {
334
+ const unsigned char ch = (unsigned char)*search->ptr;
335
+ switch (ch_len) {
336
+ case 1: {
337
+ switch (ch) {
338
+ case '"': fbuffer_append(search->buffer, "\\\"", 2); break;
339
+ case '\\': fbuffer_append(search->buffer, "\\\\", 2); break;
340
+ case '/': fbuffer_append(search->buffer, "\\/", 2); break;
341
+ case '\b': fbuffer_append(search->buffer, "\\b", 2); break;
342
+ case '\f': fbuffer_append(search->buffer, "\\f", 2); break;
343
+ case '\n': fbuffer_append(search->buffer, "\\n", 2); break;
344
+ case '\r': fbuffer_append(search->buffer, "\\r", 2); break;
345
+ case '\t': fbuffer_append(search->buffer, "\\t", 2); break;
346
+ default: {
347
+ const char *hexdig = "0123456789abcdef";
348
+ char scratch[6] = { '\\', 'u', '0', '0', 0, 0 };
349
+ scratch[4] = hexdig[(ch >> 4) & 0xf];
350
+ scratch[5] = hexdig[ch & 0xf];
351
+ fbuffer_append(search->buffer, scratch, 6);
270
352
  break;
353
+ }
271
354
  }
272
- } else {
273
- switch (c) {
274
- case '\\':
275
- escape = "\\\\";
276
- escape_len = 2;
355
+ break;
356
+ }
357
+ default: {
358
+ const char *hexdig = "0123456789abcdef";
359
+ char scratch[12] = { '\\', 'u', 0, 0, 0, 0, '\\', 'u' };
360
+
361
+ uint32_t wchar = 0;
362
+
363
+ switch(ch_len) {
364
+ case 2:
365
+ wchar = ch & 0x1F;
277
366
  break;
278
- case '"':
279
- escape = "\\\"";
280
- escape_len = 2;
367
+ case 3:
368
+ wchar = ch & 0x0F;
281
369
  break;
282
- case '/':
283
- if(escape_slash) {
284
- escape = "\\/";
285
- escape_len = 2;
286
- break;
287
- }
288
- default:
289
- {
290
- unsigned short clen = 1;
291
- if (!ascii_only) {
292
- clen += trailingBytesForUTF8[c];
293
- if (end + clen > len) {
294
- rb_raise(rb_path2class("JSON::GeneratorError"),
295
- "partial character in source, but hit end");
296
- }
297
- if (!isLegalUTF8((UTF8 *) p, clen)) {
298
- rb_raise(rb_path2class("JSON::GeneratorError"),
299
- "source sequence is illegal/malformed utf-8");
300
- }
301
- }
302
- end += clen;
303
- }
304
- continue;
370
+ case 4:
371
+ wchar = ch & 0x07;
305
372
  break;
306
373
  }
374
+
375
+ for (short i = 1; i < ch_len; i++) {
376
+ wchar = (wchar << 6) | (search->ptr[i] & 0x3F);
377
+ }
378
+
379
+ if (wchar <= 0xFFFF) {
380
+ scratch[2] = hexdig[wchar >> 12];
381
+ scratch[3] = hexdig[(wchar >> 8) & 0xf];
382
+ scratch[4] = hexdig[(wchar >> 4) & 0xf];
383
+ scratch[5] = hexdig[wchar & 0xf];
384
+ fbuffer_append(search->buffer, scratch, 6);
385
+ } else {
386
+ uint16_t hi, lo;
387
+ wchar -= 0x10000;
388
+ hi = 0xD800 + (uint16_t)(wchar >> 10);
389
+ lo = 0xDC00 + (uint16_t)(wchar & 0x3FF);
390
+
391
+ scratch[2] = hexdig[hi >> 12];
392
+ scratch[3] = hexdig[(hi >> 8) & 0xf];
393
+ scratch[4] = hexdig[(hi >> 4) & 0xf];
394
+ scratch[5] = hexdig[hi & 0xf];
395
+
396
+ scratch[8] = hexdig[lo >> 12];
397
+ scratch[9] = hexdig[(lo >> 8) & 0xf];
398
+ scratch[10] = hexdig[(lo >> 4) & 0xf];
399
+ scratch[11] = hexdig[lo & 0xf];
400
+
401
+ fbuffer_append(search->buffer, scratch, 12);
402
+ }
403
+
404
+ break;
307
405
  }
308
- fbuffer_append(buffer, ptr + start, end - start);
309
- fbuffer_append(buffer, escape, escape_len);
310
- start = ++end;
311
- escape = NULL;
312
406
  }
313
- fbuffer_append(buffer, ptr + start, end - start);
407
+ search->cursor = (search->ptr += ch_len);
314
408
  }
315
409
 
316
- static char *fstrndup(const char *ptr, unsigned long len) {
317
- char *result;
318
- if (len <= 0) return NULL;
319
- result = ALLOC_N(char, len);
320
- memcpy(result, ptr, len);
321
- return result;
410
+ static void convert_UTF8_to_ASCII_only_JSON(search_state *search, const unsigned char escape_table[256])
411
+ {
412
+ unsigned char ch_len;
413
+ while ((ch_len = search_ascii_only_escape(search, escape_table))) {
414
+ full_escape_UTF8_char(search, ch_len);
415
+ }
322
416
  }
323
417
 
324
418
  /*
@@ -413,7 +507,9 @@ static char *fstrndup(const char *ptr, unsigned long len) {
413
507
  */
414
508
  static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self)
415
509
  {
416
- GENERATE_JSON(object);
510
+ rb_check_arity(argc, 0, 1);
511
+ VALUE Vstate = cState_from_state_s(cState, argc == 1 ? argv[0] : Qnil);
512
+ return cState_partial_generate(Vstate, self, generate_json_object, Qfalse);
417
513
  }
418
514
 
419
515
  /*
@@ -425,7 +521,9 @@ static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self)
425
521
  * produced JSON string output further.
426
522
  */
427
523
  static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self) {
428
- GENERATE_JSON(array);
524
+ rb_check_arity(argc, 0, 1);
525
+ VALUE Vstate = cState_from_state_s(cState, argc == 1 ? argv[0] : Qnil);
526
+ return cState_partial_generate(Vstate, self, generate_json_array, Qfalse);
429
527
  }
430
528
 
431
529
  #ifdef RUBY_INTEGER_UNIFICATION
@@ -436,7 +534,9 @@ static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self) {
436
534
  */
437
535
  static VALUE mInteger_to_json(int argc, VALUE *argv, VALUE self)
438
536
  {
439
- GENERATE_JSON(integer);
537
+ rb_check_arity(argc, 0, 1);
538
+ VALUE Vstate = cState_from_state_s(cState, argc == 1 ? argv[0] : Qnil);
539
+ return cState_partial_generate(Vstate, self, generate_json_integer, Qfalse);
440
540
  }
441
541
 
442
542
  #else
@@ -447,7 +547,9 @@ static VALUE mInteger_to_json(int argc, VALUE *argv, VALUE self)
447
547
  */
448
548
  static VALUE mFixnum_to_json(int argc, VALUE *argv, VALUE self)
449
549
  {
450
- GENERATE_JSON(fixnum);
550
+ rb_check_arity(argc, 0, 1);
551
+ VALUE Vstate = cState_from_state_s(cState, argc == 1 ? argv[0] : Qnil);
552
+ return cState_partial_generate(Vstate, self, generate_json_fixnum, Qfalse);
451
553
  }
452
554
 
453
555
  /*
@@ -457,7 +559,9 @@ static VALUE mFixnum_to_json(int argc, VALUE *argv, VALUE self)
457
559
  */
458
560
  static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self)
459
561
  {
460
- GENERATE_JSON(bignum);
562
+ rb_check_arity(argc, 0, 1);
563
+ VALUE Vstate = cState_from_state_s(cState, argc == 1 ? argv[0] : Qnil);
564
+ return cState_partial_generate(Vstate, self, generate_json_bignum, Qfalse);
461
565
  }
462
566
  #endif
463
567
 
@@ -468,7 +572,9 @@ static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self)
468
572
  */
469
573
  static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self)
470
574
  {
471
- GENERATE_JSON(float);
575
+ rb_check_arity(argc, 0, 1);
576
+ VALUE Vstate = cState_from_state_s(cState, argc == 1 ? argv[0] : Qnil);
577
+ return cState_partial_generate(Vstate, self, generate_json_float, Qfalse);
472
578
  }
473
579
 
474
580
  /*
@@ -478,6 +584,7 @@ static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self)
478
584
  */
479
585
  static VALUE mString_included_s(VALUE self, VALUE modul) {
480
586
  VALUE result = rb_funcall(modul, i_extend, 1, mString_Extend);
587
+ rb_call_super(1, &modul);
481
588
  return result;
482
589
  }
483
590
 
@@ -490,7 +597,9 @@ static VALUE mString_included_s(VALUE self, VALUE modul) {
490
597
  */
491
598
  static VALUE mString_to_json(int argc, VALUE *argv, VALUE self)
492
599
  {
493
- GENERATE_JSON(string);
600
+ rb_check_arity(argc, 0, 1);
601
+ VALUE Vstate = cState_from_state_s(cState, argc == 1 ? argv[0] : Qnil);
602
+ return cState_partial_generate(Vstate, self, generate_json_string, Qfalse);
494
603
  }
495
604
 
496
605
  /*
@@ -507,7 +616,7 @@ static VALUE mString_to_json_raw_object(VALUE self)
507
616
  VALUE result = rb_hash_new();
508
617
  rb_hash_aset(result, rb_funcall(mJSON, i_create_id, 0), rb_class_name(rb_obj_class(self)));
509
618
  ary = rb_funcall(self, i_unpack, 1, rb_str_new2("C*"));
510
- rb_hash_aset(result, rb_str_new2("raw"), ary);
619
+ rb_hash_aset(result, rb_utf8_str_new_lit("raw"), ary);
511
620
  return result;
512
621
  }
513
622
 
@@ -545,7 +654,8 @@ static VALUE mString_Extend_json_create(VALUE self, VALUE o)
545
654
  */
546
655
  static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self)
547
656
  {
548
- GENERATE_JSON(true);
657
+ rb_check_arity(argc, 0, 1);
658
+ return rb_utf8_str_new("true", 4);
549
659
  }
550
660
 
551
661
  /*
@@ -555,7 +665,8 @@ static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self)
555
665
  */
556
666
  static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self)
557
667
  {
558
- GENERATE_JSON(false);
668
+ rb_check_arity(argc, 0, 1);
669
+ return rb_utf8_str_new("false", 5);
559
670
  }
560
671
 
561
672
  /*
@@ -565,7 +676,8 @@ static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self)
565
676
  */
566
677
  static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self)
567
678
  {
568
- GENERATE_JSON(null);
679
+ rb_check_arity(argc, 0, 1);
680
+ return rb_utf8_str_new("null", 4);
569
681
  }
570
682
 
571
683
  /*
@@ -582,36 +694,40 @@ static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self)
582
694
  rb_scan_args(argc, argv, "01", &state);
583
695
  Check_Type(string, T_STRING);
584
696
  state = cState_from_state_s(cState, state);
585
- return cState_partial_generate(state, string);
697
+ return cState_partial_generate(state, string, generate_json_string, Qfalse);
698
+ }
699
+
700
+ static void State_mark(void *ptr)
701
+ {
702
+ JSON_Generator_State *state = ptr;
703
+ rb_gc_mark_movable(state->indent);
704
+ rb_gc_mark_movable(state->space);
705
+ rb_gc_mark_movable(state->space_before);
706
+ rb_gc_mark_movable(state->object_nl);
707
+ rb_gc_mark_movable(state->array_nl);
708
+ rb_gc_mark_movable(state->as_json);
709
+ }
710
+
711
+ static void State_compact(void *ptr)
712
+ {
713
+ JSON_Generator_State *state = ptr;
714
+ state->indent = rb_gc_location(state->indent);
715
+ state->space = rb_gc_location(state->space);
716
+ state->space_before = rb_gc_location(state->space_before);
717
+ state->object_nl = rb_gc_location(state->object_nl);
718
+ state->array_nl = rb_gc_location(state->array_nl);
719
+ state->as_json = rb_gc_location(state->as_json);
586
720
  }
587
721
 
588
722
  static void State_free(void *ptr)
589
723
  {
590
724
  JSON_Generator_State *state = ptr;
591
- if (state->indent) ruby_xfree(state->indent);
592
- if (state->space) ruby_xfree(state->space);
593
- if (state->space_before) ruby_xfree(state->space_before);
594
- if (state->object_nl) ruby_xfree(state->object_nl);
595
- if (state->array_nl) ruby_xfree(state->array_nl);
596
- if (state->array_delim) fbuffer_free(state->array_delim);
597
- if (state->object_delim) fbuffer_free(state->object_delim);
598
- if (state->object_delim2) fbuffer_free(state->object_delim2);
599
725
  ruby_xfree(state);
600
726
  }
601
727
 
602
728
  static size_t State_memsize(const void *ptr)
603
729
  {
604
- const JSON_Generator_State *state = ptr;
605
- size_t size = sizeof(*state);
606
- if (state->indent) size += state->indent_len + 1;
607
- if (state->space) size += state->space_len + 1;
608
- if (state->space_before) size += state->space_before_len + 1;
609
- if (state->object_nl) size += state->object_nl_len + 1;
610
- if (state->array_nl) size += state->array_nl_len + 1;
611
- if (state->array_delim) size += FBUFFER_CAPA(state->array_delim);
612
- if (state->object_delim) size += FBUFFER_CAPA(state->object_delim);
613
- if (state->object_delim2) size += FBUFFER_CAPA(state->object_delim2);
614
- return size;
730
+ return sizeof(JSON_Generator_State);
615
731
  }
616
732
 
617
733
  #ifndef HAVE_RB_EXT_RACTOR_SAFE
@@ -619,193 +735,57 @@ static size_t State_memsize(const void *ptr)
619
735
  # define RUBY_TYPED_FROZEN_SHAREABLE 0
620
736
  #endif
621
737
 
622
- #ifdef NEW_TYPEDDATA_WRAPPER
623
738
  static const rb_data_type_t JSON_Generator_State_type = {
624
739
  "JSON/Generator/State",
625
- {NULL, State_free, State_memsize,},
626
- #ifdef RUBY_TYPED_FREE_IMMEDIATELY
740
+ {
741
+ .dmark = State_mark,
742
+ .dfree = State_free,
743
+ .dsize = State_memsize,
744
+ .dcompact = State_compact,
745
+ },
627
746
  0, 0,
628
- RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_FROZEN_SHAREABLE,
629
- #endif
747
+ RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_FROZEN_SHAREABLE,
630
748
  };
631
- #endif
632
-
633
- static VALUE cState_s_allocate(VALUE klass)
634
- {
635
- JSON_Generator_State *state;
636
- return TypedData_Make_Struct(klass, JSON_Generator_State,
637
- &JSON_Generator_State_type, state);
638
- }
639
749
 
640
- /*
641
- * call-seq: configure(opts)
642
- *
643
- * Configure this State instance with the Hash _opts_, and return
644
- * itself.
645
- */
646
- static VALUE cState_configure(VALUE self, VALUE opts)
750
+ static void state_init(JSON_Generator_State *state)
647
751
  {
648
- VALUE tmp;
649
- GET_STATE(self);
650
- tmp = rb_check_convert_type(opts, T_HASH, "Hash", "to_hash");
651
- if (NIL_P(tmp)) tmp = rb_convert_type(opts, T_HASH, "Hash", "to_h");
652
- opts = tmp;
653
- tmp = rb_hash_aref(opts, ID2SYM(i_indent));
654
- if (RTEST(tmp)) {
655
- unsigned long len;
656
- Check_Type(tmp, T_STRING);
657
- len = RSTRING_LEN(tmp);
658
- state->indent = fstrndup(RSTRING_PTR(tmp), len + 1);
659
- state->indent_len = len;
660
- }
661
- tmp = rb_hash_aref(opts, ID2SYM(i_space));
662
- if (RTEST(tmp)) {
663
- unsigned long len;
664
- Check_Type(tmp, T_STRING);
665
- len = RSTRING_LEN(tmp);
666
- state->space = fstrndup(RSTRING_PTR(tmp), len + 1);
667
- state->space_len = len;
668
- }
669
- tmp = rb_hash_aref(opts, ID2SYM(i_space_before));
670
- if (RTEST(tmp)) {
671
- unsigned long len;
672
- Check_Type(tmp, T_STRING);
673
- len = RSTRING_LEN(tmp);
674
- state->space_before = fstrndup(RSTRING_PTR(tmp), len + 1);
675
- state->space_before_len = len;
676
- }
677
- tmp = rb_hash_aref(opts, ID2SYM(i_array_nl));
678
- if (RTEST(tmp)) {
679
- unsigned long len;
680
- Check_Type(tmp, T_STRING);
681
- len = RSTRING_LEN(tmp);
682
- state->array_nl = fstrndup(RSTRING_PTR(tmp), len + 1);
683
- state->array_nl_len = len;
684
- }
685
- tmp = rb_hash_aref(opts, ID2SYM(i_object_nl));
686
- if (RTEST(tmp)) {
687
- unsigned long len;
688
- Check_Type(tmp, T_STRING);
689
- len = RSTRING_LEN(tmp);
690
- state->object_nl = fstrndup(RSTRING_PTR(tmp), len + 1);
691
- state->object_nl_len = len;
692
- }
693
- tmp = ID2SYM(i_max_nesting);
694
752
  state->max_nesting = 100;
695
- if (option_given_p(opts, tmp)) {
696
- VALUE max_nesting = rb_hash_aref(opts, tmp);
697
- if (RTEST(max_nesting)) {
698
- Check_Type(max_nesting, T_FIXNUM);
699
- state->max_nesting = FIX2LONG(max_nesting);
700
- } else {
701
- state->max_nesting = 0;
702
- }
703
- }
704
- tmp = ID2SYM(i_depth);
705
- state->depth = 0;
706
- if (option_given_p(opts, tmp)) {
707
- VALUE depth = rb_hash_aref(opts, tmp);
708
- if (RTEST(depth)) {
709
- Check_Type(depth, T_FIXNUM);
710
- state->depth = FIX2LONG(depth);
711
- } else {
712
- state->depth = 0;
713
- }
714
- }
715
- tmp = ID2SYM(i_buffer_initial_length);
716
- if (option_given_p(opts, tmp)) {
717
- VALUE buffer_initial_length = rb_hash_aref(opts, tmp);
718
- if (RTEST(buffer_initial_length)) {
719
- long initial_length;
720
- Check_Type(buffer_initial_length, T_FIXNUM);
721
- initial_length = FIX2LONG(buffer_initial_length);
722
- if (initial_length > 0) state->buffer_initial_length = initial_length;
723
- }
724
- }
725
- tmp = rb_hash_aref(opts, ID2SYM(i_allow_nan));
726
- state->allow_nan = RTEST(tmp);
727
- tmp = rb_hash_aref(opts, ID2SYM(i_ascii_only));
728
- state->ascii_only = RTEST(tmp);
729
- tmp = rb_hash_aref(opts, ID2SYM(i_escape_slash));
730
- state->escape_slash = RTEST(tmp);
731
- return self;
732
- }
733
-
734
- static void set_state_ivars(VALUE hash, VALUE state)
735
- {
736
- VALUE ivars = rb_obj_instance_variables(state);
737
- int i = 0;
738
- for (i = 0; i < RARRAY_LEN(ivars); i++) {
739
- VALUE key = rb_funcall(rb_ary_entry(ivars, i), i_to_s, 0);
740
- long key_len = RSTRING_LEN(key);
741
- VALUE value = rb_iv_get(state, StringValueCStr(key));
742
- rb_hash_aset(hash, rb_str_intern(rb_str_substr(key, 1, key_len - 1)), value);
743
- }
753
+ state->buffer_initial_length = FBUFFER_INITIAL_LENGTH_DEFAULT;
744
754
  }
745
755
 
746
- /*
747
- * call-seq: to_h
748
- *
749
- * Returns the configuration instance variables as a hash, that can be
750
- * passed to the configure method.
751
- */
752
- static VALUE cState_to_h(VALUE self)
756
+ static VALUE cState_s_allocate(VALUE klass)
753
757
  {
754
- VALUE result = rb_hash_new();
755
- GET_STATE(self);
756
- set_state_ivars(result, self);
757
- rb_hash_aset(result, ID2SYM(i_indent), rb_str_new(state->indent, state->indent_len));
758
- rb_hash_aset(result, ID2SYM(i_space), rb_str_new(state->space, state->space_len));
759
- rb_hash_aset(result, ID2SYM(i_space_before), rb_str_new(state->space_before, state->space_before_len));
760
- rb_hash_aset(result, ID2SYM(i_object_nl), rb_str_new(state->object_nl, state->object_nl_len));
761
- rb_hash_aset(result, ID2SYM(i_array_nl), rb_str_new(state->array_nl, state->array_nl_len));
762
- rb_hash_aset(result, ID2SYM(i_allow_nan), state->allow_nan ? Qtrue : Qfalse);
763
- rb_hash_aset(result, ID2SYM(i_ascii_only), state->ascii_only ? Qtrue : Qfalse);
764
- rb_hash_aset(result, ID2SYM(i_max_nesting), LONG2FIX(state->max_nesting));
765
- rb_hash_aset(result, ID2SYM(i_escape_slash), state->escape_slash ? Qtrue : Qfalse);
766
- rb_hash_aset(result, ID2SYM(i_depth), LONG2FIX(state->depth));
767
- rb_hash_aset(result, ID2SYM(i_buffer_initial_length), LONG2FIX(state->buffer_initial_length));
768
- return result;
758
+ JSON_Generator_State *state;
759
+ VALUE obj = TypedData_Make_Struct(klass, JSON_Generator_State, &JSON_Generator_State_type, state);
760
+ state_init(state);
761
+ return obj;
769
762
  }
770
763
 
771
- /*
772
- * call-seq: [](name)
773
- *
774
- * Returns the value returned by method +name+.
775
- */
776
- static VALUE cState_aref(VALUE self, VALUE name)
764
+ static void vstate_spill(struct generate_json_data *data)
777
765
  {
778
- name = rb_funcall(name, i_to_s, 0);
779
- if (RTEST(rb_funcall(self, i_respond_to_p, 1, name))) {
780
- return rb_funcall(self, i_send, 1, name);
781
- } else {
782
- return rb_attr_get(self, rb_intern_str(rb_str_concat(rb_str_new2("@"), name)));
783
- }
766
+ VALUE vstate = cState_s_allocate(cState);
767
+ GET_STATE(vstate);
768
+ MEMCPY(state, data->state, JSON_Generator_State, 1);
769
+ data->state = state;
770
+ data->vstate = vstate;
771
+ RB_OBJ_WRITTEN(vstate, Qundef, state->indent);
772
+ RB_OBJ_WRITTEN(vstate, Qundef, state->space);
773
+ RB_OBJ_WRITTEN(vstate, Qundef, state->space_before);
774
+ RB_OBJ_WRITTEN(vstate, Qundef, state->object_nl);
775
+ RB_OBJ_WRITTEN(vstate, Qundef, state->array_nl);
776
+ RB_OBJ_WRITTEN(vstate, Qundef, state->as_json);
784
777
  }
785
778
 
786
- /*
787
- * call-seq: []=(name, value)
788
- *
789
- * Sets the attribute name to value.
790
- */
791
- static VALUE cState_aset(VALUE self, VALUE name, VALUE value)
779
+ static inline VALUE vstate_get(struct generate_json_data *data)
792
780
  {
793
- VALUE name_writer;
794
-
795
- name = rb_funcall(name, i_to_s, 0);
796
- name_writer = rb_str_cat2(rb_str_dup(name), "=");
797
- if (RTEST(rb_funcall(self, i_respond_to_p, 1, name_writer))) {
798
- return rb_funcall(self, i_send, 2, name_writer, value);
799
- } else {
800
- rb_ivar_set(self, rb_intern_str(rb_str_concat(rb_str_new2("@"), name)), value);
781
+ if (RB_UNLIKELY(!data->vstate)) {
782
+ vstate_spill(data);
801
783
  }
802
- return Qnil;
784
+ return data->vstate;
803
785
  }
804
786
 
805
787
  struct hash_foreach_arg {
806
- FBuffer *buffer;
807
- JSON_Generator_State *state;
808
- VALUE Vstate;
788
+ struct generate_json_data *data;
809
789
  int iter;
810
790
  };
811
791
 
@@ -813,314 +793,439 @@ static int
813
793
  json_object_i(VALUE key, VALUE val, VALUE _arg)
814
794
  {
815
795
  struct hash_foreach_arg *arg = (struct hash_foreach_arg *)_arg;
816
- FBuffer *buffer = arg->buffer;
817
- JSON_Generator_State *state = arg->state;
818
- VALUE Vstate = arg->Vstate;
819
-
820
- char *object_nl = state->object_nl;
821
- long object_nl_len = state->object_nl_len;
822
- char *indent = state->indent;
823
- long indent_len = state->indent_len;
824
- char *delim = FBUFFER_PTR(state->object_delim);
825
- long delim_len = FBUFFER_LEN(state->object_delim);
826
- char *delim2 = FBUFFER_PTR(state->object_delim2);
827
- long delim2_len = FBUFFER_LEN(state->object_delim2);
796
+ struct generate_json_data *data = arg->data;
797
+
798
+ FBuffer *buffer = data->buffer;
799
+ JSON_Generator_State *state = data->state;
800
+
828
801
  long depth = state->depth;
829
802
  int j;
830
- VALUE klass, key_to_s;
831
803
 
832
- if (arg->iter > 0) fbuffer_append(buffer, delim, delim_len);
833
- if (object_nl) {
834
- fbuffer_append(buffer, object_nl, object_nl_len);
804
+ if (arg->iter > 0) fbuffer_append_char(buffer, ',');
805
+ if (RB_UNLIKELY(data->state->object_nl)) {
806
+ fbuffer_append_str(buffer, data->state->object_nl);
835
807
  }
836
- if (indent) {
808
+ if (RB_UNLIKELY(data->state->indent)) {
837
809
  for (j = 0; j < depth; j++) {
838
- fbuffer_append(buffer, indent, indent_len);
810
+ fbuffer_append_str(buffer, data->state->indent);
839
811
  }
840
812
  }
841
813
 
842
- klass = CLASS_OF(key);
843
- if (klass == rb_cString) {
844
- key_to_s = key;
845
- } else if (klass == rb_cSymbol) {
846
- key_to_s = rb_id2str(SYM2ID(key));
814
+ VALUE key_to_s;
815
+ switch(rb_type(key)) {
816
+ case T_STRING:
817
+ if (RB_LIKELY(RBASIC_CLASS(key) == rb_cString)) {
818
+ key_to_s = key;
819
+ } else {
820
+ key_to_s = rb_funcall(key, i_to_s, 0);
821
+ }
822
+ break;
823
+ case T_SYMBOL:
824
+ key_to_s = rb_sym2str(key);
825
+ break;
826
+ default:
827
+ key_to_s = rb_convert_type(key, T_STRING, "String", "to_s");
828
+ break;
829
+ }
830
+
831
+ if (RB_LIKELY(RBASIC_CLASS(key_to_s) == rb_cString)) {
832
+ generate_json_string(buffer, data, key_to_s);
847
833
  } else {
848
- key_to_s = rb_funcall(key, i_to_s, 0);
834
+ generate_json(buffer, data, key_to_s);
849
835
  }
850
- Check_Type(key_to_s, T_STRING);
851
- generate_json(buffer, Vstate, state, key_to_s);
852
- fbuffer_append(buffer, delim2, delim2_len);
853
- generate_json(buffer, Vstate, state, val);
836
+ if (RB_UNLIKELY(state->space_before)) fbuffer_append_str(buffer, data->state->space_before);
837
+ fbuffer_append_char(buffer, ':');
838
+ if (RB_UNLIKELY(state->space)) fbuffer_append_str(buffer, data->state->space);
839
+ generate_json(buffer, data, val);
854
840
 
855
841
  arg->iter++;
856
842
  return ST_CONTINUE;
857
843
  }
858
844
 
859
- static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
845
+ static inline long increase_depth(struct generate_json_data *data)
860
846
  {
861
- char *object_nl = state->object_nl;
862
- long object_nl_len = state->object_nl_len;
863
- char *indent = state->indent;
864
- long indent_len = state->indent_len;
865
- long max_nesting = state->max_nesting;
847
+ JSON_Generator_State *state = data->state;
866
848
  long depth = ++state->depth;
849
+ if (RB_UNLIKELY(depth > state->max_nesting && state->max_nesting)) {
850
+ rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
851
+ }
852
+ return depth;
853
+ }
854
+
855
+ static void generate_json_object(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
856
+ {
867
857
  int j;
868
- struct hash_foreach_arg arg;
858
+ long depth = increase_depth(data);
869
859
 
870
- if (max_nesting != 0 && depth > max_nesting) {
871
- fbuffer_free(buffer);
872
- rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
860
+ if (RHASH_SIZE(obj) == 0) {
861
+ fbuffer_append(buffer, "{}", 2);
862
+ --data->state->depth;
863
+ return;
873
864
  }
865
+
874
866
  fbuffer_append_char(buffer, '{');
875
867
 
876
- arg.buffer = buffer;
877
- arg.state = state;
878
- arg.Vstate = Vstate;
879
- arg.iter = 0;
868
+ struct hash_foreach_arg arg = {
869
+ .data = data,
870
+ .iter = 0,
871
+ };
880
872
  rb_hash_foreach(obj, json_object_i, (VALUE)&arg);
881
873
 
882
- depth = --state->depth;
883
- if (object_nl) {
884
- fbuffer_append(buffer, object_nl, object_nl_len);
885
- if (indent) {
874
+ depth = --data->state->depth;
875
+ if (RB_UNLIKELY(data->state->object_nl)) {
876
+ fbuffer_append_str(buffer, data->state->object_nl);
877
+ if (RB_UNLIKELY(data->state->indent)) {
886
878
  for (j = 0; j < depth; j++) {
887
- fbuffer_append(buffer, indent, indent_len);
879
+ fbuffer_append_str(buffer, data->state->indent);
888
880
  }
889
881
  }
890
882
  }
891
883
  fbuffer_append_char(buffer, '}');
892
884
  }
893
885
 
894
- static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
886
+ static void generate_json_array(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
895
887
  {
896
- char *array_nl = state->array_nl;
897
- long array_nl_len = state->array_nl_len;
898
- char *indent = state->indent;
899
- long indent_len = state->indent_len;
900
- long max_nesting = state->max_nesting;
901
- char *delim = FBUFFER_PTR(state->array_delim);
902
- long delim_len = FBUFFER_LEN(state->array_delim);
903
- long depth = ++state->depth;
904
888
  int i, j;
905
- if (max_nesting != 0 && depth > max_nesting) {
906
- fbuffer_free(buffer);
907
- rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
889
+ long depth = increase_depth(data);
890
+
891
+ if (RARRAY_LEN(obj) == 0) {
892
+ fbuffer_append(buffer, "[]", 2);
893
+ --data->state->depth;
894
+ return;
908
895
  }
896
+
909
897
  fbuffer_append_char(buffer, '[');
910
- if (array_nl) fbuffer_append(buffer, array_nl, array_nl_len);
898
+ if (RB_UNLIKELY(data->state->array_nl)) fbuffer_append_str(buffer, data->state->array_nl);
911
899
  for(i = 0; i < RARRAY_LEN(obj); i++) {
912
- if (i > 0) fbuffer_append(buffer, delim, delim_len);
913
- if (indent) {
900
+ if (i > 0) {
901
+ fbuffer_append_char(buffer, ',');
902
+ if (RB_UNLIKELY(data->state->array_nl)) fbuffer_append_str(buffer, data->state->array_nl);
903
+ }
904
+ if (RB_UNLIKELY(data->state->indent)) {
914
905
  for (j = 0; j < depth; j++) {
915
- fbuffer_append(buffer, indent, indent_len);
906
+ fbuffer_append_str(buffer, data->state->indent);
916
907
  }
917
908
  }
918
- generate_json(buffer, Vstate, state, rb_ary_entry(obj, i));
909
+ generate_json(buffer, data, RARRAY_AREF(obj, i));
919
910
  }
920
- state->depth = --depth;
921
- if (array_nl) {
922
- fbuffer_append(buffer, array_nl, array_nl_len);
923
- if (indent) {
911
+ data->state->depth = --depth;
912
+ if (RB_UNLIKELY(data->state->array_nl)) {
913
+ fbuffer_append_str(buffer, data->state->array_nl);
914
+ if (RB_UNLIKELY(data->state->indent)) {
924
915
  for (j = 0; j < depth; j++) {
925
- fbuffer_append(buffer, indent, indent_len);
916
+ fbuffer_append_str(buffer, data->state->indent);
926
917
  }
927
918
  }
928
919
  }
929
920
  fbuffer_append_char(buffer, ']');
930
921
  }
931
922
 
932
- #ifdef HAVE_RUBY_ENCODING_H
933
- static int enc_utf8_compatible_p(rb_encoding *enc)
923
+ static inline int enc_utf8_compatible_p(int enc_idx)
934
924
  {
935
- if (enc == rb_usascii_encoding()) return 1;
936
- if (enc == rb_utf8_encoding()) return 1;
925
+ if (enc_idx == usascii_encindex) return 1;
926
+ if (enc_idx == utf8_encindex) return 1;
937
927
  return 0;
938
928
  }
939
- #endif
940
929
 
941
- static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
930
+ static VALUE encode_json_string_try(VALUE str)
931
+ {
932
+ return rb_funcall(str, i_encode, 1, Encoding_UTF_8);
933
+ }
934
+
935
+ static VALUE encode_json_string_rescue(VALUE str, VALUE exception)
936
+ {
937
+ raise_generator_error_str(str, rb_funcall(exception, rb_intern("message"), 0));
938
+ return Qundef;
939
+ }
940
+
941
+ static inline VALUE ensure_valid_encoding(VALUE str)
942
942
  {
943
+ int encindex = RB_ENCODING_GET(str);
944
+ VALUE utf8_string;
945
+ if (RB_UNLIKELY(!enc_utf8_compatible_p(encindex))) {
946
+ if (encindex == binary_encindex) {
947
+ utf8_string = rb_enc_associate_index(rb_str_dup(str), utf8_encindex);
948
+ switch (rb_enc_str_coderange(utf8_string)) {
949
+ case ENC_CODERANGE_7BIT:
950
+ return utf8_string;
951
+ case ENC_CODERANGE_VALID:
952
+ // For historical reason, we silently reinterpret binary strings as UTF-8 if it would work.
953
+ // TODO: Raise in 3.0.0
954
+ rb_warn("JSON.generate: UTF-8 string passed as BINARY, this will raise an encoding error in json 3.0");
955
+ return utf8_string;
956
+ break;
957
+ }
958
+ }
959
+
960
+ str = rb_rescue(encode_json_string_try, str, encode_json_string_rescue, str);
961
+ }
962
+ return str;
963
+ }
964
+
965
+ static void generate_json_string(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
966
+ {
967
+ obj = ensure_valid_encoding(obj);
968
+
943
969
  fbuffer_append_char(buffer, '"');
944
- #ifdef HAVE_RUBY_ENCODING_H
945
- if (!enc_utf8_compatible_p(rb_enc_get(obj))) {
946
- obj = rb_str_export_to_enc(obj, rb_utf8_encoding());
970
+
971
+ long len;
972
+ search_state search;
973
+ search.buffer = buffer;
974
+ RSTRING_GETMEM(obj, search.ptr, len);
975
+ search.cursor = search.ptr;
976
+ search.end = search.ptr + len;
977
+
978
+ switch(rb_enc_str_coderange(obj)) {
979
+ case ENC_CODERANGE_7BIT:
980
+ case ENC_CODERANGE_VALID:
981
+ if (RB_UNLIKELY(data->state->ascii_only)) {
982
+ convert_UTF8_to_ASCII_only_JSON(&search, data->state->script_safe ? script_safe_escape_table : ascii_only_escape_table);
983
+ } else if (RB_UNLIKELY(data->state->script_safe)) {
984
+ convert_UTF8_to_script_safe_JSON(&search);
985
+ } else {
986
+ convert_UTF8_to_JSON(&search);
987
+ }
988
+ break;
989
+ default:
990
+ raise_generator_error(obj, "source sequence is illegal/malformed utf-8");
991
+ break;
947
992
  }
948
- #endif
949
- if (state->ascii_only) {
950
- convert_UTF8_to_JSON_ASCII(buffer, obj, state->escape_slash);
993
+ fbuffer_append_char(buffer, '"');
994
+ }
995
+
996
+ static void generate_json_fallback(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
997
+ {
998
+ VALUE tmp;
999
+ if (rb_respond_to(obj, i_to_json)) {
1000
+ tmp = rb_funcall(obj, i_to_json, 1, vstate_get(data));
1001
+ Check_Type(tmp, T_STRING);
1002
+ fbuffer_append_str(buffer, tmp);
951
1003
  } else {
952
- convert_UTF8_to_JSON(buffer, obj, state->escape_slash);
1004
+ tmp = rb_funcall(obj, i_to_s, 0);
1005
+ Check_Type(tmp, T_STRING);
1006
+ generate_json_string(buffer, data, tmp);
953
1007
  }
954
- fbuffer_append_char(buffer, '"');
955
1008
  }
956
1009
 
957
- static void generate_json_null(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
1010
+ static inline void generate_json_symbol(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1011
+ {
1012
+ if (data->state->strict) {
1013
+ generate_json_string(buffer, data, rb_sym2str(obj));
1014
+ } else {
1015
+ generate_json_fallback(buffer, data, obj);
1016
+ }
1017
+ }
1018
+
1019
+ static void generate_json_null(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
958
1020
  {
959
1021
  fbuffer_append(buffer, "null", 4);
960
1022
  }
961
1023
 
962
- static void generate_json_false(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
1024
+ static void generate_json_false(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
963
1025
  {
964
1026
  fbuffer_append(buffer, "false", 5);
965
1027
  }
966
1028
 
967
- static void generate_json_true(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
1029
+ static void generate_json_true(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
968
1030
  {
969
1031
  fbuffer_append(buffer, "true", 4);
970
1032
  }
971
1033
 
972
- static void generate_json_fixnum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
1034
+ static void generate_json_fixnum(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
973
1035
  {
974
1036
  fbuffer_append_long(buffer, FIX2LONG(obj));
975
1037
  }
976
1038
 
977
- static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
1039
+ static void generate_json_bignum(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
978
1040
  {
979
1041
  VALUE tmp = rb_funcall(obj, i_to_s, 0);
980
1042
  fbuffer_append_str(buffer, tmp);
981
1043
  }
982
1044
 
983
1045
  #ifdef RUBY_INTEGER_UNIFICATION
984
- static void generate_json_integer(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
1046
+ static void generate_json_integer(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
985
1047
  {
986
1048
  if (FIXNUM_P(obj))
987
- generate_json_fixnum(buffer, Vstate, state, obj);
1049
+ generate_json_fixnum(buffer, data, obj);
988
1050
  else
989
- generate_json_bignum(buffer, Vstate, state, obj);
1051
+ generate_json_bignum(buffer, data, obj);
990
1052
  }
991
1053
  #endif
992
- static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
1054
+
1055
+ static void generate_json_float(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
993
1056
  {
994
1057
  double value = RFLOAT_VALUE(obj);
995
- char allow_nan = state->allow_nan;
996
- VALUE tmp = rb_funcall(obj, i_to_s, 0);
997
- if (!allow_nan) {
998
- if (isinf(value)) {
999
- fbuffer_free(buffer);
1000
- rb_raise(eGeneratorError, "%"PRIsVALUE" not allowed in JSON", RB_OBJ_STRING(tmp));
1001
- } else if (isnan(value)) {
1002
- fbuffer_free(buffer);
1003
- rb_raise(eGeneratorError, "%"PRIsVALUE" not allowed in JSON", RB_OBJ_STRING(tmp));
1058
+ char allow_nan = data->state->allow_nan;
1059
+ if (isinf(value) || isnan(value)) {
1060
+ /* for NaN and Infinity values we either raise an error or rely on Float#to_s. */
1061
+ if (!allow_nan) {
1062
+ if (data->state->strict && data->state->as_json) {
1063
+ VALUE casted_obj = rb_proc_call_with_block(data->state->as_json, 1, &obj, Qnil);
1064
+ if (casted_obj != obj) {
1065
+ increase_depth(data);
1066
+ generate_json(buffer, data, casted_obj);
1067
+ data->state->depth--;
1068
+ return;
1069
+ }
1070
+ }
1071
+ raise_generator_error(obj, "%"PRIsVALUE" not allowed in JSON", rb_funcall(obj, i_to_s, 0));
1004
1072
  }
1073
+
1074
+ VALUE tmp = rb_funcall(obj, i_to_s, 0);
1075
+ fbuffer_append_str(buffer, tmp);
1076
+ return;
1005
1077
  }
1006
- fbuffer_append_str(buffer, tmp);
1078
+
1079
+ /* This implementation writes directly into the buffer. We reserve
1080
+ * the 24 characters that fpconv_dtoa states as its maximum, plus
1081
+ * 2 more characters for the potential ".0" suffix.
1082
+ */
1083
+ fbuffer_inc_capa(buffer, 26);
1084
+ char* d = buffer->ptr + buffer->len;
1085
+ int len = fpconv_dtoa(value, d);
1086
+
1087
+ /* fpconv_dtoa converts a float to its shortest string representation,
1088
+ * but it adds a ".0" if this is a plain integer.
1089
+ */
1090
+ buffer->len += len;
1007
1091
  }
1008
1092
 
1009
- static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
1093
+ static void generate_json_fragment(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1010
1094
  {
1011
- VALUE tmp;
1012
- VALUE klass = CLASS_OF(obj);
1013
- if (klass == rb_cHash) {
1014
- generate_json_object(buffer, Vstate, state, obj);
1015
- } else if (klass == rb_cArray) {
1016
- generate_json_array(buffer, Vstate, state, obj);
1017
- } else if (klass == rb_cString) {
1018
- generate_json_string(buffer, Vstate, state, obj);
1019
- } else if (obj == Qnil) {
1020
- generate_json_null(buffer, Vstate, state, obj);
1095
+ VALUE fragment = RSTRUCT_GET(obj, 0);
1096
+ Check_Type(fragment, T_STRING);
1097
+ fbuffer_append_str(buffer, fragment);
1098
+ }
1099
+
1100
+ static void generate_json(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1101
+ {
1102
+ bool as_json_called = false;
1103
+ start:
1104
+ if (obj == Qnil) {
1105
+ generate_json_null(buffer, data, obj);
1021
1106
  } else if (obj == Qfalse) {
1022
- generate_json_false(buffer, Vstate, state, obj);
1107
+ generate_json_false(buffer, data, obj);
1023
1108
  } else if (obj == Qtrue) {
1024
- generate_json_true(buffer, Vstate, state, obj);
1025
- } else if (FIXNUM_P(obj)) {
1026
- generate_json_fixnum(buffer, Vstate, state, obj);
1027
- } else if (RB_TYPE_P(obj, T_BIGNUM)) {
1028
- generate_json_bignum(buffer, Vstate, state, obj);
1029
- } else if (klass == rb_cFloat) {
1030
- generate_json_float(buffer, Vstate, state, obj);
1031
- } else if (rb_respond_to(obj, i_to_json)) {
1032
- tmp = rb_funcall(obj, i_to_json, 1, Vstate);
1033
- Check_Type(tmp, T_STRING);
1034
- fbuffer_append_str(buffer, tmp);
1109
+ generate_json_true(buffer, data, obj);
1110
+ } else if (RB_SPECIAL_CONST_P(obj)) {
1111
+ if (RB_FIXNUM_P(obj)) {
1112
+ generate_json_fixnum(buffer, data, obj);
1113
+ } else if (RB_FLONUM_P(obj)) {
1114
+ generate_json_float(buffer, data, obj);
1115
+ } else if (RB_STATIC_SYM_P(obj)) {
1116
+ generate_json_symbol(buffer, data, obj);
1117
+ } else {
1118
+ goto general;
1119
+ }
1035
1120
  } else {
1036
- tmp = rb_funcall(obj, i_to_s, 0);
1037
- Check_Type(tmp, T_STRING);
1038
- generate_json_string(buffer, Vstate, state, tmp);
1121
+ VALUE klass = RBASIC_CLASS(obj);
1122
+ switch (RB_BUILTIN_TYPE(obj)) {
1123
+ case T_BIGNUM:
1124
+ generate_json_bignum(buffer, data, obj);
1125
+ break;
1126
+ case T_HASH:
1127
+ if (klass != rb_cHash) goto general;
1128
+ generate_json_object(buffer, data, obj);
1129
+ break;
1130
+ case T_ARRAY:
1131
+ if (klass != rb_cArray) goto general;
1132
+ generate_json_array(buffer, data, obj);
1133
+ break;
1134
+ case T_STRING:
1135
+ if (klass != rb_cString) goto general;
1136
+ generate_json_string(buffer, data, obj);
1137
+ break;
1138
+ case T_SYMBOL:
1139
+ generate_json_symbol(buffer, data, obj);
1140
+ break;
1141
+ case T_FLOAT:
1142
+ if (klass != rb_cFloat) goto general;
1143
+ generate_json_float(buffer, data, obj);
1144
+ break;
1145
+ case T_STRUCT:
1146
+ if (klass != cFragment) goto general;
1147
+ generate_json_fragment(buffer, data, obj);
1148
+ break;
1149
+ default:
1150
+ general:
1151
+ if (data->state->strict) {
1152
+ if (RTEST(data->state->as_json) && !as_json_called) {
1153
+ obj = rb_proc_call_with_block(data->state->as_json, 1, &obj, Qnil);
1154
+ as_json_called = true;
1155
+ goto start;
1156
+ } else {
1157
+ raise_generator_error(obj, "%"PRIsVALUE" not allowed in JSON", CLASS_OF(obj));
1158
+ }
1159
+ } else {
1160
+ generate_json_fallback(buffer, data, obj);
1161
+ }
1162
+ }
1039
1163
  }
1040
1164
  }
1041
1165
 
1042
- static FBuffer *cState_prepare_buffer(VALUE self)
1166
+ static VALUE generate_json_try(VALUE d)
1043
1167
  {
1044
- FBuffer *buffer;
1045
- GET_STATE(self);
1046
- buffer = fbuffer_alloc(state->buffer_initial_length);
1168
+ struct generate_json_data *data = (struct generate_json_data *)d;
1047
1169
 
1048
- if (state->object_delim) {
1049
- fbuffer_clear(state->object_delim);
1050
- } else {
1051
- state->object_delim = fbuffer_alloc(16);
1052
- }
1053
- fbuffer_append_char(state->object_delim, ',');
1054
- if (state->object_delim2) {
1055
- fbuffer_clear(state->object_delim2);
1056
- } else {
1057
- state->object_delim2 = fbuffer_alloc(16);
1058
- }
1059
- if (state->space_before) fbuffer_append(state->object_delim2, state->space_before, state->space_before_len);
1060
- fbuffer_append_char(state->object_delim2, ':');
1061
- if (state->space) fbuffer_append(state->object_delim2, state->space, state->space_len);
1170
+ data->func(data->buffer, data, data->obj);
1062
1171
 
1063
- if (state->array_delim) {
1064
- fbuffer_clear(state->array_delim);
1065
- } else {
1066
- state->array_delim = fbuffer_alloc(16);
1067
- }
1068
- fbuffer_append_char(state->array_delim, ',');
1069
- if (state->array_nl) fbuffer_append(state->array_delim, state->array_nl, state->array_nl_len);
1070
- return buffer;
1172
+ return Qnil;
1071
1173
  }
1072
1174
 
1073
- static VALUE cState_partial_generate(VALUE self, VALUE obj)
1175
+ static VALUE generate_json_rescue(VALUE d, VALUE exc)
1176
+ {
1177
+ struct generate_json_data *data = (struct generate_json_data *)d;
1178
+ fbuffer_free(data->buffer);
1179
+
1180
+ rb_exc_raise(exc);
1181
+
1182
+ return Qundef;
1183
+ }
1184
+
1185
+ static VALUE cState_partial_generate(VALUE self, VALUE obj, generator_func func, VALUE io)
1074
1186
  {
1075
- FBuffer *buffer = cState_prepare_buffer(self);
1076
1187
  GET_STATE(self);
1077
- generate_json(buffer, self, state, obj);
1078
- return fbuffer_to_s(buffer);
1188
+
1189
+ char stack_buffer[FBUFFER_STACK_SIZE];
1190
+ FBuffer buffer = {
1191
+ .io = RTEST(io) ? io : Qfalse,
1192
+ };
1193
+ fbuffer_stack_init(&buffer, state->buffer_initial_length, stack_buffer, FBUFFER_STACK_SIZE);
1194
+
1195
+ struct generate_json_data data = {
1196
+ .buffer = &buffer,
1197
+ .vstate = self,
1198
+ .state = state,
1199
+ .obj = obj,
1200
+ .func = func
1201
+ };
1202
+ rb_rescue(generate_json_try, (VALUE)&data, generate_json_rescue, (VALUE)&data);
1203
+
1204
+ return fbuffer_finalize(&buffer);
1079
1205
  }
1080
1206
 
1081
- /*
1082
- * call-seq: generate(obj)
1207
+ /* call-seq:
1208
+ * generate(obj) -> String
1209
+ * generate(obj, anIO) -> anIO
1083
1210
  *
1084
1211
  * Generates a valid JSON document from object +obj+ and returns the
1085
1212
  * result. If no valid JSON document can be created this method raises a
1086
1213
  * GeneratorError exception.
1087
1214
  */
1088
- static VALUE cState_generate(VALUE self, VALUE obj)
1215
+ static VALUE cState_generate(int argc, VALUE *argv, VALUE self)
1089
1216
  {
1090
- VALUE result = cState_partial_generate(self, obj);
1217
+ rb_check_arity(argc, 1, 2);
1218
+ VALUE obj = argv[0];
1219
+ VALUE io = argc > 1 ? argv[1] : Qnil;
1220
+ VALUE result = cState_partial_generate(self, obj, generate_json, io);
1091
1221
  GET_STATE(self);
1092
1222
  (void)state;
1093
1223
  return result;
1094
1224
  }
1095
1225
 
1096
- /*
1097
- * call-seq: new(opts = {})
1098
- *
1099
- * Instantiates a new State object, configured by _opts_.
1100
- *
1101
- * _opts_ can have the following keys:
1102
- *
1103
- * * *indent*: a string used to indent levels (default: ''),
1104
- * * *space*: a string that is put after, a : or , delimiter (default: ''),
1105
- * * *space_before*: a string that is put before a : pair delimiter (default: ''),
1106
- * * *object_nl*: a string that is put at the end of a JSON object (default: ''),
1107
- * * *array_nl*: a string that is put at the end of a JSON array (default: ''),
1108
- * * *allow_nan*: true if NaN, Infinity, and -Infinity should be
1109
- * generated, otherwise an exception is thrown, if these values are
1110
- * encountered. This options defaults to false.
1111
- * * *ascii_only*: true if only ASCII characters should be generated. This
1112
- * option defaults to false.
1113
- * * *buffer_initial_length*: sets the initial length of the generator's
1114
- * internal buffer.
1115
- */
1116
1226
  static VALUE cState_initialize(int argc, VALUE *argv, VALUE self)
1117
1227
  {
1118
- VALUE opts;
1119
- GET_STATE(self);
1120
- state->max_nesting = 100;
1121
- state->buffer_initial_length = FBUFFER_INITIAL_LENGTH_DEFAULT;
1122
- rb_scan_args(argc, argv, "01", &opts);
1123
- if (!NIL_P(opts)) cState_configure(self, opts);
1228
+ rb_warn("The json gem extension was loaded with the stdlib ruby code. You should upgrade rubygems with `gem update --system`");
1124
1229
  return self;
1125
1230
  }
1126
1231
 
@@ -1140,14 +1245,12 @@ static VALUE cState_init_copy(VALUE obj, VALUE orig)
1140
1245
  if (!objState) rb_raise(rb_eArgError, "unallocated JSON::State");
1141
1246
 
1142
1247
  MEMCPY(objState, origState, JSON_Generator_State, 1);
1143
- objState->indent = fstrndup(origState->indent, origState->indent_len);
1144
- objState->space = fstrndup(origState->space, origState->space_len);
1145
- objState->space_before = fstrndup(origState->space_before, origState->space_before_len);
1146
- objState->object_nl = fstrndup(origState->object_nl, origState->object_nl_len);
1147
- objState->array_nl = fstrndup(origState->array_nl, origState->array_nl_len);
1148
- if (origState->array_delim) objState->array_delim = fbuffer_dup(origState->array_delim);
1149
- if (origState->object_delim) objState->object_delim = fbuffer_dup(origState->object_delim);
1150
- if (origState->object_delim2) objState->object_delim2 = fbuffer_dup(origState->object_delim2);
1248
+ objState->indent = origState->indent;
1249
+ objState->space = origState->space;
1250
+ objState->space_before = origState->space_before;
1251
+ objState->object_nl = origState->object_nl;
1252
+ objState->array_nl = origState->array_nl;
1253
+ objState->as_json = origState->as_json;
1151
1254
  return obj;
1152
1255
  }
1153
1256
 
@@ -1177,7 +1280,18 @@ static VALUE cState_from_state_s(VALUE self, VALUE opts)
1177
1280
  static VALUE cState_indent(VALUE self)
1178
1281
  {
1179
1282
  GET_STATE(self);
1180
- return state->indent ? rb_str_new(state->indent, state->indent_len) : rb_str_new2("");
1283
+ return state->indent ? state->indent : rb_str_freeze(rb_utf8_str_new("", 0));
1284
+ }
1285
+
1286
+ static VALUE string_config(VALUE config)
1287
+ {
1288
+ if (RTEST(config)) {
1289
+ Check_Type(config, T_STRING);
1290
+ if (RSTRING_LEN(config)) {
1291
+ return rb_str_new_frozen(config);
1292
+ }
1293
+ }
1294
+ return Qfalse;
1181
1295
  }
1182
1296
 
1183
1297
  /*
@@ -1187,21 +1301,8 @@ static VALUE cState_indent(VALUE self)
1187
1301
  */
1188
1302
  static VALUE cState_indent_set(VALUE self, VALUE indent)
1189
1303
  {
1190
- unsigned long len;
1191
1304
  GET_STATE(self);
1192
- Check_Type(indent, T_STRING);
1193
- len = RSTRING_LEN(indent);
1194
- if (len == 0) {
1195
- if (state->indent) {
1196
- ruby_xfree(state->indent);
1197
- state->indent = NULL;
1198
- state->indent_len = 0;
1199
- }
1200
- } else {
1201
- if (state->indent) ruby_xfree(state->indent);
1202
- state->indent = fstrndup(RSTRING_PTR(indent), len);
1203
- state->indent_len = len;
1204
- }
1305
+ RB_OBJ_WRITE(self, &state->indent, string_config(indent));
1205
1306
  return Qnil;
1206
1307
  }
1207
1308
 
@@ -1214,7 +1315,7 @@ static VALUE cState_indent_set(VALUE self, VALUE indent)
1214
1315
  static VALUE cState_space(VALUE self)
1215
1316
  {
1216
1317
  GET_STATE(self);
1217
- return state->space ? rb_str_new(state->space, state->space_len) : rb_str_new2("");
1318
+ return state->space ? state->space : rb_str_freeze(rb_utf8_str_new("", 0));
1218
1319
  }
1219
1320
 
1220
1321
  /*
@@ -1225,21 +1326,8 @@ static VALUE cState_space(VALUE self)
1225
1326
  */
1226
1327
  static VALUE cState_space_set(VALUE self, VALUE space)
1227
1328
  {
1228
- unsigned long len;
1229
1329
  GET_STATE(self);
1230
- Check_Type(space, T_STRING);
1231
- len = RSTRING_LEN(space);
1232
- if (len == 0) {
1233
- if (state->space) {
1234
- ruby_xfree(state->space);
1235
- state->space = NULL;
1236
- state->space_len = 0;
1237
- }
1238
- } else {
1239
- if (state->space) ruby_xfree(state->space);
1240
- state->space = fstrndup(RSTRING_PTR(space), len);
1241
- state->space_len = len;
1242
- }
1330
+ RB_OBJ_WRITE(self, &state->space, string_config(space));
1243
1331
  return Qnil;
1244
1332
  }
1245
1333
 
@@ -1251,7 +1339,7 @@ static VALUE cState_space_set(VALUE self, VALUE space)
1251
1339
  static VALUE cState_space_before(VALUE self)
1252
1340
  {
1253
1341
  GET_STATE(self);
1254
- return state->space_before ? rb_str_new(state->space_before, state->space_before_len) : rb_str_new2("");
1342
+ return state->space_before ? state->space_before : rb_str_freeze(rb_utf8_str_new("", 0));
1255
1343
  }
1256
1344
 
1257
1345
  /*
@@ -1261,21 +1349,8 @@ static VALUE cState_space_before(VALUE self)
1261
1349
  */
1262
1350
  static VALUE cState_space_before_set(VALUE self, VALUE space_before)
1263
1351
  {
1264
- unsigned long len;
1265
1352
  GET_STATE(self);
1266
- Check_Type(space_before, T_STRING);
1267
- len = RSTRING_LEN(space_before);
1268
- if (len == 0) {
1269
- if (state->space_before) {
1270
- ruby_xfree(state->space_before);
1271
- state->space_before = NULL;
1272
- state->space_before_len = 0;
1273
- }
1274
- } else {
1275
- if (state->space_before) ruby_xfree(state->space_before);
1276
- state->space_before = fstrndup(RSTRING_PTR(space_before), len);
1277
- state->space_before_len = len;
1278
- }
1353
+ RB_OBJ_WRITE(self, &state->space_before, string_config(space_before));
1279
1354
  return Qnil;
1280
1355
  }
1281
1356
 
@@ -1288,7 +1363,7 @@ static VALUE cState_space_before_set(VALUE self, VALUE space_before)
1288
1363
  static VALUE cState_object_nl(VALUE self)
1289
1364
  {
1290
1365
  GET_STATE(self);
1291
- return state->object_nl ? rb_str_new(state->object_nl, state->object_nl_len) : rb_str_new2("");
1366
+ return state->object_nl ? state->object_nl : rb_str_freeze(rb_utf8_str_new("", 0));
1292
1367
  }
1293
1368
 
1294
1369
  /*
@@ -1299,20 +1374,8 @@ static VALUE cState_object_nl(VALUE self)
1299
1374
  */
1300
1375
  static VALUE cState_object_nl_set(VALUE self, VALUE object_nl)
1301
1376
  {
1302
- unsigned long len;
1303
1377
  GET_STATE(self);
1304
- Check_Type(object_nl, T_STRING);
1305
- len = RSTRING_LEN(object_nl);
1306
- if (len == 0) {
1307
- if (state->object_nl) {
1308
- ruby_xfree(state->object_nl);
1309
- state->object_nl = NULL;
1310
- }
1311
- } else {
1312
- if (state->object_nl) ruby_xfree(state->object_nl);
1313
- state->object_nl = fstrndup(RSTRING_PTR(object_nl), len);
1314
- state->object_nl_len = len;
1315
- }
1378
+ RB_OBJ_WRITE(self, &state->object_nl, string_config(object_nl));
1316
1379
  return Qnil;
1317
1380
  }
1318
1381
 
@@ -1324,7 +1387,7 @@ static VALUE cState_object_nl_set(VALUE self, VALUE object_nl)
1324
1387
  static VALUE cState_array_nl(VALUE self)
1325
1388
  {
1326
1389
  GET_STATE(self);
1327
- return state->array_nl ? rb_str_new(state->array_nl, state->array_nl_len) : rb_str_new2("");
1390
+ return state->array_nl ? state->array_nl : rb_str_freeze(rb_utf8_str_new("", 0));
1328
1391
  }
1329
1392
 
1330
1393
  /*
@@ -1334,23 +1397,33 @@ static VALUE cState_array_nl(VALUE self)
1334
1397
  */
1335
1398
  static VALUE cState_array_nl_set(VALUE self, VALUE array_nl)
1336
1399
  {
1337
- unsigned long len;
1338
1400
  GET_STATE(self);
1339
- Check_Type(array_nl, T_STRING);
1340
- len = RSTRING_LEN(array_nl);
1341
- if (len == 0) {
1342
- if (state->array_nl) {
1343
- ruby_xfree(state->array_nl);
1344
- state->array_nl = NULL;
1345
- }
1346
- } else {
1347
- if (state->array_nl) ruby_xfree(state->array_nl);
1348
- state->array_nl = fstrndup(RSTRING_PTR(array_nl), len);
1349
- state->array_nl_len = len;
1350
- }
1401
+ RB_OBJ_WRITE(self, &state->array_nl, string_config(array_nl));
1351
1402
  return Qnil;
1352
1403
  }
1353
1404
 
1405
+ /*
1406
+ * call-seq: as_json()
1407
+ *
1408
+ * This string is put at the end of a line that holds a JSON array.
1409
+ */
1410
+ static VALUE cState_as_json(VALUE self)
1411
+ {
1412
+ GET_STATE(self);
1413
+ return state->as_json;
1414
+ }
1415
+
1416
+ /*
1417
+ * call-seq: as_json=(as_json)
1418
+ *
1419
+ * This string is put at the end of a line that holds a JSON array.
1420
+ */
1421
+ static VALUE cState_as_json_set(VALUE self, VALUE as_json)
1422
+ {
1423
+ GET_STATE(self);
1424
+ RB_OBJ_WRITE(self, &state->as_json, rb_convert_type(as_json, T_DATA, "Proc", "to_proc"));
1425
+ return Qnil;
1426
+ }
1354
1427
 
1355
1428
  /*
1356
1429
  * call-seq: check_circular?
@@ -1376,6 +1449,11 @@ static VALUE cState_max_nesting(VALUE self)
1376
1449
  return LONG2FIX(state->max_nesting);
1377
1450
  }
1378
1451
 
1452
+ static long long_config(VALUE num)
1453
+ {
1454
+ return RTEST(num) ? FIX2LONG(num) : 0;
1455
+ }
1456
+
1379
1457
  /*
1380
1458
  * call-seq: max_nesting=(depth)
1381
1459
  *
@@ -1385,32 +1463,63 @@ static VALUE cState_max_nesting(VALUE self)
1385
1463
  static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
1386
1464
  {
1387
1465
  GET_STATE(self);
1388
- Check_Type(depth, T_FIXNUM);
1389
- return state->max_nesting = FIX2LONG(depth);
1466
+ state->max_nesting = long_config(depth);
1467
+ return Qnil;
1390
1468
  }
1391
1469
 
1392
1470
  /*
1393
- * call-seq: escape_slash
1471
+ * call-seq: script_safe
1394
1472
  *
1395
1473
  * If this boolean is true, the forward slashes will be escaped in
1396
1474
  * the json output.
1397
1475
  */
1398
- static VALUE cState_escape_slash(VALUE self)
1476
+ static VALUE cState_script_safe(VALUE self)
1399
1477
  {
1400
1478
  GET_STATE(self);
1401
- return state->escape_slash ? Qtrue : Qfalse;
1479
+ return state->script_safe ? Qtrue : Qfalse;
1402
1480
  }
1403
1481
 
1404
1482
  /*
1405
- * call-seq: escape_slash=(depth)
1483
+ * call-seq: script_safe=(enable)
1406
1484
  *
1407
1485
  * This sets whether or not the forward slashes will be escaped in
1408
1486
  * the json output.
1409
1487
  */
1410
- static VALUE cState_escape_slash_set(VALUE self, VALUE enable)
1488
+ static VALUE cState_script_safe_set(VALUE self, VALUE enable)
1489
+ {
1490
+ GET_STATE(self);
1491
+ state->script_safe = RTEST(enable);
1492
+ return Qnil;
1493
+ }
1494
+
1495
+ /*
1496
+ * call-seq: strict
1497
+ *
1498
+ * If this boolean is false, types unsupported by the JSON format will
1499
+ * be serialized as strings.
1500
+ * If this boolean is true, types unsupported by the JSON format will
1501
+ * raise a JSON::GeneratorError.
1502
+ */
1503
+ static VALUE cState_strict(VALUE self)
1411
1504
  {
1412
1505
  GET_STATE(self);
1413
- state->escape_slash = RTEST(enable);
1506
+ return state->strict ? Qtrue : Qfalse;
1507
+ }
1508
+
1509
+ /*
1510
+ * call-seq: strict=(enable)
1511
+ *
1512
+ * This sets whether or not to serialize types unsupported by the
1513
+ * JSON format as strings.
1514
+ * If this boolean is false, types unsupported by the JSON format will
1515
+ * be serialized as strings.
1516
+ * If this boolean is true, types unsupported by the JSON format will
1517
+ * raise a JSON::GeneratorError.
1518
+ */
1519
+ static VALUE cState_strict_set(VALUE self, VALUE enable)
1520
+ {
1521
+ GET_STATE(self);
1522
+ state->strict = RTEST(enable);
1414
1523
  return Qnil;
1415
1524
  }
1416
1525
 
@@ -1426,6 +1535,18 @@ static VALUE cState_allow_nan_p(VALUE self)
1426
1535
  return state->allow_nan ? Qtrue : Qfalse;
1427
1536
  }
1428
1537
 
1538
+ /*
1539
+ * call-seq: allow_nan=(enable)
1540
+ *
1541
+ * This sets whether or not to serialize NaN, Infinity, and -Infinity
1542
+ */
1543
+ static VALUE cState_allow_nan_set(VALUE self, VALUE enable)
1544
+ {
1545
+ GET_STATE(self);
1546
+ state->allow_nan = RTEST(enable);
1547
+ return Qnil;
1548
+ }
1549
+
1429
1550
  /*
1430
1551
  * call-seq: ascii_only?
1431
1552
  *
@@ -1438,6 +1559,18 @@ static VALUE cState_ascii_only_p(VALUE self)
1438
1559
  return state->ascii_only ? Qtrue : Qfalse;
1439
1560
  }
1440
1561
 
1562
+ /*
1563
+ * call-seq: ascii_only=(enable)
1564
+ *
1565
+ * This sets whether only ASCII characters should be generated.
1566
+ */
1567
+ static VALUE cState_ascii_only_set(VALUE self, VALUE enable)
1568
+ {
1569
+ GET_STATE(self);
1570
+ state->ascii_only = RTEST(enable);
1571
+ return Qnil;
1572
+ }
1573
+
1441
1574
  /*
1442
1575
  * call-seq: depth
1443
1576
  *
@@ -1458,8 +1591,7 @@ static VALUE cState_depth(VALUE self)
1458
1591
  static VALUE cState_depth_set(VALUE self, VALUE depth)
1459
1592
  {
1460
1593
  GET_STATE(self);
1461
- Check_Type(depth, T_FIXNUM);
1462
- state->depth = FIX2LONG(depth);
1594
+ state->depth = long_config(depth);
1463
1595
  return Qnil;
1464
1596
  }
1465
1597
 
@@ -1474,6 +1606,15 @@ static VALUE cState_buffer_initial_length(VALUE self)
1474
1606
  return LONG2FIX(state->buffer_initial_length);
1475
1607
  }
1476
1608
 
1609
+ static void buffer_initial_length_set(JSON_Generator_State *state, VALUE buffer_initial_length)
1610
+ {
1611
+ Check_Type(buffer_initial_length, T_FIXNUM);
1612
+ long initial_length = FIX2LONG(buffer_initial_length);
1613
+ if (initial_length > 0) {
1614
+ state->buffer_initial_length = initial_length;
1615
+ }
1616
+ }
1617
+
1477
1618
  /*
1478
1619
  * call-seq: buffer_initial_length=(length)
1479
1620
  *
@@ -1482,16 +1623,76 @@ static VALUE cState_buffer_initial_length(VALUE self)
1482
1623
  */
1483
1624
  static VALUE cState_buffer_initial_length_set(VALUE self, VALUE buffer_initial_length)
1484
1625
  {
1485
- long initial_length;
1486
1626
  GET_STATE(self);
1487
- Check_Type(buffer_initial_length, T_FIXNUM);
1488
- initial_length = FIX2LONG(buffer_initial_length);
1489
- if (initial_length > 0) {
1490
- state->buffer_initial_length = initial_length;
1491
- }
1627
+ buffer_initial_length_set(state, buffer_initial_length);
1492
1628
  return Qnil;
1493
1629
  }
1494
1630
 
1631
+ static int configure_state_i(VALUE key, VALUE val, VALUE _arg)
1632
+ {
1633
+ JSON_Generator_State *state = (JSON_Generator_State *)_arg;
1634
+
1635
+ if (key == sym_indent) { state->indent = string_config(val); }
1636
+ else if (key == sym_space) { state->space = string_config(val); }
1637
+ else if (key == sym_space_before) { state->space_before = string_config(val); }
1638
+ else if (key == sym_object_nl) { state->object_nl = string_config(val); }
1639
+ else if (key == sym_array_nl) { state->array_nl = string_config(val); }
1640
+ else if (key == sym_max_nesting) { state->max_nesting = long_config(val); }
1641
+ else if (key == sym_allow_nan) { state->allow_nan = RTEST(val); }
1642
+ else if (key == sym_ascii_only) { state->ascii_only = RTEST(val); }
1643
+ else if (key == sym_depth) { state->depth = long_config(val); }
1644
+ else if (key == sym_buffer_initial_length) { buffer_initial_length_set(state, val); }
1645
+ else if (key == sym_script_safe) { state->script_safe = RTEST(val); }
1646
+ else if (key == sym_escape_slash) { state->script_safe = RTEST(val); }
1647
+ else if (key == sym_strict) { state->strict = RTEST(val); }
1648
+ else if (key == sym_as_json) { state->as_json = RTEST(val) ? rb_convert_type(val, T_DATA, "Proc", "to_proc") : Qfalse; }
1649
+ return ST_CONTINUE;
1650
+ }
1651
+
1652
+ static void configure_state(JSON_Generator_State *state, VALUE config)
1653
+ {
1654
+ if (!RTEST(config)) return;
1655
+
1656
+ Check_Type(config, T_HASH);
1657
+
1658
+ if (!RHASH_SIZE(config)) return;
1659
+
1660
+ // We assume in most cases few keys are set so it's faster to go over
1661
+ // the provided keys than to check all possible keys.
1662
+ rb_hash_foreach(config, configure_state_i, (VALUE)state);
1663
+ }
1664
+
1665
+ static VALUE cState_configure(VALUE self, VALUE opts)
1666
+ {
1667
+ GET_STATE(self);
1668
+ configure_state(state, opts);
1669
+ return self;
1670
+ }
1671
+
1672
+ static VALUE cState_m_generate(VALUE klass, VALUE obj, VALUE opts, VALUE io)
1673
+ {
1674
+ JSON_Generator_State state = {0};
1675
+ state_init(&state);
1676
+ configure_state(&state, opts);
1677
+
1678
+ char stack_buffer[FBUFFER_STACK_SIZE];
1679
+ FBuffer buffer = {
1680
+ .io = RTEST(io) ? io : Qfalse,
1681
+ };
1682
+ fbuffer_stack_init(&buffer, state.buffer_initial_length, stack_buffer, FBUFFER_STACK_SIZE);
1683
+
1684
+ struct generate_json_data data = {
1685
+ .buffer = &buffer,
1686
+ .vstate = Qfalse,
1687
+ .state = &state,
1688
+ .obj = obj,
1689
+ .func = generate_json,
1690
+ };
1691
+ rb_rescue(generate_json_try, (VALUE)&data, generate_json_rescue, (VALUE)&data);
1692
+
1693
+ return fbuffer_finalize(&buffer);
1694
+ }
1695
+
1495
1696
  /*
1496
1697
  *
1497
1698
  */
@@ -1505,18 +1706,26 @@ void Init_generator(void)
1505
1706
  rb_require("json/common");
1506
1707
 
1507
1708
  mJSON = rb_define_module("JSON");
1508
- mExt = rb_define_module_under(mJSON, "Ext");
1509
- mGenerator = rb_define_module_under(mExt, "Generator");
1510
1709
 
1710
+ rb_global_variable(&cFragment);
1711
+ cFragment = rb_const_get(mJSON, rb_intern("Fragment"));
1712
+
1713
+ VALUE mExt = rb_define_module_under(mJSON, "Ext");
1714
+ VALUE mGenerator = rb_define_module_under(mExt, "Generator");
1715
+
1716
+ rb_global_variable(&eGeneratorError);
1511
1717
  eGeneratorError = rb_path2class("JSON::GeneratorError");
1718
+
1719
+ rb_global_variable(&eNestingError);
1512
1720
  eNestingError = rb_path2class("JSON::NestingError");
1513
- rb_gc_register_mark_object(eGeneratorError);
1514
- rb_gc_register_mark_object(eNestingError);
1515
1721
 
1516
1722
  cState = rb_define_class_under(mGenerator, "State", rb_cObject);
1517
1723
  rb_define_alloc_func(cState, cState_s_allocate);
1518
1724
  rb_define_singleton_method(cState, "from_state", cState_from_state_s, 1);
1519
1725
  rb_define_method(cState, "initialize", cState_initialize, -1);
1726
+ rb_define_alias(cState, "initialize", "initialize"); // avoid method redefinition warnings
1727
+ rb_define_private_method(cState, "_configure", cState_configure, 1);
1728
+
1520
1729
  rb_define_method(cState, "initialize_copy", cState_init_copy, 1);
1521
1730
  rb_define_method(cState, "indent", cState_indent, 0);
1522
1731
  rb_define_method(cState, "indent=", cState_indent_set, 1);
@@ -1528,81 +1737,105 @@ void Init_generator(void)
1528
1737
  rb_define_method(cState, "object_nl=", cState_object_nl_set, 1);
1529
1738
  rb_define_method(cState, "array_nl", cState_array_nl, 0);
1530
1739
  rb_define_method(cState, "array_nl=", cState_array_nl_set, 1);
1740
+ rb_define_method(cState, "as_json", cState_as_json, 0);
1741
+ rb_define_method(cState, "as_json=", cState_as_json_set, 1);
1531
1742
  rb_define_method(cState, "max_nesting", cState_max_nesting, 0);
1532
1743
  rb_define_method(cState, "max_nesting=", cState_max_nesting_set, 1);
1533
- rb_define_method(cState, "escape_slash", cState_escape_slash, 0);
1534
- rb_define_method(cState, "escape_slash?", cState_escape_slash, 0);
1535
- rb_define_method(cState, "escape_slash=", cState_escape_slash_set, 1);
1744
+ rb_define_method(cState, "script_safe", cState_script_safe, 0);
1745
+ rb_define_method(cState, "script_safe?", cState_script_safe, 0);
1746
+ rb_define_method(cState, "script_safe=", cState_script_safe_set, 1);
1747
+ rb_define_alias(cState, "escape_slash", "script_safe");
1748
+ rb_define_alias(cState, "escape_slash?", "script_safe?");
1749
+ rb_define_alias(cState, "escape_slash=", "script_safe=");
1750
+ rb_define_method(cState, "strict", cState_strict, 0);
1751
+ rb_define_method(cState, "strict?", cState_strict, 0);
1752
+ rb_define_method(cState, "strict=", cState_strict_set, 1);
1536
1753
  rb_define_method(cState, "check_circular?", cState_check_circular_p, 0);
1537
1754
  rb_define_method(cState, "allow_nan?", cState_allow_nan_p, 0);
1755
+ rb_define_method(cState, "allow_nan=", cState_allow_nan_set, 1);
1538
1756
  rb_define_method(cState, "ascii_only?", cState_ascii_only_p, 0);
1757
+ rb_define_method(cState, "ascii_only=", cState_ascii_only_set, 1);
1539
1758
  rb_define_method(cState, "depth", cState_depth, 0);
1540
1759
  rb_define_method(cState, "depth=", cState_depth_set, 1);
1541
1760
  rb_define_method(cState, "buffer_initial_length", cState_buffer_initial_length, 0);
1542
1761
  rb_define_method(cState, "buffer_initial_length=", cState_buffer_initial_length_set, 1);
1543
- rb_define_method(cState, "configure", cState_configure, 1);
1544
- rb_define_alias(cState, "merge", "configure");
1545
- rb_define_method(cState, "to_h", cState_to_h, 0);
1546
- rb_define_alias(cState, "to_hash", "to_h");
1547
- rb_define_method(cState, "[]", cState_aref, 1);
1548
- rb_define_method(cState, "[]=", cState_aset, 2);
1549
- rb_define_method(cState, "generate", cState_generate, 1);
1550
-
1551
- mGeneratorMethods = rb_define_module_under(mGenerator, "GeneratorMethods");
1552
- mObject = rb_define_module_under(mGeneratorMethods, "Object");
1762
+ rb_define_method(cState, "generate", cState_generate, -1);
1763
+ rb_define_alias(cState, "generate_new", "generate"); // :nodoc:
1764
+
1765
+ rb_define_singleton_method(cState, "generate", cState_m_generate, 3);
1766
+
1767
+ VALUE mGeneratorMethods = rb_define_module_under(mGenerator, "GeneratorMethods");
1768
+
1769
+ VALUE mObject = rb_define_module_under(mGeneratorMethods, "Object");
1553
1770
  rb_define_method(mObject, "to_json", mObject_to_json, -1);
1554
- mHash = rb_define_module_under(mGeneratorMethods, "Hash");
1771
+
1772
+ VALUE mHash = rb_define_module_under(mGeneratorMethods, "Hash");
1555
1773
  rb_define_method(mHash, "to_json", mHash_to_json, -1);
1556
- mArray = rb_define_module_under(mGeneratorMethods, "Array");
1774
+
1775
+ VALUE mArray = rb_define_module_under(mGeneratorMethods, "Array");
1557
1776
  rb_define_method(mArray, "to_json", mArray_to_json, -1);
1777
+
1558
1778
  #ifdef RUBY_INTEGER_UNIFICATION
1559
- mInteger = rb_define_module_under(mGeneratorMethods, "Integer");
1779
+ VALUE mInteger = rb_define_module_under(mGeneratorMethods, "Integer");
1560
1780
  rb_define_method(mInteger, "to_json", mInteger_to_json, -1);
1561
1781
  #else
1562
- mFixnum = rb_define_module_under(mGeneratorMethods, "Fixnum");
1782
+ VALUE mFixnum = rb_define_module_under(mGeneratorMethods, "Fixnum");
1563
1783
  rb_define_method(mFixnum, "to_json", mFixnum_to_json, -1);
1564
- mBignum = rb_define_module_under(mGeneratorMethods, "Bignum");
1784
+
1785
+ VALUE mBignum = rb_define_module_under(mGeneratorMethods, "Bignum");
1565
1786
  rb_define_method(mBignum, "to_json", mBignum_to_json, -1);
1566
1787
  #endif
1567
- mFloat = rb_define_module_under(mGeneratorMethods, "Float");
1788
+ VALUE mFloat = rb_define_module_under(mGeneratorMethods, "Float");
1568
1789
  rb_define_method(mFloat, "to_json", mFloat_to_json, -1);
1569
- mString = rb_define_module_under(mGeneratorMethods, "String");
1790
+
1791
+ VALUE mString = rb_define_module_under(mGeneratorMethods, "String");
1570
1792
  rb_define_singleton_method(mString, "included", mString_included_s, 1);
1571
1793
  rb_define_method(mString, "to_json", mString_to_json, -1);
1572
1794
  rb_define_method(mString, "to_json_raw", mString_to_json_raw, -1);
1573
1795
  rb_define_method(mString, "to_json_raw_object", mString_to_json_raw_object, 0);
1796
+
1574
1797
  mString_Extend = rb_define_module_under(mString, "Extend");
1575
1798
  rb_define_method(mString_Extend, "json_create", mString_Extend_json_create, 1);
1576
- mTrueClass = rb_define_module_under(mGeneratorMethods, "TrueClass");
1799
+
1800
+ VALUE mTrueClass = rb_define_module_under(mGeneratorMethods, "TrueClass");
1577
1801
  rb_define_method(mTrueClass, "to_json", mTrueClass_to_json, -1);
1578
- mFalseClass = rb_define_module_under(mGeneratorMethods, "FalseClass");
1802
+
1803
+ VALUE mFalseClass = rb_define_module_under(mGeneratorMethods, "FalseClass");
1579
1804
  rb_define_method(mFalseClass, "to_json", mFalseClass_to_json, -1);
1580
- mNilClass = rb_define_module_under(mGeneratorMethods, "NilClass");
1805
+
1806
+ VALUE mNilClass = rb_define_module_under(mGeneratorMethods, "NilClass");
1581
1807
  rb_define_method(mNilClass, "to_json", mNilClass_to_json, -1);
1582
1808
 
1809
+ rb_global_variable(&Encoding_UTF_8);
1810
+ Encoding_UTF_8 = rb_const_get(rb_path2class("Encoding"), rb_intern("UTF_8"));
1811
+
1583
1812
  i_to_s = rb_intern("to_s");
1584
1813
  i_to_json = rb_intern("to_json");
1585
1814
  i_new = rb_intern("new");
1586
- i_indent = rb_intern("indent");
1587
- i_space = rb_intern("space");
1588
- i_space_before = rb_intern("space_before");
1589
- i_object_nl = rb_intern("object_nl");
1590
- i_array_nl = rb_intern("array_nl");
1591
- i_max_nesting = rb_intern("max_nesting");
1592
- i_escape_slash = rb_intern("escape_slash");
1593
- i_allow_nan = rb_intern("allow_nan");
1594
- i_ascii_only = rb_intern("ascii_only");
1595
- i_depth = rb_intern("depth");
1596
- i_buffer_initial_length = rb_intern("buffer_initial_length");
1597
1815
  i_pack = rb_intern("pack");
1598
1816
  i_unpack = rb_intern("unpack");
1599
1817
  i_create_id = rb_intern("create_id");
1600
1818
  i_extend = rb_intern("extend");
1601
- i_key_p = rb_intern("key?");
1602
- i_aref = rb_intern("[]");
1603
- i_send = rb_intern("__send__");
1604
- i_respond_to_p = rb_intern("respond_to?");
1605
- i_match = rb_intern("match");
1606
- i_keys = rb_intern("keys");
1607
- i_dup = rb_intern("dup");
1819
+ i_encode = rb_intern("encode");
1820
+
1821
+ sym_indent = ID2SYM(rb_intern("indent"));
1822
+ sym_space = ID2SYM(rb_intern("space"));
1823
+ sym_space_before = ID2SYM(rb_intern("space_before"));
1824
+ sym_object_nl = ID2SYM(rb_intern("object_nl"));
1825
+ sym_array_nl = ID2SYM(rb_intern("array_nl"));
1826
+ sym_max_nesting = ID2SYM(rb_intern("max_nesting"));
1827
+ sym_allow_nan = ID2SYM(rb_intern("allow_nan"));
1828
+ sym_ascii_only = ID2SYM(rb_intern("ascii_only"));
1829
+ sym_depth = ID2SYM(rb_intern("depth"));
1830
+ sym_buffer_initial_length = ID2SYM(rb_intern("buffer_initial_length"));
1831
+ sym_script_safe = ID2SYM(rb_intern("script_safe"));
1832
+ sym_escape_slash = ID2SYM(rb_intern("escape_slash"));
1833
+ sym_strict = ID2SYM(rb_intern("strict"));
1834
+ sym_as_json = ID2SYM(rb_intern("as_json"));
1835
+
1836
+ usascii_encindex = rb_usascii_encindex();
1837
+ utf8_encindex = rb_utf8_encindex();
1838
+ binary_encindex = rb_ascii8bit_encindex();
1839
+
1840
+ rb_require("json/ext/generator/state");
1608
1841
  }