oj 3.10.6 → 3.12.0

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