json 2.7.2 → 2.10.1

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