oj_windows 3.16.15

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 (102) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +44 -0
  3. data/LICENSE +21 -0
  4. data/README.md +164 -0
  5. data/ext/oj_windows/buf.h +85 -0
  6. data/ext/oj_windows/cache.c +339 -0
  7. data/ext/oj_windows/cache.h +22 -0
  8. data/ext/oj_windows/cache8.c +105 -0
  9. data/ext/oj_windows/cache8.h +21 -0
  10. data/ext/oj_windows/circarray.c +64 -0
  11. data/ext/oj_windows/circarray.h +22 -0
  12. data/ext/oj_windows/code.c +214 -0
  13. data/ext/oj_windows/code.h +40 -0
  14. data/ext/oj_windows/compat.c +239 -0
  15. data/ext/oj_windows/custom.c +1074 -0
  16. data/ext/oj_windows/debug.c +126 -0
  17. data/ext/oj_windows/dump.c +1556 -0
  18. data/ext/oj_windows/dump.h +110 -0
  19. data/ext/oj_windows/dump_compat.c +901 -0
  20. data/ext/oj_windows/dump_leaf.c +162 -0
  21. data/ext/oj_windows/dump_object.c +710 -0
  22. data/ext/oj_windows/dump_strict.c +405 -0
  23. data/ext/oj_windows/encode.h +16 -0
  24. data/ext/oj_windows/err.c +57 -0
  25. data/ext/oj_windows/err.h +67 -0
  26. data/ext/oj_windows/extconf.rb +77 -0
  27. data/ext/oj_windows/fast.c +1710 -0
  28. data/ext/oj_windows/intern.c +325 -0
  29. data/ext/oj_windows/intern.h +22 -0
  30. data/ext/oj_windows/mem.c +320 -0
  31. data/ext/oj_windows/mem.h +53 -0
  32. data/ext/oj_windows/mimic_json.c +919 -0
  33. data/ext/oj_windows/object.c +726 -0
  34. data/ext/oj_windows/odd.c +245 -0
  35. data/ext/oj_windows/odd.h +43 -0
  36. data/ext/oj_windows/oj.c +2097 -0
  37. data/ext/oj_windows/oj.h +420 -0
  38. data/ext/oj_windows/parse.c +1317 -0
  39. data/ext/oj_windows/parse.h +113 -0
  40. data/ext/oj_windows/parser.c +1600 -0
  41. data/ext/oj_windows/parser.h +103 -0
  42. data/ext/oj_windows/rails.c +1484 -0
  43. data/ext/oj_windows/rails.h +18 -0
  44. data/ext/oj_windows/reader.c +222 -0
  45. data/ext/oj_windows/reader.h +137 -0
  46. data/ext/oj_windows/resolve.c +80 -0
  47. data/ext/oj_windows/resolve.h +12 -0
  48. data/ext/oj_windows/rxclass.c +144 -0
  49. data/ext/oj_windows/rxclass.h +26 -0
  50. data/ext/oj_windows/saj.c +675 -0
  51. data/ext/oj_windows/saj2.c +584 -0
  52. data/ext/oj_windows/saj2.h +23 -0
  53. data/ext/oj_windows/scp.c +187 -0
  54. data/ext/oj_windows/simd.h +47 -0
  55. data/ext/oj_windows/sparse.c +946 -0
  56. data/ext/oj_windows/stream_writer.c +329 -0
  57. data/ext/oj_windows/strict.c +189 -0
  58. data/ext/oj_windows/string_writer.c +517 -0
  59. data/ext/oj_windows/trace.c +72 -0
  60. data/ext/oj_windows/trace.h +55 -0
  61. data/ext/oj_windows/usual.c +1218 -0
  62. data/ext/oj_windows/usual.h +69 -0
  63. data/ext/oj_windows/util.c +136 -0
  64. data/ext/oj_windows/util.h +20 -0
  65. data/ext/oj_windows/val_stack.c +101 -0
  66. data/ext/oj_windows/val_stack.h +151 -0
  67. data/ext/oj_windows/validate.c +46 -0
  68. data/ext/oj_windows/wab.c +584 -0
  69. data/lib/oj/active_support_helper.rb +39 -0
  70. data/lib/oj/bag.rb +95 -0
  71. data/lib/oj/easy_hash.rb +52 -0
  72. data/lib/oj/error.rb +21 -0
  73. data/lib/oj/json.rb +188 -0
  74. data/lib/oj/mimic.rb +301 -0
  75. data/lib/oj/saj.rb +80 -0
  76. data/lib/oj/schandler.rb +143 -0
  77. data/lib/oj/state.rb +135 -0
  78. data/lib/oj/version.rb +4 -0
  79. data/lib/oj_windows/active_support_helper.rb +39 -0
  80. data/lib/oj_windows/bag.rb +95 -0
  81. data/lib/oj_windows/easy_hash.rb +52 -0
  82. data/lib/oj_windows/error.rb +21 -0
  83. data/lib/oj_windows/json.rb +188 -0
  84. data/lib/oj_windows/mimic.rb +301 -0
  85. data/lib/oj_windows/saj.rb +80 -0
  86. data/lib/oj_windows/schandler.rb +143 -0
  87. data/lib/oj_windows/state.rb +135 -0
  88. data/lib/oj_windows/version.rb +4 -0
  89. data/lib/oj_windows.rb +15 -0
  90. data/pages/Advanced.md +38 -0
  91. data/pages/Compatibility.md +49 -0
  92. data/pages/Custom.md +37 -0
  93. data/pages/Encoding.md +61 -0
  94. data/pages/InstallOptions.md +20 -0
  95. data/pages/JsonGem.md +60 -0
  96. data/pages/Modes.md +94 -0
  97. data/pages/Options.md +339 -0
  98. data/pages/Parser.md +134 -0
  99. data/pages/Rails.md +85 -0
  100. data/pages/Security.md +43 -0
  101. data/pages/WAB.md +12 -0
  102. metadata +242 -0
@@ -0,0 +1,1484 @@
1
+ // Copyright (c) 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 "rails.h"
5
+
6
+ #include "code.h"
7
+ #include "encode.h"
8
+ #include "mem.h"
9
+ #include "trace.h"
10
+ #include "util.h"
11
+
12
+ #ifdef _MSC_VER
13
+ #define OJ_INFINITY HUGE_VAL
14
+ #else
15
+ #define OJ_INFINITY (1.0 / 0.0)
16
+ #endif
17
+
18
+ // TBD keep static array of strings and functions to help with rails optimization
19
+ typedef struct _encoder {
20
+ struct _rOptTable ropts;
21
+ struct _options opts;
22
+ VALUE arg;
23
+ } *Encoder;
24
+
25
+ bool oj_rails_hash_opt = false;
26
+ bool oj_rails_array_opt = false;
27
+ bool oj_rails_float_opt = false;
28
+
29
+ extern void oj_mimic_json_methods(VALUE json);
30
+
31
+ static void dump_rails_val(VALUE obj, int depth, Out out, bool as_ok);
32
+
33
+ extern VALUE Oj;
34
+
35
+ static struct _rOptTable ropts = {0, 0, NULL};
36
+
37
+ static VALUE encoder_class = Qnil;
38
+ static bool escape_html = true;
39
+ static bool xml_time = true;
40
+
41
+ static ROpt create_opt(ROptTable rot, VALUE clas);
42
+
43
+ ROpt oj_rails_get_opt(ROptTable rot, VALUE clas) {
44
+ if (NULL == rot) {
45
+ rot = &ropts;
46
+ }
47
+ if (0 < rot->len) {
48
+ int lo = 0;
49
+ int hi = rot->len - 1;
50
+ int mid;
51
+ VALUE v;
52
+
53
+ if (clas < rot->table->clas || rot->table[hi].clas < clas) {
54
+ return NULL;
55
+ }
56
+ if (rot->table[lo].clas == clas) {
57
+ return rot->table;
58
+ }
59
+ if (rot->table[hi].clas == clas) {
60
+ return &rot->table[hi];
61
+ }
62
+ while (2 <= hi - lo) {
63
+ mid = (hi + lo) / 2;
64
+ v = rot->table[mid].clas;
65
+ if (v == clas) {
66
+ return &rot->table[mid];
67
+ }
68
+ if (v < clas) {
69
+ lo = mid;
70
+ } else {
71
+ hi = mid;
72
+ }
73
+ }
74
+ }
75
+ return NULL;
76
+ }
77
+
78
+ static ROptTable copy_opts(ROptTable src, ROptTable dest) {
79
+ dest->len = src->len;
80
+ dest->alen = src->alen;
81
+ if (NULL == src->table) {
82
+ dest->table = NULL;
83
+ } else {
84
+ dest->table = OJ_R_ALLOC_N(struct _rOpt, dest->alen);
85
+ memcpy(dest->table, src->table, sizeof(struct _rOpt) * dest->alen);
86
+ }
87
+ return NULL;
88
+ }
89
+
90
+ static int dump_attr_cb(ID key, VALUE value, VALUE ov) {
91
+ Out out = (Out)ov;
92
+ int depth = out->depth;
93
+ size_t size = depth * out->indent + 1;
94
+ const char *attr = rb_id2name(key);
95
+
96
+ // Some exceptions such as NoMethodError have an invisible attribute where
97
+ // the key name is NULL. Not an empty string but NULL.
98
+ if (NULL == attr) {
99
+ attr = "";
100
+ }
101
+ if (0 == strcmp("bt", attr) || 0 == strcmp("mesg", attr)) {
102
+ return ST_CONTINUE;
103
+ }
104
+ assure_size(out, size);
105
+ fill_indent(out, depth);
106
+ if ('@' == *attr) {
107
+ attr++;
108
+ oj_dump_cstr(attr, strlen(attr), 0, 0, out);
109
+ } else {
110
+ char buf[32];
111
+
112
+ *buf = '~';
113
+ strncpy(buf + 1, attr, sizeof(buf) - 2);
114
+ buf[sizeof(buf) - 1] = '\0';
115
+ oj_dump_cstr(buf, strlen(buf), 0, 0, out);
116
+ }
117
+ *out->cur++ = ':';
118
+ dump_rails_val(value, depth, out, true);
119
+ out->depth = depth;
120
+ *out->cur++ = ',';
121
+
122
+ return ST_CONTINUE;
123
+ }
124
+
125
+ static void dump_obj_attrs(VALUE obj, int depth, Out out, bool as_ok) {
126
+ assure_size(out, 2);
127
+ *out->cur++ = '{';
128
+ out->depth = depth + 1;
129
+ rb_ivar_foreach(obj, dump_attr_cb, (VALUE)out);
130
+ if (',' == *(out->cur - 1)) {
131
+ out->cur--; // backup to overwrite last comma
132
+ }
133
+ out->depth = depth;
134
+ fill_indent(out, depth);
135
+ *out->cur++ = '}';
136
+ *out->cur = '\0';
137
+ }
138
+
139
+ static void dump_struct(VALUE obj, int depth, Out out, bool as_ok) {
140
+ int d3 = depth + 2;
141
+ size_t size = d3 * out->indent + 2;
142
+ size_t sep_len = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
143
+ volatile VALUE ma;
144
+ volatile VALUE v;
145
+ int cnt;
146
+ int i;
147
+ size_t len;
148
+ const char *name;
149
+
150
+ #ifdef RSTRUCT_LEN
151
+ #if RSTRUCT_LEN_RETURNS_INTEGER_OBJECT
152
+ cnt = (int)NUM2LONG(RSTRUCT_LEN(obj));
153
+ #else // RSTRUCT_LEN_RETURNS_INTEGER_OBJECT
154
+ cnt = (int)RSTRUCT_LEN(obj);
155
+ #endif // RSTRUCT_LEN_RETURNS_INTEGER_OBJECT
156
+ #else
157
+ // This is a bit risky as a struct in C ruby is not the same as a Struct
158
+ // class in interpreted Ruby so length() may not be defined.
159
+ cnt = FIX2INT(rb_funcall(obj, oj_length_id, 0));
160
+ #endif
161
+ ma = rb_struct_s_members(rb_obj_class(obj));
162
+ assure_size(out, 2);
163
+ *out->cur++ = '{';
164
+ for (i = 0; i < cnt; i++) {
165
+ volatile VALUE s = rb_sym2str(RARRAY_AREF(ma, i));
166
+
167
+ name = RSTRING_PTR(s);
168
+ len = RSTRING_LEN(s);
169
+ assure_size(out, size + sep_len + 6);
170
+ if (0 < i) {
171
+ *out->cur++ = ',';
172
+ }
173
+ fill_indent(out, d3);
174
+ *out->cur++ = '"';
175
+ APPEND_CHARS(out->cur, name, len);
176
+ *out->cur++ = '"';
177
+ if (0 < out->opts->dump_opts.before_size) {
178
+ APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
179
+ }
180
+ *out->cur++ = ':';
181
+ if (0 < out->opts->dump_opts.after_size) {
182
+ APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
183
+ }
184
+ #ifdef RSTRUCT_LEN
185
+ v = RSTRUCT_GET(obj, i);
186
+ #else
187
+ v = rb_struct_aref(obj, INT2FIX(i));
188
+ #endif
189
+ dump_rails_val(v, d3, out, true);
190
+ }
191
+ fill_indent(out, depth);
192
+ *out->cur++ = '}';
193
+ *out->cur = '\0';
194
+ }
195
+
196
+ static ID to_a_id = 0;
197
+
198
+ static void dump_enumerable(VALUE obj, int depth, Out out, bool as_ok) {
199
+ if (0 == to_a_id) {
200
+ to_a_id = rb_intern("to_a");
201
+ }
202
+ dump_rails_val(rb_funcall(obj, to_a_id, 0), depth, out, false);
203
+ }
204
+
205
+ static void dump_bigdecimal(VALUE obj, int depth, Out out, bool as_ok) {
206
+ volatile VALUE rstr = oj_safe_string_convert(obj);
207
+ const char *str = RSTRING_PTR(rstr);
208
+
209
+ if ('I' == *str || 'N' == *str || ('-' == *str && 'I' == str[1])) {
210
+ oj_dump_nil(Qnil, depth, out, false);
211
+ } else if (out->opts->int_range_max != 0 || out->opts->int_range_min != 0) {
212
+ oj_dump_cstr(str, RSTRING_LEN(rstr), 0, 0, out);
213
+ } else if (Yes == out->opts->bigdec_as_num) {
214
+ oj_dump_raw(str, RSTRING_LEN(rstr), out);
215
+ } else {
216
+ oj_dump_cstr(str, RSTRING_LEN(rstr), 0, 0, out);
217
+ }
218
+ }
219
+
220
+ static void dump_sec_nano(VALUE obj, int64_t sec, long nsec, Out out) {
221
+ char buf[64];
222
+ struct _timeInfo ti;
223
+ long one = 1000000000;
224
+ long tzsecs = NUM2LONG(rb_funcall2(obj, oj_utc_offset_id, 0, 0));
225
+ int tzhour, tzmin;
226
+ char tzsign = '+';
227
+ int len;
228
+
229
+ if (out->end - out->cur <= 36) {
230
+ assure_size(out, 36);
231
+ }
232
+ if (9 > out->opts->sec_prec) {
233
+ int i;
234
+
235
+ // Rails does not round when reducing precision but instead floors,
236
+ for (i = 9 - out->opts->sec_prec; 0 < i; i--) {
237
+ nsec = nsec / 10;
238
+ one /= 10;
239
+ }
240
+ if (one <= nsec) {
241
+ nsec -= one;
242
+ sec++;
243
+ }
244
+ }
245
+ // 2012-01-05T23:58:07.123456000+09:00 or 2012/01/05 23:58:07 +0900
246
+ sec += tzsecs;
247
+ sec_as_time(sec, &ti);
248
+ if (0 > tzsecs) {
249
+ tzsign = '-';
250
+ tzhour = (int)(tzsecs / -3600);
251
+ tzmin = (int)(tzsecs / -60) - (tzhour * 60);
252
+ } else {
253
+ tzhour = (int)(tzsecs / 3600);
254
+ tzmin = (int)(tzsecs / 60) - (tzhour * 60);
255
+ }
256
+ if (!xml_time) {
257
+ len = sprintf(buf,
258
+ "%04d/%02d/%02d %02d:%02d:%02d %c%02d%02d",
259
+ ti.year,
260
+ ti.mon,
261
+ ti.day,
262
+ ti.hour,
263
+ ti.min,
264
+ ti.sec,
265
+ tzsign,
266
+ tzhour,
267
+ tzmin);
268
+ } else if (0 == out->opts->sec_prec) {
269
+ if (0 == tzsecs && rb_funcall2(obj, oj_utcq_id, 0, 0)) {
270
+ len = sprintf(buf, "%04d-%02d-%02dT%02d:%02d:%02dZ", ti.year, ti.mon, ti.day, ti.hour, ti.min, ti.sec);
271
+ } else {
272
+ len = sprintf(buf,
273
+ "%04d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
274
+ ti.year,
275
+ ti.mon,
276
+ ti.day,
277
+ ti.hour,
278
+ ti.min,
279
+ ti.sec,
280
+ tzsign,
281
+ tzhour,
282
+ tzmin);
283
+ }
284
+ } else if (0 == tzsecs && rb_funcall2(obj, oj_utcq_id, 0, 0)) {
285
+ char format[64] = "%04d-%02d-%02dT%02d:%02d:%02d.%09ldZ";
286
+
287
+ len = 30;
288
+ if (9 > out->opts->sec_prec) {
289
+ format[32] = '0' + out->opts->sec_prec;
290
+ len -= 9 - out->opts->sec_prec;
291
+ }
292
+ len = sprintf(buf, format, ti.year, ti.mon, ti.day, ti.hour, ti.min, ti.sec, nsec);
293
+ } else {
294
+ char format[64] = "%04d-%02d-%02dT%02d:%02d:%02d.%09ld%c%02d:%02d";
295
+
296
+ len = 35;
297
+ if (9 > out->opts->sec_prec) {
298
+ format[32] = '0' + out->opts->sec_prec;
299
+ len -= 9 - out->opts->sec_prec;
300
+ }
301
+ len = sprintf(buf, format, ti.year, ti.mon, ti.day, ti.hour, ti.min, ti.sec, nsec, tzsign, tzhour, tzmin);
302
+ }
303
+ oj_dump_cstr(buf, len, 0, 0, out);
304
+ }
305
+
306
+ static void dump_time(VALUE obj, int depth, Out out, bool as_ok) {
307
+ long long sec;
308
+ long long nsec;
309
+
310
+ if (16 <= sizeof(struct timespec)) {
311
+ struct timespec ts = rb_time_timespec(obj);
312
+
313
+ sec = (long long)ts.tv_sec;
314
+ nsec = ts.tv_nsec;
315
+ } else {
316
+ sec = NUM2LL(rb_funcall2(obj, oj_tv_sec_id, 0, 0));
317
+ nsec = NUM2LL(rb_funcall2(obj, oj_tv_nsec_id, 0, 0));
318
+ }
319
+ dump_sec_nano(obj, sec, nsec, out);
320
+ }
321
+
322
+ static void dump_timewithzone(VALUE obj, int depth, Out out, bool as_ok) {
323
+ int64_t sec = NUM2LONG(rb_funcall2(obj, oj_tv_sec_id, 0, 0));
324
+ long long nsec = 0;
325
+
326
+ if (rb_respond_to(obj, oj_tv_nsec_id)) {
327
+ nsec = NUM2LL(rb_funcall2(obj, oj_tv_nsec_id, 0, 0));
328
+ } else if (rb_respond_to(obj, oj_tv_usec_id)) {
329
+ nsec = NUM2LL(rb_funcall2(obj, oj_tv_usec_id, 0, 0)) * 1000;
330
+ }
331
+ dump_sec_nano(obj, sec, nsec, out);
332
+ }
333
+
334
+ static void dump_to_s(VALUE obj, int depth, Out out, bool as_ok) {
335
+ volatile VALUE rstr = oj_safe_string_convert(obj);
336
+
337
+ oj_dump_cstr(RSTRING_PTR(rstr), RSTRING_LEN(rstr), 0, 0, out);
338
+ }
339
+
340
+ static ID parameters_id = 0;
341
+
342
+ typedef struct _strLen {
343
+ const char *str;
344
+ size_t len;
345
+ } *StrLen;
346
+
347
+ static void dump_actioncontroller_parameters(VALUE obj, int depth, Out out, bool as_ok) {
348
+ if (0 == parameters_id) {
349
+ parameters_id = rb_intern("@parameters");
350
+ }
351
+ out->argc = 0;
352
+ dump_rails_val(rb_ivar_get(obj, parameters_id), depth, out, true);
353
+ }
354
+
355
+ static StrLen columns_array(VALUE rcols, int *ccnt) {
356
+ volatile VALUE v;
357
+ StrLen cp;
358
+ StrLen cols;
359
+ size_t i;
360
+ size_t cnt = RARRAY_LEN(rcols);
361
+
362
+ *ccnt = (int)cnt;
363
+ cols = OJ_R_ALLOC_N(struct _strLen, cnt);
364
+ for (i = 0, cp = cols; i < cnt; i++, cp++) {
365
+ v = RARRAY_AREF(rcols, i);
366
+ if (T_STRING != rb_type(v)) {
367
+ v = oj_safe_string_convert(v);
368
+ }
369
+ cp->str = StringValuePtr(v);
370
+ cp->len = RSTRING_LEN(v);
371
+ }
372
+ return cols;
373
+ }
374
+
375
+ static void dump_row(VALUE row, StrLen cols, int ccnt, int depth, Out out) {
376
+ size_t size;
377
+ int d2 = depth + 1;
378
+ int i;
379
+
380
+ assure_size(out, 2);
381
+ *out->cur++ = '{';
382
+ size = depth * out->indent + 3;
383
+ for (i = 0; i < ccnt; i++, cols++) {
384
+ assure_size(out, size);
385
+ if (out->opts->dump_opts.use) {
386
+ if (0 < out->opts->dump_opts.array_size) {
387
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
388
+ }
389
+ if (0 < out->opts->dump_opts.indent_size) {
390
+ int i;
391
+ for (i = d2; 0 < i; i--) {
392
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
393
+ }
394
+ }
395
+ } else {
396
+ fill_indent(out, d2);
397
+ }
398
+ oj_dump_cstr(cols->str, cols->len, 0, 0, out);
399
+ *out->cur++ = ':';
400
+ dump_rails_val(RARRAY_AREF(row, i), depth, out, true);
401
+ if (i < ccnt - 1) {
402
+ *out->cur++ = ',';
403
+ }
404
+ }
405
+ size = depth * out->indent + 1;
406
+ assure_size(out, size);
407
+ if (out->opts->dump_opts.use) {
408
+ if (0 < out->opts->dump_opts.array_size) {
409
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
410
+ }
411
+ if (0 < out->opts->dump_opts.indent_size) {
412
+ int i;
413
+
414
+ for (i = depth; 0 < i; i--) {
415
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
416
+ }
417
+ }
418
+ } else {
419
+ fill_indent(out, depth);
420
+ }
421
+ *out->cur++ = '}';
422
+ }
423
+
424
+ static ID rows_id = 0;
425
+ static ID columns_id = 0;
426
+
427
+ static void dump_activerecord_result(VALUE obj, int depth, Out out, bool as_ok) {
428
+ volatile VALUE rows;
429
+ StrLen cols;
430
+ int ccnt = 0;
431
+ size_t i;
432
+ size_t rcnt;
433
+ size_t size;
434
+ int d2 = depth + 1;
435
+
436
+ if (0 == rows_id) {
437
+ rows_id = rb_intern("@rows");
438
+ columns_id = rb_intern("@columns");
439
+ }
440
+ out->argc = 0;
441
+ cols = columns_array(rb_ivar_get(obj, columns_id), &ccnt);
442
+ rows = rb_ivar_get(obj, rows_id);
443
+ rcnt = RARRAY_LEN(rows);
444
+ assure_size(out, 2);
445
+ *out->cur++ = '[';
446
+ if (out->opts->dump_opts.use) {
447
+ size = d2 * out->opts->dump_opts.indent_size + out->opts->dump_opts.array_size + 1;
448
+ } else {
449
+ size = d2 * out->indent + 2;
450
+ }
451
+ assure_size(out, 2);
452
+ for (i = 0; i < rcnt; i++) {
453
+ assure_size(out, size);
454
+ if (out->opts->dump_opts.use) {
455
+ if (0 < out->opts->dump_opts.array_size) {
456
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
457
+ }
458
+ if (0 < out->opts->dump_opts.indent_size) {
459
+ int i;
460
+ for (i = d2; 0 < i; i--) {
461
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
462
+ }
463
+ }
464
+ } else {
465
+ fill_indent(out, d2);
466
+ }
467
+ dump_row(RARRAY_AREF(rows, i), cols, ccnt, d2, out);
468
+ if (i < rcnt - 1) {
469
+ *out->cur++ = ',';
470
+ }
471
+ }
472
+ OJ_R_FREE(cols);
473
+ size = depth * out->indent + 1;
474
+ assure_size(out, size);
475
+ if (out->opts->dump_opts.use) {
476
+ if (0 < out->opts->dump_opts.array_size) {
477
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
478
+ }
479
+ if (0 < out->opts->dump_opts.indent_size) {
480
+ int i;
481
+
482
+ for (i = depth; 0 < i; i--) {
483
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
484
+ }
485
+ }
486
+ } else {
487
+ fill_indent(out, depth);
488
+ }
489
+ *out->cur++ = ']';
490
+ }
491
+
492
+ typedef struct _namedFunc {
493
+ const char *name;
494
+ DumpFunc func;
495
+ } *NamedFunc;
496
+
497
+ static void dump_as_string(VALUE obj, int depth, Out out, bool as_ok) {
498
+ if (oj_code_dump(oj_compat_codes, obj, depth, out)) {
499
+ out->argc = 0;
500
+ return;
501
+ }
502
+ oj_dump_obj_to_s(obj, out);
503
+ }
504
+
505
+ static void dump_as_json(VALUE obj, int depth, Out out, bool as_ok) {
506
+ volatile VALUE ja;
507
+
508
+ TRACE(out->opts->trace, "as_json", obj, depth + 1, TraceRubyIn);
509
+ // Some classes elect to not take an options argument so check the arity
510
+ // of as_json.
511
+ if (0 == rb_obj_method_arity(obj, oj_as_json_id)) {
512
+ ja = rb_funcall(obj, oj_as_json_id, 0);
513
+ } else {
514
+ ja = rb_funcall2(obj, oj_as_json_id, out->argc, out->argv);
515
+ }
516
+ TRACE(out->opts->trace, "as_json", obj, depth + 1, TraceRubyOut);
517
+
518
+ out->argc = 0;
519
+ if (ja == obj || !as_ok) {
520
+ // Once as_json is called it should never be called again on the same
521
+ // object with as_ok.
522
+ dump_rails_val(ja, depth, out, false);
523
+ } else {
524
+ int type = rb_type(ja);
525
+
526
+ if (T_HASH == type || T_ARRAY == type) {
527
+ dump_rails_val(ja, depth, out, true);
528
+ } else {
529
+ dump_rails_val(ja, depth, out, true);
530
+ }
531
+ }
532
+ }
533
+
534
+ static void dump_regexp(VALUE obj, int depth, Out out, bool as_ok) {
535
+ if (as_ok && rb_respond_to(obj, oj_as_json_id)) {
536
+ dump_as_json(obj, depth, out, false);
537
+ return;
538
+ }
539
+ dump_as_string(obj, depth, out, as_ok);
540
+ }
541
+
542
+ static struct _namedFunc dump_map[] = {
543
+ {"ActionController::Parameters", dump_actioncontroller_parameters},
544
+ {"ActiveRecord::Result", dump_activerecord_result},
545
+ {"ActiveSupport::TimeWithZone", dump_timewithzone},
546
+ {"BigDecimal", dump_bigdecimal},
547
+ {"Range", dump_to_s},
548
+ {"Regexp", dump_regexp},
549
+ //{ "Regexp", dump_to_s },
550
+ {"Time", dump_time},
551
+ {NULL, NULL},
552
+ };
553
+
554
+ static VALUE activerecord_base = Qundef;
555
+ static ID attributes_id = 0;
556
+
557
+ static void dump_activerecord(VALUE obj, int depth, Out out, bool as_ok) {
558
+ if (0 == attributes_id) {
559
+ attributes_id = rb_intern("@attributes");
560
+ }
561
+ out->argc = 0;
562
+ dump_rails_val(rb_ivar_get(obj, attributes_id), depth, out, true);
563
+ }
564
+
565
+ static ROpt create_opt(ROptTable rot, VALUE clas) {
566
+ ROpt ro;
567
+ NamedFunc nf;
568
+ const char *classname = rb_class2name(clas);
569
+ int olen = rot->len;
570
+
571
+ rot->len++;
572
+ if (NULL == rot->table) {
573
+ rot->alen = 256;
574
+ rot->table = OJ_R_ALLOC_N(struct _rOpt, rot->alen);
575
+ memset(rot->table, 0, sizeof(struct _rOpt) * rot->alen);
576
+ } else if (rot->alen <= rot->len) {
577
+ rot->alen *= 2;
578
+ OJ_R_REALLOC_N(rot->table, struct _rOpt, rot->alen);
579
+ memset(rot->table + olen, 0, sizeof(struct _rOpt) * olen);
580
+ }
581
+ if (0 == olen) {
582
+ ro = rot->table;
583
+ } else if (rot->table[olen - 1].clas < clas) {
584
+ ro = &rot->table[olen];
585
+ } else {
586
+ int i;
587
+
588
+ for (i = 0, ro = rot->table; i < olen; i++, ro++) {
589
+ if (clas < ro->clas) {
590
+ memmove(ro + 1, ro, sizeof(struct _rOpt) * (olen - i));
591
+ break;
592
+ }
593
+ }
594
+ }
595
+ ro->clas = clas;
596
+ ro->on = true;
597
+ ro->dump = dump_obj_attrs;
598
+ for (nf = dump_map; NULL != nf->name; nf++) {
599
+ if (0 == strcmp(nf->name, classname)) {
600
+ ro->dump = nf->func;
601
+ break;
602
+ }
603
+ }
604
+ if (ro->dump == dump_obj_attrs) {
605
+ if (Qundef == activerecord_base) {
606
+ // If not defined let an exception be raised.
607
+ VALUE ar = rb_const_get_at(rb_cObject, rb_intern("ActiveRecord"));
608
+
609
+ if (Qundef != ar) {
610
+ activerecord_base = rb_const_get_at(ar, rb_intern("Base"));
611
+ }
612
+ }
613
+ if (Qundef != activerecord_base && Qtrue == rb_class_inherited_p(clas, activerecord_base)) {
614
+ ro->dump = dump_activerecord;
615
+ } else if (Qtrue == rb_class_inherited_p(clas, rb_cStruct)) { // check before enumerable
616
+ ro->dump = dump_struct;
617
+ } else if (Qtrue == rb_class_inherited_p(clas, rb_mEnumerable)) {
618
+ ro->dump = dump_enumerable;
619
+ } else if (Qtrue == rb_class_inherited_p(clas, rb_eException)) {
620
+ ro->dump = dump_to_s;
621
+ }
622
+ }
623
+ return ro;
624
+ }
625
+
626
+ static void encoder_free(void *ptr) {
627
+ if (NULL != ptr) {
628
+ Encoder e = (Encoder)ptr;
629
+
630
+ if (NULL != e->ropts.table) {
631
+ OJ_R_FREE(e->ropts.table);
632
+ }
633
+ OJ_R_FREE(ptr);
634
+ }
635
+ }
636
+
637
+ static void encoder_mark(void *ptr) {
638
+ if (NULL != ptr) {
639
+ Encoder e = (Encoder)ptr;
640
+
641
+ if (Qnil != e->arg) {
642
+ rb_gc_mark(e->arg);
643
+ }
644
+ }
645
+ }
646
+
647
+ static const rb_data_type_t oj_encoder_type = {
648
+ "oj_windows/encoder",
649
+ {
650
+ encoder_mark,
651
+ encoder_free,
652
+ NULL,
653
+ },
654
+ 0,
655
+ 0,
656
+ };
657
+
658
+ /* Document-method: new
659
+ * call-seq: new(options=nil)
660
+ *
661
+ * Creates a new Encoder.
662
+ * - *options* [_Hash_] formatting options
663
+ */
664
+ static VALUE encoder_new(int argc, VALUE *argv, VALUE self) {
665
+ Encoder e = OJ_R_ALLOC(struct _encoder);
666
+
667
+ e->opts = oj_default_options;
668
+ copy_opts(&ropts, &e->ropts);
669
+
670
+ if (1 <= argc && Qnil != *argv) {
671
+ e->arg = *argv;
672
+ } else {
673
+ e->arg = rb_hash_new();
674
+ }
675
+ oj_parse_options(*argv, &e->opts);
676
+
677
+ return TypedData_Wrap_Struct(encoder_class, &oj_encoder_type, e);
678
+ }
679
+
680
+ static VALUE resolve_classpath(const char *name) {
681
+ char class_name[1024];
682
+ VALUE clas;
683
+ char *end = class_name + sizeof(class_name) - 1;
684
+ char *s;
685
+ const char *n = name;
686
+ ID cid;
687
+
688
+ clas = rb_cObject;
689
+ for (s = class_name; '\0' != *n; n++) {
690
+ if (':' == *n) {
691
+ *s = '\0';
692
+ n++;
693
+ if (':' != *n) {
694
+ return Qnil;
695
+ }
696
+ cid = rb_intern(class_name);
697
+ if (!rb_const_defined_at(clas, cid)) {
698
+ return Qnil;
699
+ }
700
+ clas = rb_const_get_at(clas, cid);
701
+ s = class_name;
702
+ } else if (end <= s) {
703
+ return Qnil;
704
+ } else {
705
+ *s++ = *n;
706
+ }
707
+ }
708
+ *s = '\0';
709
+ cid = rb_intern(class_name);
710
+ if (!rb_const_defined_at(clas, cid)) {
711
+ return Qnil;
712
+ }
713
+ clas = rb_const_get_at(clas, cid);
714
+
715
+ return clas;
716
+ }
717
+
718
+ static void optimize(int argc, VALUE *argv, ROptTable rot, bool on) {
719
+ ROpt ro;
720
+
721
+ if (0 == argc) {
722
+ int i;
723
+ NamedFunc nf;
724
+ VALUE clas;
725
+
726
+ oj_rails_hash_opt = on;
727
+ oj_rails_array_opt = on;
728
+ oj_rails_float_opt = on;
729
+
730
+ for (nf = dump_map; NULL != nf->name; nf++) {
731
+ if (Qnil != (clas = resolve_classpath(nf->name))) {
732
+ if (NULL == oj_rails_get_opt(rot, clas)) {
733
+ create_opt(rot, clas);
734
+ }
735
+ }
736
+ }
737
+ for (i = 0; i < rot->len; i++) {
738
+ rot->table[i].on = on;
739
+ }
740
+ }
741
+ for (; 0 < argc; argc--, argv++) {
742
+ if (rb_cHash == *argv) {
743
+ oj_rails_hash_opt = on;
744
+ } else if (rb_cArray == *argv) {
745
+ oj_rails_array_opt = on;
746
+ } else if (rb_cFloat == *argv) {
747
+ oj_rails_float_opt = on;
748
+ } else if (oj_string_writer_class == *argv) {
749
+ string_writer_optimized = on;
750
+ } else if (NULL != (ro = oj_rails_get_opt(rot, *argv)) || NULL != (ro = create_opt(rot, *argv))) {
751
+ ro->on = on;
752
+ }
753
+ }
754
+ }
755
+
756
+ /* Document-method optimize
757
+ * call-seq: optimize(*classes)
758
+ *
759
+ * Use Oj rails optimized routines to encode the specified classes. This
760
+ * ignores the as_json() method on the class and uses an internal encoding
761
+ * instead. Passing in no classes indicates all should use the optimized
762
+ * version of encoding for all previously optimized classes. Passing in the
763
+ * Object class set a global switch that will then use the optimized behavior
764
+ * for all classes.
765
+ *
766
+ * - *classes* [_Class_] a list of classes to optimize
767
+ */
768
+ static VALUE encoder_optimize(int argc, VALUE *argv, VALUE self) {
769
+ Encoder e;
770
+ TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);
771
+
772
+ optimize(argc, argv, &e->ropts, true);
773
+
774
+ return Qnil;
775
+ }
776
+
777
+ /* Document-method: optimize
778
+ * call-seq: optimize(*classes)
779
+ *
780
+ * Use Oj rails optimized routines to encode the specified classes. This
781
+ * ignores the as_json() method on the class and uses an internal encoding
782
+ * instead. Passing in no classes indicates all should use the optimized
783
+ * version of encoding for all previously optimized classes. Passing in the
784
+ * Object class set a global switch that will then use the optimized behavior
785
+ * for all classes.
786
+ *
787
+ * - *classes* [_Class_] a list of classes to optimize
788
+ */
789
+ static VALUE rails_optimize(int argc, VALUE *argv, VALUE self) {
790
+ optimize(argc, argv, &ropts, true);
791
+ string_writer_optimized = true;
792
+
793
+ return Qnil;
794
+ }
795
+
796
+ /* Document-module: mimic_JSON
797
+ * call-seq: mimic_JSON()
798
+ *
799
+ * Sets the JSON method to use Oj similar to Oj.mimic_JSON except with the
800
+ * ActiveSupport monkey patches instead of the json gem monkey patches.
801
+ */
802
+ VALUE
803
+ rails_mimic_json(VALUE self) {
804
+ VALUE json;
805
+
806
+ if (rb_const_defined_at(rb_cObject, rb_intern("JSON"))) {
807
+ json = rb_const_get_at(rb_cObject, rb_intern("JSON"));
808
+ } else {
809
+ json = rb_define_module("JSON");
810
+ }
811
+ oj_mimic_json_methods(json);
812
+ // Setting the default mode breaks the prmoise in the docs not to.
813
+ // oj_default_options.mode = RailsMode;
814
+
815
+ return Qnil;
816
+ }
817
+
818
+ /* Document-method: deoptimize
819
+ * call-seq: deoptimize(*classes)
820
+ *
821
+ * Turn off Oj rails optimization on the specified classes.
822
+ *
823
+ * - *classes* [_Class_] a list of classes to deoptimize
824
+ */
825
+ static VALUE encoder_deoptimize(int argc, VALUE *argv, VALUE self) {
826
+ Encoder e;
827
+ TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);
828
+
829
+ optimize(argc, argv, &e->ropts, false);
830
+
831
+ return Qnil;
832
+ }
833
+
834
+ /* Document-method: deoptimize
835
+ * call-seq: deoptimize(*classes)
836
+ *
837
+ * Turn off Oj rails optimization on the specified classes.
838
+ *
839
+ * - *classes* [_Class_] a list of classes to deoptimize
840
+ */
841
+ static VALUE rails_deoptimize(int argc, VALUE *argv, VALUE self) {
842
+ optimize(argc, argv, &ropts, false);
843
+ string_writer_optimized = false;
844
+
845
+ return Qnil;
846
+ }
847
+
848
+ /* Document-method:optimized?
849
+ * call-seq: optimized?(clas)
850
+ *
851
+ * - *clas* [_Class_] Class to check
852
+ *
853
+ * @return true if the class is being optimized for rails and false otherwise
854
+ */
855
+ static VALUE encoder_optimized(VALUE self, VALUE clas) {
856
+ Encoder e;
857
+ ROpt ro;
858
+
859
+ TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);
860
+ ro = oj_rails_get_opt(&e->ropts, clas);
861
+
862
+ if (NULL == ro) {
863
+ return Qfalse;
864
+ }
865
+ return (ro->on) ? Qtrue : Qfalse;
866
+ }
867
+
868
+ /* Document-method: optimized?
869
+ * call-seq: optimized?(clas)
870
+ *
871
+ * Returns true if the specified Class is being optimized.
872
+ */
873
+ static VALUE rails_optimized(VALUE self, VALUE clas) {
874
+ ROpt ro = oj_rails_get_opt(&ropts, clas);
875
+
876
+ if (NULL == ro) {
877
+ return Qfalse;
878
+ }
879
+ return (ro->on) ? Qtrue : Qfalse;
880
+ }
881
+
882
+ typedef struct _oo {
883
+ Out out;
884
+ VALUE obj;
885
+ } *OO;
886
+
887
+ static VALUE protect_dump(VALUE ov) {
888
+ OO oo = (OO)ov;
889
+
890
+ dump_rails_val(oo->obj, 0, oo->out, true);
891
+
892
+ return Qnil;
893
+ }
894
+
895
+ static VALUE encode(VALUE obj, ROptTable ropts, Options opts, int argc, VALUE *argv) {
896
+ struct _out out;
897
+ struct _options copts = *opts;
898
+ volatile VALUE rstr = Qnil;
899
+ struct _oo oo;
900
+ int line = 0;
901
+
902
+ oo.out = &out;
903
+ oo.obj = obj;
904
+ copts.str_rx.head = NULL;
905
+ copts.str_rx.tail = NULL;
906
+ copts.mode = RailsMode;
907
+ if (escape_html) {
908
+ copts.escape_mode = RailsXEsc;
909
+ } else {
910
+ copts.escape_mode = RailsEsc;
911
+ }
912
+
913
+ oj_out_init(&out);
914
+
915
+ out.omit_nil = copts.dump_opts.omit_nil;
916
+ out.cur = out.buf;
917
+ out.circ_cnt = 0;
918
+ out.opts = &copts;
919
+ out.hash_cnt = 0;
920
+ out.indent = copts.indent;
921
+ out.argc = argc;
922
+ out.argv = argv;
923
+ out.ropts = ropts;
924
+ if (Yes == copts.circular) {
925
+ oj_cache8_new(&out.circ_cache);
926
+ }
927
+ // dump_rails_val(*argv, 0, &out, true);
928
+ rb_protect(protect_dump, (VALUE)&oo, &line);
929
+
930
+ if (0 == line) {
931
+ if (0 < out.indent) {
932
+ switch (*(out.cur - 1)) {
933
+ case ']':
934
+ case '}': assure_size(&out, 2); *out.cur++ = '\n';
935
+ default: break;
936
+ }
937
+ }
938
+ *out.cur = '\0';
939
+
940
+ if (0 == out.buf) {
941
+ rb_raise(rb_eNoMemError, "Not enough memory.");
942
+ }
943
+ rstr = rb_utf8_str_new_cstr(out.buf);
944
+ }
945
+ if (Yes == copts.circular) {
946
+ oj_cache8_delete(out.circ_cache);
947
+ }
948
+
949
+ oj_out_free(&out);
950
+
951
+ if (0 != line) {
952
+ rb_jump_tag(line);
953
+ }
954
+ return rstr;
955
+ }
956
+
957
+ /* Document-method: encode
958
+ * call-seq: encode(obj)
959
+ *
960
+ * - *obj* [_Object_] object to encode
961
+ *
962
+ * Returns encoded object as a JSON string.
963
+ */
964
+ static VALUE encoder_encode(VALUE self, VALUE obj) {
965
+ Encoder e;
966
+ TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);
967
+
968
+ if (Qnil != e->arg) {
969
+ VALUE argv[1] = {e->arg};
970
+
971
+ return encode(obj, &e->ropts, &e->opts, 1, argv);
972
+ }
973
+ return encode(obj, &e->ropts, &e->opts, 0, NULL);
974
+ }
975
+
976
+ /* Document-method: encode
977
+ * call-seq: encode(obj, opts=nil)
978
+ *
979
+ * Encode obj as a JSON String.
980
+ *
981
+ * - *obj* [_Object_|Hash|Array] object to convert to a JSON String
982
+ * - *opts* [_Hash_] options
983
+ *
984
+ * Returns [_String_]
985
+ */
986
+ static VALUE rails_encode(int argc, VALUE *argv, VALUE self) {
987
+ if (1 > argc) {
988
+ rb_raise(rb_eArgError, "wrong number of arguments (0 for 1).");
989
+ }
990
+ if (1 == argc) {
991
+ return encode(*argv, NULL, &oj_default_options, 0, NULL);
992
+ } else {
993
+ return encode(*argv, NULL, &oj_default_options, argc - 1, argv + 1);
994
+ }
995
+ }
996
+
997
+ static VALUE rails_use_standard_json_time_format(VALUE self, VALUE state) {
998
+ if (Qtrue == state || Qfalse == state) {
999
+ // no change needed
1000
+ } else if (Qnil == state) {
1001
+ state = Qfalse;
1002
+ } else {
1003
+ state = Qtrue;
1004
+ }
1005
+ rb_iv_set(self, "@use_standard_json_time_format", state);
1006
+ xml_time = Qtrue == state;
1007
+
1008
+ return state;
1009
+ }
1010
+
1011
+ static VALUE rails_use_standard_json_time_format_get(VALUE self) {
1012
+ return xml_time ? Qtrue : Qfalse;
1013
+ }
1014
+
1015
+ static VALUE rails_escape_html_entities_in_json(VALUE self, VALUE state) {
1016
+ rb_iv_set(self, "@escape_html_entities_in_json", state);
1017
+ escape_html = Qtrue == state;
1018
+
1019
+ return state;
1020
+ }
1021
+
1022
+ static VALUE rails_escape_html_entities_in_json_get(VALUE self) {
1023
+ return escape_html ? Qtrue : Qfalse;
1024
+ }
1025
+
1026
+ static VALUE rails_time_precision(VALUE self, VALUE prec) {
1027
+ rb_iv_set(self, "@time_precision", prec);
1028
+ oj_default_options.sec_prec = NUM2INT(prec);
1029
+ oj_default_options.sec_prec_set = true;
1030
+
1031
+ return prec;
1032
+ }
1033
+
1034
+ /* Document-method: set_encoder
1035
+ * call-seq: set_encoder()
1036
+ *
1037
+ * Sets the ActiveSupport.encoder to Oj::Rails::Encoder and wraps some of the
1038
+ * formatting globals used by ActiveSupport to allow the use of those globals
1039
+ * in the Oj::Rails optimizations.
1040
+ */
1041
+ static VALUE rails_set_encoder(VALUE self) {
1042
+ VALUE active;
1043
+ VALUE json;
1044
+ VALUE encoding;
1045
+ VALUE pv;
1046
+ VALUE verbose;
1047
+ VALUE enc = resolve_classpath("ActiveSupport::JSON::Encoding");
1048
+
1049
+ if (Qnil != enc) {
1050
+ escape_html = Qtrue == rb_iv_get(self, "@escape_html_entities_in_json");
1051
+ xml_time = Qtrue == rb_iv_get(enc, "@use_standard_json_time_format");
1052
+ }
1053
+ if (rb_const_defined_at(rb_cObject, rb_intern("ActiveSupport"))) {
1054
+ active = rb_const_get_at(rb_cObject, rb_intern("ActiveSupport"));
1055
+ } else {
1056
+ rb_raise(rb_eStandardError, "ActiveSupport not loaded.");
1057
+ }
1058
+ rb_funcall(active, rb_intern("json_encoder="), 1, encoder_class);
1059
+
1060
+ json = rb_const_get_at(active, rb_intern("JSON"));
1061
+ encoding = rb_const_get_at(json, rb_intern("Encoding"));
1062
+
1063
+ // rb_undef_method doesn't work for modules or maybe sometimes
1064
+ // doesn't. Anyway setting verbose should hide the warning.
1065
+ verbose = rb_gv_get("$VERBOSE");
1066
+ rb_gv_set("$VERBOSE", Qfalse);
1067
+ rb_undef_method(encoding, "use_standard_json_time_format=");
1068
+ rb_define_module_function(encoding, "use_standard_json_time_format=", rails_use_standard_json_time_format, 1);
1069
+ rb_undef_method(encoding, "use_standard_json_time_format");
1070
+ rb_define_module_function(encoding, "use_standard_json_time_format", rails_use_standard_json_time_format_get, 0);
1071
+
1072
+ pv = rb_iv_get(encoding, "@escape_html_entities_in_json");
1073
+ escape_html = Qtrue == pv;
1074
+ rb_undef_method(encoding, "escape_html_entities_in_json=");
1075
+ rb_define_module_function(encoding, "escape_html_entities_in_json=", rails_escape_html_entities_in_json, 1);
1076
+ rb_undef_method(encoding, "escape_html_entities_in_json");
1077
+ rb_define_module_function(encoding, "escape_html_entities_in_json", rails_escape_html_entities_in_json_get, 0);
1078
+
1079
+ pv = rb_iv_get(encoding, "@time_precision");
1080
+ oj_default_options.sec_prec = NUM2INT(pv);
1081
+ oj_default_options.sec_prec_set = true;
1082
+ rb_undef_method(encoding, "time_precision=");
1083
+ rb_define_module_function(encoding, "time_precision=", rails_time_precision, 1);
1084
+ rb_gv_set("$VERBOSE", verbose);
1085
+
1086
+ return Qnil;
1087
+ }
1088
+
1089
+ /* Document-method: set_decoder
1090
+ * call-seq: set_decoder()
1091
+ *
1092
+ * Sets the JSON.parse function to be the Oj::parse function which is json gem
1093
+ * compatible.
1094
+ */
1095
+ static VALUE rails_set_decoder(VALUE self) {
1096
+ VALUE json;
1097
+ VALUE json_error;
1098
+ VALUE verbose;
1099
+
1100
+ if (rb_const_defined_at(rb_cObject, rb_intern("JSON"))) {
1101
+ json = rb_const_get_at(rb_cObject, rb_intern("JSON"));
1102
+ } else {
1103
+ json = rb_define_module("JSON");
1104
+ }
1105
+ if (rb_const_defined_at(json, rb_intern("JSONError"))) {
1106
+ json_error = rb_const_get(json, rb_intern("JSONError"));
1107
+ } else {
1108
+ json_error = rb_define_class_under(json, "JSONError", rb_eStandardError);
1109
+ }
1110
+
1111
+ rb_global_variable(&oj_json_parser_error_class);
1112
+ if (rb_const_defined_at(json, rb_intern("ParserError"))) {
1113
+ oj_json_parser_error_class = rb_const_get(json, rb_intern("ParserError"));
1114
+ } else {
1115
+ oj_json_parser_error_class = rb_define_class_under(json, "ParserError", json_error);
1116
+ }
1117
+ // rb_undef_method doesn't work for modules or maybe sometimes
1118
+ // doesn't. Anyway setting verbose should hide the warning.
1119
+ verbose = rb_gv_get("$VERBOSE");
1120
+ rb_gv_set("$VERBOSE", Qfalse);
1121
+ rb_undef_method(json, "parse");
1122
+ rb_define_module_function(json, "parse", oj_mimic_parse, -1);
1123
+ rb_gv_set("$VERBOSE", verbose);
1124
+
1125
+ return Qnil;
1126
+ }
1127
+
1128
+ /* Document-module: Oj.optimize_rails()
1129
+ *
1130
+ * Sets the Oj as the Rails encoder and decoder. Oj::Rails.optimize is also
1131
+ * called.
1132
+ */
1133
+ VALUE
1134
+ oj_optimize_rails(VALUE self) {
1135
+ rails_set_encoder(self);
1136
+ rails_set_decoder(self);
1137
+ rails_optimize(0, NULL, self);
1138
+ rails_mimic_json(self);
1139
+
1140
+ return Qnil;
1141
+ }
1142
+
1143
+ /* Document-module: Oj::Rails
1144
+ *
1145
+ * Module that provides rails and active support compatibility.
1146
+ */
1147
+ /* Document-class: Oj::Rails::Encoder
1148
+ *
1149
+ * The Oj ActiveSupport compliant encoder.
1150
+ */
1151
+ void oj_mimic_rails_init(void) {
1152
+ VALUE rails = rb_define_module_under(Oj, "Rails");
1153
+
1154
+ rb_define_module_function(rails, "encode", rails_encode, -1);
1155
+
1156
+ encoder_class = rb_define_class_under(rails, "Encoder", rb_cObject);
1157
+ rb_gc_register_address(&encoder_class);
1158
+ rb_undef_alloc_func(encoder_class);
1159
+
1160
+ rb_define_module_function(encoder_class, "new", encoder_new, -1);
1161
+ rb_define_module_function(rails, "optimize", rails_optimize, -1);
1162
+ rb_define_module_function(rails, "deoptimize", rails_deoptimize, -1);
1163
+ rb_define_module_function(rails, "optimized?", rails_optimized, 1);
1164
+ rb_define_module_function(rails, "mimic_JSON", rails_mimic_json, 0);
1165
+
1166
+ rb_define_module_function(rails, "set_encoder", rails_set_encoder, 0);
1167
+ rb_define_module_function(rails, "set_decoder", rails_set_decoder, 0);
1168
+
1169
+ rb_define_method(encoder_class, "encode", encoder_encode, 1);
1170
+ rb_define_method(encoder_class, "optimize", encoder_optimize, -1);
1171
+ rb_define_method(encoder_class, "deoptimize", encoder_deoptimize, -1);
1172
+ rb_define_method(encoder_class, "optimized?", encoder_optimized, 1);
1173
+ }
1174
+
1175
+ static void dump_to_hash(VALUE obj, int depth, Out out) {
1176
+ dump_rails_val(rb_funcall(obj, oj_to_hash_id, 0), depth, out, true);
1177
+ }
1178
+
1179
+ static void dump_float(VALUE obj, int depth, Out out, bool as_ok) {
1180
+ char buf[64];
1181
+ char *b;
1182
+ double d = rb_num2dbl(obj);
1183
+ size_t cnt = 0;
1184
+
1185
+ if (0.0 == d) {
1186
+ b = buf;
1187
+ *b++ = '0';
1188
+ *b++ = '.';
1189
+ *b++ = '0';
1190
+ *b++ = '\0';
1191
+ cnt = 3;
1192
+ } else {
1193
+ if (isnan(d) || OJ_INFINITY == d || -OJ_INFINITY == d) {
1194
+ strcpy(buf, "null");
1195
+ cnt = 4;
1196
+ } else if (d == (double)(long long int)d) {
1197
+ cnt = snprintf(buf, sizeof(buf), "%.1f", d);
1198
+ } else if (oj_rails_float_opt) {
1199
+ cnt = oj_dump_float_printf(buf, sizeof(buf), obj, d, "%0.16g");
1200
+ } else {
1201
+ volatile VALUE rstr = oj_safe_string_convert(obj);
1202
+
1203
+ strcpy(buf, RSTRING_PTR(rstr));
1204
+ cnt = RSTRING_LEN(rstr);
1205
+ }
1206
+ }
1207
+ assure_size(out, cnt);
1208
+ for (b = buf; '\0' != *b; b++) {
1209
+ *out->cur++ = *b;
1210
+ }
1211
+ *out->cur = '\0';
1212
+ }
1213
+
1214
+ static void dump_array(VALUE a, int depth, Out out, bool as_ok) {
1215
+ size_t size;
1216
+ size_t i;
1217
+ size_t cnt;
1218
+ int d2 = depth + 1;
1219
+
1220
+ if (Yes == out->opts->circular) {
1221
+ if (0 > oj_check_circular(a, out)) {
1222
+ oj_dump_nil(Qnil, 0, out, false);
1223
+ return;
1224
+ }
1225
+ }
1226
+ // if (!oj_rails_array_opt && as_ok && 0 < out->argc && rb_respond_to(a, oj_as_json_id)) {
1227
+ if (as_ok && 0 < out->argc && rb_respond_to(a, oj_as_json_id)) {
1228
+ dump_as_json(a, depth, out, false);
1229
+ return;
1230
+ }
1231
+ cnt = RARRAY_LEN(a);
1232
+ *out->cur++ = '[';
1233
+ size = 2;
1234
+ assure_size(out, size);
1235
+ if (0 == cnt) {
1236
+ *out->cur++ = ']';
1237
+ } else {
1238
+ if (out->opts->dump_opts.use) {
1239
+ size = d2 * out->opts->dump_opts.indent_size + out->opts->dump_opts.array_size + 1;
1240
+ } else {
1241
+ size = d2 * out->indent + 2;
1242
+ }
1243
+ assure_size(out, size * cnt);
1244
+ cnt--;
1245
+ for (i = 0; i <= cnt; i++) {
1246
+ if (out->opts->dump_opts.use) {
1247
+ if (0 < out->opts->dump_opts.array_size) {
1248
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
1249
+ }
1250
+ if (0 < out->opts->dump_opts.indent_size) {
1251
+ int i;
1252
+ for (i = d2; 0 < i; i--) {
1253
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
1254
+ }
1255
+ }
1256
+ } else {
1257
+ fill_indent(out, d2);
1258
+ }
1259
+ dump_rails_val(RARRAY_AREF(a, i), d2, out, true);
1260
+ if (i < cnt) {
1261
+ *out->cur++ = ',';
1262
+ }
1263
+ }
1264
+ size = depth * out->indent + 1;
1265
+ assure_size(out, size);
1266
+ if (out->opts->dump_opts.use) {
1267
+ if (0 < out->opts->dump_opts.array_size) {
1268
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
1269
+ }
1270
+ if (0 < out->opts->dump_opts.indent_size) {
1271
+ int i;
1272
+
1273
+ for (i = depth; 0 < i; i--) {
1274
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
1275
+ }
1276
+ }
1277
+ } else {
1278
+ fill_indent(out, depth);
1279
+ }
1280
+ *out->cur++ = ']';
1281
+ }
1282
+ *out->cur = '\0';
1283
+ }
1284
+
1285
+ static int hash_cb(VALUE key, VALUE value, VALUE ov) {
1286
+ Out out = (Out)ov;
1287
+ int depth = out->depth;
1288
+ long size;
1289
+ int rtype = rb_type(key);
1290
+
1291
+ if (out->omit_nil && Qnil == value) {
1292
+ return ST_CONTINUE;
1293
+ }
1294
+ if (rtype != T_STRING && rtype != T_SYMBOL) {
1295
+ key = oj_safe_string_convert(key);
1296
+ rtype = rb_type(key);
1297
+ }
1298
+ if (!out->opts->dump_opts.use) {
1299
+ size = depth * out->indent + 1;
1300
+ assure_size(out, size);
1301
+ fill_indent(out, depth);
1302
+ if (rtype == T_STRING) {
1303
+ oj_dump_str(key, 0, out, false);
1304
+ } else {
1305
+ oj_dump_sym(key, 0, out, false);
1306
+ }
1307
+ *out->cur++ = ':';
1308
+ } else {
1309
+ size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
1310
+ assure_size(out, size);
1311
+ if (0 < out->opts->dump_opts.hash_size) {
1312
+ APPEND_CHARS(out->cur, out->opts->dump_opts.hash_nl, out->opts->dump_opts.hash_size);
1313
+ }
1314
+ if (0 < out->opts->dump_opts.indent_size) {
1315
+ int i;
1316
+ for (i = depth; 0 < i; i--) {
1317
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
1318
+ }
1319
+ }
1320
+ if (rtype == T_STRING) {
1321
+ oj_dump_str(key, 0, out, false);
1322
+ } else {
1323
+ oj_dump_sym(key, 0, out, false);
1324
+ }
1325
+ size = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
1326
+ assure_size(out, size);
1327
+ if (0 < out->opts->dump_opts.before_size) {
1328
+ APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
1329
+ }
1330
+ *out->cur++ = ':';
1331
+ if (0 < out->opts->dump_opts.after_size) {
1332
+ APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
1333
+ }
1334
+ }
1335
+ dump_rails_val(value, depth, out, true);
1336
+ out->depth = depth;
1337
+ *out->cur++ = ',';
1338
+
1339
+ return ST_CONTINUE;
1340
+ }
1341
+
1342
+ static void dump_hash(VALUE obj, int depth, Out out, bool as_ok) {
1343
+ int cnt;
1344
+ size_t size;
1345
+
1346
+ if (Yes == out->opts->circular) {
1347
+ if (0 > oj_check_circular(obj, out)) {
1348
+ oj_dump_nil(Qnil, 0, out, false);
1349
+ return;
1350
+ }
1351
+ }
1352
+ if ((!oj_rails_hash_opt || 0 < out->argc) && as_ok && rb_respond_to(obj, oj_as_json_id)) {
1353
+ dump_as_json(obj, depth, out, false);
1354
+ return;
1355
+ }
1356
+ cnt = (int)RHASH_SIZE(obj);
1357
+ size = depth * out->indent + 2;
1358
+ assure_size(out, 2);
1359
+ *out->cur++ = '{';
1360
+ if (0 == cnt) {
1361
+ *out->cur++ = '}';
1362
+ } else {
1363
+ out->depth = depth + 1;
1364
+ rb_hash_foreach(obj, hash_cb, (VALUE)out);
1365
+ if (',' == *(out->cur - 1)) {
1366
+ out->cur--; // backup to overwrite last comma
1367
+ }
1368
+ if (!out->opts->dump_opts.use) {
1369
+ assure_size(out, size);
1370
+ fill_indent(out, depth);
1371
+ } else {
1372
+ size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
1373
+ assure_size(out, size);
1374
+ if (0 < out->opts->dump_opts.hash_size) {
1375
+ APPEND_CHARS(out->cur, out->opts->dump_opts.hash_nl, out->opts->dump_opts.hash_size);
1376
+ }
1377
+ if (0 < out->opts->dump_opts.indent_size) {
1378
+ int i;
1379
+
1380
+ for (i = depth; 0 < i; i--) {
1381
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
1382
+ }
1383
+ }
1384
+ }
1385
+ *out->cur++ = '}';
1386
+ }
1387
+ *out->cur = '\0';
1388
+ }
1389
+
1390
+ static void dump_obj(VALUE obj, int depth, Out out, bool as_ok) {
1391
+ VALUE clas;
1392
+
1393
+ if (oj_code_dump(oj_compat_codes, obj, depth, out)) {
1394
+ out->argc = 0;
1395
+ return;
1396
+ }
1397
+ clas = rb_obj_class(obj);
1398
+ if (as_ok) {
1399
+ ROpt ro;
1400
+
1401
+ if (NULL != (ro = oj_rails_get_opt(out->ropts, clas)) && ro->on) {
1402
+ ro->dump(obj, depth, out, as_ok);
1403
+ } else if (Yes == out->opts->raw_json && rb_respond_to(obj, oj_raw_json_id)) {
1404
+ oj_dump_raw_json(obj, depth, out);
1405
+ } else if (rb_respond_to(obj, oj_as_json_id)) {
1406
+ dump_as_json(obj, depth, out, true);
1407
+ } else if (rb_respond_to(obj, oj_to_hash_id)) {
1408
+ dump_to_hash(obj, depth, out);
1409
+ } else if (oj_bigdecimal_class == clas) {
1410
+ dump_bigdecimal(obj, depth, out, false);
1411
+ } else {
1412
+ oj_dump_obj_to_s(obj, out);
1413
+ }
1414
+ } else if (Yes == out->opts->raw_json && rb_respond_to(obj, oj_raw_json_id)) {
1415
+ oj_dump_raw_json(obj, depth, out);
1416
+ } else if (rb_respond_to(obj, oj_to_hash_id)) {
1417
+ // Always attempt to_hash.
1418
+ dump_to_hash(obj, depth, out);
1419
+ } else if (oj_bigdecimal_class == clas) {
1420
+ dump_bigdecimal(obj, depth, out, false);
1421
+ } else {
1422
+ oj_dump_obj_to_s(obj, out);
1423
+ }
1424
+ }
1425
+
1426
+ static DumpFunc rails_funcs[] = {
1427
+ NULL, // RUBY_T_NONE = 0x00,
1428
+ dump_obj, // RUBY_T_OBJECT = 0x01,
1429
+ oj_dump_class, // RUBY_T_CLASS = 0x02,
1430
+ oj_dump_class, // RUBY_T_MODULE = 0x03,
1431
+ dump_float, // RUBY_T_FLOAT = 0x04,
1432
+ oj_dump_str, // RUBY_T_STRING = 0x05,
1433
+ dump_regexp, // RUBY_T_REGEXP = 0x06,
1434
+ // dump_as_string, // RUBY_T_REGEXP = 0x06,
1435
+ dump_array, // RUBY_T_ARRAY = 0x07,
1436
+ dump_hash, // RUBY_T_HASH = 0x08,
1437
+ dump_obj, // RUBY_T_STRUCT = 0x09,
1438
+ oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
1439
+ dump_as_string, // RUBY_T_FILE = 0x0b,
1440
+ dump_obj, // RUBY_T_DATA = 0x0c,
1441
+ NULL, // RUBY_T_MATCH = 0x0d,
1442
+ // Rails raises a stack error on Complex and Rational. It also corrupts
1443
+ // something which causes a segfault on the next call. Oj will not mimic
1444
+ // that behavior.
1445
+ dump_as_string, // RUBY_T_COMPLEX = 0x0e,
1446
+ dump_as_string, // RUBY_T_RATIONAL = 0x0f,
1447
+ NULL, // 0x10
1448
+ oj_dump_nil, // RUBY_T_NIL = 0x11,
1449
+ oj_dump_true, // RUBY_T_TRUE = 0x12,
1450
+ oj_dump_false, // RUBY_T_FALSE = 0x13,
1451
+ oj_dump_sym, // RUBY_T_SYMBOL = 0x14,
1452
+ oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
1453
+ };
1454
+
1455
+ static void dump_rails_val(VALUE obj, int depth, Out out, bool as_ok) {
1456
+ int type = rb_type(obj);
1457
+
1458
+ TRACE(out->opts->trace, "dump", obj, depth, TraceIn);
1459
+ if (MAX_DEPTH < depth) {
1460
+ rb_raise(rb_eNoMemError, "Too deeply nested.\n");
1461
+ }
1462
+ if (0 < type && type <= RUBY_T_FIXNUM) {
1463
+ DumpFunc f = rails_funcs[type];
1464
+
1465
+ if (NULL != f) {
1466
+ f(obj, depth, out, as_ok);
1467
+ TRACE(out->opts->trace, "dump", obj, depth, TraceOut);
1468
+ return;
1469
+ }
1470
+ }
1471
+ oj_dump_nil(Qnil, depth, out, false);
1472
+ TRACE(out->opts->trace, "dump", Qnil, depth, TraceOut);
1473
+ }
1474
+
1475
+ void oj_dump_rails_val(VALUE obj, int depth, Out out) {
1476
+ out->opts->str_rx.head = NULL;
1477
+ out->opts->str_rx.tail = NULL;
1478
+ if (escape_html) {
1479
+ out->opts->escape_mode = RailsXEsc;
1480
+ } else {
1481
+ out->opts->escape_mode = RailsEsc;
1482
+ }
1483
+ dump_rails_val(obj, depth, out, true);
1484
+ }