scout 5.7.3.pre.2 → 5.7.4

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