es-scout 5.3.0.es1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (140) hide show
  1. data/AUTHORS +4 -0
  2. data/CHANGELOG +225 -0
  3. data/COPYING +340 -0
  4. data/INSTALL +18 -0
  5. data/LICENSE +6 -0
  6. data/README +66 -0
  7. data/Rakefile +74 -0
  8. data/TODO +6 -0
  9. data/bin/es-scout +10 -0
  10. data/data/cacert.pem +3154 -0
  11. data/data/code_key.pub +13 -0
  12. data/data/gpl-2.0.txt +339 -0
  13. data/data/lgpl-2.1.txt +504 -0
  14. data/lib/es-scout/command/install.rb +68 -0
  15. data/lib/es-scout/command/run.rb +56 -0
  16. data/lib/es-scout/command/test.rb +62 -0
  17. data/lib/es-scout/command/troubleshoot.rb +142 -0
  18. data/lib/es-scout/command.rb +258 -0
  19. data/lib/es-scout/plugin.rb +237 -0
  20. data/lib/es-scout/plugin_options.rb +80 -0
  21. data/lib/es-scout/scout_logger.rb +19 -0
  22. data/lib/es-scout/server.rb +578 -0
  23. data/lib/es-scout.rb +11 -0
  24. data/vendor/json_pure/CHANGES +162 -0
  25. data/vendor/json_pure/COPYING +58 -0
  26. data/vendor/json_pure/GPL +340 -0
  27. data/vendor/json_pure/README +358 -0
  28. data/vendor/json_pure/Rakefile +292 -0
  29. data/vendor/json_pure/TODO +1 -0
  30. data/vendor/json_pure/VERSION +1 -0
  31. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkComparison.log +52 -0
  32. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast-autocorrelation.dat +1000 -0
  33. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast.dat +1001 -0
  34. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty-autocorrelation.dat +900 -0
  35. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty.dat +901 -0
  36. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe-autocorrelation.dat +1000 -0
  37. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe.dat +1001 -0
  38. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt.log +261 -0
  39. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast-autocorrelation.dat +1000 -0
  40. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast.dat +1001 -0
  41. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty-autocorrelation.dat +1000 -0
  42. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty.dat +1001 -0
  43. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe-autocorrelation.dat +1000 -0
  44. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe.dat +1001 -0
  45. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure.log +262 -0
  46. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator-autocorrelation.dat +1000 -0
  47. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator.dat +1001 -0
  48. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails.log +82 -0
  49. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkComparison.log +34 -0
  50. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser-autocorrelation.dat +900 -0
  51. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser.dat +901 -0
  52. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt.log +81 -0
  53. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser-autocorrelation.dat +1000 -0
  54. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser.dat +1001 -0
  55. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure.log +82 -0
  56. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser-autocorrelation.dat +1000 -0
  57. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser.dat +1001 -0
  58. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails.log +82 -0
  59. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser-autocorrelation.dat +1000 -0
  60. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser.dat +1001 -0
  61. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML.log +82 -0
  62. data/vendor/json_pure/benchmarks/generator2_benchmark.rb +222 -0
  63. data/vendor/json_pure/benchmarks/generator_benchmark.rb +224 -0
  64. data/vendor/json_pure/benchmarks/ohai.json +1216 -0
  65. data/vendor/json_pure/benchmarks/ohai.ruby +1 -0
  66. data/vendor/json_pure/benchmarks/parser2_benchmark.rb +251 -0
  67. data/vendor/json_pure/benchmarks/parser_benchmark.rb +259 -0
  68. data/vendor/json_pure/bin/edit_json.rb +9 -0
  69. data/vendor/json_pure/bin/prettify_json.rb +75 -0
  70. data/vendor/json_pure/data/example.json +1 -0
  71. data/vendor/json_pure/data/index.html +38 -0
  72. data/vendor/json_pure/data/prototype.js +4184 -0
  73. data/vendor/json_pure/ext/json/ext/generator/extconf.rb +16 -0
  74. data/vendor/json_pure/ext/json/ext/generator/generator.c +1341 -0
  75. data/vendor/json_pure/ext/json/ext/generator/generator.h +170 -0
  76. data/vendor/json_pure/ext/json/ext/parser/extconf.rb +15 -0
  77. data/vendor/json_pure/ext/json/ext/parser/parser.c +1935 -0
  78. data/vendor/json_pure/ext/json/ext/parser/parser.h +71 -0
  79. data/vendor/json_pure/ext/json/ext/parser/parser.rl +792 -0
  80. data/vendor/json_pure/install.rb +26 -0
  81. data/vendor/json_pure/lib/json/Array.xpm +21 -0
  82. data/vendor/json_pure/lib/json/FalseClass.xpm +21 -0
  83. data/vendor/json_pure/lib/json/Hash.xpm +21 -0
  84. data/vendor/json_pure/lib/json/Key.xpm +73 -0
  85. data/vendor/json_pure/lib/json/NilClass.xpm +21 -0
  86. data/vendor/json_pure/lib/json/Numeric.xpm +28 -0
  87. data/vendor/json_pure/lib/json/String.xpm +96 -0
  88. data/vendor/json_pure/lib/json/TrueClass.xpm +21 -0
  89. data/vendor/json_pure/lib/json/add/core.rb +148 -0
  90. data/vendor/json_pure/lib/json/add/rails.rb +58 -0
  91. data/vendor/json_pure/lib/json/common.rb +397 -0
  92. data/vendor/json_pure/lib/json/editor.rb +1371 -0
  93. data/vendor/json_pure/lib/json/ext.rb +15 -0
  94. data/vendor/json_pure/lib/json/json.xpm +1499 -0
  95. data/vendor/json_pure/lib/json/pure/generator.rb +452 -0
  96. data/vendor/json_pure/lib/json/pure/parser.rb +307 -0
  97. data/vendor/json_pure/lib/json/pure.rb +77 -0
  98. data/vendor/json_pure/lib/json/version.rb +8 -0
  99. data/vendor/json_pure/lib/json.rb +10 -0
  100. data/vendor/json_pure/tests/fixtures/fail1.json +1 -0
  101. data/vendor/json_pure/tests/fixtures/fail10.json +1 -0
  102. data/vendor/json_pure/tests/fixtures/fail11.json +1 -0
  103. data/vendor/json_pure/tests/fixtures/fail12.json +1 -0
  104. data/vendor/json_pure/tests/fixtures/fail13.json +1 -0
  105. data/vendor/json_pure/tests/fixtures/fail14.json +1 -0
  106. data/vendor/json_pure/tests/fixtures/fail18.json +1 -0
  107. data/vendor/json_pure/tests/fixtures/fail19.json +1 -0
  108. data/vendor/json_pure/tests/fixtures/fail2.json +1 -0
  109. data/vendor/json_pure/tests/fixtures/fail20.json +1 -0
  110. data/vendor/json_pure/tests/fixtures/fail21.json +1 -0
  111. data/vendor/json_pure/tests/fixtures/fail22.json +1 -0
  112. data/vendor/json_pure/tests/fixtures/fail23.json +1 -0
  113. data/vendor/json_pure/tests/fixtures/fail24.json +1 -0
  114. data/vendor/json_pure/tests/fixtures/fail25.json +1 -0
  115. data/vendor/json_pure/tests/fixtures/fail27.json +2 -0
  116. data/vendor/json_pure/tests/fixtures/fail28.json +2 -0
  117. data/vendor/json_pure/tests/fixtures/fail3.json +1 -0
  118. data/vendor/json_pure/tests/fixtures/fail4.json +1 -0
  119. data/vendor/json_pure/tests/fixtures/fail5.json +1 -0
  120. data/vendor/json_pure/tests/fixtures/fail6.json +1 -0
  121. data/vendor/json_pure/tests/fixtures/fail7.json +1 -0
  122. data/vendor/json_pure/tests/fixtures/fail8.json +1 -0
  123. data/vendor/json_pure/tests/fixtures/fail9.json +1 -0
  124. data/vendor/json_pure/tests/fixtures/pass1.json +56 -0
  125. data/vendor/json_pure/tests/fixtures/pass15.json +1 -0
  126. data/vendor/json_pure/tests/fixtures/pass16.json +1 -0
  127. data/vendor/json_pure/tests/fixtures/pass17.json +1 -0
  128. data/vendor/json_pure/tests/fixtures/pass2.json +1 -0
  129. data/vendor/json_pure/tests/fixtures/pass26.json +1 -0
  130. data/vendor/json_pure/tests/fixtures/pass3.json +6 -0
  131. data/vendor/json_pure/tests/test_json.rb +340 -0
  132. data/vendor/json_pure/tests/test_json_addition.rb +162 -0
  133. data/vendor/json_pure/tests/test_json_encoding.rb +68 -0
  134. data/vendor/json_pure/tests/test_json_fixtures.rb +34 -0
  135. data/vendor/json_pure/tests/test_json_generate.rb +122 -0
  136. data/vendor/json_pure/tests/test_json_rails.rb +144 -0
  137. data/vendor/json_pure/tests/test_json_unicode.rb +76 -0
  138. data/vendor/json_pure/tools/fuzz.rb +139 -0
  139. data/vendor/json_pure/tools/server.rb +61 -0
  140. metadata +233 -0
@@ -0,0 +1,1341 @@
1
+ #include "generator.h"
2
+
3
+ #ifdef HAVE_RUBY_ENCODING_H
4
+ static VALUE CEncoding_UTF_8;
5
+ static ID i_encoding, i_encode;
6
+ #endif
7
+
8
+ static VALUE mJSON, mExt, mGenerator, cState, mGeneratorMethods, mObject,
9
+ mHash, mArray, mInteger, mFloat, mString, mString_Extend,
10
+ mTrueClass, mFalseClass, mNilClass, eGeneratorError,
11
+ eNestingError, CRegexp_MULTILINE, CJSON_SAFE_STATE_PROTOTYPE;
12
+
13
+ static ID i_to_s, i_to_json, i_new, i_indent, i_space, i_space_before,
14
+ i_object_nl, i_array_nl, i_max_nesting, i_allow_nan, i_ascii_only,
15
+ i_pack, i_unpack, i_create_id, i_extend, i_key_p, i_aref, i_send,
16
+ i_respond_to_p, i_match;
17
+
18
+ /*
19
+ * Copyright 2001-2004 Unicode, Inc.
20
+ *
21
+ * Disclaimer
22
+ *
23
+ * This source code is provided as is by Unicode, Inc. No claims are
24
+ * made as to fitness for any particular purpose. No warranties of any
25
+ * kind are expressed or implied. The recipient agrees to determine
26
+ * applicability of information provided. If this file has been
27
+ * purchased on magnetic or optical media from Unicode, Inc., the
28
+ * sole remedy for any claim will be exchange of defective media
29
+ * within 90 days of receipt.
30
+ *
31
+ * Limitations on Rights to Redistribute This Code
32
+ *
33
+ * Unicode, Inc. hereby grants the right to freely use the information
34
+ * supplied in this file in the creation of products supporting the
35
+ * Unicode Standard, and to make copies of this file in any form
36
+ * for internal or external distribution as long as this notice
37
+ * remains attached.
38
+ */
39
+
40
+ /*
41
+ * Index into the table below with the first byte of a UTF-8 sequence to
42
+ * get the number of trailing bytes that are supposed to follow it.
43
+ * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
44
+ * left as-is for anyone who may want to do such conversion, which was
45
+ * allowed in earlier algorithms.
46
+ */
47
+ static const char trailingBytesForUTF8[256] = {
48
+ 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,
49
+ 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,
50
+ 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,
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
+ 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,
55
+ 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
56
+ };
57
+
58
+ /*
59
+ * Magic values subtracted from a buffer value during UTF8 conversion.
60
+ * This table contains as many values as there might be trailing bytes
61
+ * in a UTF-8 sequence.
62
+ */
63
+ static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
64
+ 0x03C82080UL, 0xFA082080UL, 0x82082080UL };
65
+
66
+ /*
67
+ * Utility routine to tell whether a sequence of bytes is legal UTF-8.
68
+ * This must be called with the length pre-determined by the first byte.
69
+ * If not calling this from ConvertUTF8to*, then the length can be set by:
70
+ * length = trailingBytesForUTF8[*source]+1;
71
+ * and the sequence is illegal right away if there aren't that many bytes
72
+ * available.
73
+ * If presented with a length > 4, this returns 0. The Unicode
74
+ * definition of UTF-8 goes up to 4-byte sequences.
75
+ */
76
+ static unsigned char isLegalUTF8(const UTF8 *source, int length)
77
+ {
78
+ UTF8 a;
79
+ const UTF8 *srcptr = source+length;
80
+ switch (length) {
81
+ default: return 0;
82
+ /* Everything else falls through when "1"... */
83
+ case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
84
+ case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
85
+ case 2: if ((a = (*--srcptr)) > 0xBF) return 0;
86
+
87
+ switch (*source) {
88
+ /* no fall-through in this inner switch */
89
+ case 0xE0: if (a < 0xA0) return 0; break;
90
+ case 0xED: if (a > 0x9F) return 0; break;
91
+ case 0xF0: if (a < 0x90) return 0; break;
92
+ case 0xF4: if (a > 0x8F) return 0; break;
93
+ default: if (a < 0x80) return 0;
94
+ }
95
+
96
+ case 1: if (*source >= 0x80 && *source < 0xC2) return 0;
97
+ }
98
+ if (*source > 0xF4) return 0;
99
+ return 1;
100
+ }
101
+
102
+ /* Escapes the UTF16 character and stores the result in the buffer buf. */
103
+ static void unicode_escape(char *buf, UTF16 character)
104
+ {
105
+ const char *digits = "0123456789abcdef";
106
+
107
+ buf[2] = digits[character >> 12];
108
+ buf[3] = digits[(character >> 8) & 0xf];
109
+ buf[4] = digits[(character >> 4) & 0xf];
110
+ buf[5] = digits[character & 0xf];
111
+ }
112
+
113
+ /* Escapes the UTF16 character and stores the result in the buffer buf, then
114
+ * the buffer buf іs appended to the FBuffer buffer. */
115
+ static void unicode_escape_to_buffer(FBuffer *buffer, char buf[6], UTF16
116
+ character)
117
+ {
118
+ unicode_escape(buf, character);
119
+ fbuffer_append(buffer, buf, 6);
120
+ }
121
+
122
+ /* Converts string to a JSON string in FBuffer buffer, where all but the ASCII
123
+ * and control characters are JSON escaped. */
124
+ static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string)
125
+ {
126
+ const UTF8 *source = (UTF8 *) RSTRING_PTR(string);
127
+ const UTF8 *sourceEnd = source + RSTRING_LEN(string);
128
+ char buf[6] = { '\\', 'u' };
129
+
130
+ while (source < sourceEnd) {
131
+ UTF32 ch = 0;
132
+ unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
133
+ if (source + extraBytesToRead >= sourceEnd) {
134
+ rb_raise(rb_path2class("JSON::GeneratorError"),
135
+ "partial character in source, but hit end");
136
+ }
137
+ if (!isLegalUTF8(source, extraBytesToRead+1)) {
138
+ rb_raise(rb_path2class("JSON::GeneratorError"),
139
+ "source sequence is illegal/malformed utf-8");
140
+ }
141
+ /*
142
+ * The cases all fall through. See "Note A" below.
143
+ */
144
+ switch (extraBytesToRead) {
145
+ case 5: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
146
+ case 4: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
147
+ case 3: ch += *source++; ch <<= 6;
148
+ case 2: ch += *source++; ch <<= 6;
149
+ case 1: ch += *source++; ch <<= 6;
150
+ case 0: ch += *source++;
151
+ }
152
+ ch -= offsetsFromUTF8[extraBytesToRead];
153
+
154
+ if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
155
+ /* UTF-16 surrogate values are illegal in UTF-32 */
156
+ if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
157
+ #if UNI_STRICT_CONVERSION
158
+ source -= (extraBytesToRead+1); /* return to the illegal value itself */
159
+ rb_raise(rb_path2class("JSON::GeneratorError"),
160
+ "source sequence is illegal/malformed utf-8");
161
+ #else
162
+ unicode_escape_to_buffer(buffer, buf, UNI_REPLACEMENT_CHAR);
163
+ #endif
164
+ } else {
165
+ /* normal case */
166
+ if (ch >= 0x20 && ch <= 0x7f) {
167
+ switch (ch) {
168
+ case '\\':
169
+ fbuffer_append(buffer, "\\\\", 2);
170
+ break;
171
+ case '"':
172
+ fbuffer_append(buffer, "\\\"", 2);
173
+ break;
174
+ default:
175
+ fbuffer_append_char(buffer, (char)ch);
176
+ break;
177
+ }
178
+ } else {
179
+ switch (ch) {
180
+ case '\n':
181
+ fbuffer_append(buffer, "\\n", 2);
182
+ break;
183
+ case '\r':
184
+ fbuffer_append(buffer, "\\r", 2);
185
+ break;
186
+ case '\t':
187
+ fbuffer_append(buffer, "\\t", 2);
188
+ break;
189
+ case '\f':
190
+ fbuffer_append(buffer, "\\f", 2);
191
+ break;
192
+ case '\b':
193
+ fbuffer_append(buffer, "\\b", 2);
194
+ break;
195
+ default:
196
+ unicode_escape_to_buffer(buffer, buf, (UTF16) ch);
197
+ break;
198
+ }
199
+ }
200
+ }
201
+ } else if (ch > UNI_MAX_UTF16) {
202
+ #if UNI_STRICT_CONVERSION
203
+ source -= (extraBytesToRead+1); /* return to the start */
204
+ rb_raise(rb_path2class("JSON::GeneratorError"),
205
+ "source sequence is illegal/malformed utf8");
206
+ #else
207
+ unicode_escape_to_buffer(buffer, buf, UNI_REPLACEMENT_CHAR);
208
+ #endif
209
+ } else {
210
+ /* target is a character in range 0xFFFF - 0x10FFFF. */
211
+ ch -= halfBase;
212
+ unicode_escape_to_buffer(buffer, buf, (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START));
213
+ unicode_escape_to_buffer(buffer, buf, (UTF16)((ch & halfMask) + UNI_SUR_LOW_START));
214
+ }
215
+ }
216
+ }
217
+
218
+ /* Converts string to a JSON string in FBuffer buffer, where only the
219
+ * characters required by the JSON standard are JSON escaped. The remaining
220
+ * characters (should be UTF8) are just passed through and appended to the
221
+ * result. */
222
+ static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string)
223
+ {
224
+ const char *ptr = RSTRING_PTR(string), *p;
225
+ int len = RSTRING_LEN(string), start = 0, end = 0;
226
+ const char *escape = NULL;
227
+ int escape_len;
228
+ unsigned char c;
229
+ char buf[6] = { '\\', 'u' };
230
+
231
+ for (start = 0, end = 0; end < len;) {
232
+ p = ptr + end;
233
+ c = (unsigned char) *p;
234
+ if (c < 0x20) {
235
+ switch (c) {
236
+ case '\n':
237
+ escape = "\\n";
238
+ escape_len = 2;
239
+ break;
240
+ case '\r':
241
+ escape = "\\r";
242
+ escape_len = 2;
243
+ break;
244
+ case '\t':
245
+ escape = "\\t";
246
+ escape_len = 2;
247
+ break;
248
+ case '\f':
249
+ escape = "\\f";
250
+ escape_len = 2;
251
+ break;
252
+ case '\b':
253
+ escape = "\\b";
254
+ escape_len = 2;
255
+ break;
256
+ default:
257
+ unicode_escape(buf, (UTF16) *p);
258
+ escape = buf;
259
+ escape_len = 6;
260
+ break;
261
+ }
262
+ } else {
263
+ switch (c) {
264
+ case '\\':
265
+ escape = "\\\\";
266
+ escape_len = 2;
267
+ break;
268
+ case '"':
269
+ escape = "\\\"";
270
+ escape_len = 2;
271
+ break;
272
+ default:
273
+ end++;
274
+ continue;
275
+ break;
276
+ }
277
+ }
278
+ fbuffer_append(buffer, ptr + start, end - start);
279
+ fbuffer_append(buffer, escape, escape_len);
280
+ start = ++end;
281
+ escape = NULL;
282
+ }
283
+ fbuffer_append(buffer, ptr + start, end - start);
284
+ }
285
+
286
+ static char *fstrndup(const char *ptr, int len) {
287
+ char *result;
288
+ if (len <= 0) return NULL;
289
+ result = ALLOC_N(char, len);
290
+ memccpy(result, ptr, 0, len);
291
+ return result;
292
+ }
293
+
294
+ /* fbuffer implementation */
295
+
296
+ static FBuffer *fbuffer_alloc()
297
+ {
298
+ FBuffer *fb = ALLOC(FBuffer);
299
+ memset((void *) fb, 0, sizeof(FBuffer));
300
+ fb->initial_length = FBUFFER_INITIAL_LENGTH;
301
+ return fb;
302
+ }
303
+
304
+ static FBuffer *fbuffer_alloc_with_length(unsigned int initial_length)
305
+ {
306
+ FBuffer *fb;
307
+ assert(initial_length > 0);
308
+ fb = ALLOC(FBuffer);
309
+ memset((void *) fb, 0, sizeof(FBuffer));
310
+ fb->initial_length = initial_length;
311
+ return fb;
312
+ }
313
+
314
+ static void fbuffer_free(FBuffer *fb)
315
+ {
316
+ if (fb->ptr) ruby_xfree(fb->ptr);
317
+ ruby_xfree(fb);
318
+ }
319
+
320
+ static void fbuffer_free_only_buffer(FBuffer *fb)
321
+ {
322
+ ruby_xfree(fb);
323
+ }
324
+
325
+ static void fbuffer_clear(FBuffer *fb)
326
+ {
327
+ fb->len = 0;
328
+ }
329
+
330
+ static void fbuffer_inc_capa(FBuffer *fb, unsigned int requested)
331
+ {
332
+ unsigned int required;
333
+
334
+ if (!fb->ptr) {
335
+ fb->ptr = ALLOC_N(char, fb->initial_length);
336
+ fb->capa = fb->initial_length;
337
+ }
338
+
339
+ for (required = fb->capa; requested > required - fb->len; required <<= 1);
340
+
341
+ if (required > fb->capa) {
342
+ fb->ptr = (char *) REALLOC_N((long*) fb->ptr, char, required);
343
+ fb->capa = required;
344
+ }
345
+ }
346
+
347
+ static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned int len)
348
+ {
349
+ if (len > 0) {
350
+ fbuffer_inc_capa(fb, len);
351
+ MEMCPY(fb->ptr + fb->len, newstr, char, len);
352
+ fb->len += len;
353
+ }
354
+ }
355
+
356
+ static void fbuffer_append_char(FBuffer *fb, char newchr)
357
+ {
358
+ fbuffer_inc_capa(fb, 1);
359
+ *(fb->ptr + fb->len) = newchr;
360
+ fb->len++;
361
+ }
362
+
363
+ static void freverse(char *start, char *end)
364
+ {
365
+ char c;
366
+
367
+ while (end > start) {
368
+ c = *end, *end-- = *start, *start++ = c;
369
+ }
370
+ }
371
+
372
+ static int fltoa(long number, char *buf)
373
+ {
374
+ static char digits[] = "0123456789";
375
+ long sign = number;
376
+ char* tmp = buf;
377
+
378
+ if (sign < 0) number = -number;
379
+ do *tmp++ = digits[number % 10]; while (number /= 10);
380
+ if (sign < 0) *tmp++ = '-';
381
+ freverse(buf, tmp - 1);
382
+ return tmp - buf;
383
+ }
384
+
385
+ static void fbuffer_append_long(FBuffer *fb, long number)
386
+ {
387
+ char buf[20];
388
+ int len = fltoa(number, buf);
389
+ fbuffer_append(fb, buf, len);
390
+ }
391
+
392
+ static FBuffer *fbuffer_dup(FBuffer *fb)
393
+ {
394
+ int len = fb->len;
395
+ FBuffer *result;
396
+
397
+ if (len > 0) {
398
+ result = fbuffer_alloc_with_length(len);
399
+ fbuffer_append(result, FBUFFER_PAIR(fb));
400
+ } else {
401
+ result = fbuffer_alloc();
402
+ }
403
+ return result;
404
+ }
405
+
406
+ /*
407
+ * Document-module: JSON::Ext::Generator
408
+ *
409
+ * This is the JSON generator implemented as a C extension. It can be
410
+ * configured to be used by setting
411
+ *
412
+ * JSON.generator = JSON::Ext::Generator
413
+ *
414
+ * with the method generator= in JSON.
415
+ *
416
+ */
417
+
418
+ /*
419
+ * call-seq: to_json(state = nil, depth = 0)
420
+ *
421
+ * Returns a JSON string containing a JSON object, that is generated from
422
+ * this Hash instance.
423
+ * _state_ is a JSON::State object, that can also be used to configure the
424
+ * produced JSON string output further.
425
+ * _depth_ is used to find out nesting depth, to indent accordingly.
426
+ */
427
+ static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self)
428
+ {
429
+ VALUE state, depth;
430
+
431
+ rb_scan_args(argc, argv, "02", &state, &depth);
432
+ state = cState_from_state_s(cState, state);
433
+ return cState_partial_generate(state, self, depth);
434
+ }
435
+
436
+ /*
437
+ * call-seq: to_json(state = nil, depth = 0)
438
+ *
439
+ * Returns a JSON string containing a JSON array, that is generated from
440
+ * this Array instance.
441
+ * _state_ is a JSON::State object, that can also be used to configure the
442
+ * produced JSON string output further.
443
+ * _depth_ is used to find out nesting depth, to indent accordingly.
444
+ */
445
+ static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self) {
446
+ VALUE state, depth;
447
+ rb_scan_args(argc, argv, "02", &state, &depth);
448
+ state = cState_from_state_s(cState, state);
449
+ return cState_partial_generate(state, self, depth);
450
+ }
451
+
452
+ /*
453
+ * call-seq: to_json(*)
454
+ *
455
+ * Returns a JSON string representation for this Integer number.
456
+ */
457
+ static VALUE mInteger_to_json(int argc, VALUE *argv, VALUE self)
458
+ {
459
+ VALUE state, depth;
460
+ rb_scan_args(argc, argv, "02", &state, &depth);
461
+ state = cState_from_state_s(cState, state);
462
+ return cState_partial_generate(state, self, depth);
463
+ }
464
+
465
+ /*
466
+ * call-seq: to_json(*)
467
+ *
468
+ * Returns a JSON string representation for this Float number.
469
+ */
470
+ static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self)
471
+ {
472
+ VALUE state, depth;
473
+ rb_scan_args(argc, argv, "02", &state, &depth);
474
+ state = cState_from_state_s(cState, state);
475
+ return cState_partial_generate(state, self, depth);
476
+ }
477
+
478
+ /*
479
+ * call-seq: String.included(modul)
480
+ *
481
+ * Extends _modul_ with the String::Extend module.
482
+ */
483
+ static VALUE mString_included_s(VALUE self, VALUE modul) {
484
+ VALUE result = rb_funcall(modul, i_extend, 1, mString_Extend);
485
+ return result;
486
+ }
487
+
488
+ /*
489
+ * call-seq: to_json(*)
490
+ *
491
+ * This string should be encoded with UTF-8 A call to this method
492
+ * returns a JSON string encoded with UTF16 big endian characters as
493
+ * \u????.
494
+ */
495
+ static VALUE mString_to_json(int argc, VALUE *argv, VALUE self)
496
+ {
497
+ VALUE state, depth;
498
+ rb_scan_args(argc, argv, "02", &state, &depth);
499
+ state = cState_from_state_s(cState, state);
500
+ return cState_partial_generate(state, self, depth);
501
+ }
502
+
503
+ /*
504
+ * call-seq: to_json_raw_object()
505
+ *
506
+ * This method creates a raw object hash, that can be nested into
507
+ * other data structures and will be generated as a raw string. This
508
+ * method should be used, if you want to convert raw strings to JSON
509
+ * instead of UTF-8 strings, e. g. binary data.
510
+ */
511
+ static VALUE mString_to_json_raw_object(VALUE self)
512
+ {
513
+ VALUE ary;
514
+ VALUE result = rb_hash_new();
515
+ rb_hash_aset(result, rb_funcall(mJSON, i_create_id, 0), rb_class_name(rb_obj_class(self)));
516
+ ary = rb_funcall(self, i_unpack, 1, rb_str_new2("C*"));
517
+ rb_hash_aset(result, rb_str_new2("raw"), ary);
518
+ return result;
519
+ }
520
+
521
+ /*
522
+ * call-seq: to_json_raw(*args)
523
+ *
524
+ * This method creates a JSON text from the result of a call to
525
+ * to_json_raw_object of this String.
526
+ */
527
+ static VALUE mString_to_json_raw(int argc, VALUE *argv, VALUE self)
528
+ {
529
+ VALUE obj = mString_to_json_raw_object(self);
530
+ Check_Type(obj, T_HASH);
531
+ return mHash_to_json(argc, argv, obj);
532
+ }
533
+
534
+ /*
535
+ * call-seq: json_create(o)
536
+ *
537
+ * Raw Strings are JSON Objects (the raw bytes are stored in an array for the
538
+ * key "raw"). The Ruby String can be created by this module method.
539
+ */
540
+ static VALUE mString_Extend_json_create(VALUE self, VALUE o)
541
+ {
542
+ VALUE ary;
543
+ Check_Type(o, T_HASH);
544
+ ary = rb_hash_aref(o, rb_str_new2("raw"));
545
+ return rb_funcall(ary, i_pack, 1, rb_str_new2("C*"));
546
+ }
547
+
548
+ /*
549
+ * call-seq: to_json(*)
550
+ *
551
+ * Returns a JSON string for true: 'true'.
552
+ */
553
+ static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self)
554
+ {
555
+ VALUE state, depth;
556
+ rb_scan_args(argc, argv, "02", &state, &depth);
557
+ state = cState_from_state_s(cState, state);
558
+ return cState_partial_generate(state, self, depth);
559
+ }
560
+
561
+ /*
562
+ * call-seq: to_json(*)
563
+ *
564
+ * Returns a JSON string for false: 'false'.
565
+ */
566
+ static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self)
567
+ {
568
+ VALUE state, depth;
569
+ rb_scan_args(argc, argv, "02", &state, &depth);
570
+ state = cState_from_state_s(cState, state);
571
+ return cState_partial_generate(state, self, depth);
572
+ }
573
+
574
+ /*
575
+ * call-seq: to_json(*)
576
+ *
577
+ */
578
+ static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self)
579
+ {
580
+ VALUE state, depth;
581
+ rb_scan_args(argc, argv, "02", &state, &depth);
582
+ state = cState_from_state_s(cState, state);
583
+ return cState_partial_generate(state, self, depth);
584
+ }
585
+
586
+ /*
587
+ * call-seq: to_json(*)
588
+ *
589
+ * Converts this object to a string (calling #to_s), converts
590
+ * it to a JSON string, and returns the result. This is a fallback, if no
591
+ * special method #to_json was defined for some object.
592
+ */
593
+ static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self)
594
+ {
595
+ VALUE state, depth;
596
+ VALUE string = rb_funcall(self, i_to_s, 0);
597
+ rb_scan_args(argc, argv, "02", &state, &depth);
598
+ Check_Type(string, T_STRING);
599
+ state = cState_from_state_s(cState, state);
600
+ return cState_partial_generate(state, string, depth);
601
+ }
602
+
603
+ static void State_free(JSON_Generator_State *state)
604
+ {
605
+ if (state->indent) ruby_xfree(state->indent);
606
+ if (state->space) ruby_xfree(state->space);
607
+ if (state->space_before) ruby_xfree(state->space_before);
608
+ if (state->object_nl) ruby_xfree(state->object_nl);
609
+ if (state->array_nl) ruby_xfree(state->array_nl);
610
+ if (state->array_delim) fbuffer_free(state->array_delim);
611
+ if (state->object_delim) fbuffer_free(state->object_delim);
612
+ if (state->object_delim2) fbuffer_free(state->object_delim2);
613
+ ruby_xfree(state);
614
+ }
615
+
616
+ static JSON_Generator_State *State_allocate()
617
+ {
618
+ JSON_Generator_State *state = ALLOC(JSON_Generator_State);
619
+ return state;
620
+ }
621
+
622
+ static VALUE cState_s_allocate(VALUE klass)
623
+ {
624
+ JSON_Generator_State *state = State_allocate();
625
+ return Data_Wrap_Struct(klass, NULL, State_free, state);
626
+ }
627
+
628
+ /*
629
+ * call-seq: configure(opts)
630
+ *
631
+ * Configure this State instance with the Hash _opts_, and return
632
+ * itself.
633
+ */
634
+ static VALUE cState_configure(VALUE self, VALUE opts)
635
+ {
636
+ VALUE tmp;
637
+ GET_STATE(self);
638
+ tmp = rb_convert_type(opts, T_HASH, "Hash", "to_hash");
639
+ if (NIL_P(tmp)) tmp = rb_convert_type(opts, T_HASH, "Hash", "to_h");
640
+ if (NIL_P(tmp)) {
641
+ rb_raise(rb_eArgError, "opts has to be hash like or convertable into a hash");
642
+ }
643
+ opts = tmp;
644
+ tmp = rb_hash_aref(opts, ID2SYM(i_indent));
645
+ if (RTEST(tmp)) {
646
+ int len;
647
+ Check_Type(tmp, T_STRING);
648
+ len = RSTRING_LEN(tmp);
649
+ state->indent = fstrndup(RSTRING_PTR(tmp), len);
650
+ state->indent_len = len;
651
+ }
652
+ tmp = rb_hash_aref(opts, ID2SYM(i_space));
653
+ if (RTEST(tmp)) {
654
+ int len;
655
+ Check_Type(tmp, T_STRING);
656
+ len = RSTRING_LEN(tmp);
657
+ state->space = fstrndup(RSTRING_PTR(tmp), len);
658
+ state->space_len = len;
659
+ }
660
+ tmp = rb_hash_aref(opts, ID2SYM(i_space_before));
661
+ if (RTEST(tmp)) {
662
+ int len;
663
+ Check_Type(tmp, T_STRING);
664
+ len = RSTRING_LEN(tmp);
665
+ state->space_before = fstrndup(RSTRING_PTR(tmp), len);
666
+ state->space_before_len = len;
667
+ }
668
+ tmp = rb_hash_aref(opts, ID2SYM(i_array_nl));
669
+ if (RTEST(tmp)) {
670
+ int len;
671
+ Check_Type(tmp, T_STRING);
672
+ len = RSTRING_LEN(tmp);
673
+ state->array_nl = fstrndup(RSTRING_PTR(tmp), len);
674
+ state->array_nl_len = len;
675
+ }
676
+ tmp = rb_hash_aref(opts, ID2SYM(i_object_nl));
677
+ if (RTEST(tmp)) {
678
+ int len;
679
+ Check_Type(tmp, T_STRING);
680
+ len = RSTRING_LEN(tmp);
681
+ state->object_nl = fstrndup(RSTRING_PTR(tmp), len);
682
+ state->object_nl_len = len;
683
+ }
684
+ tmp = ID2SYM(i_max_nesting);
685
+ state->max_nesting = 19;
686
+ if (option_given_p(opts, tmp)) {
687
+ VALUE max_nesting = rb_hash_aref(opts, tmp);
688
+ if (RTEST(max_nesting)) {
689
+ Check_Type(max_nesting, T_FIXNUM);
690
+ state->max_nesting = FIX2LONG(max_nesting);
691
+ } else {
692
+ state->max_nesting = 0;
693
+ }
694
+ }
695
+ tmp = rb_hash_aref(opts, ID2SYM(i_allow_nan));
696
+ state->allow_nan = RTEST(tmp);
697
+ tmp = rb_hash_aref(opts, ID2SYM(i_ascii_only));
698
+ state->ascii_only = RTEST(tmp);
699
+ return self;
700
+ }
701
+
702
+ /*
703
+ * call-seq: to_h
704
+ *
705
+ * Returns the configuration instance variables as a hash, that can be
706
+ * passed to the configure method.
707
+ */
708
+ static VALUE cState_to_h(VALUE self)
709
+ {
710
+ VALUE result = rb_hash_new();
711
+ GET_STATE(self);
712
+ rb_hash_aset(result, ID2SYM(i_indent), rb_str_new(state->indent, state->indent_len));
713
+ rb_hash_aset(result, ID2SYM(i_space), rb_str_new(state->space, state->space_len));
714
+ rb_hash_aset(result, ID2SYM(i_space_before), rb_str_new(state->space_before, state->space_before_len));
715
+ rb_hash_aset(result, ID2SYM(i_object_nl), rb_str_new(state->object_nl, state->object_nl_len));
716
+ rb_hash_aset(result, ID2SYM(i_array_nl), rb_str_new(state->array_nl, state->array_nl_len));
717
+ rb_hash_aset(result, ID2SYM(i_allow_nan), state->allow_nan ? Qtrue : Qfalse);
718
+ rb_hash_aset(result, ID2SYM(i_ascii_only), state->ascii_only ? Qtrue : Qfalse);
719
+ rb_hash_aset(result, ID2SYM(i_max_nesting), LONG2FIX(state->max_nesting));
720
+ return result;
721
+ }
722
+
723
+ /*
724
+ * call-seq: [](name)
725
+ *
726
+ * Return the value returned by method +name+.
727
+ */
728
+ static VALUE cState_aref(VALUE self, VALUE name)
729
+ {
730
+ GET_STATE(self);
731
+ if (RTEST(rb_funcall(self, i_respond_to_p, 1, name))) {
732
+ return rb_funcall(self, i_send, 1, name);
733
+ } else {
734
+ return Qnil;
735
+ }
736
+ }
737
+
738
+ static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj, long depth)
739
+ {
740
+ VALUE tmp;
741
+ switch (TYPE(obj)) {
742
+ case T_HASH:
743
+ {
744
+ char *object_nl = state->object_nl;
745
+ long object_nl_len = state->object_nl_len;
746
+ char *indent = state->indent;
747
+ long indent_len = state->indent_len;
748
+ long max_nesting = state->max_nesting;
749
+ char *delim = FBUFFER_PTR(state->object_delim);
750
+ long delim_len = FBUFFER_LEN(state->object_delim);
751
+ char *delim2 = FBUFFER_PTR(state->object_delim2);
752
+ long delim2_len = FBUFFER_LEN(state->object_delim2);
753
+ int i, j;
754
+ VALUE key, key_to_s, keys;
755
+ depth++;
756
+ if (max_nesting != 0 && depth > max_nesting) {
757
+ fbuffer_free(buffer);
758
+ rb_raise(eNestingError, "nesting of %ld is too deep", depth);
759
+ }
760
+ fbuffer_append_char(buffer, '{');
761
+ keys = rb_funcall(obj, rb_intern("keys"), 0);
762
+ for(i = 0; i < RARRAY_LEN(keys); i++) {
763
+ if (i > 0) fbuffer_append(buffer, delim, delim_len);
764
+ if (object_nl) {
765
+ fbuffer_append(buffer, object_nl, object_nl_len);
766
+ }
767
+ if (indent) {
768
+ for (j = 0; j < depth; j++) {
769
+ fbuffer_append(buffer, indent, indent_len);
770
+ }
771
+ }
772
+ key = rb_ary_entry(keys, i);
773
+ key_to_s = rb_funcall(key, i_to_s, 0);
774
+ Check_Type(key_to_s, T_STRING);
775
+ generate_json(buffer, Vstate, state, key_to_s, depth);
776
+ fbuffer_append(buffer, delim2, delim2_len);
777
+ generate_json(buffer, Vstate, state, rb_hash_aref(obj, key), depth);
778
+ }
779
+ depth--;
780
+ if (object_nl) {
781
+ fbuffer_append(buffer, object_nl, object_nl_len);
782
+ if (indent) {
783
+ for (j = 0; j < depth; j++) {
784
+ fbuffer_append(buffer, indent, indent_len);
785
+ }
786
+ }
787
+ }
788
+ fbuffer_append_char(buffer, '}');
789
+ }
790
+ break;
791
+ case T_ARRAY:
792
+ {
793
+ char *array_nl = state->array_nl;
794
+ long array_nl_len = state->array_nl_len;
795
+ char *indent = state->indent;
796
+ long indent_len = state->indent_len;
797
+ long max_nesting = state->max_nesting;
798
+ char *delim = FBUFFER_PTR(state->array_delim);
799
+ long delim_len = FBUFFER_LEN(state->array_delim);
800
+ int i, j;
801
+ depth++;
802
+ if (max_nesting != 0 && depth > max_nesting) {
803
+ fbuffer_free(buffer);
804
+ rb_raise(eNestingError, "nesting of %ld is too deep", depth);
805
+ }
806
+ fbuffer_append_char(buffer, '[');
807
+ if (array_nl) fbuffer_append(buffer, array_nl, array_nl_len);
808
+ for(i = 0; i < RARRAY_LEN(obj); i++) {
809
+ if (i > 0) fbuffer_append(buffer, delim, delim_len);
810
+ if (indent) {
811
+ for (j = 0; j < depth; j++) {
812
+ fbuffer_append(buffer, indent, indent_len);
813
+ }
814
+ }
815
+ generate_json(buffer, Vstate, state, rb_ary_entry(obj, i), depth);
816
+ }
817
+ depth--;
818
+ if (array_nl) {
819
+ fbuffer_append(buffer, array_nl, array_nl_len);
820
+ if (indent) {
821
+ for (j = 0; j < depth; j++) {
822
+ fbuffer_append(buffer, indent, indent_len);
823
+ }
824
+ }
825
+ }
826
+ fbuffer_append_char(buffer, ']');
827
+ }
828
+ break;
829
+ case T_STRING:
830
+ fbuffer_append_char(buffer, '"');
831
+ #ifdef HAVE_RUBY_ENCODING_H
832
+ obj = rb_funcall(obj, i_encode, 1, CEncoding_UTF_8);
833
+ #endif
834
+ if (state->ascii_only) {
835
+ convert_UTF8_to_JSON_ASCII(buffer, obj);
836
+ } else {
837
+ convert_UTF8_to_JSON(buffer, obj);
838
+ }
839
+ fbuffer_append_char(buffer, '"');
840
+ break;
841
+ case T_NIL:
842
+ fbuffer_append(buffer, "null", 4);
843
+ break;
844
+ case T_FALSE:
845
+ fbuffer_append(buffer, "false", 5);
846
+ break;
847
+ case T_TRUE:
848
+ fbuffer_append(buffer, "true", 4);
849
+ break;
850
+ case T_FIXNUM:
851
+ fbuffer_append_long(buffer, FIX2LONG(obj));
852
+ break;
853
+ case T_BIGNUM:
854
+ tmp = rb_funcall(obj, i_to_s, 0);
855
+ fbuffer_append(buffer, RSTRING_PAIR(tmp));
856
+ break;
857
+ case T_FLOAT:
858
+ {
859
+ double value = RFLOAT_VALUE(obj);
860
+ char allow_nan = state->allow_nan;
861
+ tmp = rb_funcall(obj, i_to_s, 0);
862
+ if (!allow_nan) {
863
+ if (isinf(value)) {
864
+ fbuffer_free(buffer);
865
+ rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp));
866
+ } else if (isnan(value)) {
867
+ fbuffer_free(buffer);
868
+ rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp));
869
+ }
870
+ }
871
+ fbuffer_append(buffer, RSTRING_PAIR(tmp));
872
+ }
873
+ break;
874
+ default:
875
+ if (rb_respond_to(obj, i_to_json)) {
876
+ tmp = rb_funcall(obj, i_to_json, 2, Vstate, INT2FIX(depth + 1));
877
+ Check_Type(tmp, T_STRING);
878
+ fbuffer_append(buffer, RSTRING_PAIR(tmp));
879
+ } else {
880
+ tmp = rb_funcall(obj, i_to_s, 0);
881
+ Check_Type(tmp, T_STRING);
882
+ generate_json(buffer, Vstate, state, tmp, depth + 1);
883
+ }
884
+ break;
885
+ }
886
+ }
887
+
888
+ /*
889
+ * call-seq: partial_generate(obj)
890
+ *
891
+ * Generates a part of a JSON document from object +obj+ and returns the
892
+ * result.
893
+ */
894
+ static VALUE cState_partial_generate(VALUE self, VALUE obj, VALUE depth)
895
+ {
896
+ VALUE result;
897
+ FBuffer *buffer = fbuffer_alloc();
898
+ GET_STATE(self);
899
+
900
+ if (state->object_delim) {
901
+ fbuffer_clear(state->object_delim);
902
+ } else {
903
+ state->object_delim = fbuffer_alloc_with_length(16);
904
+ }
905
+ fbuffer_append_char(state->object_delim, ',');
906
+ if (state->object_delim2) {
907
+ fbuffer_clear(state->object_delim2);
908
+ } else {
909
+ state->object_delim2 = fbuffer_alloc_with_length(16);
910
+ }
911
+ fbuffer_append_char(state->object_delim2, ':');
912
+ if (state->space) fbuffer_append(state->object_delim2, state->space, state->space_len);
913
+
914
+ if (state->array_delim) {
915
+ fbuffer_clear(state->array_delim);
916
+ } else {
917
+ state->array_delim = fbuffer_alloc_with_length(16);
918
+ }
919
+ fbuffer_append_char(state->array_delim, ',');
920
+ if (state->array_nl) fbuffer_append(state->array_delim, state->array_nl, state->array_nl_len);
921
+
922
+ generate_json(buffer, self, state, obj, NIL_P(depth) ? 0 : FIX2INT(depth));
923
+ result = rb_str_new(FBUFFER_PAIR(buffer));
924
+ fbuffer_free(buffer);
925
+ FORCE_UTF8(result);
926
+ return result;
927
+ }
928
+
929
+ /*
930
+ * call-seq: generate(obj)
931
+ *
932
+ * Generates a valid JSON document from object +obj+ and returns the
933
+ * result. If no valid JSON document can be created this method raises a
934
+ * GeneratorError exception.
935
+ */
936
+ static VALUE cState_generate(VALUE self, VALUE obj)
937
+ {
938
+ VALUE result = cState_partial_generate(self, obj, Qnil);
939
+ VALUE re, args[2];
940
+ args[0] = rb_str_new2("\\A\\s*(?:\\[.*\\]|\\{.*\\})\\s*\\Z");
941
+ args[1] = CRegexp_MULTILINE;
942
+ re = rb_class_new_instance(2, args, rb_cRegexp);
943
+ if (NIL_P(rb_funcall(re, i_match, 1, result))) {
944
+ rb_raise(eGeneratorError, "only generation of JSON objects or arrays allowed");
945
+ }
946
+ return result;
947
+ }
948
+
949
+ /*
950
+ * call-seq: new(opts = {})
951
+ *
952
+ * Instantiates a new State object, configured by _opts_.
953
+ *
954
+ * _opts_ can have the following keys:
955
+ *
956
+ * * *indent*: a string used to indent levels (default: ''),
957
+ * * *space*: a string that is put after, a : or , delimiter (default: ''),
958
+ * * *space_before*: a string that is put before a : pair delimiter (default: ''),
959
+ * * *object_nl*: a string that is put at the end of a JSON object (default: ''),
960
+ * * *array_nl*: a string that is put at the end of a JSON array (default: ''),
961
+ * * *allow_nan*: true if NaN, Infinity, and -Infinity should be
962
+ * generated, otherwise an exception is thrown, if these values are
963
+ * encountered. This options defaults to false.
964
+ */
965
+ static VALUE cState_initialize(int argc, VALUE *argv, VALUE self)
966
+ {
967
+ VALUE opts;
968
+ GET_STATE(self);
969
+ MEMZERO(state, JSON_Generator_State, 1);
970
+ state->max_nesting = 19;
971
+ rb_scan_args(argc, argv, "01", &opts);
972
+ if (!NIL_P(opts)) cState_configure(self, opts);
973
+ return self;
974
+ }
975
+
976
+ /*
977
+ * call-seq: initialize_copy(orig)
978
+ *
979
+ * Initializes this object from orig if it to be duplicated/cloned and returns
980
+ * it.
981
+ */
982
+ static VALUE cState_init_copy(VALUE obj, VALUE orig)
983
+ {
984
+ JSON_Generator_State *objState, *origState;
985
+
986
+ Data_Get_Struct(obj, JSON_Generator_State, objState);
987
+ Data_Get_Struct(orig, JSON_Generator_State, origState);
988
+ if (!objState) rb_raise(rb_eArgError, "unallocated JSON::State");
989
+
990
+ MEMCPY(objState, origState, JSON_Generator_State, 1);
991
+ objState->indent = fstrndup(origState->indent, origState->indent_len);
992
+ objState->space = fstrndup(origState->space, origState->space_len);
993
+ objState->space_before = fstrndup(origState->space_before, origState->space_before_len);
994
+ objState->object_nl = fstrndup(origState->object_nl, origState->object_nl_len);
995
+ objState->array_nl = fstrndup(origState->array_nl, origState->array_nl_len);
996
+ if (origState->array_delim) objState->array_delim = fbuffer_dup(origState->array_delim);
997
+ if (origState->object_delim) objState->object_delim = fbuffer_dup(origState->object_delim);
998
+ if (origState->object_delim2) objState->object_delim2 = fbuffer_dup(origState->object_delim2);
999
+ return obj;
1000
+ }
1001
+
1002
+ /*
1003
+ * call-seq: from_state(opts)
1004
+ *
1005
+ * Creates a State object from _opts_, which ought to be Hash to create a
1006
+ * new State instance configured by _opts_, something else to create an
1007
+ * unconfigured instance. If _opts_ is a State object, it is just returned.
1008
+ */
1009
+ static VALUE cState_from_state_s(VALUE self, VALUE opts)
1010
+ {
1011
+ if (rb_obj_is_kind_of(opts, self)) {
1012
+ return opts;
1013
+ } else if (rb_obj_is_kind_of(opts, rb_cHash)) {
1014
+ return rb_funcall(self, i_new, 1, opts);
1015
+ } else {
1016
+ if (NIL_P(CJSON_SAFE_STATE_PROTOTYPE)) {
1017
+ CJSON_SAFE_STATE_PROTOTYPE = rb_const_get(mJSON, rb_intern("SAFE_STATE_PROTOTYPE"));
1018
+ }
1019
+ return CJSON_SAFE_STATE_PROTOTYPE;
1020
+ }
1021
+ }
1022
+
1023
+ /*
1024
+ * call-seq: indent()
1025
+ *
1026
+ * This string is used to indent levels in the JSON text.
1027
+ */
1028
+ static VALUE cState_indent(VALUE self)
1029
+ {
1030
+ GET_STATE(self);
1031
+ return state->indent ? rb_str_new2(state->indent) : rb_str_new2("");
1032
+ }
1033
+
1034
+ /*
1035
+ * call-seq: indent=(indent)
1036
+ *
1037
+ * This string is used to indent levels in the JSON text.
1038
+ */
1039
+ static VALUE cState_indent_set(VALUE self, VALUE indent)
1040
+ {
1041
+ GET_STATE(self);
1042
+ Check_Type(indent, T_STRING);
1043
+ if (RSTRING_LEN(indent) == 0) {
1044
+ if (state->indent) {
1045
+ ruby_xfree(state->indent);
1046
+ state->indent = NULL;
1047
+ }
1048
+ } else {
1049
+ if (state->indent) ruby_xfree(state->indent);
1050
+ state->indent = strdup(RSTRING_PTR(indent));
1051
+ }
1052
+ return Qnil;
1053
+ }
1054
+
1055
+ /*
1056
+ * call-seq: space()
1057
+ *
1058
+ * This string is used to insert a space between the tokens in a JSON
1059
+ * string.
1060
+ */
1061
+ static VALUE cState_space(VALUE self)
1062
+ {
1063
+ GET_STATE(self);
1064
+ return state->space ? rb_str_new2(state->space) : rb_str_new2("");
1065
+ }
1066
+
1067
+ /*
1068
+ * call-seq: space=(space)
1069
+ *
1070
+ * This string is used to insert a space between the tokens in a JSON
1071
+ * string.
1072
+ */
1073
+ static VALUE cState_space_set(VALUE self, VALUE space)
1074
+ {
1075
+ GET_STATE(self);
1076
+ Check_Type(space, T_STRING);
1077
+ if (RSTRING_LEN(space) == 0) {
1078
+ if (state->space) {
1079
+ ruby_xfree(state->space);
1080
+ state->space = NULL;
1081
+ }
1082
+ } else {
1083
+ if (state->space) ruby_xfree(state->space);
1084
+ state->space = strdup(RSTRING_PTR(space));
1085
+ }
1086
+ return Qnil;
1087
+ }
1088
+
1089
+ /*
1090
+ * call-seq: space_before()
1091
+ *
1092
+ * This string is used to insert a space before the ':' in JSON objects.
1093
+ */
1094
+ static VALUE cState_space_before(VALUE self)
1095
+ {
1096
+ GET_STATE(self);
1097
+ return state->space_before ? rb_str_new2(state->space_before) : rb_str_new2("");
1098
+ }
1099
+
1100
+ /*
1101
+ * call-seq: space_before=(space_before)
1102
+ *
1103
+ * This string is used to insert a space before the ':' in JSON objects.
1104
+ */
1105
+ static VALUE cState_space_before_set(VALUE self, VALUE space_before)
1106
+ {
1107
+ GET_STATE(self);
1108
+ Check_Type(space_before, T_STRING);
1109
+ if (RSTRING_LEN(space_before) == 0) {
1110
+ if (state->space_before) {
1111
+ ruby_xfree(state->space_before);
1112
+ state->space_before = NULL;
1113
+ }
1114
+ } else {
1115
+ if (state->space_before) ruby_xfree(state->space_before);
1116
+ state->space_before = strdup(RSTRING_PTR(space_before));
1117
+ }
1118
+ return Qnil;
1119
+ }
1120
+
1121
+ /*
1122
+ * call-seq: object_nl()
1123
+ *
1124
+ * This string is put at the end of a line that holds a JSON object (or
1125
+ * Hash).
1126
+ */
1127
+ static VALUE cState_object_nl(VALUE self)
1128
+ {
1129
+ GET_STATE(self);
1130
+ return state->object_nl ? rb_str_new2(state->object_nl) : rb_str_new2("");
1131
+ }
1132
+
1133
+ /*
1134
+ * call-seq: object_nl=(object_nl)
1135
+ *
1136
+ * This string is put at the end of a line that holds a JSON object (or
1137
+ * Hash).
1138
+ */
1139
+ static VALUE cState_object_nl_set(VALUE self, VALUE object_nl)
1140
+ {
1141
+ GET_STATE(self);
1142
+ Check_Type(object_nl, T_STRING);
1143
+ if (RSTRING_LEN(object_nl) == 0) {
1144
+ if (state->object_nl) {
1145
+ ruby_xfree(state->object_nl);
1146
+ state->object_nl = NULL;
1147
+ }
1148
+ } else {
1149
+ if (state->object_nl) ruby_xfree(state->object_nl);
1150
+ state->object_nl = strdup(RSTRING_PTR(object_nl));
1151
+ }
1152
+ return Qnil;
1153
+ }
1154
+
1155
+ /*
1156
+ * call-seq: array_nl()
1157
+ *
1158
+ * This string is put at the end of a line that holds a JSON array.
1159
+ */
1160
+ static VALUE cState_array_nl(VALUE self)
1161
+ {
1162
+ GET_STATE(self);
1163
+ return state->array_nl ? rb_str_new2(state->array_nl) : rb_str_new2("");
1164
+ }
1165
+
1166
+ /*
1167
+ * call-seq: array_nl=(array_nl)
1168
+ *
1169
+ * This string is put at the end of a line that holds a JSON array.
1170
+ */
1171
+ static VALUE cState_array_nl_set(VALUE self, VALUE array_nl)
1172
+ {
1173
+ GET_STATE(self);
1174
+ Check_Type(array_nl, T_STRING);
1175
+ if (RSTRING_LEN(array_nl) == 0) {
1176
+ if (state->array_nl) {
1177
+ ruby_xfree(state->array_nl);
1178
+ state->array_nl = NULL;
1179
+ }
1180
+ } else {
1181
+ if (state->array_nl) ruby_xfree(state->array_nl);
1182
+ state->array_nl = strdup(RSTRING_PTR(array_nl));
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
+ *
1251
+ */
1252
+ void Init_generator()
1253
+ {
1254
+ rb_require("json/common");
1255
+
1256
+ mJSON = rb_define_module("JSON");
1257
+ mExt = rb_define_module_under(mJSON, "Ext");
1258
+ mGenerator = rb_define_module_under(mExt, "Generator");
1259
+
1260
+ eGeneratorError = rb_path2class("JSON::GeneratorError");
1261
+ eNestingError = rb_path2class("JSON::NestingError");
1262
+
1263
+ cState = rb_define_class_under(mGenerator, "State", rb_cObject);
1264
+ rb_define_alloc_func(cState, cState_s_allocate);
1265
+ rb_define_singleton_method(cState, "from_state", cState_from_state_s, 1);
1266
+ rb_define_method(cState, "initialize", cState_initialize, -1);
1267
+ rb_define_method(cState, "initialize_copy", cState_init_copy, 1);
1268
+ rb_define_method(cState, "indent", cState_indent, 0);
1269
+ rb_define_method(cState, "indent=", cState_indent_set, 1);
1270
+ rb_define_method(cState, "space", cState_space, 0);
1271
+ rb_define_method(cState, "space=", cState_space_set, 1);
1272
+ rb_define_method(cState, "space_before", cState_space_before, 0);
1273
+ rb_define_method(cState, "space_before=", cState_space_before_set, 1);
1274
+ rb_define_method(cState, "object_nl", cState_object_nl, 0);
1275
+ rb_define_method(cState, "object_nl=", cState_object_nl_set, 1);
1276
+ rb_define_method(cState, "array_nl", cState_array_nl, 0);
1277
+ rb_define_method(cState, "array_nl=", cState_array_nl_set, 1);
1278
+ rb_define_method(cState, "max_nesting", cState_max_nesting, 0);
1279
+ rb_define_method(cState, "max_nesting=", cState_max_nesting_set, 1);
1280
+ rb_define_method(cState, "check_circular?", cState_check_circular_p, 0);
1281
+ rb_define_method(cState, "allow_nan?", cState_allow_nan_p, 0);
1282
+ rb_define_method(cState, "ascii_only?", cState_ascii_only_p, 0);
1283
+ rb_define_method(cState, "configure", cState_configure, 1);
1284
+ rb_define_method(cState, "to_h", cState_to_h, 0);
1285
+ rb_define_method(cState, "[]", cState_aref, 1);
1286
+ rb_define_method(cState, "generate", cState_generate, 1);
1287
+ rb_define_method(cState, "partial_generate", cState_partial_generate, 1);
1288
+
1289
+ mGeneratorMethods = rb_define_module_under(mGenerator, "GeneratorMethods");
1290
+ mObject = rb_define_module_under(mGeneratorMethods, "Object");
1291
+ rb_define_method(mObject, "to_json", mObject_to_json, -1);
1292
+ mHash = rb_define_module_under(mGeneratorMethods, "Hash");
1293
+ rb_define_method(mHash, "to_json", mHash_to_json, -1);
1294
+ mArray = rb_define_module_under(mGeneratorMethods, "Array");
1295
+ rb_define_method(mArray, "to_json", mArray_to_json, -1);
1296
+ mInteger = rb_define_module_under(mGeneratorMethods, "Integer");
1297
+ rb_define_method(mInteger, "to_json", mInteger_to_json, -1);
1298
+ mFloat = rb_define_module_under(mGeneratorMethods, "Float");
1299
+ rb_define_method(mFloat, "to_json", mFloat_to_json, -1);
1300
+ mString = rb_define_module_under(mGeneratorMethods, "String");
1301
+ rb_define_singleton_method(mString, "included", mString_included_s, 1);
1302
+ rb_define_method(mString, "to_json", mString_to_json, -1);
1303
+ rb_define_method(mString, "to_json_raw", mString_to_json_raw, -1);
1304
+ rb_define_method(mString, "to_json_raw_object", mString_to_json_raw_object, 0);
1305
+ mString_Extend = rb_define_module_under(mString, "Extend");
1306
+ rb_define_method(mString_Extend, "json_create", mString_Extend_json_create, 1);
1307
+ mTrueClass = rb_define_module_under(mGeneratorMethods, "TrueClass");
1308
+ rb_define_method(mTrueClass, "to_json", mTrueClass_to_json, -1);
1309
+ mFalseClass = rb_define_module_under(mGeneratorMethods, "FalseClass");
1310
+ rb_define_method(mFalseClass, "to_json", mFalseClass_to_json, -1);
1311
+ mNilClass = rb_define_module_under(mGeneratorMethods, "NilClass");
1312
+ rb_define_method(mNilClass, "to_json", mNilClass_to_json, -1);
1313
+
1314
+ CRegexp_MULTILINE = rb_const_get(rb_cRegexp, rb_intern("MULTILINE"));
1315
+ i_to_s = rb_intern("to_s");
1316
+ i_to_json = rb_intern("to_json");
1317
+ i_new = rb_intern("new");
1318
+ i_indent = rb_intern("indent");
1319
+ i_space = rb_intern("space");
1320
+ i_space_before = rb_intern("space_before");
1321
+ i_object_nl = rb_intern("object_nl");
1322
+ i_array_nl = rb_intern("array_nl");
1323
+ i_max_nesting = rb_intern("max_nesting");
1324
+ i_allow_nan = rb_intern("allow_nan");
1325
+ i_ascii_only = rb_intern("ascii_only");
1326
+ i_pack = rb_intern("pack");
1327
+ i_unpack = rb_intern("unpack");
1328
+ i_create_id = rb_intern("create_id");
1329
+ i_extend = rb_intern("extend");
1330
+ i_key_p = rb_intern("key?");
1331
+ i_aref = rb_intern("[]");
1332
+ i_send = rb_intern("__send__");
1333
+ i_respond_to_p = rb_intern("respond_to?");
1334
+ i_match = rb_intern("match");
1335
+ #ifdef HAVE_RUBY_ENCODING_H
1336
+ CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
1337
+ i_encoding = rb_intern("encoding");
1338
+ i_encode = rb_intern("encode");
1339
+ #endif
1340
+ CJSON_SAFE_STATE_PROTOTYPE = Qnil;
1341
+ }