File: rails.c

package info (click to toggle)
ruby-oj 3.16.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,208 kB
  • sloc: ansic: 20,028; ruby: 11,642; sh: 70; makefile: 17
file content (1480 lines) | stat: -rw-r--r-- 45,885 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
// Copyright (c) 2017 Peter Ohler. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license details.

#include "rails.h"

#include "code.h"
#include "encode.h"
#include "mem.h"
#include "trace.h"
#include "util.h"

#define OJ_INFINITY (1.0 / 0.0)

// TBD keep static array of strings and functions to help with rails optimization
typedef struct _encoder {
    struct _rOptTable ropts;
    struct _options   opts;
    VALUE             arg;
} *Encoder;

bool oj_rails_hash_opt  = false;
bool oj_rails_array_opt = false;
bool oj_rails_float_opt = false;

extern void oj_mimic_json_methods(VALUE json);

static void dump_rails_val(VALUE obj, int depth, Out out, bool as_ok);

extern VALUE Oj;

static struct _rOptTable ropts = {0, 0, NULL};

static VALUE encoder_class = Qnil;
static bool  escape_html   = true;
static bool  xml_time      = true;

static ROpt create_opt(ROptTable rot, VALUE clas);

ROpt oj_rails_get_opt(ROptTable rot, VALUE clas) {
    if (NULL == rot) {
        rot = &ropts;
    }
    if (0 < rot->len) {
        int   lo = 0;
        int   hi = rot->len - 1;
        int   mid;
        VALUE v;

        if (clas < rot->table->clas || rot->table[hi].clas < clas) {
            return NULL;
        }
        if (rot->table[lo].clas == clas) {
            return rot->table;
        }
        if (rot->table[hi].clas == clas) {
            return &rot->table[hi];
        }
        while (2 <= hi - lo) {
            mid = (hi + lo) / 2;
            v   = rot->table[mid].clas;
            if (v == clas) {
                return &rot->table[mid];
            }
            if (v < clas) {
                lo = mid;
            } else {
                hi = mid;
            }
        }
    }
    return NULL;
}

static ROptTable copy_opts(ROptTable src, ROptTable dest) {
    dest->len  = src->len;
    dest->alen = src->alen;
    if (NULL == src->table) {
        dest->table = NULL;
    } else {
        dest->table = OJ_R_ALLOC_N(struct _rOpt, dest->alen);
        memcpy(dest->table, src->table, sizeof(struct _rOpt) * dest->alen);
    }
    return NULL;
}

static int dump_attr_cb(ID key, VALUE value, VALUE ov) {
    Out         out   = (Out)ov;
    int         depth = out->depth;
    size_t      size  = depth * out->indent + 1;
    const char *attr  = rb_id2name(key);

    // Some exceptions such as NoMethodError have an invisible attribute where
    // the key name is NULL. Not an empty string but NULL.
    if (NULL == attr) {
        attr = "";
    }
    if (0 == strcmp("bt", attr) || 0 == strcmp("mesg", attr)) {
        return ST_CONTINUE;
    }
    assure_size(out, size);
    fill_indent(out, depth);
    if ('@' == *attr) {
        attr++;
        oj_dump_cstr(attr, strlen(attr), 0, 0, out);
    } else {
        char buf[32];

        *buf = '~';
        strncpy(buf + 1, attr, sizeof(buf) - 2);
        buf[sizeof(buf) - 1] = '\0';
        oj_dump_cstr(buf, strlen(buf), 0, 0, out);
    }
    *out->cur++ = ':';
    dump_rails_val(value, depth, out, true);
    out->depth  = depth;
    *out->cur++ = ',';

    return ST_CONTINUE;
}

static void dump_obj_attrs(VALUE obj, int depth, Out out, bool as_ok) {
    assure_size(out, 2);
    *out->cur++ = '{';
    out->depth  = depth + 1;
    rb_ivar_foreach(obj, dump_attr_cb, (VALUE)out);
    if (',' == *(out->cur - 1)) {
        out->cur--;  // backup to overwrite last comma
    }
    out->depth = depth;
    fill_indent(out, depth);
    *out->cur++ = '}';
    *out->cur   = '\0';
}

static void dump_struct(VALUE obj, int depth, Out out, bool as_ok) {
    int            d3      = depth + 2;
    size_t         size    = d3 * out->indent + 2;
    size_t         sep_len = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
    volatile VALUE ma;
    volatile VALUE v;
    int            cnt;
    int            i;
    size_t         len;
    const char    *name;

#ifdef RSTRUCT_LEN
#if RSTRUCT_LEN_RETURNS_INTEGER_OBJECT
    cnt = (int)NUM2LONG(RSTRUCT_LEN(obj));
#else   // RSTRUCT_LEN_RETURNS_INTEGER_OBJECT
    cnt = (int)RSTRUCT_LEN(obj);
#endif  // RSTRUCT_LEN_RETURNS_INTEGER_OBJECT
#else
    // This is a bit risky as a struct in C ruby is not the same as a Struct
    // class in interpreted Ruby so length() may not be defined.
    cnt = FIX2INT(rb_funcall(obj, oj_length_id, 0));
#endif
    ma = rb_struct_s_members(rb_obj_class(obj));
    assure_size(out, 2);
    *out->cur++ = '{';
    for (i = 0; i < cnt; i++) {
        volatile VALUE s = rb_sym2str(RARRAY_AREF(ma, i));

        name = RSTRING_PTR(s);
        len  = RSTRING_LEN(s);
        assure_size(out, size + sep_len + 6);
        if (0 < i) {
            *out->cur++ = ',';
        }
        fill_indent(out, d3);
        *out->cur++ = '"';
        APPEND_CHARS(out->cur, name, len);
        *out->cur++ = '"';
        if (0 < out->opts->dump_opts.before_size) {
            APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
        }
        *out->cur++ = ':';
        if (0 < out->opts->dump_opts.after_size) {
            APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
        }
#ifdef RSTRUCT_LEN
        v = RSTRUCT_GET(obj, i);
#else
        v = rb_struct_aref(obj, INT2FIX(i));
#endif
        dump_rails_val(v, d3, out, true);
    }
    fill_indent(out, depth);
    *out->cur++ = '}';
    *out->cur   = '\0';
}

static ID to_a_id = 0;

static void dump_enumerable(VALUE obj, int depth, Out out, bool as_ok) {
    if (0 == to_a_id) {
        to_a_id = rb_intern("to_a");
    }
    dump_rails_val(rb_funcall(obj, to_a_id, 0), depth, out, false);
}

static void dump_bigdecimal(VALUE obj, int depth, Out out, bool as_ok) {
    volatile VALUE rstr = oj_safe_string_convert(obj);
    const char    *str  = RSTRING_PTR(rstr);

    if ('I' == *str || 'N' == *str || ('-' == *str && 'I' == str[1])) {
        oj_dump_nil(Qnil, depth, out, false);
    } else if (out->opts->int_range_max != 0 || out->opts->int_range_min != 0) {
        oj_dump_cstr(str, RSTRING_LEN(rstr), 0, 0, out);
    } else if (Yes == out->opts->bigdec_as_num) {
        oj_dump_raw(str, RSTRING_LEN(rstr), out);
    } else {
        oj_dump_cstr(str, RSTRING_LEN(rstr), 0, 0, out);
    }
}

static void dump_sec_nano(VALUE obj, int64_t sec, long nsec, Out out) {
    char             buf[64];
    struct _timeInfo ti;
    long             one    = 1000000000;
    long             tzsecs = NUM2LONG(rb_funcall2(obj, oj_utc_offset_id, 0, 0));
    int              tzhour, tzmin;
    char             tzsign = '+';
    int              len;

    if (out->end - out->cur <= 36) {
        assure_size(out, 36);
    }
    if (9 > out->opts->sec_prec) {
        int i;

        // Rails does not round when reducing precision but instead floors,
        for (i = 9 - out->opts->sec_prec; 0 < i; i--) {
            nsec = nsec / 10;
            one /= 10;
        }
        if (one <= nsec) {
            nsec -= one;
            sec++;
        }
    }
    // 2012-01-05T23:58:07.123456000+09:00 or 2012/01/05 23:58:07 +0900
    sec += tzsecs;
    sec_as_time(sec, &ti);
    if (0 > tzsecs) {
        tzsign = '-';
        tzhour = (int)(tzsecs / -3600);
        tzmin  = (int)(tzsecs / -60) - (tzhour * 60);
    } else {
        tzhour = (int)(tzsecs / 3600);
        tzmin  = (int)(tzsecs / 60) - (tzhour * 60);
    }
    if (!xml_time) {
        len = sprintf(buf,
                      "%04d/%02d/%02d %02d:%02d:%02d %c%02d%02d",
                      ti.year,
                      ti.mon,
                      ti.day,
                      ti.hour,
                      ti.min,
                      ti.sec,
                      tzsign,
                      tzhour,
                      tzmin);
    } else if (0 == out->opts->sec_prec) {
        if (0 == tzsecs && rb_funcall2(obj, oj_utcq_id, 0, 0)) {
            len = sprintf(buf, "%04d-%02d-%02dT%02d:%02d:%02dZ", ti.year, ti.mon, ti.day, ti.hour, ti.min, ti.sec);
        } else {
            len = sprintf(buf,
                          "%04d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
                          ti.year,
                          ti.mon,
                          ti.day,
                          ti.hour,
                          ti.min,
                          ti.sec,
                          tzsign,
                          tzhour,
                          tzmin);
        }
    } else if (0 == tzsecs && rb_funcall2(obj, oj_utcq_id, 0, 0)) {
        char format[64] = "%04d-%02d-%02dT%02d:%02d:%02d.%09ldZ";

        len = 30;
        if (9 > out->opts->sec_prec) {
            format[32] = '0' + out->opts->sec_prec;
            len -= 9 - out->opts->sec_prec;
        }
        len = sprintf(buf, format, ti.year, ti.mon, ti.day, ti.hour, ti.min, ti.sec, nsec);
    } else {
        char format[64] = "%04d-%02d-%02dT%02d:%02d:%02d.%09ld%c%02d:%02d";

        len = 35;
        if (9 > out->opts->sec_prec) {
            format[32] = '0' + out->opts->sec_prec;
            len -= 9 - out->opts->sec_prec;
        }
        len = sprintf(buf, format, ti.year, ti.mon, ti.day, ti.hour, ti.min, ti.sec, nsec, tzsign, tzhour, tzmin);
    }
    oj_dump_cstr(buf, len, 0, 0, out);
}

static void dump_time(VALUE obj, int depth, Out out, bool as_ok) {
    long long sec;
    long long nsec;

    if (16 <= sizeof(struct timespec)) {
        struct timespec ts = rb_time_timespec(obj);

        sec  = (long long)ts.tv_sec;
        nsec = ts.tv_nsec;
    } else {
        sec  = NUM2LL(rb_funcall2(obj, oj_tv_sec_id, 0, 0));
        nsec = NUM2LL(rb_funcall2(obj, oj_tv_nsec_id, 0, 0));
    }
    dump_sec_nano(obj, sec, nsec, out);
}

static void dump_timewithzone(VALUE obj, int depth, Out out, bool as_ok) {
    int64_t   sec  = NUM2LONG(rb_funcall2(obj, oj_tv_sec_id, 0, 0));
    long long nsec = 0;

    if (rb_respond_to(obj, oj_tv_nsec_id)) {
        nsec = NUM2LL(rb_funcall2(obj, oj_tv_nsec_id, 0, 0));
    } else if (rb_respond_to(obj, oj_tv_usec_id)) {
        nsec = NUM2LL(rb_funcall2(obj, oj_tv_usec_id, 0, 0)) * 1000;
    }
    dump_sec_nano(obj, sec, nsec, out);
}

static void dump_to_s(VALUE obj, int depth, Out out, bool as_ok) {
    volatile VALUE rstr = oj_safe_string_convert(obj);

    oj_dump_cstr(RSTRING_PTR(rstr), RSTRING_LEN(rstr), 0, 0, out);
}

static ID parameters_id = 0;

typedef struct _strLen {
    const char *str;
    size_t      len;
} *StrLen;

static void dump_actioncontroller_parameters(VALUE obj, int depth, Out out, bool as_ok) {
    if (0 == parameters_id) {
        parameters_id = rb_intern("@parameters");
    }
    out->argc = 0;
    dump_rails_val(rb_ivar_get(obj, parameters_id), depth, out, true);
}

static StrLen columns_array(VALUE rcols, int *ccnt) {
    volatile VALUE v;
    StrLen         cp;
    StrLen         cols;
    size_t         i;
    size_t         cnt = RARRAY_LEN(rcols);

    *ccnt = (int)cnt;
    cols  = OJ_R_ALLOC_N(struct _strLen, cnt);
    for (i = 0, cp = cols; i < cnt; i++, cp++) {
        v = RARRAY_AREF(rcols, i);
        if (T_STRING != rb_type(v)) {
            v = oj_safe_string_convert(v);
        }
        cp->str = StringValuePtr(v);
        cp->len = RSTRING_LEN(v);
    }
    return cols;
}

static void dump_row(VALUE row, StrLen cols, int ccnt, int depth, Out out) {
    size_t size;
    int    d2 = depth + 1;
    int    i;

    assure_size(out, 2);
    *out->cur++ = '{';
    size        = depth * out->indent + 3;
    for (i = 0; i < ccnt; i++, cols++) {
        assure_size(out, size);
        if (out->opts->dump_opts.use) {
            if (0 < out->opts->dump_opts.array_size) {
                APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
            }
            if (0 < out->opts->dump_opts.indent_size) {
                int i;
                for (i = d2; 0 < i; i--) {
                    APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
                }
            }
        } else {
            fill_indent(out, d2);
        }
        oj_dump_cstr(cols->str, cols->len, 0, 0, out);
        *out->cur++ = ':';
        dump_rails_val(RARRAY_AREF(row, i), depth, out, true);
        if (i < ccnt - 1) {
            *out->cur++ = ',';
        }
    }
    size = depth * out->indent + 1;
    assure_size(out, size);
    if (out->opts->dump_opts.use) {
        if (0 < out->opts->dump_opts.array_size) {
            APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
        }
        if (0 < out->opts->dump_opts.indent_size) {
            int i;

            for (i = depth; 0 < i; i--) {
                APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
            }
        }
    } else {
        fill_indent(out, depth);
    }
    *out->cur++ = '}';
}

static ID rows_id    = 0;
static ID columns_id = 0;

static void dump_activerecord_result(VALUE obj, int depth, Out out, bool as_ok) {
    volatile VALUE rows;
    StrLen         cols;
    int            ccnt = 0;
    size_t         i;
    size_t         rcnt;
    size_t         size;
    int            d2 = depth + 1;

    if (0 == rows_id) {
        rows_id    = rb_intern("@rows");
        columns_id = rb_intern("@columns");
    }
    out->argc = 0;
    cols      = columns_array(rb_ivar_get(obj, columns_id), &ccnt);
    rows      = rb_ivar_get(obj, rows_id);
    rcnt      = RARRAY_LEN(rows);
    assure_size(out, 2);
    *out->cur++ = '[';
    if (out->opts->dump_opts.use) {
        size = d2 * out->opts->dump_opts.indent_size + out->opts->dump_opts.array_size + 1;
    } else {
        size = d2 * out->indent + 2;
    }
    assure_size(out, 2);
    for (i = 0; i < rcnt; i++) {
        assure_size(out, size);
        if (out->opts->dump_opts.use) {
            if (0 < out->opts->dump_opts.array_size) {
                APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
            }
            if (0 < out->opts->dump_opts.indent_size) {
                int i;
                for (i = d2; 0 < i; i--) {
                    APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
                }
            }
        } else {
            fill_indent(out, d2);
        }
        dump_row(RARRAY_AREF(rows, i), cols, ccnt, d2, out);
        if (i < rcnt - 1) {
            *out->cur++ = ',';
        }
    }
    OJ_R_FREE(cols);
    size = depth * out->indent + 1;
    assure_size(out, size);
    if (out->opts->dump_opts.use) {
        if (0 < out->opts->dump_opts.array_size) {
            APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
        }
        if (0 < out->opts->dump_opts.indent_size) {
            int i;

            for (i = depth; 0 < i; i--) {
                APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
            }
        }
    } else {
        fill_indent(out, depth);
    }
    *out->cur++ = ']';
}

typedef struct _namedFunc {
    const char *name;
    DumpFunc    func;
} *NamedFunc;

static void dump_as_string(VALUE obj, int depth, Out out, bool as_ok) {
    if (oj_code_dump(oj_compat_codes, obj, depth, out)) {
        out->argc = 0;
        return;
    }
    oj_dump_obj_to_s(obj, out);
}

static void dump_as_json(VALUE obj, int depth, Out out, bool as_ok) {
    volatile VALUE ja;

    TRACE(out->opts->trace, "as_json", obj, depth + 1, TraceRubyIn);
    // Some classes elect to not take an options argument so check the arity
    // of as_json.
    if (0 == rb_obj_method_arity(obj, oj_as_json_id)) {
        ja = rb_funcall(obj, oj_as_json_id, 0);
    } else {
        ja = rb_funcall2(obj, oj_as_json_id, out->argc, out->argv);
    }
    TRACE(out->opts->trace, "as_json", obj, depth + 1, TraceRubyOut);

    out->argc = 0;
    if (ja == obj || !as_ok) {
        // Once as_json is called it should never be called again on the same
        // object with as_ok.
        dump_rails_val(ja, depth, out, false);
    } else {
        int type = rb_type(ja);

        if (T_HASH == type || T_ARRAY == type) {
            dump_rails_val(ja, depth, out, true);
        } else {
            dump_rails_val(ja, depth, out, true);
        }
    }
}

static void dump_regexp(VALUE obj, int depth, Out out, bool as_ok) {
    if (as_ok && rb_respond_to(obj, oj_as_json_id)) {
        dump_as_json(obj, depth, out, false);
        return;
    }
    dump_as_string(obj, depth, out, as_ok);
}

static struct _namedFunc dump_map[] = {
    {"ActionController::Parameters", dump_actioncontroller_parameters},
    {"ActiveRecord::Result", dump_activerecord_result},
    {"ActiveSupport::TimeWithZone", dump_timewithzone},
    {"BigDecimal", dump_bigdecimal},
    {"Range", dump_to_s},
    {"Regexp", dump_regexp},
    //{ "Regexp", dump_to_s },
    {"Time", dump_time},
    {NULL, NULL},
};

static VALUE activerecord_base = Qundef;
static ID    attributes_id     = 0;

static void dump_activerecord(VALUE obj, int depth, Out out, bool as_ok) {
    if (0 == attributes_id) {
        attributes_id = rb_intern("@attributes");
    }
    out->argc = 0;
    dump_rails_val(rb_ivar_get(obj, attributes_id), depth, out, true);
}

static ROpt create_opt(ROptTable rot, VALUE clas) {
    ROpt        ro;
    NamedFunc   nf;
    const char *classname = rb_class2name(clas);
    int         olen      = rot->len;

    rot->len++;
    if (NULL == rot->table) {
        rot->alen  = 256;
        rot->table = OJ_R_ALLOC_N(struct _rOpt, rot->alen);
        memset(rot->table, 0, sizeof(struct _rOpt) * rot->alen);
    } else if (rot->alen <= rot->len) {
        rot->alen *= 2;
        OJ_R_REALLOC_N(rot->table, struct _rOpt, rot->alen);
        memset(rot->table + olen, 0, sizeof(struct _rOpt) * olen);
    }
    if (0 == olen) {
        ro = rot->table;
    } else if (rot->table[olen - 1].clas < clas) {
        ro = &rot->table[olen];
    } else {
        int i;

        for (i = 0, ro = rot->table; i < olen; i++, ro++) {
            if (clas < ro->clas) {
                memmove(ro + 1, ro, sizeof(struct _rOpt) * (olen - i));
                break;
            }
        }
    }
    ro->clas = clas;
    ro->on   = true;
    ro->dump = dump_obj_attrs;
    for (nf = dump_map; NULL != nf->name; nf++) {
        if (0 == strcmp(nf->name, classname)) {
            ro->dump = nf->func;
            break;
        }
    }
    if (ro->dump == dump_obj_attrs) {
        if (Qundef == activerecord_base) {
            // If not defined let an exception be raised.
            VALUE ar = rb_const_get_at(rb_cObject, rb_intern("ActiveRecord"));

            if (Qundef != ar) {
                activerecord_base = rb_const_get_at(ar, rb_intern("Base"));
            }
        }
        if (Qundef != activerecord_base && Qtrue == rb_class_inherited_p(clas, activerecord_base)) {
            ro->dump = dump_activerecord;
        } else if (Qtrue == rb_class_inherited_p(clas, rb_cStruct)) {  // check before enumerable
            ro->dump = dump_struct;
        } else if (Qtrue == rb_class_inherited_p(clas, rb_mEnumerable)) {
            ro->dump = dump_enumerable;
        } else if (Qtrue == rb_class_inherited_p(clas, rb_eException)) {
            ro->dump = dump_to_s;
        }
    }
    return ro;
}

static void encoder_free(void *ptr) {
    if (NULL != ptr) {
        Encoder e = (Encoder)ptr;

        if (NULL != e->ropts.table) {
            OJ_R_FREE(e->ropts.table);
        }
        OJ_R_FREE(ptr);
    }
}

static void encoder_mark(void *ptr) {
    if (NULL != ptr) {
        Encoder e = (Encoder)ptr;

        if (Qnil != e->arg) {
            rb_gc_mark(e->arg);
        }
    }
}

static const rb_data_type_t oj_encoder_type = {
    "Oj/encoder",
    {
        encoder_mark,
        encoder_free,
        NULL,
    },
    0,
    0,
};

/* Document-method: new
 *	call-seq: new(options=nil)
 *
 * Creates a new Encoder.
 * - *options* [_Hash_] formatting options
 */
static VALUE encoder_new(int argc, VALUE *argv, VALUE self) {
    Encoder e = OJ_R_ALLOC(struct _encoder);

    e->opts = oj_default_options;
    copy_opts(&ropts, &e->ropts);

    if (1 <= argc && Qnil != *argv) {
        e->arg = *argv;
    } else {
        e->arg = rb_hash_new();
    }
    oj_parse_options(e->arg, &e->opts);

    return TypedData_Wrap_Struct(encoder_class, &oj_encoder_type, e);
}

static VALUE resolve_classpath(const char *name) {
    char        class_name[1024];
    VALUE       clas;
    char       *end = class_name + sizeof(class_name) - 1;
    char       *s;
    const char *n = name;
    ID          cid;

    clas = rb_cObject;
    for (s = class_name; '\0' != *n; n++) {
        if (':' == *n) {
            *s = '\0';
            n++;
            if (':' != *n) {
                return Qnil;
            }
            cid = rb_intern(class_name);
            if (!rb_const_defined_at(clas, cid)) {
                return Qnil;
            }
            clas = rb_const_get_at(clas, cid);
            s    = class_name;
        } else if (end <= s) {
            return Qnil;
        } else {
            *s++ = *n;
        }
    }
    *s  = '\0';
    cid = rb_intern(class_name);
    if (!rb_const_defined_at(clas, cid)) {
        return Qnil;
    }
    clas = rb_const_get_at(clas, cid);

    return clas;
}

static void optimize(int argc, VALUE *argv, ROptTable rot, bool on) {
    ROpt ro;

    if (0 == argc) {
        int       i;
        NamedFunc nf;
        VALUE     clas;

        oj_rails_hash_opt  = on;
        oj_rails_array_opt = on;
        oj_rails_float_opt = on;

        for (nf = dump_map; NULL != nf->name; nf++) {
            if (Qnil != (clas = resolve_classpath(nf->name))) {
                if (NULL == oj_rails_get_opt(rot, clas)) {
                    create_opt(rot, clas);
                }
            }
        }
        for (i = 0; i < rot->len; i++) {
            rot->table[i].on = on;
        }
    }
    for (; 0 < argc; argc--, argv++) {
        if (rb_cHash == *argv) {
            oj_rails_hash_opt = on;
        } else if (rb_cArray == *argv) {
            oj_rails_array_opt = on;
        } else if (rb_cFloat == *argv) {
            oj_rails_float_opt = on;
        } else if (oj_string_writer_class == *argv) {
            string_writer_optimized = on;
        } else if (NULL != (ro = oj_rails_get_opt(rot, *argv)) || NULL != (ro = create_opt(rot, *argv))) {
            ro->on = on;
        }
    }
}

/* Document-method optimize
 *	call-seq: optimize(*classes)
 *
 * Use Oj rails optimized routines to encode the specified classes. This
 * ignores the as_json() method on the class and uses an internal encoding
 * instead. Passing in no classes indicates all should use the optimized
 * version of encoding for all previously optimized classes. Passing in the
 * Object class set a global switch that will then use the optimized behavior
 * for all classes.
 *
 * - *classes* [_Class_] a list of classes to optimize
 */
static VALUE encoder_optimize(int argc, VALUE *argv, VALUE self) {
    Encoder e;
    TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);

    optimize(argc, argv, &e->ropts, true);

    return Qnil;
}

/* Document-method: optimize
 *	call-seq: optimize(*classes)
 *
 * Use Oj rails optimized routines to encode the specified classes. This
 * ignores the as_json() method on the class and uses an internal encoding
 * instead. Passing in no classes indicates all should use the optimized
 * version of encoding for all previously optimized classes. Passing in the
 * Object class set a global switch that will then use the optimized behavior
 * for all classes.
 *
 * - *classes* [_Class_] a list of classes to optimize
 */
static VALUE rails_optimize(int argc, VALUE *argv, VALUE self) {
    optimize(argc, argv, &ropts, true);
    string_writer_optimized = true;

    return Qnil;
}

/* Document-module: mimic_JSON
 *	call-seq: mimic_JSON()
 *
 * Sets the JSON method to use Oj similar to Oj.mimic_JSON except with the
 * ActiveSupport monkey patches instead of the json gem monkey patches.
 */
VALUE
rails_mimic_json(VALUE self) {
    VALUE json;

    if (rb_const_defined_at(rb_cObject, rb_intern("JSON"))) {
        json = rb_const_get_at(rb_cObject, rb_intern("JSON"));
    } else {
        json = rb_define_module("JSON");
    }
    oj_mimic_json_methods(json);
    // Setting the default mode breaks the prmoise in the docs not to.
    // oj_default_options.mode = RailsMode;

    return Qnil;
}

/* Document-method: deoptimize
 *	call-seq: deoptimize(*classes)
 *
 * Turn off Oj rails optimization on the specified classes.
 *
 * - *classes* [_Class_] a list of classes to deoptimize
 */
static VALUE encoder_deoptimize(int argc, VALUE *argv, VALUE self) {
    Encoder e;
    TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);

    optimize(argc, argv, &e->ropts, false);

    return Qnil;
}

/* Document-method: deoptimize
 *	call-seq: deoptimize(*classes)
 *
 * Turn off Oj rails optimization on the specified classes.
 *
 * - *classes* [_Class_] a list of classes to deoptimize
 */
static VALUE rails_deoptimize(int argc, VALUE *argv, VALUE self) {
    optimize(argc, argv, &ropts, false);
    string_writer_optimized = false;

    return Qnil;
}

/* Document-method:optimized?
 *	call-seq: optimized?(clas)
 *
 * - *clas* [_Class_] Class to check
 *
 * @return true if the class is being optimized for rails and false otherwise
 */
static VALUE encoder_optimized(VALUE self, VALUE clas) {
    Encoder e;
    ROpt    ro;

    TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);
    ro = oj_rails_get_opt(&e->ropts, clas);

    if (NULL == ro) {
        return Qfalse;
    }
    return (ro->on) ? Qtrue : Qfalse;
}

/* Document-method: optimized?
 *	call-seq: optimized?(clas)
 *
 * Returns true if the specified Class is being optimized.
 */
static VALUE rails_optimized(VALUE self, VALUE clas) {
    ROpt ro = oj_rails_get_opt(&ropts, clas);

    if (NULL == ro) {
        return Qfalse;
    }
    return (ro->on) ? Qtrue : Qfalse;
}

typedef struct _oo {
    Out   out;
    VALUE obj;
} *OO;

static VALUE protect_dump(VALUE ov) {
    OO oo = (OO)ov;

    dump_rails_val(oo->obj, 0, oo->out, true);

    return Qnil;
}

static VALUE encode(VALUE obj, ROptTable ropts, Options opts, int argc, VALUE *argv) {
    struct _out     out;
    struct _options copts = *opts;
    volatile VALUE  rstr  = Qnil;
    struct _oo      oo;
    int             line = 0;

    oo.out            = &out;
    oo.obj            = obj;
    copts.str_rx.head = NULL;
    copts.str_rx.tail = NULL;
    copts.mode        = RailsMode;
    if (escape_html) {
        copts.escape_mode = RailsXEsc;
    } else {
        copts.escape_mode = RailsEsc;
    }

    oj_out_init(&out);

    out.omit_nil = copts.dump_opts.omit_nil;
    out.cur      = out.buf;
    out.circ_cnt = 0;
    out.opts     = &copts;
    out.hash_cnt = 0;
    out.indent   = copts.indent;
    out.argc     = argc;
    out.argv     = argv;
    out.ropts    = ropts;
    if (Yes == copts.circular) {
        oj_cache8_new(&out.circ_cache);
    }
    // dump_rails_val(*argv, 0, &out, true);
    rb_protect(protect_dump, (VALUE)&oo, &line);

    if (0 == line) {
        if (0 < out.indent) {
            switch (*(out.cur - 1)) {
            case ']':
            case '}': assure_size(&out, 2); *out.cur++ = '\n';
            default: break;
            }
        }
        *out.cur = '\0';

        if (0 == out.buf) {
            rb_raise(rb_eNoMemError, "Not enough memory.");
        }
        rstr = rb_utf8_str_new_cstr(out.buf);
    }
    if (Yes == copts.circular) {
        oj_cache8_delete(out.circ_cache);
    }

    oj_out_free(&out);

    if (0 != line) {
        rb_jump_tag(line);
    }
    return rstr;
}

/* Document-method: encode
 *	call-seq: encode(obj)
 *
 * - *obj* [_Object_] object to encode
 *
 * Returns encoded object as a JSON string.
 */
static VALUE encoder_encode(VALUE self, VALUE obj) {
    Encoder e;
    TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);

    if (Qnil != e->arg) {
        VALUE argv[1] = {e->arg};

        return encode(obj, &e->ropts, &e->opts, 1, argv);
    }
    return encode(obj, &e->ropts, &e->opts, 0, NULL);
}

/* Document-method: encode
 *	call-seq: encode(obj, opts=nil)
 *
 * Encode obj as a JSON String.
 *
 * - *obj* [_Object_|Hash|Array] object to convert to a JSON String
 * - *opts* [_Hash_] options
 *
 * Returns [_String_]
 */
static VALUE rails_encode(int argc, VALUE *argv, VALUE self) {
    if (1 > argc) {
        rb_raise(rb_eArgError, "wrong number of arguments (0 for 1).");
    }
    if (1 == argc) {
        return encode(*argv, NULL, &oj_default_options, 0, NULL);
    } else {
        return encode(*argv, NULL, &oj_default_options, argc - 1, argv + 1);
    }
}

static VALUE rails_use_standard_json_time_format(VALUE self, VALUE state) {
    if (Qtrue == state || Qfalse == state) {
        // no change needed
    } else if (Qnil == state) {
        state = Qfalse;
    } else {
        state = Qtrue;
    }
    rb_iv_set(self, "@use_standard_json_time_format", state);
    xml_time = Qtrue == state;

    return state;
}

static VALUE rails_use_standard_json_time_format_get(VALUE self) {
    return xml_time ? Qtrue : Qfalse;
}

static VALUE rails_escape_html_entities_in_json(VALUE self, VALUE state) {
    rb_iv_set(self, "@escape_html_entities_in_json", state);
    escape_html = Qtrue == state;

    return state;
}

static VALUE rails_escape_html_entities_in_json_get(VALUE self) {
    return escape_html ? Qtrue : Qfalse;
}

static VALUE rails_time_precision(VALUE self, VALUE prec) {
    rb_iv_set(self, "@time_precision", prec);
    oj_default_options.sec_prec     = NUM2INT(prec);
    oj_default_options.sec_prec_set = true;

    return prec;
}

/* Document-method: set_encoder
 *	call-seq: set_encoder()
 *
 * Sets the ActiveSupport.encoder to Oj::Rails::Encoder and wraps some of the
 * formatting globals used by ActiveSupport to allow the use of those globals
 * in the Oj::Rails optimizations.
 */
static VALUE rails_set_encoder(VALUE self) {
    VALUE active;
    VALUE json;
    VALUE encoding;
    VALUE pv;
    VALUE verbose;
    VALUE enc = resolve_classpath("ActiveSupport::JSON::Encoding");

    if (Qnil != enc) {
        escape_html = Qtrue == rb_iv_get(self, "@escape_html_entities_in_json");
        xml_time    = Qtrue == rb_iv_get(enc, "@use_standard_json_time_format");
    }
    if (rb_const_defined_at(rb_cObject, rb_intern("ActiveSupport"))) {
        active = rb_const_get_at(rb_cObject, rb_intern("ActiveSupport"));
    } else {
        rb_raise(rb_eStandardError, "ActiveSupport not loaded.");
    }
    rb_funcall(active, rb_intern("json_encoder="), 1, encoder_class);

    json     = rb_const_get_at(active, rb_intern("JSON"));
    encoding = rb_const_get_at(json, rb_intern("Encoding"));

    // rb_undef_method doesn't work for modules or maybe sometimes
    // doesn't. Anyway setting verbose should hide the warning.
    verbose = rb_gv_get("$VERBOSE");
    rb_gv_set("$VERBOSE", Qfalse);
    rb_undef_method(encoding, "use_standard_json_time_format=");
    rb_define_module_function(encoding, "use_standard_json_time_format=", rails_use_standard_json_time_format, 1);
    rb_undef_method(encoding, "use_standard_json_time_format");
    rb_define_module_function(encoding, "use_standard_json_time_format", rails_use_standard_json_time_format_get, 0);

    pv          = rb_iv_get(encoding, "@escape_html_entities_in_json");
    escape_html = Qtrue == pv;
    rb_undef_method(encoding, "escape_html_entities_in_json=");
    rb_define_module_function(encoding, "escape_html_entities_in_json=", rails_escape_html_entities_in_json, 1);
    rb_undef_method(encoding, "escape_html_entities_in_json");
    rb_define_module_function(encoding, "escape_html_entities_in_json", rails_escape_html_entities_in_json_get, 0);

    pv                              = rb_iv_get(encoding, "@time_precision");
    oj_default_options.sec_prec     = NUM2INT(pv);
    oj_default_options.sec_prec_set = true;
    rb_undef_method(encoding, "time_precision=");
    rb_define_module_function(encoding, "time_precision=", rails_time_precision, 1);
    rb_gv_set("$VERBOSE", verbose);

    return Qnil;
}

/* Document-method: set_decoder
 *	call-seq: set_decoder()
 *
 * Sets the JSON.parse function to be the Oj::parse function which is json gem
 * compatible.
 */
static VALUE rails_set_decoder(VALUE self) {
    VALUE json;
    VALUE json_error;
    VALUE verbose;

    if (rb_const_defined_at(rb_cObject, rb_intern("JSON"))) {
        json = rb_const_get_at(rb_cObject, rb_intern("JSON"));
    } else {
        json = rb_define_module("JSON");
    }
    if (rb_const_defined_at(json, rb_intern("JSONError"))) {
        json_error = rb_const_get(json, rb_intern("JSONError"));
    } else {
        json_error = rb_define_class_under(json, "JSONError", rb_eStandardError);
    }

    rb_global_variable(&oj_json_parser_error_class);
    if (rb_const_defined_at(json, rb_intern("ParserError"))) {
        oj_json_parser_error_class = rb_const_get(json, rb_intern("ParserError"));
    } else {
        oj_json_parser_error_class = rb_define_class_under(json, "ParserError", json_error);
    }
    // rb_undef_method doesn't work for modules or maybe sometimes
    // doesn't. Anyway setting verbose should hide the warning.
    verbose = rb_gv_get("$VERBOSE");
    rb_gv_set("$VERBOSE", Qfalse);
    rb_undef_method(json, "parse");
    rb_define_module_function(json, "parse", oj_mimic_parse, -1);
    rb_gv_set("$VERBOSE", verbose);

    return Qnil;
}

/* Document-module: Oj.optimize_rails()
 *
 * Sets the Oj as the Rails encoder and decoder. Oj::Rails.optimize is also
 * called.
 */
VALUE
oj_optimize_rails(VALUE self) {
    rails_set_encoder(self);
    rails_set_decoder(self);
    rails_optimize(0, NULL, self);
    rails_mimic_json(self);

    return Qnil;
}

/* Document-module: Oj::Rails
 *
 * Module that provides rails and active support compatibility.
 */
/* Document-class: Oj::Rails::Encoder
 *
 * The Oj ActiveSupport compliant encoder.
 */
void oj_mimic_rails_init(void) {
    VALUE rails = rb_define_module_under(Oj, "Rails");

    rb_define_module_function(rails, "encode", rails_encode, -1);

    encoder_class = rb_define_class_under(rails, "Encoder", rb_cObject);
    rb_gc_register_address(&encoder_class);
    rb_undef_alloc_func(encoder_class);

    rb_define_module_function(encoder_class, "new", encoder_new, -1);
    rb_define_module_function(rails, "optimize", rails_optimize, -1);
    rb_define_module_function(rails, "deoptimize", rails_deoptimize, -1);
    rb_define_module_function(rails, "optimized?", rails_optimized, 1);
    rb_define_module_function(rails, "mimic_JSON", rails_mimic_json, 0);

    rb_define_module_function(rails, "set_encoder", rails_set_encoder, 0);
    rb_define_module_function(rails, "set_decoder", rails_set_decoder, 0);

    rb_define_method(encoder_class, "encode", encoder_encode, 1);
    rb_define_method(encoder_class, "optimize", encoder_optimize, -1);
    rb_define_method(encoder_class, "deoptimize", encoder_deoptimize, -1);
    rb_define_method(encoder_class, "optimized?", encoder_optimized, 1);
}

static void dump_to_hash(VALUE obj, int depth, Out out) {
    dump_rails_val(rb_funcall(obj, oj_to_hash_id, 0), depth, out, true);
}

static void dump_float(VALUE obj, int depth, Out out, bool as_ok) {
    char   buf[64];
    char  *b;
    double d   = rb_num2dbl(obj);
    size_t cnt = 0;

    if (0.0 == d) {
        b    = buf;
        *b++ = '0';
        *b++ = '.';
        *b++ = '0';
        *b++ = '\0';
        cnt  = 3;
    } else {
        if (isnan(d) || OJ_INFINITY == d || -OJ_INFINITY == d) {
            strcpy(buf, "null");
            cnt = 4;
        } else if (d == (double)(long long int)d) {
            cnt = snprintf(buf, sizeof(buf), "%.1f", d);
        } else if (oj_rails_float_opt) {
            cnt = oj_dump_float_printf(buf, sizeof(buf), obj, d, "%0.16g");
        } else {
            volatile VALUE rstr = oj_safe_string_convert(obj);

            strcpy(buf, RSTRING_PTR(rstr));
            cnt = RSTRING_LEN(rstr);
        }
    }
    assure_size(out, cnt);
    for (b = buf; '\0' != *b; b++) {
        *out->cur++ = *b;
    }
    *out->cur = '\0';
}

static void dump_array(VALUE a, int depth, Out out, bool as_ok) {
    size_t size;
    size_t i;
    size_t cnt;
    int    d2 = depth + 1;

    if (Yes == out->opts->circular) {
        if (0 > oj_check_circular(a, out)) {
            oj_dump_nil(Qnil, 0, out, false);
            return;
        }
    }
    // if (!oj_rails_array_opt && as_ok && 0 < out->argc && rb_respond_to(a, oj_as_json_id)) {
    if (as_ok && 0 < out->argc && rb_respond_to(a, oj_as_json_id)) {
        dump_as_json(a, depth, out, false);
        return;
    }
    cnt         = RARRAY_LEN(a);
    *out->cur++ = '[';
    size        = 2;
    assure_size(out, size);
    if (0 == cnt) {
        *out->cur++ = ']';
    } else {
        if (out->opts->dump_opts.use) {
            size = d2 * out->opts->dump_opts.indent_size + out->opts->dump_opts.array_size + 1;
        } else {
            size = d2 * out->indent + 2;
        }
        assure_size(out, size * cnt);
        cnt--;
        for (i = 0; i <= cnt; i++) {
            if (out->opts->dump_opts.use) {
                if (0 < out->opts->dump_opts.array_size) {
                    APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
                }
                if (0 < out->opts->dump_opts.indent_size) {
                    int i;
                    for (i = d2; 0 < i; i--) {
                        APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
                    }
                }
            } else {
                fill_indent(out, d2);
            }
            dump_rails_val(RARRAY_AREF(a, i), d2, out, true);
            if (i < cnt) {
                *out->cur++ = ',';
            }
        }
        size = depth * out->indent + 1;
        assure_size(out, size);
        if (out->opts->dump_opts.use) {
            if (0 < out->opts->dump_opts.array_size) {
                APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
            }
            if (0 < out->opts->dump_opts.indent_size) {
                int i;

                for (i = depth; 0 < i; i--) {
                    APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
                }
            }
        } else {
            fill_indent(out, depth);
        }
        *out->cur++ = ']';
    }
    *out->cur = '\0';
}

static int hash_cb(VALUE key, VALUE value, VALUE ov) {
    Out  out   = (Out)ov;
    int  depth = out->depth;
    long size;
    int  rtype = rb_type(key);

    if (out->omit_nil && Qnil == value) {
        return ST_CONTINUE;
    }
    if (rtype != T_STRING && rtype != T_SYMBOL) {
        key   = oj_safe_string_convert(key);
        rtype = rb_type(key);
    }
    if (!out->opts->dump_opts.use) {
        size = depth * out->indent + 1;
        assure_size(out, size);
        fill_indent(out, depth);
        if (rtype == T_STRING) {
            oj_dump_str(key, 0, out, false);
        } else {
            oj_dump_sym(key, 0, out, false);
        }
        *out->cur++ = ':';
    } else {
        size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
        assure_size(out, size);
        if (0 < out->opts->dump_opts.hash_size) {
            APPEND_CHARS(out->cur, out->opts->dump_opts.hash_nl, out->opts->dump_opts.hash_size);
        }
        if (0 < out->opts->dump_opts.indent_size) {
            int i;
            for (i = depth; 0 < i; i--) {
                APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
            }
        }
        if (rtype == T_STRING) {
            oj_dump_str(key, 0, out, false);
        } else {
            oj_dump_sym(key, 0, out, false);
        }
        size = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
        assure_size(out, size);
        if (0 < out->opts->dump_opts.before_size) {
            APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
        }
        *out->cur++ = ':';
        if (0 < out->opts->dump_opts.after_size) {
            APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
        }
    }
    dump_rails_val(value, depth, out, true);
    out->depth  = depth;
    *out->cur++ = ',';

    return ST_CONTINUE;
}

static void dump_hash(VALUE obj, int depth, Out out, bool as_ok) {
    int    cnt;
    size_t size;

    if (Yes == out->opts->circular) {
        if (0 > oj_check_circular(obj, out)) {
            oj_dump_nil(Qnil, 0, out, false);
            return;
        }
    }
    if ((!oj_rails_hash_opt || 0 < out->argc) && as_ok && rb_respond_to(obj, oj_as_json_id)) {
        dump_as_json(obj, depth, out, false);
        return;
    }
    cnt  = (int)RHASH_SIZE(obj);
    size = depth * out->indent + 2;
    assure_size(out, 2);
    *out->cur++ = '{';
    if (0 == cnt) {
        *out->cur++ = '}';
    } else {
        out->depth = depth + 1;
        rb_hash_foreach(obj, hash_cb, (VALUE)out);
        if (',' == *(out->cur - 1)) {
            out->cur--;  // backup to overwrite last comma
        }
        if (!out->opts->dump_opts.use) {
            assure_size(out, size);
            fill_indent(out, depth);
        } else {
            size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
            assure_size(out, size);
            if (0 < out->opts->dump_opts.hash_size) {
                APPEND_CHARS(out->cur, out->opts->dump_opts.hash_nl, out->opts->dump_opts.hash_size);
            }
            if (0 < out->opts->dump_opts.indent_size) {
                int i;

                for (i = depth; 0 < i; i--) {
                    APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
                }
            }
        }
        *out->cur++ = '}';
    }
    *out->cur = '\0';
}

static void dump_obj(VALUE obj, int depth, Out out, bool as_ok) {
    VALUE clas;

    if (oj_code_dump(oj_compat_codes, obj, depth, out)) {
        out->argc = 0;
        return;
    }
    clas = rb_obj_class(obj);
    if (as_ok) {
        ROpt ro;

        if (NULL != (ro = oj_rails_get_opt(out->ropts, clas)) && ro->on) {
            ro->dump(obj, depth, out, as_ok);
        } else if (Yes == out->opts->raw_json && rb_respond_to(obj, oj_raw_json_id)) {
            oj_dump_raw_json(obj, depth, out);
        } else if (rb_respond_to(obj, oj_as_json_id)) {
            dump_as_json(obj, depth, out, true);
        } else if (rb_respond_to(obj, oj_to_hash_id)) {
            dump_to_hash(obj, depth, out);
        } else if (oj_bigdecimal_class == clas) {
            dump_bigdecimal(obj, depth, out, false);
        } else {
            oj_dump_obj_to_s(obj, out);
        }
    } else if (Yes == out->opts->raw_json && rb_respond_to(obj, oj_raw_json_id)) {
        oj_dump_raw_json(obj, depth, out);
    } else if (rb_respond_to(obj, oj_to_hash_id)) {
        // Always attempt to_hash.
        dump_to_hash(obj, depth, out);
    } else if (oj_bigdecimal_class == clas) {
        dump_bigdecimal(obj, depth, out, false);
    } else {
        oj_dump_obj_to_s(obj, out);
    }
}

static DumpFunc rails_funcs[] = {
    NULL,           // RUBY_T_NONE     = 0x00,
    dump_obj,       // RUBY_T_OBJECT   = 0x01,
    oj_dump_class,  // RUBY_T_CLASS    = 0x02,
    oj_dump_class,  // RUBY_T_MODULE   = 0x03,
    dump_float,     // RUBY_T_FLOAT    = 0x04,
    oj_dump_str,    // RUBY_T_STRING   = 0x05,
    dump_regexp,    // RUBY_T_REGEXP   = 0x06,
    // dump_as_string,	// RUBY_T_REGEXP   = 0x06,
    dump_array,      // RUBY_T_ARRAY    = 0x07,
    dump_hash,       // RUBY_T_HASH     = 0x08,
    dump_obj,        // RUBY_T_STRUCT   = 0x09,
    oj_dump_bignum,  // RUBY_T_BIGNUM   = 0x0a,
    dump_as_string,  // RUBY_T_FILE     = 0x0b,
    dump_obj,        // RUBY_T_DATA     = 0x0c,
    NULL,            // RUBY_T_MATCH    = 0x0d,
    // Rails raises a stack error on Complex and Rational. It also corrupts
    // something which causes a segfault on the next call. Oj will not mimic
    // that behavior.
    dump_as_string,  // RUBY_T_COMPLEX  = 0x0e,
    dump_as_string,  // RUBY_T_RATIONAL = 0x0f,
    NULL,            // 0x10
    oj_dump_nil,     // RUBY_T_NIL      = 0x11,
    oj_dump_true,    // RUBY_T_TRUE     = 0x12,
    oj_dump_false,   // RUBY_T_FALSE    = 0x13,
    oj_dump_sym,     // RUBY_T_SYMBOL   = 0x14,
    oj_dump_fixnum,  // RUBY_T_FIXNUM   = 0x15,
};

static void dump_rails_val(VALUE obj, int depth, Out out, bool as_ok) {
    int type = rb_type(obj);

    TRACE(out->opts->trace, "dump", obj, depth, TraceIn);
    if (MAX_DEPTH < depth) {
        rb_raise(rb_eNoMemError, "Too deeply nested.\n");
    }
    if (0 < type && type <= RUBY_T_FIXNUM) {
        DumpFunc f = rails_funcs[type];

        if (NULL != f) {
            f(obj, depth, out, as_ok);
            TRACE(out->opts->trace, "dump", obj, depth, TraceOut);
            return;
        }
    }
    oj_dump_nil(Qnil, depth, out, false);
    TRACE(out->opts->trace, "dump", Qnil, depth, TraceOut);
}

void oj_dump_rails_val(VALUE obj, int depth, Out out) {
    out->opts->str_rx.head = NULL;
    out->opts->str_rx.tail = NULL;
    if (escape_html) {
        out->opts->escape_mode = RailsXEsc;
    } else {
        out->opts->escape_mode = RailsEsc;
    }
    dump_rails_val(obj, depth, out, true);
}