json_pure 2.2.0 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +51 -0
  3. data/LICENSE +56 -0
  4. data/README.md +17 -1
  5. data/VERSION +1 -1
  6. data/json_pure.gemspec +49 -26
  7. data/lib/json.rb +549 -29
  8. data/lib/json/add/bigdecimal.rb +2 -2
  9. data/lib/json/add/complex.rb +2 -3
  10. data/lib/json/add/rational.rb +2 -3
  11. data/lib/json/add/regexp.rb +2 -2
  12. data/lib/json/common.rb +370 -125
  13. data/lib/json/pure/generator.rb +31 -10
  14. data/lib/json/pure/parser.rb +31 -5
  15. data/lib/json/version.rb +1 -1
  16. data/tests/fixtures/fail29.json +1 -0
  17. data/tests/fixtures/fail30.json +1 -0
  18. data/tests/fixtures/fail31.json +1 -0
  19. data/tests/fixtures/fail32.json +1 -0
  20. data/tests/json_addition_test.rb +0 -4
  21. data/tests/json_common_interface_test.rb +47 -4
  22. data/tests/json_fixtures_test.rb +9 -1
  23. data/tests/json_generator_test.rb +30 -8
  24. data/tests/json_parser_test.rb +39 -14
  25. data/tests/lib/core_assertions.rb +763 -0
  26. data/tests/lib/envutil.rb +365 -0
  27. data/tests/lib/find_executable.rb +22 -0
  28. data/tests/lib/helper.rb +4 -0
  29. data/tests/ractor_test.rb +30 -0
  30. data/tests/test_helper.rb +3 -3
  31. metadata +28 -48
  32. data/.gitignore +0 -17
  33. data/.travis.yml +0 -23
  34. data/README-json-jruby.md +0 -33
  35. data/Rakefile +0 -408
  36. data/diagrams/.keep +0 -0
  37. data/ext/json/ext/fbuffer/fbuffer.h +0 -187
  38. data/ext/json/ext/generator/depend +0 -1
  39. data/ext/json/ext/generator/extconf.rb +0 -4
  40. data/ext/json/ext/generator/generator.c +0 -1444
  41. data/ext/json/ext/generator/generator.h +0 -171
  42. data/ext/json/ext/parser/depend +0 -1
  43. data/ext/json/ext/parser/extconf.rb +0 -6
  44. data/ext/json/ext/parser/parser.c +0 -2131
  45. data/ext/json/ext/parser/parser.h +0 -91
  46. data/ext/json/ext/parser/parser.rl +0 -891
  47. data/ext/json/extconf.rb +0 -2
  48. data/install.rb +0 -23
  49. data/java/src/json/ext/ByteListTranscoder.java +0 -166
  50. data/java/src/json/ext/Generator.java +0 -443
  51. data/java/src/json/ext/GeneratorMethods.java +0 -231
  52. data/java/src/json/ext/GeneratorService.java +0 -42
  53. data/java/src/json/ext/GeneratorState.java +0 -490
  54. data/java/src/json/ext/OptionsReader.java +0 -113
  55. data/java/src/json/ext/Parser.java +0 -2362
  56. data/java/src/json/ext/Parser.rl +0 -893
  57. data/java/src/json/ext/ParserService.java +0 -34
  58. data/java/src/json/ext/RuntimeInfo.java +0 -116
  59. data/java/src/json/ext/StringDecoder.java +0 -166
  60. data/java/src/json/ext/StringEncoder.java +0 -111
  61. data/java/src/json/ext/Utils.java +0 -88
  62. data/json-java.gemspec +0 -38
  63. data/json.gemspec +0 -0
  64. data/references/rfc7159.txt +0 -899
  65. data/tools/diff.sh +0 -18
  66. data/tools/fuzz.rb +0 -131
  67. data/tools/server.rb +0 -62
File without changes
@@ -1,187 +0,0 @@
1
-
2
- #ifndef _FBUFFER_H_
3
- #define _FBUFFER_H_
4
-
5
- #include "ruby.h"
6
-
7
- #ifndef RHASH_SIZE
8
- #define RHASH_SIZE(hsh) (RHASH(hsh)->tbl->num_entries)
9
- #endif
10
-
11
- #ifndef RFLOAT_VALUE
12
- #define RFLOAT_VALUE(val) (RFLOAT(val)->value)
13
- #endif
14
-
15
- #ifndef RARRAY_LEN
16
- #define RARRAY_LEN(ARRAY) RARRAY(ARRAY)->len
17
- #endif
18
- #ifndef RSTRING_PTR
19
- #define RSTRING_PTR(string) RSTRING(string)->ptr
20
- #endif
21
- #ifndef RSTRING_LEN
22
- #define RSTRING_LEN(string) RSTRING(string)->len
23
- #endif
24
-
25
- #ifdef PRIsVALUE
26
- # define RB_OBJ_CLASSNAME(obj) rb_obj_class(obj)
27
- # define RB_OBJ_STRING(obj) (obj)
28
- #else
29
- # define PRIsVALUE "s"
30
- # define RB_OBJ_CLASSNAME(obj) rb_obj_classname(obj)
31
- # define RB_OBJ_STRING(obj) StringValueCStr(obj)
32
- #endif
33
-
34
- #ifdef HAVE_RUBY_ENCODING_H
35
- #include "ruby/encoding.h"
36
- #define FORCE_UTF8(obj) rb_enc_associate((obj), rb_utf8_encoding())
37
- #else
38
- #define FORCE_UTF8(obj)
39
- #endif
40
-
41
- /* We don't need to guard objects for rbx, so let's do nothing at all. */
42
- #ifndef RB_GC_GUARD
43
- #define RB_GC_GUARD(object)
44
- #endif
45
-
46
- typedef struct FBufferStruct {
47
- unsigned long initial_length;
48
- char *ptr;
49
- unsigned long len;
50
- unsigned long capa;
51
- } FBuffer;
52
-
53
- #define FBUFFER_INITIAL_LENGTH_DEFAULT 1024
54
-
55
- #define FBUFFER_PTR(fb) (fb->ptr)
56
- #define FBUFFER_LEN(fb) (fb->len)
57
- #define FBUFFER_CAPA(fb) (fb->capa)
58
- #define FBUFFER_PAIR(fb) FBUFFER_PTR(fb), FBUFFER_LEN(fb)
59
-
60
- static FBuffer *fbuffer_alloc(unsigned long initial_length);
61
- static void fbuffer_free(FBuffer *fb);
62
- static void fbuffer_clear(FBuffer *fb);
63
- static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len);
64
- #ifdef JSON_GENERATOR
65
- static void fbuffer_append_long(FBuffer *fb, long number);
66
- #endif
67
- static void fbuffer_append_char(FBuffer *fb, char newchr);
68
- #ifdef JSON_GENERATOR
69
- static FBuffer *fbuffer_dup(FBuffer *fb);
70
- static VALUE fbuffer_to_s(FBuffer *fb);
71
- #endif
72
-
73
- static FBuffer *fbuffer_alloc(unsigned long initial_length)
74
- {
75
- FBuffer *fb;
76
- if (initial_length <= 0) initial_length = FBUFFER_INITIAL_LENGTH_DEFAULT;
77
- fb = ALLOC(FBuffer);
78
- memset((void *) fb, 0, sizeof(FBuffer));
79
- fb->initial_length = initial_length;
80
- return fb;
81
- }
82
-
83
- static void fbuffer_free(FBuffer *fb)
84
- {
85
- if (fb->ptr) ruby_xfree(fb->ptr);
86
- ruby_xfree(fb);
87
- }
88
-
89
- static void fbuffer_clear(FBuffer *fb)
90
- {
91
- fb->len = 0;
92
- }
93
-
94
- static void fbuffer_inc_capa(FBuffer *fb, unsigned long requested)
95
- {
96
- unsigned long required;
97
-
98
- if (!fb->ptr) {
99
- fb->ptr = ALLOC_N(char, fb->initial_length);
100
- fb->capa = fb->initial_length;
101
- }
102
-
103
- for (required = fb->capa; requested > required - fb->len; required <<= 1);
104
-
105
- if (required > fb->capa) {
106
- REALLOC_N(fb->ptr, char, required);
107
- fb->capa = required;
108
- }
109
- }
110
-
111
- static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
112
- {
113
- if (len > 0) {
114
- fbuffer_inc_capa(fb, len);
115
- MEMCPY(fb->ptr + fb->len, newstr, char, len);
116
- fb->len += len;
117
- }
118
- }
119
-
120
- #ifdef JSON_GENERATOR
121
- static void fbuffer_append_str(FBuffer *fb, VALUE str)
122
- {
123
- const char *newstr = StringValuePtr(str);
124
- unsigned long len = RSTRING_LEN(str);
125
-
126
- RB_GC_GUARD(str);
127
-
128
- fbuffer_append(fb, newstr, len);
129
- }
130
- #endif
131
-
132
- static void fbuffer_append_char(FBuffer *fb, char newchr)
133
- {
134
- fbuffer_inc_capa(fb, 1);
135
- *(fb->ptr + fb->len) = newchr;
136
- fb->len++;
137
- }
138
-
139
- #ifdef JSON_GENERATOR
140
- static void freverse(char *start, char *end)
141
- {
142
- char c;
143
-
144
- while (end > start) {
145
- c = *end, *end-- = *start, *start++ = c;
146
- }
147
- }
148
-
149
- static long fltoa(long number, char *buf)
150
- {
151
- static char digits[] = "0123456789";
152
- long sign = number;
153
- char* tmp = buf;
154
-
155
- if (sign < 0) number = -number;
156
- do *tmp++ = digits[number % 10]; while (number /= 10);
157
- if (sign < 0) *tmp++ = '-';
158
- freverse(buf, tmp - 1);
159
- return tmp - buf;
160
- }
161
-
162
- static void fbuffer_append_long(FBuffer *fb, long number)
163
- {
164
- char buf[20];
165
- unsigned long len = fltoa(number, buf);
166
- fbuffer_append(fb, buf, len);
167
- }
168
-
169
- static FBuffer *fbuffer_dup(FBuffer *fb)
170
- {
171
- unsigned long len = fb->len;
172
- FBuffer *result;
173
-
174
- result = fbuffer_alloc(len);
175
- fbuffer_append(result, FBUFFER_PAIR(fb));
176
- return result;
177
- }
178
-
179
- static VALUE fbuffer_to_s(FBuffer *fb)
180
- {
181
- VALUE result = rb_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
182
- fbuffer_free(fb);
183
- FORCE_UTF8(result);
184
- return result;
185
- }
186
- #endif
187
- #endif
@@ -1 +0,0 @@
1
- generator.o: generator.c generator.h $(srcdir)/../fbuffer/fbuffer.h
@@ -1,4 +0,0 @@
1
- require 'mkmf'
2
-
3
- $defs << "-DJSON_GENERATOR"
4
- create_makefile 'json/ext/generator'
@@ -1,1444 +0,0 @@
1
- #include "../fbuffer/fbuffer.h"
2
- #include "generator.h"
3
-
4
- #ifdef HAVE_RUBY_ENCODING_H
5
- static VALUE CEncoding_UTF_8;
6
- static ID i_encoding, i_encode;
7
- #endif
8
-
9
- static VALUE mJSON, mExt, mGenerator, cState, mGeneratorMethods, mObject,
10
- mHash, mArray,
11
- #ifdef RUBY_INTEGER_UNIFICATION
12
- mInteger,
13
- #else
14
- mFixnum, mBignum,
15
- #endif
16
- mFloat, mString, mString_Extend,
17
- mTrueClass, mFalseClass, mNilClass, eGeneratorError,
18
- eNestingError, CRegexp_MULTILINE, CJSON_SAFE_STATE_PROTOTYPE,
19
- i_SAFE_STATE_PROTOTYPE;
20
-
21
- static ID i_to_s, i_to_json, i_new, i_indent, i_space, i_space_before,
22
- i_object_nl, i_array_nl, i_max_nesting, i_allow_nan, i_ascii_only,
23
- i_pack, i_unpack, i_create_id, i_extend, i_key_p,
24
- i_aref, i_send, i_respond_to_p, i_match, i_keys, i_depth,
25
- i_buffer_initial_length, i_dup;
26
-
27
- /*
28
- * Copyright 2001-2004 Unicode, Inc.
29
- *
30
- * Disclaimer
31
- *
32
- * This source code is provided as is by Unicode, Inc. No claims are
33
- * made as to fitness for any particular purpose. No warranties of any
34
- * kind are expressed or implied. The recipient agrees to determine
35
- * applicability of information provided. If this file has been
36
- * purchased on magnetic or optical media from Unicode, Inc., the
37
- * sole remedy for any claim will be exchange of defective media
38
- * within 90 days of receipt.
39
- *
40
- * Limitations on Rights to Redistribute This Code
41
- *
42
- * Unicode, Inc. hereby grants the right to freely use the information
43
- * supplied in this file in the creation of products supporting the
44
- * Unicode Standard, and to make copies of this file in any form
45
- * for internal or external distribution as long as this notice
46
- * remains attached.
47
- */
48
-
49
- /*
50
- * Index into the table below with the first byte of a UTF-8 sequence to
51
- * get the number of trailing bytes that are supposed to follow it.
52
- * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
53
- * left as-is for anyone who may want to do such conversion, which was
54
- * allowed in earlier algorithms.
55
- */
56
- static const char trailingBytesForUTF8[256] = {
57
- 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,
58
- 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,
59
- 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,
60
- 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,
61
- 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,
62
- 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,
63
- 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,
64
- 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
65
- };
66
-
67
- /*
68
- * Magic values subtracted from a buffer value during UTF8 conversion.
69
- * This table contains as many values as there might be trailing bytes
70
- * in a UTF-8 sequence.
71
- */
72
- static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
73
- 0x03C82080UL, 0xFA082080UL, 0x82082080UL };
74
-
75
- /*
76
- * Utility routine to tell whether a sequence of bytes is legal UTF-8.
77
- * This must be called with the length pre-determined by the first byte.
78
- * If not calling this from ConvertUTF8to*, then the length can be set by:
79
- * length = trailingBytesForUTF8[*source]+1;
80
- * and the sequence is illegal right away if there aren't that many bytes
81
- * available.
82
- * If presented with a length > 4, this returns 0. The Unicode
83
- * definition of UTF-8 goes up to 4-byte sequences.
84
- */
85
- static unsigned char isLegalUTF8(const UTF8 *source, unsigned long length)
86
- {
87
- UTF8 a;
88
- const UTF8 *srcptr = source+length;
89
- switch (length) {
90
- default: return 0;
91
- /* Everything else falls through when "1"... */
92
- case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
93
- case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
94
- case 2: if ((a = (*--srcptr)) > 0xBF) return 0;
95
-
96
- switch (*source) {
97
- /* no fall-through in this inner switch */
98
- case 0xE0: if (a < 0xA0) return 0; break;
99
- case 0xED: if (a > 0x9F) return 0; break;
100
- case 0xF0: if (a < 0x90) return 0; break;
101
- case 0xF4: if (a > 0x8F) return 0; break;
102
- default: if (a < 0x80) return 0;
103
- }
104
-
105
- case 1: if (*source >= 0x80 && *source < 0xC2) return 0;
106
- }
107
- if (*source > 0xF4) return 0;
108
- return 1;
109
- }
110
-
111
- /* Escapes the UTF16 character and stores the result in the buffer buf. */
112
- static void unicode_escape(char *buf, UTF16 character)
113
- {
114
- const char *digits = "0123456789abcdef";
115
-
116
- buf[2] = digits[character >> 12];
117
- buf[3] = digits[(character >> 8) & 0xf];
118
- buf[4] = digits[(character >> 4) & 0xf];
119
- buf[5] = digits[character & 0xf];
120
- }
121
-
122
- /* Escapes the UTF16 character and stores the result in the buffer buf, then
123
- * the buffer buf is appended to the FBuffer buffer. */
124
- static void unicode_escape_to_buffer(FBuffer *buffer, char buf[6], UTF16
125
- character)
126
- {
127
- unicode_escape(buf, character);
128
- fbuffer_append(buffer, buf, 6);
129
- }
130
-
131
- /* Converts string to a JSON string in FBuffer buffer, where all but the ASCII
132
- * and control characters are JSON escaped. */
133
- static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string)
134
- {
135
- const UTF8 *source = (UTF8 *) RSTRING_PTR(string);
136
- const UTF8 *sourceEnd = source + RSTRING_LEN(string);
137
- char buf[6] = { '\\', 'u' };
138
-
139
- while (source < sourceEnd) {
140
- UTF32 ch = 0;
141
- unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
142
- if (source + extraBytesToRead >= sourceEnd) {
143
- rb_raise(rb_path2class("JSON::GeneratorError"),
144
- "partial character in source, but hit end");
145
- }
146
- if (!isLegalUTF8(source, extraBytesToRead+1)) {
147
- rb_raise(rb_path2class("JSON::GeneratorError"),
148
- "source sequence is illegal/malformed utf-8");
149
- }
150
- /*
151
- * The cases all fall through. See "Note A" below.
152
- */
153
- switch (extraBytesToRead) {
154
- case 5: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
155
- case 4: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
156
- case 3: ch += *source++; ch <<= 6;
157
- case 2: ch += *source++; ch <<= 6;
158
- case 1: ch += *source++; ch <<= 6;
159
- case 0: ch += *source++;
160
- }
161
- ch -= offsetsFromUTF8[extraBytesToRead];
162
-
163
- if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
164
- /* UTF-16 surrogate values are illegal in UTF-32 */
165
- if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
166
- #if UNI_STRICT_CONVERSION
167
- source -= (extraBytesToRead+1); /* return to the illegal value itself */
168
- rb_raise(rb_path2class("JSON::GeneratorError"),
169
- "source sequence is illegal/malformed utf-8");
170
- #else
171
- unicode_escape_to_buffer(buffer, buf, UNI_REPLACEMENT_CHAR);
172
- #endif
173
- } else {
174
- /* normal case */
175
- if (ch >= 0x20 && ch <= 0x7f) {
176
- switch (ch) {
177
- case '\\':
178
- fbuffer_append(buffer, "\\\\", 2);
179
- break;
180
- case '"':
181
- fbuffer_append(buffer, "\\\"", 2);
182
- break;
183
- default:
184
- fbuffer_append_char(buffer, (char)ch);
185
- break;
186
- }
187
- } else {
188
- switch (ch) {
189
- case '\n':
190
- fbuffer_append(buffer, "\\n", 2);
191
- break;
192
- case '\r':
193
- fbuffer_append(buffer, "\\r", 2);
194
- break;
195
- case '\t':
196
- fbuffer_append(buffer, "\\t", 2);
197
- break;
198
- case '\f':
199
- fbuffer_append(buffer, "\\f", 2);
200
- break;
201
- case '\b':
202
- fbuffer_append(buffer, "\\b", 2);
203
- break;
204
- default:
205
- unicode_escape_to_buffer(buffer, buf, (UTF16) ch);
206
- break;
207
- }
208
- }
209
- }
210
- } else if (ch > UNI_MAX_UTF16) {
211
- #if UNI_STRICT_CONVERSION
212
- source -= (extraBytesToRead+1); /* return to the start */
213
- rb_raise(rb_path2class("JSON::GeneratorError"),
214
- "source sequence is illegal/malformed utf8");
215
- #else
216
- unicode_escape_to_buffer(buffer, buf, UNI_REPLACEMENT_CHAR);
217
- #endif
218
- } else {
219
- /* target is a character in range 0xFFFF - 0x10FFFF. */
220
- ch -= halfBase;
221
- unicode_escape_to_buffer(buffer, buf, (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START));
222
- unicode_escape_to_buffer(buffer, buf, (UTF16)((ch & halfMask) + UNI_SUR_LOW_START));
223
- }
224
- }
225
- RB_GC_GUARD(string);
226
- }
227
-
228
- /* Converts string to a JSON string in FBuffer buffer, where only the
229
- * characters required by the JSON standard are JSON escaped. The remaining
230
- * characters (should be UTF8) are just passed through and appended to the
231
- * result. */
232
- static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string)
233
- {
234
- const char *ptr = RSTRING_PTR(string), *p;
235
- unsigned long len = RSTRING_LEN(string), start = 0, end = 0;
236
- const char *escape = NULL;
237
- int escape_len;
238
- unsigned char c;
239
- char buf[6] = { '\\', 'u' };
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;
270
- break;
271
- }
272
- } else {
273
- switch (c) {
274
- case '\\':
275
- escape = "\\\\";
276
- escape_len = 2;
277
- break;
278
- case '"':
279
- escape = "\\\"";
280
- escape_len = 2;
281
- break;
282
- default:
283
- {
284
- unsigned short clen = trailingBytesForUTF8[c] + 1;
285
- if (end + clen > len) {
286
- rb_raise(rb_path2class("JSON::GeneratorError"),
287
- "partial character in source, but hit end");
288
- }
289
- if (!isLegalUTF8((UTF8 *) p, clen)) {
290
- rb_raise(rb_path2class("JSON::GeneratorError"),
291
- "source sequence is illegal/malformed utf-8");
292
- }
293
- end += clen;
294
- }
295
- continue;
296
- break;
297
- }
298
- }
299
- fbuffer_append(buffer, ptr + start, end - start);
300
- fbuffer_append(buffer, escape, escape_len);
301
- start = ++end;
302
- escape = NULL;
303
- }
304
- fbuffer_append(buffer, ptr + start, end - start);
305
- }
306
-
307
- static char *fstrndup(const char *ptr, unsigned long len) {
308
- char *result;
309
- if (len <= 0) return NULL;
310
- result = ALLOC_N(char, len);
311
- memcpy(result, ptr, len);
312
- return result;
313
- }
314
-
315
- /*
316
- * Document-module: JSON::Ext::Generator
317
- *
318
- * This is the JSON generator implemented as a C extension. It can be
319
- * configured to be used by setting
320
- *
321
- * JSON.generator = JSON::Ext::Generator
322
- *
323
- * with the method generator= in JSON.
324
- *
325
- */
326
-
327
- /*
328
- * call-seq: to_json(state = nil)
329
- *
330
- * Returns a JSON string containing a JSON object, that is generated from
331
- * this Hash instance.
332
- * _state_ is a JSON::State object, that can also be used to configure the
333
- * produced JSON string output further.
334
- */
335
- static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self)
336
- {
337
- GENERATE_JSON(object);
338
- }
339
-
340
- /*
341
- * call-seq: to_json(state = nil)
342
- *
343
- * Returns a JSON string containing a JSON array, that is generated from
344
- * this Array instance.
345
- * _state_ is a JSON::State object, that can also be used to configure the
346
- * produced JSON string output further.
347
- */
348
- static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self) {
349
- GENERATE_JSON(array);
350
- }
351
-
352
- #ifdef RUBY_INTEGER_UNIFICATION
353
- /*
354
- * call-seq: to_json(*)
355
- *
356
- * Returns a JSON string representation for this Integer number.
357
- */
358
- static VALUE mInteger_to_json(int argc, VALUE *argv, VALUE self)
359
- {
360
- GENERATE_JSON(integer);
361
- }
362
-
363
- #else
364
- /*
365
- * call-seq: to_json(*)
366
- *
367
- * Returns a JSON string representation for this Integer number.
368
- */
369
- static VALUE mFixnum_to_json(int argc, VALUE *argv, VALUE self)
370
- {
371
- GENERATE_JSON(fixnum);
372
- }
373
-
374
- /*
375
- * call-seq: to_json(*)
376
- *
377
- * Returns a JSON string representation for this Integer number.
378
- */
379
- static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self)
380
- {
381
- GENERATE_JSON(bignum);
382
- }
383
- #endif
384
-
385
- /*
386
- * call-seq: to_json(*)
387
- *
388
- * Returns a JSON string representation for this Float number.
389
- */
390
- static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self)
391
- {
392
- GENERATE_JSON(float);
393
- }
394
-
395
- /*
396
- * call-seq: String.included(modul)
397
- *
398
- * Extends _modul_ with the String::Extend module.
399
- */
400
- static VALUE mString_included_s(VALUE self, VALUE modul) {
401
- VALUE result = rb_funcall(modul, i_extend, 1, mString_Extend);
402
- return result;
403
- }
404
-
405
- /*
406
- * call-seq: to_json(*)
407
- *
408
- * This string should be encoded with UTF-8 A call to this method
409
- * returns a JSON string encoded with UTF16 big endian characters as
410
- * \u????.
411
- */
412
- static VALUE mString_to_json(int argc, VALUE *argv, VALUE self)
413
- {
414
- GENERATE_JSON(string);
415
- }
416
-
417
- /*
418
- * call-seq: to_json_raw_object()
419
- *
420
- * This method creates a raw object hash, that can be nested into
421
- * other data structures and will be generated as a raw string. This
422
- * method should be used, if you want to convert raw strings to JSON
423
- * instead of UTF-8 strings, e. g. binary data.
424
- */
425
- static VALUE mString_to_json_raw_object(VALUE self)
426
- {
427
- VALUE ary;
428
- VALUE result = rb_hash_new();
429
- rb_hash_aset(result, rb_funcall(mJSON, i_create_id, 0), rb_class_name(rb_obj_class(self)));
430
- ary = rb_funcall(self, i_unpack, 1, rb_str_new2("C*"));
431
- rb_hash_aset(result, rb_str_new2("raw"), ary);
432
- return result;
433
- }
434
-
435
- /*
436
- * call-seq: to_json_raw(*args)
437
- *
438
- * This method creates a JSON text from the result of a call to
439
- * to_json_raw_object of this String.
440
- */
441
- static VALUE mString_to_json_raw(int argc, VALUE *argv, VALUE self)
442
- {
443
- VALUE obj = mString_to_json_raw_object(self);
444
- Check_Type(obj, T_HASH);
445
- return mHash_to_json(argc, argv, obj);
446
- }
447
-
448
- /*
449
- * call-seq: json_create(o)
450
- *
451
- * Raw Strings are JSON Objects (the raw bytes are stored in an array for the
452
- * key "raw"). The Ruby String can be created by this module method.
453
- */
454
- static VALUE mString_Extend_json_create(VALUE self, VALUE o)
455
- {
456
- VALUE ary;
457
- Check_Type(o, T_HASH);
458
- ary = rb_hash_aref(o, rb_str_new2("raw"));
459
- return rb_funcall(ary, i_pack, 1, rb_str_new2("C*"));
460
- }
461
-
462
- /*
463
- * call-seq: to_json(*)
464
- *
465
- * Returns a JSON string for true: 'true'.
466
- */
467
- static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self)
468
- {
469
- GENERATE_JSON(true);
470
- }
471
-
472
- /*
473
- * call-seq: to_json(*)
474
- *
475
- * Returns a JSON string for false: 'false'.
476
- */
477
- static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self)
478
- {
479
- GENERATE_JSON(false);
480
- }
481
-
482
- /*
483
- * call-seq: to_json(*)
484
- *
485
- * Returns a JSON string for nil: 'null'.
486
- */
487
- static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self)
488
- {
489
- GENERATE_JSON(null);
490
- }
491
-
492
- /*
493
- * call-seq: to_json(*)
494
- *
495
- * Converts this object to a string (calling #to_s), converts
496
- * it to a JSON string, and returns the result. This is a fallback, if no
497
- * special method #to_json was defined for some object.
498
- */
499
- static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self)
500
- {
501
- VALUE state;
502
- VALUE string = rb_funcall(self, i_to_s, 0);
503
- rb_scan_args(argc, argv, "01", &state);
504
- Check_Type(string, T_STRING);
505
- state = cState_from_state_s(cState, state);
506
- return cState_partial_generate(state, string);
507
- }
508
-
509
- static void State_free(void *ptr)
510
- {
511
- JSON_Generator_State *state = ptr;
512
- if (state->indent) ruby_xfree(state->indent);
513
- if (state->space) ruby_xfree(state->space);
514
- if (state->space_before) ruby_xfree(state->space_before);
515
- if (state->object_nl) ruby_xfree(state->object_nl);
516
- if (state->array_nl) ruby_xfree(state->array_nl);
517
- if (state->array_delim) fbuffer_free(state->array_delim);
518
- if (state->object_delim) fbuffer_free(state->object_delim);
519
- if (state->object_delim2) fbuffer_free(state->object_delim2);
520
- ruby_xfree(state);
521
- }
522
-
523
- static size_t State_memsize(const void *ptr)
524
- {
525
- const JSON_Generator_State *state = ptr;
526
- size_t size = sizeof(*state);
527
- if (state->indent) size += state->indent_len + 1;
528
- if (state->space) size += state->space_len + 1;
529
- if (state->space_before) size += state->space_before_len + 1;
530
- if (state->object_nl) size += state->object_nl_len + 1;
531
- if (state->array_nl) size += state->array_nl_len + 1;
532
- if (state->array_delim) size += FBUFFER_CAPA(state->array_delim);
533
- if (state->object_delim) size += FBUFFER_CAPA(state->object_delim);
534
- if (state->object_delim2) size += FBUFFER_CAPA(state->object_delim2);
535
- return size;
536
- }
537
-
538
- #ifdef NEW_TYPEDDATA_WRAPPER
539
- static const rb_data_type_t JSON_Generator_State_type = {
540
- "JSON/Generator/State",
541
- {NULL, State_free, State_memsize,},
542
- #ifdef RUBY_TYPED_FREE_IMMEDIATELY
543
- 0, 0,
544
- RUBY_TYPED_FREE_IMMEDIATELY,
545
- #endif
546
- };
547
- #endif
548
-
549
- static VALUE cState_s_allocate(VALUE klass)
550
- {
551
- JSON_Generator_State *state;
552
- return TypedData_Make_Struct(klass, JSON_Generator_State,
553
- &JSON_Generator_State_type, state);
554
- }
555
-
556
- /*
557
- * call-seq: configure(opts)
558
- *
559
- * Configure this State instance with the Hash _opts_, and return
560
- * itself.
561
- */
562
- static VALUE cState_configure(VALUE self, VALUE opts)
563
- {
564
- VALUE tmp;
565
- GET_STATE(self);
566
- tmp = rb_check_convert_type(opts, T_HASH, "Hash", "to_hash");
567
- if (NIL_P(tmp)) tmp = rb_convert_type(opts, T_HASH, "Hash", "to_h");
568
- opts = tmp;
569
- tmp = rb_hash_aref(opts, ID2SYM(i_indent));
570
- if (RTEST(tmp)) {
571
- unsigned long len;
572
- Check_Type(tmp, T_STRING);
573
- len = RSTRING_LEN(tmp);
574
- state->indent = fstrndup(RSTRING_PTR(tmp), len + 1);
575
- state->indent_len = len;
576
- }
577
- tmp = rb_hash_aref(opts, ID2SYM(i_space));
578
- if (RTEST(tmp)) {
579
- unsigned long len;
580
- Check_Type(tmp, T_STRING);
581
- len = RSTRING_LEN(tmp);
582
- state->space = fstrndup(RSTRING_PTR(tmp), len + 1);
583
- state->space_len = len;
584
- }
585
- tmp = rb_hash_aref(opts, ID2SYM(i_space_before));
586
- if (RTEST(tmp)) {
587
- unsigned long len;
588
- Check_Type(tmp, T_STRING);
589
- len = RSTRING_LEN(tmp);
590
- state->space_before = fstrndup(RSTRING_PTR(tmp), len + 1);
591
- state->space_before_len = len;
592
- }
593
- tmp = rb_hash_aref(opts, ID2SYM(i_array_nl));
594
- if (RTEST(tmp)) {
595
- unsigned long len;
596
- Check_Type(tmp, T_STRING);
597
- len = RSTRING_LEN(tmp);
598
- state->array_nl = fstrndup(RSTRING_PTR(tmp), len + 1);
599
- state->array_nl_len = len;
600
- }
601
- tmp = rb_hash_aref(opts, ID2SYM(i_object_nl));
602
- if (RTEST(tmp)) {
603
- unsigned long len;
604
- Check_Type(tmp, T_STRING);
605
- len = RSTRING_LEN(tmp);
606
- state->object_nl = fstrndup(RSTRING_PTR(tmp), len + 1);
607
- state->object_nl_len = len;
608
- }
609
- tmp = ID2SYM(i_max_nesting);
610
- state->max_nesting = 100;
611
- if (option_given_p(opts, tmp)) {
612
- VALUE max_nesting = rb_hash_aref(opts, tmp);
613
- if (RTEST(max_nesting)) {
614
- Check_Type(max_nesting, T_FIXNUM);
615
- state->max_nesting = FIX2LONG(max_nesting);
616
- } else {
617
- state->max_nesting = 0;
618
- }
619
- }
620
- tmp = ID2SYM(i_depth);
621
- state->depth = 0;
622
- if (option_given_p(opts, tmp)) {
623
- VALUE depth = rb_hash_aref(opts, tmp);
624
- if (RTEST(depth)) {
625
- Check_Type(depth, T_FIXNUM);
626
- state->depth = FIX2LONG(depth);
627
- } else {
628
- state->depth = 0;
629
- }
630
- }
631
- tmp = ID2SYM(i_buffer_initial_length);
632
- if (option_given_p(opts, tmp)) {
633
- VALUE buffer_initial_length = rb_hash_aref(opts, tmp);
634
- if (RTEST(buffer_initial_length)) {
635
- long initial_length;
636
- Check_Type(buffer_initial_length, T_FIXNUM);
637
- initial_length = FIX2LONG(buffer_initial_length);
638
- if (initial_length > 0) state->buffer_initial_length = initial_length;
639
- }
640
- }
641
- tmp = rb_hash_aref(opts, ID2SYM(i_allow_nan));
642
- state->allow_nan = RTEST(tmp);
643
- tmp = rb_hash_aref(opts, ID2SYM(i_ascii_only));
644
- state->ascii_only = RTEST(tmp);
645
- return self;
646
- }
647
-
648
- static void set_state_ivars(VALUE hash, VALUE state)
649
- {
650
- VALUE ivars = rb_obj_instance_variables(state);
651
- int i = 0;
652
- for (i = 0; i < RARRAY_LEN(ivars); i++) {
653
- VALUE key = rb_funcall(rb_ary_entry(ivars, i), i_to_s, 0);
654
- long key_len = RSTRING_LEN(key);
655
- VALUE value = rb_iv_get(state, StringValueCStr(key));
656
- rb_hash_aset(hash, rb_str_intern(rb_str_substr(key, 1, key_len - 1)), value);
657
- }
658
- }
659
-
660
- /*
661
- * call-seq: to_h
662
- *
663
- * Returns the configuration instance variables as a hash, that can be
664
- * passed to the configure method.
665
- */
666
- static VALUE cState_to_h(VALUE self)
667
- {
668
- VALUE result = rb_hash_new();
669
- GET_STATE(self);
670
- set_state_ivars(result, self);
671
- rb_hash_aset(result, ID2SYM(i_indent), rb_str_new(state->indent, state->indent_len));
672
- rb_hash_aset(result, ID2SYM(i_space), rb_str_new(state->space, state->space_len));
673
- rb_hash_aset(result, ID2SYM(i_space_before), rb_str_new(state->space_before, state->space_before_len));
674
- rb_hash_aset(result, ID2SYM(i_object_nl), rb_str_new(state->object_nl, state->object_nl_len));
675
- rb_hash_aset(result, ID2SYM(i_array_nl), rb_str_new(state->array_nl, state->array_nl_len));
676
- rb_hash_aset(result, ID2SYM(i_allow_nan), state->allow_nan ? Qtrue : Qfalse);
677
- rb_hash_aset(result, ID2SYM(i_ascii_only), state->ascii_only ? Qtrue : Qfalse);
678
- rb_hash_aset(result, ID2SYM(i_max_nesting), LONG2FIX(state->max_nesting));
679
- rb_hash_aset(result, ID2SYM(i_depth), LONG2FIX(state->depth));
680
- rb_hash_aset(result, ID2SYM(i_buffer_initial_length), LONG2FIX(state->buffer_initial_length));
681
- return result;
682
- }
683
-
684
- /*
685
- * call-seq: [](name)
686
- *
687
- * Returns the value returned by method +name+.
688
- */
689
- static VALUE cState_aref(VALUE self, VALUE name)
690
- {
691
- name = rb_funcall(name, i_to_s, 0);
692
- if (RTEST(rb_funcall(self, i_respond_to_p, 1, name))) {
693
- return rb_funcall(self, i_send, 1, name);
694
- } else {
695
- return rb_ivar_get(self, rb_intern_str(rb_str_concat(rb_str_new2("@"), name)));
696
- }
697
- }
698
-
699
- /*
700
- * call-seq: []=(name, value)
701
- *
702
- * Sets the attribute name to value.
703
- */
704
- static VALUE cState_aset(VALUE self, VALUE name, VALUE value)
705
- {
706
- VALUE name_writer;
707
-
708
- name = rb_funcall(name, i_to_s, 0);
709
- name_writer = rb_str_cat2(rb_str_dup(name), "=");
710
- if (RTEST(rb_funcall(self, i_respond_to_p, 1, name_writer))) {
711
- return rb_funcall(self, i_send, 2, name_writer, value);
712
- } else {
713
- rb_ivar_set(self, rb_intern_str(rb_str_concat(rb_str_new2("@"), name)), value);
714
- }
715
- return Qnil;
716
- }
717
-
718
- static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
719
- {
720
- char *object_nl = state->object_nl;
721
- long object_nl_len = state->object_nl_len;
722
- char *indent = state->indent;
723
- long indent_len = state->indent_len;
724
- long max_nesting = state->max_nesting;
725
- char *delim = FBUFFER_PTR(state->object_delim);
726
- long delim_len = FBUFFER_LEN(state->object_delim);
727
- char *delim2 = FBUFFER_PTR(state->object_delim2);
728
- long delim2_len = FBUFFER_LEN(state->object_delim2);
729
- long depth = ++state->depth;
730
- int i, j;
731
- VALUE key, key_to_s, keys;
732
- if (max_nesting != 0 && depth > max_nesting) {
733
- fbuffer_free(buffer);
734
- rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
735
- }
736
- fbuffer_append_char(buffer, '{');
737
- keys = rb_funcall(obj, i_keys, 0);
738
- for(i = 0; i < RARRAY_LEN(keys); i++) {
739
- if (i > 0) fbuffer_append(buffer, delim, delim_len);
740
- if (object_nl) {
741
- fbuffer_append(buffer, object_nl, object_nl_len);
742
- }
743
- if (indent) {
744
- for (j = 0; j < depth; j++) {
745
- fbuffer_append(buffer, indent, indent_len);
746
- }
747
- }
748
- key = rb_ary_entry(keys, i);
749
- key_to_s = rb_funcall(key, i_to_s, 0);
750
- Check_Type(key_to_s, T_STRING);
751
- generate_json(buffer, Vstate, state, key_to_s);
752
- fbuffer_append(buffer, delim2, delim2_len);
753
- generate_json(buffer, Vstate, state, rb_hash_aref(obj, key));
754
- }
755
- depth = --state->depth;
756
- if (object_nl) {
757
- fbuffer_append(buffer, object_nl, object_nl_len);
758
- if (indent) {
759
- for (j = 0; j < depth; j++) {
760
- fbuffer_append(buffer, indent, indent_len);
761
- }
762
- }
763
- }
764
- fbuffer_append_char(buffer, '}');
765
- }
766
-
767
- static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
768
- {
769
- char *array_nl = state->array_nl;
770
- long array_nl_len = state->array_nl_len;
771
- char *indent = state->indent;
772
- long indent_len = state->indent_len;
773
- long max_nesting = state->max_nesting;
774
- char *delim = FBUFFER_PTR(state->array_delim);
775
- long delim_len = FBUFFER_LEN(state->array_delim);
776
- long depth = ++state->depth;
777
- int i, j;
778
- if (max_nesting != 0 && depth > max_nesting) {
779
- fbuffer_free(buffer);
780
- rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
781
- }
782
- fbuffer_append_char(buffer, '[');
783
- if (array_nl) fbuffer_append(buffer, array_nl, array_nl_len);
784
- for(i = 0; i < RARRAY_LEN(obj); i++) {
785
- if (i > 0) fbuffer_append(buffer, delim, delim_len);
786
- if (indent) {
787
- for (j = 0; j < depth; j++) {
788
- fbuffer_append(buffer, indent, indent_len);
789
- }
790
- }
791
- generate_json(buffer, Vstate, state, rb_ary_entry(obj, i));
792
- }
793
- state->depth = --depth;
794
- if (array_nl) {
795
- fbuffer_append(buffer, array_nl, array_nl_len);
796
- if (indent) {
797
- for (j = 0; j < depth; j++) {
798
- fbuffer_append(buffer, indent, indent_len);
799
- }
800
- }
801
- }
802
- fbuffer_append_char(buffer, ']');
803
- }
804
-
805
- static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
806
- {
807
- fbuffer_append_char(buffer, '"');
808
- #ifdef HAVE_RUBY_ENCODING_H
809
- obj = rb_funcall(obj, i_encode, 1, CEncoding_UTF_8);
810
- #endif
811
- if (state->ascii_only) {
812
- convert_UTF8_to_JSON_ASCII(buffer, obj);
813
- } else {
814
- convert_UTF8_to_JSON(buffer, obj);
815
- }
816
- fbuffer_append_char(buffer, '"');
817
- }
818
-
819
- static void generate_json_null(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
820
- {
821
- fbuffer_append(buffer, "null", 4);
822
- }
823
-
824
- static void generate_json_false(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
825
- {
826
- fbuffer_append(buffer, "false", 5);
827
- }
828
-
829
- static void generate_json_true(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
830
- {
831
- fbuffer_append(buffer, "true", 4);
832
- }
833
-
834
- static void generate_json_fixnum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
835
- {
836
- fbuffer_append_long(buffer, FIX2LONG(obj));
837
- }
838
-
839
- static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
840
- {
841
- VALUE tmp = rb_funcall(obj, i_to_s, 0);
842
- fbuffer_append_str(buffer, tmp);
843
- }
844
-
845
- #ifdef RUBY_INTEGER_UNIFICATION
846
- static void generate_json_integer(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
847
- {
848
- if (FIXNUM_P(obj))
849
- generate_json_fixnum(buffer, Vstate, state, obj);
850
- else
851
- generate_json_bignum(buffer, Vstate, state, obj);
852
- }
853
- #endif
854
- static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
855
- {
856
- double value = RFLOAT_VALUE(obj);
857
- char allow_nan = state->allow_nan;
858
- VALUE tmp = rb_funcall(obj, i_to_s, 0);
859
- if (!allow_nan) {
860
- if (isinf(value)) {
861
- fbuffer_free(buffer);
862
- rb_raise(eGeneratorError, "%u: %"PRIsVALUE" not allowed in JSON", __LINE__, RB_OBJ_STRING(tmp));
863
- } else if (isnan(value)) {
864
- fbuffer_free(buffer);
865
- rb_raise(eGeneratorError, "%u: %"PRIsVALUE" not allowed in JSON", __LINE__, RB_OBJ_STRING(tmp));
866
- }
867
- }
868
- fbuffer_append_str(buffer, tmp);
869
- }
870
-
871
- static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
872
- {
873
- VALUE tmp;
874
- VALUE klass = CLASS_OF(obj);
875
- if (klass == rb_cHash) {
876
- generate_json_object(buffer, Vstate, state, obj);
877
- } else if (klass == rb_cArray) {
878
- generate_json_array(buffer, Vstate, state, obj);
879
- } else if (klass == rb_cString) {
880
- generate_json_string(buffer, Vstate, state, obj);
881
- } else if (obj == Qnil) {
882
- generate_json_null(buffer, Vstate, state, obj);
883
- } else if (obj == Qfalse) {
884
- generate_json_false(buffer, Vstate, state, obj);
885
- } else if (obj == Qtrue) {
886
- generate_json_true(buffer, Vstate, state, obj);
887
- } else if (FIXNUM_P(obj)) {
888
- generate_json_fixnum(buffer, Vstate, state, obj);
889
- } else if (RB_TYPE_P(obj, T_BIGNUM)) {
890
- generate_json_bignum(buffer, Vstate, state, obj);
891
- } else if (klass == rb_cFloat) {
892
- generate_json_float(buffer, Vstate, state, obj);
893
- } else if (rb_respond_to(obj, i_to_json)) {
894
- tmp = rb_funcall(obj, i_to_json, 1, Vstate);
895
- Check_Type(tmp, T_STRING);
896
- fbuffer_append_str(buffer, tmp);
897
- } else {
898
- tmp = rb_funcall(obj, i_to_s, 0);
899
- Check_Type(tmp, T_STRING);
900
- generate_json_string(buffer, Vstate, state, tmp);
901
- }
902
- }
903
-
904
- static FBuffer *cState_prepare_buffer(VALUE self)
905
- {
906
- FBuffer *buffer;
907
- GET_STATE(self);
908
- buffer = fbuffer_alloc(state->buffer_initial_length);
909
-
910
- if (state->object_delim) {
911
- fbuffer_clear(state->object_delim);
912
- } else {
913
- state->object_delim = fbuffer_alloc(16);
914
- }
915
- fbuffer_append_char(state->object_delim, ',');
916
- if (state->object_delim2) {
917
- fbuffer_clear(state->object_delim2);
918
- } else {
919
- state->object_delim2 = fbuffer_alloc(16);
920
- }
921
- if (state->space_before) fbuffer_append(state->object_delim2, state->space_before, state->space_before_len);
922
- fbuffer_append_char(state->object_delim2, ':');
923
- if (state->space) fbuffer_append(state->object_delim2, state->space, state->space_len);
924
-
925
- if (state->array_delim) {
926
- fbuffer_clear(state->array_delim);
927
- } else {
928
- state->array_delim = fbuffer_alloc(16);
929
- }
930
- fbuffer_append_char(state->array_delim, ',');
931
- if (state->array_nl) fbuffer_append(state->array_delim, state->array_nl, state->array_nl_len);
932
- return buffer;
933
- }
934
-
935
- static VALUE cState_partial_generate(VALUE self, VALUE obj)
936
- {
937
- FBuffer *buffer = cState_prepare_buffer(self);
938
- GET_STATE(self);
939
- generate_json(buffer, self, state, obj);
940
- return fbuffer_to_s(buffer);
941
- }
942
-
943
- /*
944
- * call-seq: generate(obj)
945
- *
946
- * Generates a valid JSON document from object +obj+ and returns the
947
- * result. If no valid JSON document can be created this method raises a
948
- * GeneratorError exception.
949
- */
950
- static VALUE cState_generate(VALUE self, VALUE obj)
951
- {
952
- VALUE result = cState_partial_generate(self, obj);
953
- GET_STATE(self);
954
- (void)state;
955
- return result;
956
- }
957
-
958
- /*
959
- * call-seq: new(opts = {})
960
- *
961
- * Instantiates a new State object, configured by _opts_.
962
- *
963
- * _opts_ can have the following keys:
964
- *
965
- * * *indent*: a string used to indent levels (default: ''),
966
- * * *space*: a string that is put after, a : or , delimiter (default: ''),
967
- * * *space_before*: a string that is put before a : pair delimiter (default: ''),
968
- * * *object_nl*: a string that is put at the end of a JSON object (default: ''),
969
- * * *array_nl*: a string that is put at the end of a JSON array (default: ''),
970
- * * *allow_nan*: true if NaN, Infinity, and -Infinity should be
971
- * generated, otherwise an exception is thrown, if these values are
972
- * encountered. This options defaults to false.
973
- * * *buffer_initial_length*: sets the initial length of the generator's
974
- * internal buffer.
975
- */
976
- static VALUE cState_initialize(int argc, VALUE *argv, VALUE self)
977
- {
978
- VALUE opts;
979
- GET_STATE(self);
980
- state->max_nesting = 100;
981
- state->buffer_initial_length = FBUFFER_INITIAL_LENGTH_DEFAULT;
982
- rb_scan_args(argc, argv, "01", &opts);
983
- if (!NIL_P(opts)) cState_configure(self, opts);
984
- return self;
985
- }
986
-
987
- /*
988
- * call-seq: initialize_copy(orig)
989
- *
990
- * Initializes this object from orig if it can be duplicated/cloned and returns
991
- * it.
992
- */
993
- static VALUE cState_init_copy(VALUE obj, VALUE orig)
994
- {
995
- JSON_Generator_State *objState, *origState;
996
-
997
- if (obj == orig) return obj;
998
- GET_STATE_TO(obj, objState);
999
- GET_STATE_TO(orig, origState);
1000
- if (!objState) rb_raise(rb_eArgError, "unallocated JSON::State");
1001
-
1002
- MEMCPY(objState, origState, JSON_Generator_State, 1);
1003
- objState->indent = fstrndup(origState->indent, origState->indent_len);
1004
- objState->space = fstrndup(origState->space, origState->space_len);
1005
- objState->space_before = fstrndup(origState->space_before, origState->space_before_len);
1006
- objState->object_nl = fstrndup(origState->object_nl, origState->object_nl_len);
1007
- objState->array_nl = fstrndup(origState->array_nl, origState->array_nl_len);
1008
- if (origState->array_delim) objState->array_delim = fbuffer_dup(origState->array_delim);
1009
- if (origState->object_delim) objState->object_delim = fbuffer_dup(origState->object_delim);
1010
- if (origState->object_delim2) objState->object_delim2 = fbuffer_dup(origState->object_delim2);
1011
- return obj;
1012
- }
1013
-
1014
- /*
1015
- * call-seq: from_state(opts)
1016
- *
1017
- * Creates a State object from _opts_, which ought to be Hash to create a
1018
- * new State instance configured by _opts_, something else to create an
1019
- * unconfigured instance. If _opts_ is a State object, it is just returned.
1020
- */
1021
- static VALUE cState_from_state_s(VALUE self, VALUE opts)
1022
- {
1023
- if (rb_obj_is_kind_of(opts, self)) {
1024
- return opts;
1025
- } else if (rb_obj_is_kind_of(opts, rb_cHash)) {
1026
- return rb_funcall(self, i_new, 1, opts);
1027
- } else {
1028
- if (NIL_P(CJSON_SAFE_STATE_PROTOTYPE)) {
1029
- CJSON_SAFE_STATE_PROTOTYPE = rb_const_get(mJSON, i_SAFE_STATE_PROTOTYPE);
1030
- }
1031
- return rb_funcall(CJSON_SAFE_STATE_PROTOTYPE, i_dup, 0);
1032
- }
1033
- }
1034
-
1035
- /*
1036
- * call-seq: indent()
1037
- *
1038
- * Returns the string that is used to indent levels in the JSON text.
1039
- */
1040
- static VALUE cState_indent(VALUE self)
1041
- {
1042
- GET_STATE(self);
1043
- return state->indent ? rb_str_new(state->indent, state->indent_len) : rb_str_new2("");
1044
- }
1045
-
1046
- /*
1047
- * call-seq: indent=(indent)
1048
- *
1049
- * Sets the string that is used to indent levels in the JSON text.
1050
- */
1051
- static VALUE cState_indent_set(VALUE self, VALUE indent)
1052
- {
1053
- unsigned long len;
1054
- GET_STATE(self);
1055
- Check_Type(indent, T_STRING);
1056
- len = RSTRING_LEN(indent);
1057
- if (len == 0) {
1058
- if (state->indent) {
1059
- ruby_xfree(state->indent);
1060
- state->indent = NULL;
1061
- state->indent_len = 0;
1062
- }
1063
- } else {
1064
- if (state->indent) ruby_xfree(state->indent);
1065
- state->indent = fstrndup(RSTRING_PTR(indent), len);
1066
- state->indent_len = len;
1067
- }
1068
- return Qnil;
1069
- }
1070
-
1071
- /*
1072
- * call-seq: space()
1073
- *
1074
- * Returns the string that is used to insert a space between the tokens in a JSON
1075
- * string.
1076
- */
1077
- static VALUE cState_space(VALUE self)
1078
- {
1079
- GET_STATE(self);
1080
- return state->space ? rb_str_new(state->space, state->space_len) : rb_str_new2("");
1081
- }
1082
-
1083
- /*
1084
- * call-seq: space=(space)
1085
- *
1086
- * Sets _space_ to the string that is used to insert a space between the tokens in a JSON
1087
- * string.
1088
- */
1089
- static VALUE cState_space_set(VALUE self, VALUE space)
1090
- {
1091
- unsigned long len;
1092
- GET_STATE(self);
1093
- Check_Type(space, T_STRING);
1094
- len = RSTRING_LEN(space);
1095
- if (len == 0) {
1096
- if (state->space) {
1097
- ruby_xfree(state->space);
1098
- state->space = NULL;
1099
- state->space_len = 0;
1100
- }
1101
- } else {
1102
- if (state->space) ruby_xfree(state->space);
1103
- state->space = fstrndup(RSTRING_PTR(space), len);
1104
- state->space_len = len;
1105
- }
1106
- return Qnil;
1107
- }
1108
-
1109
- /*
1110
- * call-seq: space_before()
1111
- *
1112
- * Returns the string that is used to insert a space before the ':' in JSON objects.
1113
- */
1114
- static VALUE cState_space_before(VALUE self)
1115
- {
1116
- GET_STATE(self);
1117
- return state->space_before ? rb_str_new(state->space_before, state->space_before_len) : rb_str_new2("");
1118
- }
1119
-
1120
- /*
1121
- * call-seq: space_before=(space_before)
1122
- *
1123
- * Sets the string that is used to insert a space before the ':' in JSON objects.
1124
- */
1125
- static VALUE cState_space_before_set(VALUE self, VALUE space_before)
1126
- {
1127
- unsigned long len;
1128
- GET_STATE(self);
1129
- Check_Type(space_before, T_STRING);
1130
- len = RSTRING_LEN(space_before);
1131
- if (len == 0) {
1132
- if (state->space_before) {
1133
- ruby_xfree(state->space_before);
1134
- state->space_before = NULL;
1135
- state->space_before_len = 0;
1136
- }
1137
- } else {
1138
- if (state->space_before) ruby_xfree(state->space_before);
1139
- state->space_before = fstrndup(RSTRING_PTR(space_before), len);
1140
- state->space_before_len = len;
1141
- }
1142
- return Qnil;
1143
- }
1144
-
1145
- /*
1146
- * call-seq: object_nl()
1147
- *
1148
- * This string is put at the end of a line that holds a JSON object (or
1149
- * Hash).
1150
- */
1151
- static VALUE cState_object_nl(VALUE self)
1152
- {
1153
- GET_STATE(self);
1154
- return state->object_nl ? rb_str_new(state->object_nl, state->object_nl_len) : rb_str_new2("");
1155
- }
1156
-
1157
- /*
1158
- * call-seq: object_nl=(object_nl)
1159
- *
1160
- * This string is put at the end of a line that holds a JSON object (or
1161
- * Hash).
1162
- */
1163
- static VALUE cState_object_nl_set(VALUE self, VALUE object_nl)
1164
- {
1165
- unsigned long len;
1166
- GET_STATE(self);
1167
- Check_Type(object_nl, T_STRING);
1168
- len = RSTRING_LEN(object_nl);
1169
- if (len == 0) {
1170
- if (state->object_nl) {
1171
- ruby_xfree(state->object_nl);
1172
- state->object_nl = NULL;
1173
- }
1174
- } else {
1175
- if (state->object_nl) ruby_xfree(state->object_nl);
1176
- state->object_nl = fstrndup(RSTRING_PTR(object_nl), len);
1177
- state->object_nl_len = len;
1178
- }
1179
- return Qnil;
1180
- }
1181
-
1182
- /*
1183
- * call-seq: array_nl()
1184
- *
1185
- * This string is put at the end of a line that holds a JSON array.
1186
- */
1187
- static VALUE cState_array_nl(VALUE self)
1188
- {
1189
- GET_STATE(self);
1190
- return state->array_nl ? rb_str_new(state->array_nl, state->array_nl_len) : rb_str_new2("");
1191
- }
1192
-
1193
- /*
1194
- * call-seq: array_nl=(array_nl)
1195
- *
1196
- * This string is put at the end of a line that holds a JSON array.
1197
- */
1198
- static VALUE cState_array_nl_set(VALUE self, VALUE array_nl)
1199
- {
1200
- unsigned long len;
1201
- GET_STATE(self);
1202
- Check_Type(array_nl, T_STRING);
1203
- len = RSTRING_LEN(array_nl);
1204
- if (len == 0) {
1205
- if (state->array_nl) {
1206
- ruby_xfree(state->array_nl);
1207
- state->array_nl = NULL;
1208
- }
1209
- } else {
1210
- if (state->array_nl) ruby_xfree(state->array_nl);
1211
- state->array_nl = fstrndup(RSTRING_PTR(array_nl), len);
1212
- state->array_nl_len = len;
1213
- }
1214
- return Qnil;
1215
- }
1216
-
1217
-
1218
- /*
1219
- * call-seq: check_circular?
1220
- *
1221
- * Returns true, if circular data structures should be checked,
1222
- * otherwise returns false.
1223
- */
1224
- static VALUE cState_check_circular_p(VALUE self)
1225
- {
1226
- GET_STATE(self);
1227
- return state->max_nesting ? Qtrue : Qfalse;
1228
- }
1229
-
1230
- /*
1231
- * call-seq: max_nesting
1232
- *
1233
- * This integer returns the maximum level of data structure nesting in
1234
- * the generated JSON, max_nesting = 0 if no maximum is checked.
1235
- */
1236
- static VALUE cState_max_nesting(VALUE self)
1237
- {
1238
- GET_STATE(self);
1239
- return LONG2FIX(state->max_nesting);
1240
- }
1241
-
1242
- /*
1243
- * call-seq: max_nesting=(depth)
1244
- *
1245
- * This sets the maximum level of data structure nesting in the generated JSON
1246
- * to the integer depth, max_nesting = 0 if no maximum should be checked.
1247
- */
1248
- static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
1249
- {
1250
- GET_STATE(self);
1251
- Check_Type(depth, T_FIXNUM);
1252
- return state->max_nesting = FIX2LONG(depth);
1253
- }
1254
-
1255
- /*
1256
- * call-seq: allow_nan?
1257
- *
1258
- * Returns true, if NaN, Infinity, and -Infinity should be generated, otherwise
1259
- * returns false.
1260
- */
1261
- static VALUE cState_allow_nan_p(VALUE self)
1262
- {
1263
- GET_STATE(self);
1264
- return state->allow_nan ? Qtrue : Qfalse;
1265
- }
1266
-
1267
- /*
1268
- * call-seq: ascii_only?
1269
- *
1270
- * Returns true, if NaN, Infinity, and -Infinity should be generated, otherwise
1271
- * returns false.
1272
- */
1273
- static VALUE cState_ascii_only_p(VALUE self)
1274
- {
1275
- GET_STATE(self);
1276
- return state->ascii_only ? Qtrue : Qfalse;
1277
- }
1278
-
1279
- /*
1280
- * call-seq: depth
1281
- *
1282
- * This integer returns the current depth of data structure nesting.
1283
- */
1284
- static VALUE cState_depth(VALUE self)
1285
- {
1286
- GET_STATE(self);
1287
- return LONG2FIX(state->depth);
1288
- }
1289
-
1290
- /*
1291
- * call-seq: depth=(depth)
1292
- *
1293
- * This sets the maximum level of data structure nesting in the generated JSON
1294
- * to the integer depth, max_nesting = 0 if no maximum should be checked.
1295
- */
1296
- static VALUE cState_depth_set(VALUE self, VALUE depth)
1297
- {
1298
- GET_STATE(self);
1299
- Check_Type(depth, T_FIXNUM);
1300
- state->depth = FIX2LONG(depth);
1301
- return Qnil;
1302
- }
1303
-
1304
- /*
1305
- * call-seq: buffer_initial_length
1306
- *
1307
- * This integer returns the current initial length of the buffer.
1308
- */
1309
- static VALUE cState_buffer_initial_length(VALUE self)
1310
- {
1311
- GET_STATE(self);
1312
- return LONG2FIX(state->buffer_initial_length);
1313
- }
1314
-
1315
- /*
1316
- * call-seq: buffer_initial_length=(length)
1317
- *
1318
- * This sets the initial length of the buffer to +length+, if +length+ > 0,
1319
- * otherwise its value isn't changed.
1320
- */
1321
- static VALUE cState_buffer_initial_length_set(VALUE self, VALUE buffer_initial_length)
1322
- {
1323
- long initial_length;
1324
- GET_STATE(self);
1325
- Check_Type(buffer_initial_length, T_FIXNUM);
1326
- initial_length = FIX2LONG(buffer_initial_length);
1327
- if (initial_length > 0) {
1328
- state->buffer_initial_length = initial_length;
1329
- }
1330
- return Qnil;
1331
- }
1332
-
1333
- /*
1334
- *
1335
- */
1336
- void Init_generator(void)
1337
- {
1338
- #undef rb_intern
1339
- rb_require("json/common");
1340
-
1341
- mJSON = rb_define_module("JSON");
1342
- mExt = rb_define_module_under(mJSON, "Ext");
1343
- mGenerator = rb_define_module_under(mExt, "Generator");
1344
-
1345
- eGeneratorError = rb_path2class("JSON::GeneratorError");
1346
- eNestingError = rb_path2class("JSON::NestingError");
1347
-
1348
- cState = rb_define_class_under(mGenerator, "State", rb_cObject);
1349
- rb_define_alloc_func(cState, cState_s_allocate);
1350
- rb_define_singleton_method(cState, "from_state", cState_from_state_s, 1);
1351
- rb_define_method(cState, "initialize", cState_initialize, -1);
1352
- rb_define_method(cState, "initialize_copy", cState_init_copy, 1);
1353
- rb_define_method(cState, "indent", cState_indent, 0);
1354
- rb_define_method(cState, "indent=", cState_indent_set, 1);
1355
- rb_define_method(cState, "space", cState_space, 0);
1356
- rb_define_method(cState, "space=", cState_space_set, 1);
1357
- rb_define_method(cState, "space_before", cState_space_before, 0);
1358
- rb_define_method(cState, "space_before=", cState_space_before_set, 1);
1359
- rb_define_method(cState, "object_nl", cState_object_nl, 0);
1360
- rb_define_method(cState, "object_nl=", cState_object_nl_set, 1);
1361
- rb_define_method(cState, "array_nl", cState_array_nl, 0);
1362
- rb_define_method(cState, "array_nl=", cState_array_nl_set, 1);
1363
- rb_define_method(cState, "max_nesting", cState_max_nesting, 0);
1364
- rb_define_method(cState, "max_nesting=", cState_max_nesting_set, 1);
1365
- rb_define_method(cState, "check_circular?", cState_check_circular_p, 0);
1366
- rb_define_method(cState, "allow_nan?", cState_allow_nan_p, 0);
1367
- rb_define_method(cState, "ascii_only?", cState_ascii_only_p, 0);
1368
- rb_define_method(cState, "depth", cState_depth, 0);
1369
- rb_define_method(cState, "depth=", cState_depth_set, 1);
1370
- rb_define_method(cState, "buffer_initial_length", cState_buffer_initial_length, 0);
1371
- rb_define_method(cState, "buffer_initial_length=", cState_buffer_initial_length_set, 1);
1372
- rb_define_method(cState, "configure", cState_configure, 1);
1373
- rb_define_alias(cState, "merge", "configure");
1374
- rb_define_method(cState, "to_h", cState_to_h, 0);
1375
- rb_define_alias(cState, "to_hash", "to_h");
1376
- rb_define_method(cState, "[]", cState_aref, 1);
1377
- rb_define_method(cState, "[]=", cState_aset, 2);
1378
- rb_define_method(cState, "generate", cState_generate, 1);
1379
-
1380
- mGeneratorMethods = rb_define_module_under(mGenerator, "GeneratorMethods");
1381
- mObject = rb_define_module_under(mGeneratorMethods, "Object");
1382
- rb_define_method(mObject, "to_json", mObject_to_json, -1);
1383
- mHash = rb_define_module_under(mGeneratorMethods, "Hash");
1384
- rb_define_method(mHash, "to_json", mHash_to_json, -1);
1385
- mArray = rb_define_module_under(mGeneratorMethods, "Array");
1386
- rb_define_method(mArray, "to_json", mArray_to_json, -1);
1387
- #ifdef RUBY_INTEGER_UNIFICATION
1388
- mInteger = rb_define_module_under(mGeneratorMethods, "Integer");
1389
- rb_define_method(mInteger, "to_json", mInteger_to_json, -1);
1390
- #else
1391
- mFixnum = rb_define_module_under(mGeneratorMethods, "Fixnum");
1392
- rb_define_method(mFixnum, "to_json", mFixnum_to_json, -1);
1393
- mBignum = rb_define_module_under(mGeneratorMethods, "Bignum");
1394
- rb_define_method(mBignum, "to_json", mBignum_to_json, -1);
1395
- #endif
1396
- mFloat = rb_define_module_under(mGeneratorMethods, "Float");
1397
- rb_define_method(mFloat, "to_json", mFloat_to_json, -1);
1398
- mString = rb_define_module_under(mGeneratorMethods, "String");
1399
- rb_define_singleton_method(mString, "included", mString_included_s, 1);
1400
- rb_define_method(mString, "to_json", mString_to_json, -1);
1401
- rb_define_method(mString, "to_json_raw", mString_to_json_raw, -1);
1402
- rb_define_method(mString, "to_json_raw_object", mString_to_json_raw_object, 0);
1403
- mString_Extend = rb_define_module_under(mString, "Extend");
1404
- rb_define_method(mString_Extend, "json_create", mString_Extend_json_create, 1);
1405
- mTrueClass = rb_define_module_under(mGeneratorMethods, "TrueClass");
1406
- rb_define_method(mTrueClass, "to_json", mTrueClass_to_json, -1);
1407
- mFalseClass = rb_define_module_under(mGeneratorMethods, "FalseClass");
1408
- rb_define_method(mFalseClass, "to_json", mFalseClass_to_json, -1);
1409
- mNilClass = rb_define_module_under(mGeneratorMethods, "NilClass");
1410
- rb_define_method(mNilClass, "to_json", mNilClass_to_json, -1);
1411
-
1412
- CRegexp_MULTILINE = rb_const_get(rb_cRegexp, rb_intern("MULTILINE"));
1413
- i_to_s = rb_intern("to_s");
1414
- i_to_json = rb_intern("to_json");
1415
- i_new = rb_intern("new");
1416
- i_indent = rb_intern("indent");
1417
- i_space = rb_intern("space");
1418
- i_space_before = rb_intern("space_before");
1419
- i_object_nl = rb_intern("object_nl");
1420
- i_array_nl = rb_intern("array_nl");
1421
- i_max_nesting = rb_intern("max_nesting");
1422
- i_allow_nan = rb_intern("allow_nan");
1423
- i_ascii_only = rb_intern("ascii_only");
1424
- i_depth = rb_intern("depth");
1425
- i_buffer_initial_length = rb_intern("buffer_initial_length");
1426
- i_pack = rb_intern("pack");
1427
- i_unpack = rb_intern("unpack");
1428
- i_create_id = rb_intern("create_id");
1429
- i_extend = rb_intern("extend");
1430
- i_key_p = rb_intern("key?");
1431
- i_aref = rb_intern("[]");
1432
- i_send = rb_intern("__send__");
1433
- i_respond_to_p = rb_intern("respond_to?");
1434
- i_match = rb_intern("match");
1435
- i_keys = rb_intern("keys");
1436
- i_dup = rb_intern("dup");
1437
- #ifdef HAVE_RUBY_ENCODING_H
1438
- CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
1439
- i_encoding = rb_intern("encoding");
1440
- i_encode = rb_intern("encode");
1441
- #endif
1442
- i_SAFE_STATE_PROTOTYPE = rb_intern("SAFE_STATE_PROTOTYPE");
1443
- CJSON_SAFE_STATE_PROTOTYPE = Qnil;
1444
- }