oj 2.18.5 → 3.16.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +1452 -0
  3. data/README.md +53 -221
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +54 -72
  6. data/ext/oj/cache.c +329 -0
  7. data/ext/oj/cache.h +22 -0
  8. data/ext/oj/cache8.c +61 -63
  9. data/ext/oj/cache8.h +12 -39
  10. data/ext/oj/circarray.c +38 -67
  11. data/ext/oj/circarray.h +16 -42
  12. data/ext/oj/code.c +214 -0
  13. data/ext/oj/code.h +40 -0
  14. data/ext/oj/compat.c +194 -110
  15. data/ext/oj/custom.c +1074 -0
  16. data/ext/oj/debug.c +126 -0
  17. data/ext/oj/dump.c +1276 -2494
  18. data/ext/oj/dump.h +110 -0
  19. data/ext/oj/dump_compat.c +897 -0
  20. data/ext/oj/dump_leaf.c +162 -0
  21. data/ext/oj/dump_object.c +710 -0
  22. data/ext/oj/dump_strict.c +399 -0
  23. data/ext/oj/encode.h +7 -42
  24. data/ext/oj/encoder.c +43 -0
  25. data/ext/oj/err.c +28 -53
  26. data/ext/oj/err.h +49 -46
  27. data/ext/oj/extconf.rb +33 -32
  28. data/ext/oj/fast.c +1082 -1098
  29. data/ext/oj/intern.c +313 -0
  30. data/ext/oj/intern.h +22 -0
  31. data/ext/oj/mem.c +318 -0
  32. data/ext/oj/mem.h +53 -0
  33. data/ext/oj/mimic_json.c +919 -0
  34. data/ext/oj/object.c +545 -625
  35. data/ext/oj/odd.c +158 -168
  36. data/ext/oj/odd.h +32 -58
  37. data/ext/oj/oj.c +1727 -2080
  38. data/ext/oj/oj.h +334 -259
  39. data/ext/oj/parse.c +974 -753
  40. data/ext/oj/parse.h +97 -90
  41. data/ext/oj/parser.c +1600 -0
  42. data/ext/oj/parser.h +103 -0
  43. data/ext/oj/rails.c +1478 -0
  44. data/ext/oj/rails.h +18 -0
  45. data/ext/oj/reader.c +136 -163
  46. data/ext/oj/reader.h +76 -112
  47. data/ext/oj/resolve.c +45 -94
  48. data/ext/oj/resolve.h +7 -34
  49. data/ext/oj/rxclass.c +144 -0
  50. data/ext/oj/rxclass.h +26 -0
  51. data/ext/oj/saj.c +445 -511
  52. data/ext/oj/saj2.c +584 -0
  53. data/ext/oj/saj2.h +23 -0
  54. data/ext/oj/scp.c +82 -143
  55. data/ext/oj/simd.h +10 -0
  56. data/ext/oj/sparse.c +749 -644
  57. data/ext/oj/stream_writer.c +329 -0
  58. data/ext/oj/strict.c +114 -112
  59. data/ext/oj/string_writer.c +517 -0
  60. data/ext/oj/trace.c +72 -0
  61. data/ext/oj/trace.h +55 -0
  62. data/ext/oj/usual.c +1218 -0
  63. data/ext/oj/usual.h +69 -0
  64. data/ext/oj/util.c +136 -0
  65. data/ext/oj/util.h +20 -0
  66. data/ext/oj/val_stack.c +75 -72
  67. data/ext/oj/val_stack.h +94 -127
  68. data/ext/oj/validate.c +46 -0
  69. data/ext/oj/wab.c +586 -0
  70. data/lib/oj/active_support_helper.rb +1 -3
  71. data/lib/oj/bag.rb +8 -1
  72. data/lib/oj/easy_hash.rb +21 -13
  73. data/lib/oj/error.rb +10 -12
  74. data/lib/oj/json.rb +188 -0
  75. data/lib/oj/mimic.rb +165 -26
  76. data/lib/oj/saj.rb +20 -6
  77. data/lib/oj/schandler.rb +5 -4
  78. data/lib/oj/state.rb +135 -0
  79. data/lib/oj/version.rb +2 -3
  80. data/lib/oj.rb +3 -31
  81. data/pages/Advanced.md +22 -0
  82. data/pages/Compatibility.md +25 -0
  83. data/pages/Custom.md +23 -0
  84. data/pages/Encoding.md +65 -0
  85. data/pages/InstallOptions.md +20 -0
  86. data/pages/JsonGem.md +94 -0
  87. data/pages/Modes.md +161 -0
  88. data/pages/Options.md +337 -0
  89. data/pages/Parser.md +309 -0
  90. data/pages/Rails.md +167 -0
  91. data/pages/Security.md +20 -0
  92. data/pages/WAB.md +13 -0
  93. metadata +126 -163
  94. data/ext/oj/hash.c +0 -163
  95. data/ext/oj/hash.h +0 -46
  96. data/ext/oj/hash_test.c +0 -512
  97. data/test/_test_active.rb +0 -76
  98. data/test/_test_active_mimic.rb +0 -96
  99. data/test/_test_mimic_rails.rb +0 -126
  100. data/test/activesupport_datetime_test.rb +0 -23
  101. data/test/bug.rb +0 -51
  102. data/test/bug2.rb +0 -10
  103. data/test/bug3.rb +0 -46
  104. data/test/bug_fast.rb +0 -32
  105. data/test/bug_load.rb +0 -24
  106. data/test/crash.rb +0 -111
  107. data/test/curl/curl_oj.rb +0 -46
  108. data/test/curl/get_oj.rb +0 -24
  109. data/test/curl/just_curl.rb +0 -31
  110. data/test/curl/just_oj.rb +0 -51
  111. data/test/example.rb +0 -11
  112. data/test/files.rb +0 -29
  113. data/test/foo.rb +0 -24
  114. data/test/helper.rb +0 -27
  115. data/test/io.rb +0 -48
  116. data/test/isolated/shared.rb +0 -310
  117. data/test/isolated/test_mimic_after.rb +0 -13
  118. data/test/isolated/test_mimic_alone.rb +0 -12
  119. data/test/isolated/test_mimic_as_json.rb +0 -45
  120. data/test/isolated/test_mimic_before.rb +0 -13
  121. data/test/isolated/test_mimic_define.rb +0 -28
  122. data/test/isolated/test_mimic_rails_after.rb +0 -22
  123. data/test/isolated/test_mimic_rails_before.rb +0 -21
  124. data/test/isolated/test_mimic_rails_datetime.rb +0 -27
  125. data/test/isolated/test_mimic_redefine.rb +0 -15
  126. data/test/mod.rb +0 -16
  127. data/test/perf.rb +0 -107
  128. data/test/perf_compat.rb +0 -128
  129. data/test/perf_fast.rb +0 -164
  130. data/test/perf_file.rb +0 -64
  131. data/test/perf_object.rb +0 -138
  132. data/test/perf_saj.rb +0 -109
  133. data/test/perf_scp.rb +0 -151
  134. data/test/perf_simple.rb +0 -287
  135. data/test/perf_strict.rb +0 -128
  136. data/test/rails.rb +0 -50
  137. data/test/russian.rb +0 -18
  138. data/test/sample/change.rb +0 -14
  139. data/test/sample/dir.rb +0 -19
  140. data/test/sample/doc.rb +0 -36
  141. data/test/sample/file.rb +0 -48
  142. data/test/sample/group.rb +0 -16
  143. data/test/sample/hasprops.rb +0 -16
  144. data/test/sample/layer.rb +0 -12
  145. data/test/sample/line.rb +0 -20
  146. data/test/sample/oval.rb +0 -10
  147. data/test/sample/rect.rb +0 -10
  148. data/test/sample/shape.rb +0 -35
  149. data/test/sample/text.rb +0 -20
  150. data/test/sample.rb +0 -55
  151. data/test/sample_json.rb +0 -37
  152. data/test/struct.rb +0 -29
  153. data/test/test_compat.rb +0 -398
  154. data/test/test_debian.rb +0 -53
  155. data/test/test_fast.rb +0 -458
  156. data/test/test_file.rb +0 -245
  157. data/test/test_gc.rb +0 -49
  158. data/test/test_hash.rb +0 -29
  159. data/test/test_object.rb +0 -745
  160. data/test/test_saj.rb +0 -186
  161. data/test/test_scp.rb +0 -396
  162. data/test/test_serializer.rb +0 -59
  163. data/test/test_strict.rb +0 -254
  164. data/test/test_various.rb +0 -1383
  165. data/test/test_writer.rb +0 -308
  166. data/test/write_timebars.rb +0 -31
@@ -0,0 +1,517 @@
1
+ // Copyright (c) 2012, 2017 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
3
+
4
+ #include "dump.h"
5
+ #include "encode.h"
6
+ #include "mem.h"
7
+
8
+ extern VALUE Oj;
9
+
10
+ bool string_writer_optimized = false;
11
+
12
+ static void key_check(StrWriter sw, const char *key) {
13
+ DumpType type = sw->types[sw->depth];
14
+
15
+ if (0 == key && (ObjectNew == type || ObjectType == type)) {
16
+ rb_raise(rb_eStandardError, "Can not push onto an Object without a key.");
17
+ }
18
+ }
19
+
20
+ static void push_type(StrWriter sw, DumpType type) {
21
+ if (sw->types_end <= sw->types + sw->depth + 1) {
22
+ size_t size = (sw->types_end - sw->types) * 2;
23
+
24
+ OJ_R_REALLOC_N(sw->types, char, size);
25
+ sw->types_end = sw->types + size;
26
+ }
27
+ sw->depth++;
28
+ sw->types[sw->depth] = type;
29
+ }
30
+
31
+ static void maybe_comma(StrWriter sw) {
32
+ switch (sw->types[sw->depth]) {
33
+ case ObjectNew: sw->types[sw->depth] = ObjectType; break;
34
+ case ArrayNew: sw->types[sw->depth] = ArrayType; break;
35
+ case ObjectType:
36
+ case ArrayType:
37
+ // Always have a few characters available in the out.buf.
38
+ *sw->out.cur++ = ',';
39
+ break;
40
+ }
41
+ }
42
+
43
+ // Used by stream writer also.
44
+ void oj_str_writer_init(StrWriter sw, int buf_size) {
45
+ sw->opts = oj_default_options;
46
+ sw->depth = 0;
47
+ sw->types = OJ_R_ALLOC_N(char, 256);
48
+ sw->types_end = sw->types + 256;
49
+ *sw->types = '\0';
50
+ sw->keyWritten = 0;
51
+
52
+ if (0 == buf_size) {
53
+ buf_size = 4096;
54
+ } else if (buf_size < 1024) {
55
+ buf_size = 1024;
56
+ }
57
+ // Must be allocated. Using the out.stack_buffer results in double frees
58
+ // and I haven't figured out why yet.
59
+ sw->out.buf = OJ_R_ALLOC_N(char, buf_size);
60
+ sw->out.cur = sw->out.buf;
61
+ sw->out.end = sw->out.buf + buf_size - BUFFER_EXTRA;
62
+ sw->out.allocated = true;
63
+
64
+ *sw->out.cur = '\0';
65
+ sw->out.circ_cache = NULL;
66
+ sw->out.circ_cnt = 0;
67
+ sw->out.hash_cnt = 0;
68
+ sw->out.opts = &sw->opts;
69
+ sw->out.indent = sw->opts.indent;
70
+ sw->out.depth = 0;
71
+ sw->out.argc = 0;
72
+ sw->out.argv = NULL;
73
+ sw->out.ropts = NULL;
74
+ sw->out.omit_nil = oj_default_options.dump_opts.omit_nil;
75
+ }
76
+
77
+ void oj_str_writer_push_key(StrWriter sw, const char *key) {
78
+ DumpType type = sw->types[sw->depth];
79
+ long size;
80
+
81
+ if (sw->keyWritten) {
82
+ rb_raise(rb_eStandardError, "Can not push more than one key before pushing a non-key.");
83
+ }
84
+ if (ObjectNew != type && ObjectType != type) {
85
+ rb_raise(rb_eStandardError, "Can only push a key onto an Object.");
86
+ }
87
+ size = sw->depth * sw->out.indent + 3;
88
+ assure_size(&sw->out, size);
89
+ maybe_comma(sw);
90
+ if (0 < sw->depth) {
91
+ fill_indent(&sw->out, sw->depth);
92
+ }
93
+ oj_dump_cstr(key, strlen(key), 0, 0, &sw->out);
94
+ *sw->out.cur++ = ':';
95
+ sw->keyWritten = 1;
96
+ }
97
+
98
+ void oj_str_writer_push_object(StrWriter sw, const char *key) {
99
+ if (sw->keyWritten) {
100
+ sw->keyWritten = 0;
101
+ assure_size(&sw->out, 1);
102
+ } else {
103
+ long size;
104
+
105
+ key_check(sw, key);
106
+ size = sw->depth * sw->out.indent + 3;
107
+ assure_size(&sw->out, size);
108
+ maybe_comma(sw);
109
+ if (0 < sw->depth) {
110
+ fill_indent(&sw->out, sw->depth);
111
+ }
112
+ if (0 != key) {
113
+ oj_dump_cstr(key, strlen(key), 0, 0, &sw->out);
114
+ *sw->out.cur++ = ':';
115
+ }
116
+ }
117
+ *sw->out.cur++ = '{';
118
+ push_type(sw, ObjectNew);
119
+ }
120
+
121
+ void oj_str_writer_push_array(StrWriter sw, const char *key) {
122
+ if (sw->keyWritten) {
123
+ sw->keyWritten = 0;
124
+ assure_size(&sw->out, 1);
125
+ } else {
126
+ long size;
127
+
128
+ key_check(sw, key);
129
+ size = sw->depth * sw->out.indent + 3;
130
+ assure_size(&sw->out, size);
131
+ maybe_comma(sw);
132
+ if (0 < sw->depth) {
133
+ fill_indent(&sw->out, sw->depth);
134
+ }
135
+ if (0 != key) {
136
+ oj_dump_cstr(key, strlen(key), 0, 0, &sw->out);
137
+ *sw->out.cur++ = ':';
138
+ }
139
+ }
140
+ *sw->out.cur++ = '[';
141
+ push_type(sw, ArrayNew);
142
+ }
143
+
144
+ void oj_str_writer_push_value(StrWriter sw, VALUE val, const char *key) {
145
+ Out out = &sw->out;
146
+
147
+ if (sw->keyWritten) {
148
+ sw->keyWritten = 0;
149
+ } else {
150
+ long size;
151
+
152
+ key_check(sw, key);
153
+ size = sw->depth * out->indent + 3;
154
+ assure_size(out, size);
155
+ maybe_comma(sw);
156
+ if (0 < sw->depth) {
157
+ fill_indent(&sw->out, sw->depth);
158
+ }
159
+ if (0 != key) {
160
+ oj_dump_cstr(key, strlen(key), 0, 0, out);
161
+ *out->cur++ = ':';
162
+ }
163
+ }
164
+ switch (out->opts->mode) {
165
+ case StrictMode: oj_dump_strict_val(val, sw->depth, out); break;
166
+ case NullMode: oj_dump_null_val(val, sw->depth, out); break;
167
+ case ObjectMode: oj_dump_obj_val(val, sw->depth, out); break;
168
+ case CompatMode: oj_dump_compat_val(val, sw->depth, out, Yes == out->opts->to_json); break;
169
+ case RailsMode: oj_dump_rails_val(val, sw->depth, out); break;
170
+ case CustomMode: oj_dump_custom_val(val, sw->depth, out, true); break;
171
+ default: oj_dump_custom_val(val, sw->depth, out, true); break;
172
+ }
173
+ }
174
+
175
+ void oj_str_writer_push_json(StrWriter sw, const char *json, const char *key) {
176
+ if (sw->keyWritten) {
177
+ sw->keyWritten = 0;
178
+ } else {
179
+ long size;
180
+
181
+ key_check(sw, key);
182
+ size = sw->depth * sw->out.indent + 3;
183
+ assure_size(&sw->out, size);
184
+ maybe_comma(sw);
185
+ if (0 < sw->depth) {
186
+ fill_indent(&sw->out, sw->depth);
187
+ }
188
+ if (0 != key) {
189
+ oj_dump_cstr(key, strlen(key), 0, 0, &sw->out);
190
+ *sw->out.cur++ = ':';
191
+ }
192
+ }
193
+ oj_dump_raw(json, strlen(json), &sw->out);
194
+ }
195
+
196
+ void oj_str_writer_pop(StrWriter sw) {
197
+ long size;
198
+ DumpType type = sw->types[sw->depth];
199
+
200
+ if (sw->keyWritten) {
201
+ sw->keyWritten = 0;
202
+ rb_raise(rb_eStandardError, "Can not pop after writing a key but no value.");
203
+ }
204
+ sw->depth--;
205
+ if (0 > sw->depth) {
206
+ rb_raise(rb_eStandardError, "Can not pop with no open array or object.");
207
+ }
208
+ size = sw->depth * sw->out.indent + 2;
209
+ assure_size(&sw->out, size);
210
+ fill_indent(&sw->out, sw->depth);
211
+ switch (type) {
212
+ case ObjectNew:
213
+ case ObjectType: *sw->out.cur++ = '}'; break;
214
+ case ArrayNew:
215
+ case ArrayType: *sw->out.cur++ = ']'; break;
216
+ }
217
+ if (0 == sw->depth && 0 <= sw->out.indent) {
218
+ *sw->out.cur++ = '\n';
219
+ }
220
+ }
221
+
222
+ void oj_str_writer_pop_all(StrWriter sw) {
223
+ while (0 < sw->depth) {
224
+ oj_str_writer_pop(sw);
225
+ }
226
+ }
227
+
228
+ static void string_writer_free(void *ptr) {
229
+ StrWriter sw;
230
+
231
+ if (0 == ptr) {
232
+ return;
233
+ }
234
+ sw = (StrWriter)ptr;
235
+
236
+ oj_out_free(&sw->out);
237
+
238
+ OJ_R_FREE(sw->types);
239
+ OJ_R_FREE(ptr);
240
+ }
241
+
242
+ static const rb_data_type_t oj_string_writer_type = {
243
+ "Oj/string_writer",
244
+ {
245
+ NULL,
246
+ string_writer_free,
247
+ NULL,
248
+ },
249
+ 0,
250
+ 0,
251
+ };
252
+
253
+ StrWriter oj_str_writer_unwrap(VALUE writer) {
254
+ StrWriter sw;
255
+ TypedData_Get_Struct(writer, struct _strWriter, &oj_string_writer_type, sw);
256
+ return sw;
257
+ }
258
+
259
+ /* Document-method: new
260
+ * call-seq: new(io, options)
261
+ *
262
+ * Creates a new StringWriter. Options are supported according the the
263
+ * specified mode or the mode in the default options. Note that if mimic_JSON
264
+ * or Oj.optimize_rails has not been called then the behavior of the modes may
265
+ * not be the same as if they were.
266
+ *
267
+ * In addition to the regular dump options for the various modes a
268
+ * _:buffer_size_ option is available. It should be set to a positive
269
+ * integer. It is considered a hint of how large the initial internal buffer
270
+ * should be.
271
+ *
272
+ * - *io* [_IO_] stream to write to
273
+ * - *options* [_Hash_] formatting options
274
+ */
275
+ static VALUE str_writer_new(int argc, VALUE *argv, VALUE self) {
276
+ StrWriter sw = OJ_R_ALLOC(struct _strWriter);
277
+
278
+ oj_str_writer_init(sw, 0);
279
+ if (1 == argc) {
280
+ oj_parse_options(argv[0], &sw->opts);
281
+ }
282
+ sw->out.argc = argc - 1;
283
+ sw->out.argv = argv + 1;
284
+ sw->out.indent = sw->opts.indent;
285
+
286
+ return TypedData_Wrap_Struct(oj_string_writer_class, &oj_string_writer_type, sw);
287
+ }
288
+
289
+ /* Document-method: push_key
290
+ * call-seq: push_key(key)
291
+ *
292
+ * Pushes a key onto the JSON document. The key will be used for the next push
293
+ * if currently in a JSON object and ignored otherwise. If a key is provided on
294
+ * the next push then that new key will be ignored.
295
+ * - *key* [_String_] the key pending for the next push
296
+ */
297
+ static VALUE str_writer_push_key(VALUE self, VALUE key) {
298
+ StrWriter sw;
299
+ TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);
300
+
301
+ oj_str_writer_push_key(sw, StringValuePtr(key));
302
+
303
+ return Qnil;
304
+ }
305
+
306
+ /* Document-method: push_object
307
+ * call-seq: push_object(key=nil)
308
+ *
309
+ * Pushes an object onto the JSON document. Future pushes will be to this object
310
+ * until a pop() is called.
311
+ * - *key* [_String_] the key if adding to an object in the JSON document
312
+ */
313
+ static VALUE str_writer_push_object(int argc, VALUE *argv, VALUE self) {
314
+ StrWriter sw;
315
+ TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);
316
+
317
+ switch (argc) {
318
+ case 0: oj_str_writer_push_object(sw, 0); break;
319
+ case 1:
320
+ if (Qnil == argv[0]) {
321
+ oj_str_writer_push_object(sw, 0);
322
+ } else {
323
+ oj_str_writer_push_object(sw, StringValuePtr(argv[0]));
324
+ }
325
+ break;
326
+ default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'."); break;
327
+ }
328
+ if (rb_block_given_p()) {
329
+ rb_yield(Qnil);
330
+ oj_str_writer_pop(sw);
331
+ }
332
+ return Qnil;
333
+ }
334
+
335
+ /* Document-method: push_array
336
+ * call-seq: push_array(key=nil)
337
+ *
338
+ * Pushes an array onto the JSON document. Future pushes will be to this object
339
+ * until a pop() is called.
340
+ * - *key* [_String_] the key if adding to an object in the JSON document
341
+ */
342
+ static VALUE str_writer_push_array(int argc, VALUE *argv, VALUE self) {
343
+ StrWriter sw;
344
+ TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);
345
+
346
+ switch (argc) {
347
+ case 0: oj_str_writer_push_array(sw, 0); break;
348
+ case 1:
349
+ if (Qnil == argv[0]) {
350
+ oj_str_writer_push_array(sw, 0);
351
+ } else {
352
+ oj_str_writer_push_array(sw, StringValuePtr(argv[0]));
353
+ }
354
+ break;
355
+ default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'."); break;
356
+ }
357
+ if (rb_block_given_p()) {
358
+ rb_yield(Qnil);
359
+ oj_str_writer_pop(sw);
360
+ }
361
+ return Qnil;
362
+ }
363
+
364
+ /* Document-method: push_value
365
+ * call-seq: push_value(value, key=nil)
366
+ *
367
+ * Pushes a value onto the JSON document.
368
+ * - *value* [_Object_] value to add to the JSON document
369
+ * - *key* [_String_] the key if adding to an object in the JSON document
370
+ */
371
+ static VALUE str_writer_push_value(int argc, VALUE *argv, VALUE self) {
372
+ StrWriter sw;
373
+ TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);
374
+
375
+ switch (argc) {
376
+ case 1: oj_str_writer_push_value(sw, *argv, 0); break;
377
+ case 2:
378
+ if (Qnil == argv[1]) {
379
+ oj_str_writer_push_value(sw, *argv, 0);
380
+ } else {
381
+ oj_str_writer_push_value(sw, *argv, StringValuePtr(argv[1]));
382
+ }
383
+ break;
384
+ default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_value'."); break;
385
+ }
386
+ return Qnil;
387
+ }
388
+
389
+ /* Document-method: push_json
390
+ * call-seq: push_json(value, key=nil)
391
+ *
392
+ * Pushes a string onto the JSON document. The String must be a valid JSON
393
+ * encoded string. No additional checking is done to verify the validity of the
394
+ * string.
395
+ * - *value* [_Object_] value to add to the JSON document
396
+ * - *key* [_String_] the key if adding to an object in the JSON document
397
+ */
398
+ static VALUE str_writer_push_json(int argc, VALUE *argv, VALUE self) {
399
+ StrWriter sw;
400
+ TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);
401
+
402
+ switch (argc) {
403
+ case 1: oj_str_writer_push_json(sw, StringValuePtr(*argv), 0); break;
404
+ case 2:
405
+ if (Qnil == argv[1]) {
406
+ oj_str_writer_push_json(sw, StringValuePtr(*argv), 0);
407
+ } else {
408
+ oj_str_writer_push_json(sw, StringValuePtr(*argv), StringValuePtr(argv[1]));
409
+ }
410
+ break;
411
+ default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_json'."); break;
412
+ }
413
+ return Qnil;
414
+ }
415
+ /* Document-method: pop
416
+ * call-seq: pop()
417
+ *
418
+ * Pops up a level in the JSON document closing the array or object that is
419
+ * currently open.
420
+ */
421
+ static VALUE str_writer_pop(VALUE self) {
422
+ StrWriter sw;
423
+ TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);
424
+
425
+ oj_str_writer_pop(sw);
426
+ return Qnil;
427
+ }
428
+
429
+ /* Document-method: pop_all
430
+ * call-seq: pop_all()
431
+ *
432
+ * Pops all level in the JSON document closing all the array or object that is
433
+ * currently open.
434
+ */
435
+ static VALUE str_writer_pop_all(VALUE self) {
436
+ StrWriter sw;
437
+ TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);
438
+
439
+ oj_str_writer_pop_all(sw);
440
+
441
+ return Qnil;
442
+ }
443
+
444
+ /* Document-method: reset
445
+ * call-seq: reset()
446
+ *
447
+ * Reset the writer back to the empty state.
448
+ */
449
+ static VALUE str_writer_reset(VALUE self) {
450
+ StrWriter sw;
451
+ TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);
452
+
453
+ sw->depth = 0;
454
+ *sw->types = '\0';
455
+ sw->keyWritten = 0;
456
+ sw->out.cur = sw->out.buf;
457
+ *sw->out.cur = '\0';
458
+
459
+ return Qnil;
460
+ }
461
+
462
+ /* Document-method: to_s
463
+ * call-seq: to_s()
464
+ *
465
+ * Returns the JSON document string in what ever state the construction is at.
466
+ *
467
+ * *return* [_String_]
468
+ */
469
+ static VALUE str_writer_to_s(VALUE self) {
470
+ StrWriter sw;
471
+ TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);
472
+ return rb_utf8_str_new(sw->out.buf, sw->out.cur - sw->out.buf);
473
+ }
474
+
475
+ /* Document-method: as_json
476
+ * call-seq: as_json(*)
477
+ *
478
+ * Returns the contents of the writer as a JSON element. If called from inside
479
+ * an array or hash by Oj the raw buffer will be used othersize a more
480
+ * inefficient parse of the contents and a return of the result is
481
+ * completed. The parse uses the strict mode. Optional arguments are ignored.
482
+ *
483
+ * *return* [_Hash_|_Array_|_String_|_Integer_|_Float_|_True_|_False_|_nil|)
484
+ */
485
+ static VALUE str_writer_as_json(int argc, VALUE *argv, VALUE self) {
486
+ if (string_writer_optimized) {
487
+ return self;
488
+ }
489
+ return rb_hash_new();
490
+ }
491
+
492
+ /* Document-class: Oj::StringWriter
493
+ *
494
+ * Supports building a JSON document one element at a time. Build the document
495
+ * by pushing values into the document. Pushing an array or an object will
496
+ * create that element in the JSON document and subsequent pushes will add the
497
+ * elements to that array or object until a pop() is called. When complete
498
+ * calling to_s() will return the JSON document. Note that calling to_s() before
499
+ * construction is complete will return the document in it's current state.
500
+ */
501
+ void oj_string_writer_init(void) {
502
+ oj_string_writer_class = rb_define_class_under(Oj, "StringWriter", rb_cObject);
503
+ rb_gc_register_address(&oj_string_writer_class);
504
+ rb_undef_alloc_func(oj_string_writer_class);
505
+ rb_define_module_function(oj_string_writer_class, "new", str_writer_new, -1);
506
+ rb_define_method(oj_string_writer_class, "push_key", str_writer_push_key, 1);
507
+ rb_define_method(oj_string_writer_class, "push_object", str_writer_push_object, -1);
508
+ rb_define_method(oj_string_writer_class, "push_array", str_writer_push_array, -1);
509
+ rb_define_method(oj_string_writer_class, "push_value", str_writer_push_value, -1);
510
+ rb_define_method(oj_string_writer_class, "push_json", str_writer_push_json, -1);
511
+ rb_define_method(oj_string_writer_class, "pop", str_writer_pop, 0);
512
+ rb_define_method(oj_string_writer_class, "pop_all", str_writer_pop_all, 0);
513
+ rb_define_method(oj_string_writer_class, "reset", str_writer_reset, 0);
514
+ rb_define_method(oj_string_writer_class, "to_s", str_writer_to_s, 0);
515
+ rb_define_method(oj_string_writer_class, "raw_json", str_writer_to_s, 0);
516
+ rb_define_method(oj_string_writer_class, "as_json", str_writer_as_json, -1);
517
+ }
data/ext/oj/trace.c ADDED
@@ -0,0 +1,72 @@
1
+ // Copyright (c) 2018 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
3
+
4
+ #include "trace.h"
5
+
6
+ #include "parse.h"
7
+
8
+ #define MAX_INDENT 256
9
+
10
+ static void fill_indent(char *indent, int depth) {
11
+ if (MAX_INDENT <= depth) {
12
+ depth = MAX_INDENT - 1;
13
+ } else if (depth < 0) {
14
+ depth = 0;
15
+ }
16
+ if (0 < depth) {
17
+ memset(indent, ' ', depth);
18
+ }
19
+ indent[depth] = '\0';
20
+ }
21
+
22
+ void oj_trace(const char *func, VALUE obj, const char *file, int line, int depth, TraceWhere where) {
23
+ char fmt[64];
24
+ char indent[MAX_INDENT];
25
+
26
+ depth *= 2;
27
+ fill_indent(indent, depth);
28
+ sprintf(fmt, "#0:%%13s:%%3d:Oj:%c:%%%ds %%s %%s\n", where, depth);
29
+ printf(fmt, file, line, indent, func, rb_obj_classname(obj));
30
+ }
31
+
32
+ void oj_trace_parse_call(const char *func, ParseInfo pi, const char *file, int line, VALUE obj) {
33
+ char fmt[64];
34
+ char indent[MAX_INDENT];
35
+ int depth = (int)(stack_size(&pi->stack) * 2);
36
+
37
+ fill_indent(indent, depth);
38
+ sprintf(fmt, "#0:%%13s:%%3d:Oj:-:%%%ds %%s %%s\n", depth);
39
+ printf(fmt, file, line, indent, func, rb_obj_classname(obj));
40
+ }
41
+
42
+ void oj_trace_parse_in(const char *func, ParseInfo pi, const char *file, int line) {
43
+ char fmt[64];
44
+ char indent[MAX_INDENT];
45
+ int depth = (int)(stack_size(&pi->stack) * 2);
46
+
47
+ fill_indent(indent, depth);
48
+ sprintf(fmt, "#0:%%13s:%%3d:Oj:}:%%%ds %%s\n", depth);
49
+ printf(fmt, file, line, indent, func);
50
+ }
51
+
52
+ void oj_trace_parse_hash_end(ParseInfo pi, const char *file, int line) {
53
+ char fmt[64];
54
+ char indent[MAX_INDENT];
55
+ int depth = (int)(stack_size(&pi->stack) * 2 - 2);
56
+ Val v = stack_peek(&pi->stack);
57
+ VALUE obj = v->val;
58
+
59
+ fill_indent(indent, depth);
60
+ sprintf(fmt, "#0:%%13s:%%3d:Oj:{:%%%ds hash_end %%s\n", depth);
61
+ printf(fmt, file, line, indent, rb_obj_classname(obj));
62
+ }
63
+
64
+ void oj_trace_parse_array_end(ParseInfo pi, const char *file, int line) {
65
+ char fmt[64];
66
+ char indent[MAX_INDENT];
67
+ int depth = (int)(stack_size(&pi->stack) * 2);
68
+
69
+ fill_indent(indent, depth);
70
+ sprintf(fmt, "#0:%%13s:%%3d:Oj:{:%%%ds array_ned\n", depth);
71
+ printf(fmt, file, line, indent);
72
+ }
data/ext/oj/trace.h ADDED
@@ -0,0 +1,55 @@
1
+ // Copyright (c) 2018 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
3
+
4
+ #ifndef OJ_TRACE_H
5
+ #define OJ_TRACE_H
6
+
7
+ #include <ruby.h>
8
+ #include <stdbool.h>
9
+
10
+ typedef enum {
11
+ TraceIn = '}',
12
+ TraceOut = '{',
13
+ TraceCall = '-',
14
+ TraceRubyIn = '>',
15
+ TraceRubyOut = '<',
16
+ } TraceWhere;
17
+
18
+ struct _parseInfo;
19
+
20
+ extern void oj_trace(const char *func, VALUE obj, const char *file, int line, int depth, TraceWhere where);
21
+ extern void oj_trace_parse_in(const char *func, struct _parseInfo *pi, const char *file, int line);
22
+ extern void oj_trace_parse_call(const char *func, struct _parseInfo *pi, const char *file, int line, VALUE obj);
23
+ extern void oj_trace_parse_hash_end(struct _parseInfo *pi, const char *file, int line);
24
+ extern void oj_trace_parse_array_end(struct _parseInfo *pi, const char *file, int line);
25
+
26
+ #ifdef OJ_ENABLE_TRACE_LOG
27
+ #define TRACE(option, func, obj, depth, where) \
28
+ if (RB_UNLIKELY(Yes == option)) { \
29
+ oj_trace(func, obj, __FILE__, __LINE__, depth, where); \
30
+ }
31
+ #define TRACE_PARSE_IN(option, func, pi) \
32
+ if (RB_UNLIKELY(Yes == option)) { \
33
+ oj_trace_parse_in(func, pi, __FILE__, __LINE__); \
34
+ }
35
+ #define TRACE_PARSE_CALL(option, func, pi, obj) \
36
+ if (RB_UNLIKELY(Yes == option)) { \
37
+ oj_trace_parse_call(func, pi, __FILE__, __LINE__, obj); \
38
+ }
39
+ #define TRACE_PARSE_HASH_END(option, pi) \
40
+ if (RB_UNLIKELY(Yes == option)) { \
41
+ oj_trace_parse_hash_end(pi, __FILE__, __LINE__); \
42
+ }
43
+ #define TRACE_PARSE_ARRAY_END(option, pi) \
44
+ if (RB_UNLIKELY(Yes == option)) { \
45
+ oj_trace_parse_array_end(pi, __FILE__, __LINE__); \
46
+ }
47
+ #else
48
+ #define TRACE(option, func, obj, depth, where)
49
+ #define TRACE_PARSE_IN(option, func, pi)
50
+ #define TRACE_PARSE_CALL(option, func, pi, obj)
51
+ #define TRACE_PARSE_HASH_END(option, pi)
52
+ #define TRACE_PARSE_ARRAY_END(option, pi)
53
+ #endif
54
+
55
+ #endif /* OJ_TRACE_H */