oj 3.11.0 → 3.11.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -1
  3. data/ext/oj/buf.h +34 -38
  4. data/ext/oj/cache8.c +59 -62
  5. data/ext/oj/cache8.h +8 -7
  6. data/ext/oj/circarray.c +33 -35
  7. data/ext/oj/circarray.h +11 -9
  8. data/ext/oj/code.c +170 -174
  9. data/ext/oj/code.h +21 -20
  10. data/ext/oj/compat.c +159 -166
  11. data/ext/oj/custom.c +802 -851
  12. data/ext/oj/dump.c +766 -778
  13. data/ext/oj/dump.h +49 -51
  14. data/ext/oj/dump_compat.c +1 -0
  15. data/ext/oj/dump_leaf.c +116 -157
  16. data/ext/oj/dump_object.c +609 -628
  17. data/ext/oj/dump_strict.c +318 -327
  18. data/ext/oj/encode.h +3 -4
  19. data/ext/oj/err.c +39 -25
  20. data/ext/oj/err.h +24 -15
  21. data/ext/oj/extconf.rb +2 -1
  22. data/ext/oj/fast.c +1008 -1038
  23. data/ext/oj/hash.c +62 -66
  24. data/ext/oj/hash.h +7 -6
  25. data/ext/oj/hash_test.c +450 -443
  26. data/ext/oj/mimic_json.c +413 -402
  27. data/ext/oj/object.c +559 -528
  28. data/ext/oj/odd.c +123 -128
  29. data/ext/oj/odd.h +27 -25
  30. data/ext/oj/oj.c +1131 -924
  31. data/ext/oj/oj.h +286 -298
  32. data/ext/oj/parse.c +938 -930
  33. data/ext/oj/parse.h +70 -69
  34. data/ext/oj/rails.c +836 -839
  35. data/ext/oj/rails.h +7 -7
  36. data/ext/oj/reader.c +135 -140
  37. data/ext/oj/reader.h +66 -79
  38. data/ext/oj/resolve.c +43 -43
  39. data/ext/oj/resolve.h +3 -2
  40. data/ext/oj/rxclass.c +67 -68
  41. data/ext/oj/rxclass.h +12 -10
  42. data/ext/oj/saj.c +451 -479
  43. data/ext/oj/scp.c +93 -103
  44. data/ext/oj/sparse.c +770 -730
  45. data/ext/oj/stream_writer.c +120 -149
  46. data/ext/oj/strict.c +71 -86
  47. data/ext/oj/string_writer.c +198 -243
  48. data/ext/oj/trace.c +29 -33
  49. data/ext/oj/trace.h +14 -11
  50. data/ext/oj/util.c +103 -103
  51. data/ext/oj/util.h +3 -2
  52. data/ext/oj/val_stack.c +47 -47
  53. data/ext/oj/val_stack.h +79 -86
  54. data/ext/oj/wab.c +291 -309
  55. data/lib/oj/bag.rb +1 -0
  56. data/lib/oj/easy_hash.rb +5 -4
  57. data/lib/oj/mimic.rb +0 -12
  58. data/lib/oj/version.rb +1 -1
  59. data/test/activerecord/result_test.rb +7 -2
  60. data/test/foo.rb +35 -32
  61. data/test/helper.rb +10 -0
  62. data/test/json_gem/json_generator_test.rb +15 -3
  63. data/test/json_gem/test_helper.rb +8 -0
  64. data/test/test_compat.rb +2 -2
  65. data/test/test_generate.rb +21 -0
  66. data/test/test_hash.rb +10 -0
  67. data/test/test_scp.rb +1 -1
  68. metadata +4 -2
data/ext/oj/dump_object.c CHANGED
@@ -1,97 +1,94 @@
1
1
  // Copyright (c) 2012, 2017 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
2
3
 
3
4
  #include "dump.h"
4
5
  #include "odd.h"
5
6
  #include "trace.h"
6
7
 
7
- static const char hex_chars[17] = "0123456789abcdef";
8
+ static const char hex_chars[17] = "0123456789abcdef";
8
9
 
9
- static void dump_obj_attrs(VALUE obj, VALUE clas, slot_t id, int depth, Out out);
10
+ static void dump_obj_attrs(VALUE obj, VALUE clas, slot_t id, int depth, Out out);
10
11
 
11
- static void
12
- dump_time(VALUE obj, Out out) {
12
+ static void dump_time(VALUE obj, Out out) {
13
13
  switch (out->opts->time_format) {
14
14
  case RubyTime:
15
- case XmlTime: oj_dump_xml_time(obj, out); break;
16
- case UnixZTime: oj_dump_time(obj, out, 1); break;
15
+ case XmlTime: oj_dump_xml_time(obj, out); break;
16
+ case UnixZTime: oj_dump_time(obj, out, 1); break;
17
17
  case UnixTime:
18
- default: oj_dump_time(obj, out, 0); break;
18
+ default: oj_dump_time(obj, out, 0); break;
19
19
  }
20
20
  }
21
21
 
22
- static void
23
- dump_data(VALUE obj, int depth, Out out, bool as_ok) {
24
- VALUE clas = rb_obj_class(obj);
22
+ static void dump_data(VALUE obj, int depth, Out out, bool as_ok) {
23
+ VALUE clas = rb_obj_class(obj);
25
24
 
26
25
  if (rb_cTime == clas) {
27
- assure_size(out, 6);
28
- *out->cur++ = '{';
29
- *out->cur++ = '"';
30
- *out->cur++ = '^';
31
- *out->cur++ = 't';
32
- *out->cur++ = '"';
33
- *out->cur++ = ':';
34
- dump_time(obj, out);
35
- *out->cur++ = '}';
36
- *out->cur = '\0';
26
+ assure_size(out, 6);
27
+ *out->cur++ = '{';
28
+ *out->cur++ = '"';
29
+ *out->cur++ = '^';
30
+ *out->cur++ = 't';
31
+ *out->cur++ = '"';
32
+ *out->cur++ = ':';
33
+ dump_time(obj, out);
34
+ *out->cur++ = '}';
35
+ *out->cur = '\0';
37
36
  } else {
38
- if (oj_bigdecimal_class == clas) {
39
- volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
40
- const char *str = rb_string_value_ptr((VALUE*)&rstr);
41
- int len = (int)RSTRING_LEN(rstr);
42
-
43
- if (No != out->opts->bigdec_as_num) {
44
- oj_dump_raw(str, len, out);
45
- } else if (0 == strcasecmp("Infinity", str)) {
46
- str = oj_nan_str(obj, out->opts->dump_opts.nan_dump, out->opts->mode, true, &len);
47
- oj_dump_raw(str, len, out);
48
- } else if (0 == strcasecmp("-Infinity", str)) {
49
- str = oj_nan_str(obj, out->opts->dump_opts.nan_dump, out->opts->mode, false, &len);
50
- oj_dump_raw(str, len, out);
51
- } else {
52
- oj_dump_cstr(str, len, 0, 0, out);
53
- }
54
- } else {
55
- long id = oj_check_circular(obj, out);
56
-
57
- if (0 <= id) {
58
- dump_obj_attrs(obj, clas, id, depth, out);
59
- }
60
- }
37
+ if (oj_bigdecimal_class == clas) {
38
+ volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
39
+ const char * str = rb_string_value_ptr((VALUE *)&rstr);
40
+ int len = (int)RSTRING_LEN(rstr);
41
+
42
+ if (No != out->opts->bigdec_as_num) {
43
+ oj_dump_raw(str, len, out);
44
+ } else if (0 == strcasecmp("Infinity", str)) {
45
+ str = oj_nan_str(obj, out->opts->dump_opts.nan_dump, out->opts->mode, true, &len);
46
+ oj_dump_raw(str, len, out);
47
+ } else if (0 == strcasecmp("-Infinity", str)) {
48
+ str = oj_nan_str(obj, out->opts->dump_opts.nan_dump, out->opts->mode, false, &len);
49
+ oj_dump_raw(str, len, out);
50
+ } else {
51
+ oj_dump_cstr(str, len, 0, 0, out);
52
+ }
53
+ } else {
54
+ long id = oj_check_circular(obj, out);
55
+
56
+ if (0 <= id) {
57
+ dump_obj_attrs(obj, clas, id, depth, out);
58
+ }
59
+ }
61
60
  }
62
61
  }
63
62
 
64
- static void
65
- dump_obj(VALUE obj, int depth, Out out, bool as_ok) {
66
- VALUE clas = rb_obj_class(obj);
63
+ static void dump_obj(VALUE obj, int depth, Out out, bool as_ok) {
64
+ VALUE clas = rb_obj_class(obj);
67
65
 
68
66
  if (oj_bigdecimal_class == clas) {
69
- volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
70
- const char *str = rb_string_value_ptr((VALUE*)&rstr);
71
- int len = (int)RSTRING_LEN(rstr);
72
-
73
- if (0 == strcasecmp("Infinity", str)) {
74
- str = oj_nan_str(obj, out->opts->dump_opts.nan_dump, out->opts->mode, true, &len);
75
- oj_dump_raw(str, len, out);
76
- } else if (0 == strcasecmp("-Infinity", str)) {
77
- str = oj_nan_str(obj, out->opts->dump_opts.nan_dump, out->opts->mode, false, &len);
78
- oj_dump_raw(str, len, out);
79
- } else {
80
- oj_dump_raw(str, len, out);
81
- }
67
+ volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
68
+ const char * str = rb_string_value_ptr((VALUE *)&rstr);
69
+ int len = (int)RSTRING_LEN(rstr);
70
+
71
+ if (0 == strcasecmp("Infinity", str)) {
72
+ str = oj_nan_str(obj, out->opts->dump_opts.nan_dump, out->opts->mode, true, &len);
73
+ oj_dump_raw(str, len, out);
74
+ } else if (0 == strcasecmp("-Infinity", str)) {
75
+ str = oj_nan_str(obj, out->opts->dump_opts.nan_dump, out->opts->mode, false, &len);
76
+ oj_dump_raw(str, len, out);
77
+ } else {
78
+ oj_dump_raw(str, len, out);
79
+ }
82
80
  } else {
83
- long id = oj_check_circular(obj, out);
81
+ long id = oj_check_circular(obj, out);
84
82
 
85
- if (0 <= id) {
86
- dump_obj_attrs(obj, clas, id, depth, out);
87
- }
83
+ if (0 <= id) {
84
+ dump_obj_attrs(obj, clas, id, depth, out);
85
+ }
88
86
  }
89
87
  }
90
88
 
91
- static void
92
- dump_class(VALUE obj, int depth, Out out, bool as_ok) {
93
- const char *s = rb_class2name(obj);
94
- size_t len = strlen(s);
89
+ static void dump_class(VALUE obj, int depth, Out out, bool as_ok) {
90
+ const char *s = rb_class2name(obj);
91
+ size_t len = strlen(s);
95
92
 
96
93
  assure_size(out, 6);
97
94
  *out->cur++ = '{';
@@ -102,597 +99,584 @@ dump_class(VALUE obj, int depth, Out out, bool as_ok) {
102
99
  *out->cur++ = ':';
103
100
  oj_dump_cstr(s, len, 0, 0, out);
104
101
  *out->cur++ = '}';
105
- *out->cur = '\0';
102
+ *out->cur = '\0';
106
103
  }
107
104
 
108
- static void
109
- dump_array_class(VALUE a, VALUE clas, int depth, Out out) {
110
- size_t size;
111
- int i, cnt;
112
- int d2 = depth + 1;
113
- long id = oj_check_circular(a, out);
105
+ static void dump_array_class(VALUE a, VALUE clas, int depth, Out out) {
106
+ size_t size;
107
+ int i, cnt;
108
+ int d2 = depth + 1;
109
+ long id = oj_check_circular(a, out);
114
110
 
115
111
  if (id < 0) {
116
- return;
112
+ return;
117
113
  }
118
114
  if (Qundef != clas && rb_cArray != clas && ObjectMode == out->opts->mode) {
119
- dump_obj_attrs(a, clas, 0, depth, out);
120
- return;
115
+ dump_obj_attrs(a, clas, 0, depth, out);
116
+ return;
121
117
  }
122
- cnt = (int)RARRAY_LEN(a);
118
+ cnt = (int)RARRAY_LEN(a);
123
119
  *out->cur++ = '[';
124
120
  if (0 < id) {
125
- assure_size(out, d2 * out->indent + 16);
126
- fill_indent(out, d2);
127
- *out->cur++ = '"';
128
- *out->cur++ = '^';
129
- *out->cur++ = 'i';
130
- dump_ulong(id, out);
131
- *out->cur++ = '"';
121
+ assure_size(out, d2 * out->indent + 16);
122
+ fill_indent(out, d2);
123
+ *out->cur++ = '"';
124
+ *out->cur++ = '^';
125
+ *out->cur++ = 'i';
126
+ dump_ulong(id, out);
127
+ *out->cur++ = '"';
132
128
  }
133
129
  size = 2;
134
130
  assure_size(out, 2);
135
131
  if (0 == cnt) {
136
- *out->cur++ = ']';
132
+ *out->cur++ = ']';
137
133
  } else {
138
- if (0 < id) {
139
- *out->cur++ = ',';
140
- }
141
- if (out->opts->dump_opts.use) {
142
- size = d2 * out->opts->dump_opts.indent_size + out->opts->dump_opts.array_size + 1;
143
- } else {
144
- size = d2 * out->indent + 2;
145
- }
146
- cnt--;
147
- for (i = 0; i <= cnt; i++) {
148
- assure_size(out, size);
149
- if (out->opts->dump_opts.use) {
150
- if (0 < out->opts->dump_opts.array_size) {
151
- strcpy(out->cur, out->opts->dump_opts.array_nl);
152
- out->cur += out->opts->dump_opts.array_size;
153
- }
154
- if (0 < out->opts->dump_opts.indent_size) {
155
- int i;
156
- for (i = d2; 0 < i; i--) {
157
- strcpy(out->cur, out->opts->dump_opts.indent_str);
158
- out->cur += out->opts->dump_opts.indent_size;
159
- }
160
- }
161
- } else {
162
- fill_indent(out, d2);
163
- }
164
- oj_dump_obj_val(rb_ary_entry(a, i), d2, out);
165
- if (i < cnt) {
166
- *out->cur++ = ',';
167
- }
168
- }
169
- size = depth * out->indent + 1;
170
- assure_size(out, size);
171
- if (out->opts->dump_opts.use) {
172
- //printf("*** d2: %u indent: %u '%s'\n", d2, out->opts->dump_opts->indent_size, out->opts->dump_opts->indent);
173
- if (0 < out->opts->dump_opts.array_size) {
174
- strcpy(out->cur, out->opts->dump_opts.array_nl);
175
- out->cur += out->opts->dump_opts.array_size;
176
- }
177
- if (0 < out->opts->dump_opts.indent_size) {
178
- int i;
179
-
180
- for (i = depth; 0 < i; i--) {
181
- strcpy(out->cur, out->opts->dump_opts.indent_str);
182
- out->cur += out->opts->dump_opts.indent_size;
183
- }
184
- }
185
- } else {
186
- fill_indent(out, depth);
187
- }
188
- *out->cur++ = ']';
134
+ if (0 < id) {
135
+ *out->cur++ = ',';
136
+ }
137
+ if (out->opts->dump_opts.use) {
138
+ size = d2 * out->opts->dump_opts.indent_size + out->opts->dump_opts.array_size + 1;
139
+ } else {
140
+ size = d2 * out->indent + 2;
141
+ }
142
+ cnt--;
143
+ for (i = 0; i <= cnt; i++) {
144
+ assure_size(out, size);
145
+ if (out->opts->dump_opts.use) {
146
+ if (0 < out->opts->dump_opts.array_size) {
147
+ strcpy(out->cur, out->opts->dump_opts.array_nl);
148
+ out->cur += out->opts->dump_opts.array_size;
149
+ }
150
+ if (0 < out->opts->dump_opts.indent_size) {
151
+ int i;
152
+ for (i = d2; 0 < i; i--) {
153
+ strcpy(out->cur, out->opts->dump_opts.indent_str);
154
+ out->cur += out->opts->dump_opts.indent_size;
155
+ }
156
+ }
157
+ } else {
158
+ fill_indent(out, d2);
159
+ }
160
+ oj_dump_obj_val(rb_ary_entry(a, i), d2, out);
161
+ if (i < cnt) {
162
+ *out->cur++ = ',';
163
+ }
164
+ }
165
+ size = depth * out->indent + 1;
166
+ assure_size(out, size);
167
+ if (out->opts->dump_opts.use) {
168
+ // printf("*** d2: %u indent: %u '%s'\n", d2, out->opts->dump_opts->indent_size,
169
+ // out->opts->dump_opts->indent);
170
+ if (0 < out->opts->dump_opts.array_size) {
171
+ strcpy(out->cur, out->opts->dump_opts.array_nl);
172
+ out->cur += out->opts->dump_opts.array_size;
173
+ }
174
+ if (0 < out->opts->dump_opts.indent_size) {
175
+ int i;
176
+
177
+ for (i = depth; 0 < i; i--) {
178
+ strcpy(out->cur, out->opts->dump_opts.indent_str);
179
+ out->cur += out->opts->dump_opts.indent_size;
180
+ }
181
+ }
182
+ } else {
183
+ fill_indent(out, depth);
184
+ }
185
+ *out->cur++ = ']';
189
186
  }
190
187
  *out->cur = '\0';
191
188
  }
192
189
 
193
- static void
194
- dump_array(VALUE obj, int depth, Out out, bool as_ok) {
190
+ static void dump_array(VALUE obj, int depth, Out out, bool as_ok) {
195
191
  dump_array_class(obj, rb_obj_class(obj), depth, out);
196
192
  }
197
193
 
198
- static void
199
- dump_str_class(VALUE obj, VALUE clas, int depth, Out out) {
194
+ static void dump_str_class(VALUE obj, VALUE clas, int depth, Out out) {
200
195
  if (Qundef != clas && rb_cString != clas) {
201
- dump_obj_attrs(obj, clas, 0, depth, out);
196
+ dump_obj_attrs(obj, clas, 0, depth, out);
202
197
  } else {
203
- const char *s = rb_string_value_ptr((VALUE*)&obj);
204
- size_t len = (int)RSTRING_LEN(obj);
205
- char s1 = s[1];
198
+ const char *s = rb_string_value_ptr((VALUE *)&obj);
199
+ size_t len = (int)RSTRING_LEN(obj);
200
+ char s1 = s[1];
206
201
 
207
- oj_dump_cstr(s, len, 0, (':' == *s || ('^' == *s && ('r' == s1 || 'i' == s1))), out);
202
+ oj_dump_cstr(s, len, 0, (':' == *s || ('^' == *s && ('r' == s1 || 'i' == s1))), out);
208
203
  }
209
204
  }
210
205
 
211
- static void
212
- dump_str(VALUE obj, int depth, Out out, bool as_ok) {
206
+ static void dump_str(VALUE obj, int depth, Out out, bool as_ok) {
213
207
  dump_str_class(obj, rb_obj_class(obj), depth, out);
214
208
  }
215
209
 
216
- static void
217
- dump_sym(VALUE obj, int depth, Out out, bool as_ok) {
218
- volatile VALUE s = rb_sym_to_s(obj);
210
+ static void dump_sym(VALUE obj, int depth, Out out, bool as_ok) {
211
+ volatile VALUE s = rb_sym_to_s(obj);
219
212
 
220
- oj_dump_cstr(rb_string_value_ptr((VALUE*)&s), (int)RSTRING_LEN(s), 1, 0, out);
213
+ oj_dump_cstr(rb_string_value_ptr((VALUE *)&s), (int)RSTRING_LEN(s), 1, 0, out);
221
214
  }
222
215
 
223
- static int
224
- hash_cb(VALUE key, VALUE value, VALUE ov) {
225
- Out out = (Out)ov;
226
- int depth = out->depth;
227
- long size = depth * out->indent + 1;
216
+ static int hash_cb(VALUE key, VALUE value, VALUE ov) {
217
+ Out out = (Out)ov;
218
+ int depth = out->depth;
219
+ long size = depth * out->indent + 1;
228
220
 
229
221
  if (oj_dump_ignore(out->opts, value)) {
230
- return ST_CONTINUE;
222
+ return ST_CONTINUE;
231
223
  }
232
224
  if (out->omit_nil && Qnil == value) {
233
- return ST_CONTINUE;
225
+ return ST_CONTINUE;
234
226
  }
235
227
  assure_size(out, size);
236
228
  fill_indent(out, depth);
237
229
  if (rb_type(key) == T_STRING) {
238
- dump_str_class(key, Qundef, depth, out);
239
- *out->cur++ = ':';
240
- oj_dump_obj_val(value, depth, out);
230
+ dump_str_class(key, Qundef, depth, out);
231
+ *out->cur++ = ':';
232
+ oj_dump_obj_val(value, depth, out);
241
233
  } else if (rb_type(key) == T_SYMBOL) {
242
- dump_sym(key, 0, out, false);
243
- *out->cur++ = ':';
244
- oj_dump_obj_val(value, depth, out);
234
+ dump_sym(key, 0, out, false);
235
+ *out->cur++ = ':';
236
+ oj_dump_obj_val(value, depth, out);
245
237
  } else {
246
- int d2 = depth + 1;
247
- long s2 = size + out->indent + 1;
248
- int i;
249
- int started = 0;
250
- uint8_t b;
251
-
252
- assure_size(out, s2 + 15);
253
- *out->cur++ = '"';
254
- *out->cur++ = '^';
255
- *out->cur++ = '#';
256
- out->hash_cnt++;
257
- for (i = 28; 0 <= i; i -= 4) {
258
- b = (uint8_t)((out->hash_cnt >> i) & 0x0000000F);
259
- if ('\0' != b) {
260
- started = 1;
261
- }
262
- if (started) {
263
- *out->cur++ = hex_chars[b];
264
- }
265
- }
266
- *out->cur++ = '"';
267
- *out->cur++ = ':';
268
- *out->cur++ = '[';
269
- fill_indent(out, d2);
270
- oj_dump_obj_val(key, d2, out);
271
- assure_size(out, s2);
272
- *out->cur++ = ',';
273
- fill_indent(out, d2);
274
- oj_dump_obj_val(value, d2, out);
275
- assure_size(out, size);
276
- fill_indent(out, depth);
277
- *out->cur++ = ']';
278
- }
279
- out->depth = depth;
238
+ int d2 = depth + 1;
239
+ long s2 = size + out->indent + 1;
240
+ int i;
241
+ int started = 0;
242
+ uint8_t b;
243
+
244
+ assure_size(out, s2 + 15);
245
+ *out->cur++ = '"';
246
+ *out->cur++ = '^';
247
+ *out->cur++ = '#';
248
+ out->hash_cnt++;
249
+ for (i = 28; 0 <= i; i -= 4) {
250
+ b = (uint8_t)((out->hash_cnt >> i) & 0x0000000F);
251
+ if ('\0' != b) {
252
+ started = 1;
253
+ }
254
+ if (started) {
255
+ *out->cur++ = hex_chars[b];
256
+ }
257
+ }
258
+ *out->cur++ = '"';
259
+ *out->cur++ = ':';
260
+ *out->cur++ = '[';
261
+ fill_indent(out, d2);
262
+ oj_dump_obj_val(key, d2, out);
263
+ assure_size(out, s2);
264
+ *out->cur++ = ',';
265
+ fill_indent(out, d2);
266
+ oj_dump_obj_val(value, d2, out);
267
+ assure_size(out, size);
268
+ fill_indent(out, depth);
269
+ *out->cur++ = ']';
270
+ }
271
+ out->depth = depth;
280
272
  *out->cur++ = ',';
281
273
 
282
274
  return ST_CONTINUE;
283
275
  }
284
276
 
285
- static void
286
- dump_hash_class(VALUE obj, VALUE clas, int depth, Out out) {
287
- int cnt;
288
- size_t size;
277
+ static void dump_hash_class(VALUE obj, VALUE clas, int depth, Out out) {
278
+ int cnt;
279
+ size_t size;
289
280
 
290
281
  if (Qundef != clas && rb_cHash != clas) {
291
- dump_obj_attrs(obj, clas, 0, depth, out);
292
- return;
282
+ dump_obj_attrs(obj, clas, 0, depth, out);
283
+ return;
293
284
  }
294
- cnt = (int)RHASH_SIZE(obj);
285
+ cnt = (int)RHASH_SIZE(obj);
295
286
  size = depth * out->indent + 2;
296
287
  assure_size(out, 2);
297
288
  if (0 == cnt) {
298
- *out->cur++ = '{';
299
- *out->cur++ = '}';
289
+ *out->cur++ = '{';
290
+ *out->cur++ = '}';
300
291
  } else {
301
- long id = oj_check_circular(obj, out);
302
-
303
- if (0 > id) {
304
- return;
305
- }
306
- *out->cur++ = '{';
307
- if (0 < id) {
308
- assure_size(out, size + 16);
309
- fill_indent(out, depth + 1);
310
- *out->cur++ = '"';
311
- *out->cur++ = '^';
312
- *out->cur++ = 'i';
313
- *out->cur++ = '"';
314
- *out->cur++ = ':';
315
- dump_ulong(id, out);
316
- *out->cur++ = ',';
317
- }
318
- out->depth = depth + 1;
319
- rb_hash_foreach(obj, hash_cb, (VALUE)out);
320
- if (',' == *(out->cur - 1)) {
321
- out->cur--; // backup to overwrite last comma
322
- }
323
- if (!out->opts->dump_opts.use) {
324
- assure_size(out, size);
325
- fill_indent(out, depth);
326
- } else {
327
- size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
328
- assure_size(out, size);
329
- if (0 < out->opts->dump_opts.hash_size) {
330
- strcpy(out->cur, out->opts->dump_opts.hash_nl);
331
- out->cur += out->opts->dump_opts.hash_size;
332
- }
333
- if (0 < out->opts->dump_opts.indent_size) {
334
- int i;
335
-
336
- for (i = depth; 0 < i; i--) {
337
- strcpy(out->cur, out->opts->dump_opts.indent_str);
338
- out->cur += out->opts->dump_opts.indent_size;
339
- }
340
- }
341
- }
342
- *out->cur++ = '}';
292
+ long id = oj_check_circular(obj, out);
293
+
294
+ if (0 > id) {
295
+ return;
296
+ }
297
+ *out->cur++ = '{';
298
+ if (0 < id) {
299
+ assure_size(out, size + 16);
300
+ fill_indent(out, depth + 1);
301
+ *out->cur++ = '"';
302
+ *out->cur++ = '^';
303
+ *out->cur++ = 'i';
304
+ *out->cur++ = '"';
305
+ *out->cur++ = ':';
306
+ dump_ulong(id, out);
307
+ *out->cur++ = ',';
308
+ }
309
+ out->depth = depth + 1;
310
+ rb_hash_foreach(obj, hash_cb, (VALUE)out);
311
+ if (',' == *(out->cur - 1)) {
312
+ out->cur--; // backup to overwrite last comma
313
+ }
314
+ if (!out->opts->dump_opts.use) {
315
+ assure_size(out, size);
316
+ fill_indent(out, depth);
317
+ } else {
318
+ size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
319
+ assure_size(out, size);
320
+ if (0 < out->opts->dump_opts.hash_size) {
321
+ strcpy(out->cur, out->opts->dump_opts.hash_nl);
322
+ out->cur += out->opts->dump_opts.hash_size;
323
+ }
324
+ if (0 < out->opts->dump_opts.indent_size) {
325
+ int i;
326
+
327
+ for (i = depth; 0 < i; i--) {
328
+ strcpy(out->cur, out->opts->dump_opts.indent_str);
329
+ out->cur += out->opts->dump_opts.indent_size;
330
+ }
331
+ }
332
+ }
333
+ *out->cur++ = '}';
343
334
  }
344
335
  *out->cur = '\0';
345
336
  }
346
337
 
347
338
  #ifdef HAVE_RB_IVAR_FOREACH
348
- static int
349
- dump_attr_cb(ID key, VALUE value, VALUE ov) {
350
- Out out = (Out)ov;
351
- int depth = out->depth;
352
- size_t size = depth * out->indent + 1;
353
- const char *attr = rb_id2name(key);
339
+ static int dump_attr_cb(ID key, VALUE value, VALUE ov) {
340
+ Out out = (Out)ov;
341
+ int depth = out->depth;
342
+ size_t size = depth * out->indent + 1;
343
+ const char *attr = rb_id2name(key);
354
344
 
355
345
  if (oj_dump_ignore(out->opts, value)) {
356
- return ST_CONTINUE;
346
+ return ST_CONTINUE;
357
347
  }
358
348
  if (out->omit_nil && Qnil == value) {
359
- return ST_CONTINUE;
349
+ return ST_CONTINUE;
360
350
  }
361
351
  // Some exceptions such as NoMethodError have an invisible attribute where
362
352
  // the key name is NULL. Not an empty string but NULL.
363
353
  if (NULL == attr) {
364
- attr = "";
354
+ attr = "";
365
355
  } else if (Yes == out->opts->ignore_under && '@' == *attr && '_' == attr[1]) {
366
- return ST_CONTINUE;
356
+ return ST_CONTINUE;
367
357
  }
368
358
  if (0 == strcmp("bt", attr) || 0 == strcmp("mesg", attr)) {
369
- return ST_CONTINUE;
359
+ return ST_CONTINUE;
370
360
  }
371
361
  assure_size(out, size);
372
362
  fill_indent(out, depth);
373
363
  if ('@' == *attr) {
374
- attr++;
375
- oj_dump_cstr(attr, strlen(attr), 0, 0, out);
364
+ attr++;
365
+ oj_dump_cstr(attr, strlen(attr), 0, 0, out);
376
366
  } else {
377
- char buf[32];
367
+ char buf[32];
378
368
 
379
- *buf = '~';
380
- strncpy(buf + 1, attr, sizeof(buf) - 2);
381
- buf[sizeof(buf) - 1] = '\0';
382
- oj_dump_cstr(buf, strlen(buf), 0, 0, out);
369
+ *buf = '~';
370
+ strncpy(buf + 1, attr, sizeof(buf) - 2);
371
+ buf[sizeof(buf) - 1] = '\0';
372
+ oj_dump_cstr(buf, strlen(buf), 0, 0, out);
383
373
  }
384
374
  *out->cur++ = ':';
385
375
  oj_dump_obj_val(value, depth, out);
386
- out->depth = depth;
376
+ out->depth = depth;
387
377
  *out->cur++ = ',';
388
378
 
389
379
  return ST_CONTINUE;
390
380
  }
391
381
  #endif
392
382
 
393
- static void
394
- dump_hash(VALUE obj, int depth, Out out, bool as_ok) {
383
+ static void dump_hash(VALUE obj, int depth, Out out, bool as_ok) {
395
384
  dump_hash_class(obj, rb_obj_class(obj), depth, out);
396
385
  }
397
386
 
398
- static void
399
- dump_odd(VALUE obj, Odd odd, VALUE clas, int depth, Out out) {
400
- ID *idp;
401
- AttrGetFunc *fp;
402
- volatile VALUE v;
403
- const char *name;
404
- size_t size;
405
- int d2 = depth + 1;
387
+ static void dump_odd(VALUE obj, Odd odd, VALUE clas, int depth, Out out) {
388
+ ID * idp;
389
+ AttrGetFunc * fp;
390
+ volatile VALUE v;
391
+ const char * name;
392
+ size_t size;
393
+ int d2 = depth + 1;
406
394
 
407
395
  assure_size(out, 2);
408
396
  *out->cur++ = '{';
409
397
  if (Qundef != clas) {
410
- const char *class_name = rb_class2name(clas);
411
- int clen = (int)strlen(class_name);
412
-
413
- size = d2 * out->indent + clen + 10;
414
- assure_size(out, size);
415
- fill_indent(out, d2);
416
- *out->cur++ = '"';
417
- *out->cur++ = '^';
418
- *out->cur++ = 'O';
419
- *out->cur++ = '"';
420
- *out->cur++ = ':';
421
- oj_dump_cstr(class_name, clen, 0, 0, out);
422
- *out->cur++ = ',';
398
+ const char *class_name = rb_class2name(clas);
399
+ int clen = (int)strlen(class_name);
400
+
401
+ size = d2 * out->indent + clen + 10;
402
+ assure_size(out, size);
403
+ fill_indent(out, d2);
404
+ *out->cur++ = '"';
405
+ *out->cur++ = '^';
406
+ *out->cur++ = 'O';
407
+ *out->cur++ = '"';
408
+ *out->cur++ = ':';
409
+ oj_dump_cstr(class_name, clen, 0, 0, out);
410
+ *out->cur++ = ',';
423
411
  }
424
412
  if (odd->raw) {
425
- v = rb_funcall(obj, *odd->attrs, 0);
426
- if (Qundef == v || T_STRING != rb_type(v)) {
427
- rb_raise(rb_eEncodingError, "Invalid type for raw JSON.");
428
- } else {
429
- const char *s = rb_string_value_ptr((VALUE*)&v);
430
- int len = (int)RSTRING_LEN(v);
431
- const char *name = rb_id2name(*odd->attrs);
432
- size_t nlen = strlen(name);
433
-
434
- size = len + d2 * out->indent + nlen + 10;
435
- assure_size(out, size);
436
- fill_indent(out, d2);
437
- *out->cur++ = '"';
438
- memcpy(out->cur, name, nlen);
439
- out->cur += nlen;
440
- *out->cur++ = '"';
441
- *out->cur++ = ':';
442
- memcpy(out->cur, s, len);
443
- out->cur += len;
444
- *out->cur = '\0';
445
- }
413
+ v = rb_funcall(obj, *odd->attrs, 0);
414
+ if (Qundef == v || T_STRING != rb_type(v)) {
415
+ rb_raise(rb_eEncodingError, "Invalid type for raw JSON.");
416
+ } else {
417
+ const char *s = rb_string_value_ptr((VALUE *)&v);
418
+ int len = (int)RSTRING_LEN(v);
419
+ const char *name = rb_id2name(*odd->attrs);
420
+ size_t nlen = strlen(name);
421
+
422
+ size = len + d2 * out->indent + nlen + 10;
423
+ assure_size(out, size);
424
+ fill_indent(out, d2);
425
+ *out->cur++ = '"';
426
+ memcpy(out->cur, name, nlen);
427
+ out->cur += nlen;
428
+ *out->cur++ = '"';
429
+ *out->cur++ = ':';
430
+ memcpy(out->cur, s, len);
431
+ out->cur += len;
432
+ *out->cur = '\0';
433
+ }
446
434
  } else {
447
- size = d2 * out->indent + 1;
448
- for (idp = odd->attrs, fp = odd->attrFuncs; 0 != *idp; idp++, fp++) {
449
- size_t nlen;
450
-
451
- assure_size(out, size);
452
- name = rb_id2name(*idp);
453
- nlen = strlen(name);
454
- if (0 != *fp) {
455
- v = (*fp)(obj);
456
- } else if (0 == strchr(name, '.')) {
457
- v = rb_funcall(obj, *idp, 0);
458
- } else {
459
- char nbuf[256];
460
- char *n2 = nbuf;
461
- char *n;
462
- char *end;
463
- ID i;
464
-
465
- if (sizeof(nbuf) <= nlen) {
466
- if (NULL == (n2 = strdup(name))) {
467
- rb_raise(rb_eNoMemError, "for attribute name.");
468
- }
469
- } else {
470
- strcpy(n2, name);
471
- }
472
- n = n2;
473
- v = obj;
474
- while (0 != (end = strchr(n, '.'))) {
475
- *end = '\0';
476
- i = rb_intern(n);
477
- v = rb_funcall(v, i, 0);
478
- n = end + 1;
479
- }
480
- i = rb_intern(n);
481
- v = rb_funcall(v, i, 0);
482
- if (nbuf != n2) {
483
- free(n2);
484
- }
485
- }
486
- fill_indent(out, d2);
487
- oj_dump_cstr(name, nlen, 0, 0, out);
488
- *out->cur++ = ':';
489
- oj_dump_obj_val(v, d2, out);
490
- assure_size(out, 2);
491
- *out->cur++ = ',';
492
- }
493
- out->cur--;
435
+ size = d2 * out->indent + 1;
436
+ for (idp = odd->attrs, fp = odd->attrFuncs; 0 != *idp; idp++, fp++) {
437
+ size_t nlen;
438
+
439
+ assure_size(out, size);
440
+ name = rb_id2name(*idp);
441
+ nlen = strlen(name);
442
+ if (0 != *fp) {
443
+ v = (*fp)(obj);
444
+ } else if (0 == strchr(name, '.')) {
445
+ v = rb_funcall(obj, *idp, 0);
446
+ } else {
447
+ char nbuf[256];
448
+ char *n2 = nbuf;
449
+ char *n;
450
+ char *end;
451
+ ID i;
452
+
453
+ if (sizeof(nbuf) <= nlen) {
454
+ if (NULL == (n2 = strdup(name))) {
455
+ rb_raise(rb_eNoMemError, "for attribute name.");
456
+ }
457
+ } else {
458
+ strcpy(n2, name);
459
+ }
460
+ n = n2;
461
+ v = obj;
462
+ while (0 != (end = strchr(n, '.'))) {
463
+ *end = '\0';
464
+ i = rb_intern(n);
465
+ v = rb_funcall(v, i, 0);
466
+ n = end + 1;
467
+ }
468
+ i = rb_intern(n);
469
+ v = rb_funcall(v, i, 0);
470
+ if (nbuf != n2) {
471
+ free(n2);
472
+ }
473
+ }
474
+ fill_indent(out, d2);
475
+ oj_dump_cstr(name, nlen, 0, 0, out);
476
+ *out->cur++ = ':';
477
+ oj_dump_obj_val(v, d2, out);
478
+ assure_size(out, 2);
479
+ *out->cur++ = ',';
480
+ }
481
+ out->cur--;
494
482
  }
495
483
  *out->cur++ = '}';
496
- *out->cur = '\0';
484
+ *out->cur = '\0';
497
485
  }
498
486
 
499
- static void
500
- dump_obj_attrs(VALUE obj, VALUE clas, slot_t id, int depth, Out out) {
501
- size_t size = 0;
502
- int d2 = depth + 1;
503
- int type = rb_type(obj);
504
- Odd odd;
487
+ static void dump_obj_attrs(VALUE obj, VALUE clas, slot_t id, int depth, Out out) {
488
+ size_t size = 0;
489
+ int d2 = depth + 1;
490
+ int type = rb_type(obj);
491
+ Odd odd;
505
492
 
506
493
  if (0 != (odd = oj_get_odd(clas))) {
507
- dump_odd(obj, odd, clas, depth + 1, out);
508
- return;
494
+ dump_odd(obj, odd, clas, depth + 1, out);
495
+ return;
509
496
  }
510
497
  assure_size(out, 2);
511
498
  *out->cur++ = '{';
512
499
  if (Qundef != clas) {
513
- const char *class_name = rb_class2name(clas);
514
- int clen = (int)strlen(class_name);
515
-
516
- assure_size(out, d2 * out->indent + clen + 10);
517
- fill_indent(out, d2);
518
- *out->cur++ = '"';
519
- *out->cur++ = '^';
520
- *out->cur++ = 'o';
521
- *out->cur++ = '"';
522
- *out->cur++ = ':';
523
- oj_dump_cstr(class_name, clen, 0, 0, out);
500
+ const char *class_name = rb_class2name(clas);
501
+ int clen = (int)strlen(class_name);
502
+
503
+ assure_size(out, d2 * out->indent + clen + 10);
504
+ fill_indent(out, d2);
505
+ *out->cur++ = '"';
506
+ *out->cur++ = '^';
507
+ *out->cur++ = 'o';
508
+ *out->cur++ = '"';
509
+ *out->cur++ = ':';
510
+ oj_dump_cstr(class_name, clen, 0, 0, out);
524
511
  }
525
512
  if (0 < id) {
526
- assure_size(out, d2 * out->indent + 16);
527
- *out->cur++ = ',';
528
- fill_indent(out, d2);
529
- *out->cur++ = '"';
530
- *out->cur++ = '^';
531
- *out->cur++ = 'i';
532
- *out->cur++ = '"';
533
- *out->cur++ = ':';
534
- dump_ulong(id, out);
513
+ assure_size(out, d2 * out->indent + 16);
514
+ *out->cur++ = ',';
515
+ fill_indent(out, d2);
516
+ *out->cur++ = '"';
517
+ *out->cur++ = '^';
518
+ *out->cur++ = 'i';
519
+ *out->cur++ = '"';
520
+ *out->cur++ = ':';
521
+ dump_ulong(id, out);
535
522
  }
536
523
  switch (type) {
537
524
  case T_STRING:
538
- assure_size(out, d2 * out->indent + 14);
539
- *out->cur++ = ',';
540
- fill_indent(out, d2);
541
- *out->cur++ = '"';
542
- *out->cur++ = 's';
543
- *out->cur++ = 'e';
544
- *out->cur++ = 'l';
545
- *out->cur++ = 'f';
546
- *out->cur++ = '"';
547
- *out->cur++ = ':';
548
- oj_dump_cstr(rb_string_value_ptr((VALUE*)&obj), (int)RSTRING_LEN(obj), 0, 0, out);
549
- break;
525
+ assure_size(out, d2 * out->indent + 14);
526
+ *out->cur++ = ',';
527
+ fill_indent(out, d2);
528
+ *out->cur++ = '"';
529
+ *out->cur++ = 's';
530
+ *out->cur++ = 'e';
531
+ *out->cur++ = 'l';
532
+ *out->cur++ = 'f';
533
+ *out->cur++ = '"';
534
+ *out->cur++ = ':';
535
+ oj_dump_cstr(rb_string_value_ptr((VALUE *)&obj), (int)RSTRING_LEN(obj), 0, 0, out);
536
+ break;
550
537
  case T_ARRAY:
551
- assure_size(out, d2 * out->indent + 14);
552
- *out->cur++ = ',';
553
- fill_indent(out, d2);
554
- *out->cur++ = '"';
555
- *out->cur++ = 's';
556
- *out->cur++ = 'e';
557
- *out->cur++ = 'l';
558
- *out->cur++ = 'f';
559
- *out->cur++ = '"';
560
- *out->cur++ = ':';
561
- dump_array_class(obj, Qundef, depth + 1, out);
562
- break;
538
+ assure_size(out, d2 * out->indent + 14);
539
+ *out->cur++ = ',';
540
+ fill_indent(out, d2);
541
+ *out->cur++ = '"';
542
+ *out->cur++ = 's';
543
+ *out->cur++ = 'e';
544
+ *out->cur++ = 'l';
545
+ *out->cur++ = 'f';
546
+ *out->cur++ = '"';
547
+ *out->cur++ = ':';
548
+ dump_array_class(obj, Qundef, depth + 1, out);
549
+ break;
563
550
  case T_HASH:
564
- assure_size(out, d2 * out->indent + 14);
565
- *out->cur++ = ',';
566
- fill_indent(out, d2);
567
- *out->cur++ = '"';
568
- *out->cur++ = 's';
569
- *out->cur++ = 'e';
570
- *out->cur++ = 'l';
571
- *out->cur++ = 'f';
572
- *out->cur++ = '"';
573
- *out->cur++ = ':';
574
- dump_hash_class(obj, Qundef, depth + 1, out);
575
- break;
576
- default:
577
- break;
551
+ assure_size(out, d2 * out->indent + 14);
552
+ *out->cur++ = ',';
553
+ fill_indent(out, d2);
554
+ *out->cur++ = '"';
555
+ *out->cur++ = 's';
556
+ *out->cur++ = 'e';
557
+ *out->cur++ = 'l';
558
+ *out->cur++ = 'f';
559
+ *out->cur++ = '"';
560
+ *out->cur++ = ':';
561
+ dump_hash_class(obj, Qundef, depth + 1, out);
562
+ break;
563
+ default: break;
578
564
  }
579
565
  {
580
- int cnt;
566
+ int cnt;
581
567
  #ifdef HAVE_RB_IVAR_COUNT
582
- cnt = (int)rb_ivar_count(obj);
568
+ cnt = (int)rb_ivar_count(obj);
583
569
  #else
584
- volatile VALUE vars = rb_funcall2(obj, oj_instance_variables_id, 0, 0);
585
- VALUE *np = RARRAY_PTR(vars);
586
- ID vid;
587
- const char *attr;
588
- int i;
589
- int first = 1;
590
-
591
- cnt = (int)RARRAY_LEN(vars);
570
+ volatile VALUE vars = rb_funcall2(obj, oj_instance_variables_id, 0, 0);
571
+ VALUE * np = RARRAY_PTR(vars);
572
+ ID vid;
573
+ const char * attr;
574
+ int i;
575
+ int first = 1;
576
+
577
+ cnt = (int)RARRAY_LEN(vars);
592
578
  #endif
593
- if (Qundef != clas && 0 < cnt) {
594
- *out->cur++ = ',';
595
- }
596
- if (0 == cnt && Qundef == clas) {
597
- // Might be something special like an Enumerable.
598
- if (Qtrue == rb_obj_is_kind_of(obj, oj_enumerable_class)) {
599
- out->cur--;
600
- oj_dump_obj_val(rb_funcall(obj, rb_intern("entries"), 0), depth, out);
601
- return;
602
- }
603
- }
604
- out->depth = depth + 1;
579
+ if (Qundef != clas && 0 < cnt) {
580
+ *out->cur++ = ',';
581
+ }
582
+ if (0 == cnt && Qundef == clas) {
583
+ // Might be something special like an Enumerable.
584
+ if (Qtrue == rb_obj_is_kind_of(obj, oj_enumerable_class)) {
585
+ out->cur--;
586
+ oj_dump_obj_val(rb_funcall(obj, rb_intern("entries"), 0), depth, out);
587
+ return;
588
+ }
589
+ }
590
+ out->depth = depth + 1;
605
591
  #ifdef HAVE_RB_IVAR_FOREACH
606
- rb_ivar_foreach(obj, dump_attr_cb, (VALUE)out);
607
- if (',' == *(out->cur - 1)) {
608
- out->cur--; // backup to overwrite last comma
609
- }
592
+ rb_ivar_foreach(obj, dump_attr_cb, (VALUE)out);
593
+ if (',' == *(out->cur - 1)) {
594
+ out->cur--; // backup to overwrite last comma
595
+ }
610
596
  #else
611
- size = d2 * out->indent + 1;
612
- for (i = cnt; 0 < i; i--, np++) {
613
- VALUE value;
614
-
615
- vid = rb_to_id(*np);
616
- attr = rb_id2name(vid);
617
- if (Yes == out->opts->ignore_under && '@' == *attr && '_' == attr[1]) {
618
- continue;
619
- }
620
- value = rb_ivar_get(obj, vid);
621
-
622
- if (oj_dump_ignore(out->opts, value)) {
623
- continue;
624
- }
625
- if (out->omit_nil && Qnil == value) {
626
- continue;
627
- }
628
- if (first) {
629
- first = 0;
630
- } else {
631
- *out->cur++ = ',';
632
- }
633
- assure_size(out, size);
634
- fill_indent(out, d2);
635
- if ('@' == *attr) {
636
- attr++;
637
- oj_dump_cstr(attr, strlen(attr), 0, 0, out);
638
- } else {
639
- char buf[32];
640
-
641
- *buf = '~';
642
- strncpy(buf + 1, attr, sizeof(buf) - 2);
643
- buf[sizeof(buf) - 1] = '\0';
644
- oj_dump_cstr(buf, strlen(attr) + 1, 0, 0, out);
645
- }
646
- *out->cur++ = ':';
647
- oj_dump_obj_val(value, d2, out);
648
- assure_size(out, 2);
649
- }
597
+ size = d2 * out->indent + 1;
598
+ for (i = cnt; 0 < i; i--, np++) {
599
+ VALUE value;
600
+
601
+ vid = rb_to_id(*np);
602
+ attr = rb_id2name(vid);
603
+ if (Yes == out->opts->ignore_under && '@' == *attr && '_' == attr[1]) {
604
+ continue;
605
+ }
606
+ value = rb_ivar_get(obj, vid);
607
+
608
+ if (oj_dump_ignore(out->opts, value)) {
609
+ continue;
610
+ }
611
+ if (out->omit_nil && Qnil == value) {
612
+ continue;
613
+ }
614
+ if (first) {
615
+ first = 0;
616
+ } else {
617
+ *out->cur++ = ',';
618
+ }
619
+ assure_size(out, size);
620
+ fill_indent(out, d2);
621
+ if ('@' == *attr) {
622
+ attr++;
623
+ oj_dump_cstr(attr, strlen(attr), 0, 0, out);
624
+ } else {
625
+ char buf[32];
626
+
627
+ *buf = '~';
628
+ strncpy(buf + 1, attr, sizeof(buf) - 2);
629
+ buf[sizeof(buf) - 1] = '\0';
630
+ oj_dump_cstr(buf, strlen(attr) + 1, 0, 0, out);
631
+ }
632
+ *out->cur++ = ':';
633
+ oj_dump_obj_val(value, d2, out);
634
+ assure_size(out, 2);
635
+ }
650
636
  #endif
651
- if (rb_obj_is_kind_of(obj, rb_eException)) {
652
- volatile VALUE rv;
653
-
654
- if (',' != *(out->cur - 1)) {
655
- *out->cur++ = ',';
656
- }
657
- // message
658
- assure_size(out, size);
659
- fill_indent(out, d2);
660
- oj_dump_cstr("~mesg", 5, 0, 0, out);
661
- *out->cur++ = ':';
662
- rv = rb_funcall2(obj, rb_intern("message"), 0, 0);
663
- oj_dump_obj_val(rv, d2, out);
664
- assure_size(out, 2);
665
- *out->cur++ = ',';
666
- // backtrace
667
- assure_size(out, size);
668
- fill_indent(out, d2);
669
- oj_dump_cstr("~bt", 3, 0, 0, out);
670
- *out->cur++ = ':';
671
- rv = rb_funcall2(obj, rb_intern("backtrace"), 0, 0);
672
- oj_dump_obj_val(rv, d2, out);
673
- assure_size(out, 2);
674
- }
675
- out->depth = depth;
637
+ if (rb_obj_is_kind_of(obj, rb_eException)) {
638
+ volatile VALUE rv;
639
+
640
+ if (',' != *(out->cur - 1)) {
641
+ *out->cur++ = ',';
642
+ }
643
+ // message
644
+ assure_size(out, size);
645
+ fill_indent(out, d2);
646
+ oj_dump_cstr("~mesg", 5, 0, 0, out);
647
+ *out->cur++ = ':';
648
+ rv = rb_funcall2(obj, rb_intern("message"), 0, 0);
649
+ oj_dump_obj_val(rv, d2, out);
650
+ assure_size(out, 2);
651
+ *out->cur++ = ',';
652
+ // backtrace
653
+ assure_size(out, size);
654
+ fill_indent(out, d2);
655
+ oj_dump_cstr("~bt", 3, 0, 0, out);
656
+ *out->cur++ = ':';
657
+ rv = rb_funcall2(obj, rb_intern("backtrace"), 0, 0);
658
+ oj_dump_obj_val(rv, d2, out);
659
+ assure_size(out, 2);
660
+ }
661
+ out->depth = depth;
676
662
  }
677
663
  fill_indent(out, depth);
678
664
  *out->cur++ = '}';
679
- *out->cur = '\0';
665
+ *out->cur = '\0';
680
666
  }
681
667
 
682
- static void
683
- dump_regexp(VALUE obj, int depth, Out out, bool as_ok) {
668
+ static void dump_regexp(VALUE obj, int depth, Out out, bool as_ok) {
684
669
  dump_obj_attrs(obj, rb_obj_class(obj), 0, depth, out);
685
670
  }
686
671
 
687
- static void
688
- dump_struct(VALUE obj, int depth, Out out, bool as_ok) {
689
- VALUE clas = rb_obj_class(obj);
690
- const char *class_name = rb_class2name(clas);
691
- int i;
692
- int d2 = depth + 1;
693
- int d3 = d2 + 1;
694
- size_t len = strlen(class_name);
695
- size_t size = d2 * out->indent + d3 * out->indent + 10 + len;
672
+ static void dump_struct(VALUE obj, int depth, Out out, bool as_ok) {
673
+ VALUE clas = rb_obj_class(obj);
674
+ const char *class_name = rb_class2name(clas);
675
+ int i;
676
+ int d2 = depth + 1;
677
+ int d3 = d2 + 1;
678
+ size_t len = strlen(class_name);
679
+ size_t size = d2 * out->indent + d3 * out->indent + 10 + len;
696
680
 
697
681
  assure_size(out, size);
698
682
  *out->cur++ = '{';
@@ -704,138 +688,135 @@ dump_struct(VALUE obj, int depth, Out out, bool as_ok) {
704
688
  *out->cur++ = ':';
705
689
  *out->cur++ = '[';
706
690
  if ('#' == *class_name) {
707
- VALUE ma = rb_struct_s_members(clas);
708
- const char *name;
709
- int cnt = (int)RARRAY_LEN(ma);
710
-
711
- *out->cur++ = '[';
712
- for (i = 0; i < cnt; i++) {
713
- volatile VALUE s = rb_sym_to_s(rb_ary_entry(ma, i));
714
-
715
- name = rb_string_value_ptr((VALUE*)&s);
716
- len = (int)RSTRING_LEN(s);
717
- size = len + 3;
718
- assure_size(out, size);
719
- if (0 < i) {
720
- *out->cur++ = ',';
721
- }
722
- *out->cur++ = '"';
723
- memcpy(out->cur, name, len);
724
- out->cur += len;
725
- *out->cur++ = '"';
726
- }
727
- *out->cur++ = ']';
691
+ VALUE ma = rb_struct_s_members(clas);
692
+ const char *name;
693
+ int cnt = (int)RARRAY_LEN(ma);
694
+
695
+ *out->cur++ = '[';
696
+ for (i = 0; i < cnt; i++) {
697
+ volatile VALUE s = rb_sym_to_s(rb_ary_entry(ma, i));
698
+
699
+ name = rb_string_value_ptr((VALUE *)&s);
700
+ len = (int)RSTRING_LEN(s);
701
+ size = len + 3;
702
+ assure_size(out, size);
703
+ if (0 < i) {
704
+ *out->cur++ = ',';
705
+ }
706
+ *out->cur++ = '"';
707
+ memcpy(out->cur, name, len);
708
+ out->cur += len;
709
+ *out->cur++ = '"';
710
+ }
711
+ *out->cur++ = ']';
728
712
  } else {
729
- fill_indent(out, d3);
730
- *out->cur++ = '"';
731
- memcpy(out->cur, class_name, len);
732
- out->cur += len;
733
- *out->cur++ = '"';
713
+ fill_indent(out, d3);
714
+ *out->cur++ = '"';
715
+ memcpy(out->cur, class_name, len);
716
+ out->cur += len;
717
+ *out->cur++ = '"';
734
718
  }
735
719
  *out->cur++ = ',';
736
- size = d3 * out->indent + 2;
720
+ size = d3 * out->indent + 2;
737
721
  #ifdef RSTRUCT_LEN
738
722
  {
739
- VALUE v;
740
- int cnt;
723
+ VALUE v;
724
+ int cnt;
741
725
  #if RSTRUCT_LEN_RETURNS_INTEGER_OBJECT
742
- cnt = (int)NUM2LONG(RSTRUCT_LEN(obj));
743
- #else // RSTRUCT_LEN_RETURNS_INTEGER_OBJECT
744
- cnt = (int)RSTRUCT_LEN(obj);
745
- #endif // RSTRUCT_LEN_RETURNS_INTEGER_OBJECT
746
-
747
- for (i = 0; i < cnt; i++) {
748
- v = RSTRUCT_GET(obj, i);
749
- if (oj_dump_ignore(out->opts, v)) {
750
- v = Qnil;
751
- }
752
- assure_size(out, size);
753
- fill_indent(out, d3);
754
- oj_dump_obj_val(v, d3, out);
755
- *out->cur++ = ',';
756
- }
726
+ cnt = (int)NUM2LONG(RSTRUCT_LEN(obj));
727
+ #else // RSTRUCT_LEN_RETURNS_INTEGER_OBJECT
728
+ cnt = (int)RSTRUCT_LEN(obj);
729
+ #endif // RSTRUCT_LEN_RETURNS_INTEGER_OBJECT
730
+
731
+ for (i = 0; i < cnt; i++) {
732
+ v = RSTRUCT_GET(obj, i);
733
+ if (oj_dump_ignore(out->opts, v)) {
734
+ v = Qnil;
735
+ }
736
+ assure_size(out, size);
737
+ fill_indent(out, d3);
738
+ oj_dump_obj_val(v, d3, out);
739
+ *out->cur++ = ',';
740
+ }
757
741
  }
758
742
  #else
759
743
  {
760
- // This is a bit risky as a struct in C ruby is not the same as a Struct
761
- // class in interpreted Ruby so length() may not be defined.
762
- int slen = FIX2INT(rb_funcall2(obj, oj_length_id, 0, 0));
763
-
764
- for (i = 0; i < slen; i++) {
765
- assure_size(out, size);
766
- fill_indent(out, d3);
767
- if (oj_dump_ignore(out->opts, v)) {
768
- v = Qnil;
769
- }
770
- oj_dump_obj_val(rb_struct_aref(obj, INT2FIX(i)), d3, out, 0, 0, true);
771
- *out->cur++ = ',';
772
- }
744
+ // This is a bit risky as a struct in C ruby is not the same as a Struct
745
+ // class in interpreted Ruby so length() may not be defined.
746
+ int slen = FIX2INT(rb_funcall2(obj, oj_length_id, 0, 0));
747
+
748
+ for (i = 0; i < slen; i++) {
749
+ assure_size(out, size);
750
+ fill_indent(out, d3);
751
+ if (oj_dump_ignore(out->opts, v)) {
752
+ v = Qnil;
753
+ }
754
+ oj_dump_obj_val(rb_struct_aref(obj, INT2FIX(i)), d3, out, 0, 0, true);
755
+ *out->cur++ = ',';
756
+ }
773
757
  }
774
758
  #endif
775
759
  out->cur--;
776
760
  *out->cur++ = ']';
777
761
  *out->cur++ = '}';
778
- *out->cur = '\0';
762
+ *out->cur = '\0';
779
763
  }
780
764
 
781
- static void
782
- dump_complex(VALUE obj, int depth, Out out, bool as_ok) {
765
+ static void dump_complex(VALUE obj, int depth, Out out, bool as_ok) {
783
766
  dump_obj_attrs(obj, rb_obj_class(obj), 0, depth, out);
784
767
  }
785
768
 
786
- static void
787
- dump_rational(VALUE obj, int depth, Out out, bool as_ok) {
769
+ static void dump_rational(VALUE obj, int depth, Out out, bool as_ok) {
788
770
  dump_obj_attrs(obj, rb_obj_class(obj), 0, depth, out);
789
771
  }
790
772
 
791
- static DumpFunc obj_funcs[] = {
792
- NULL, // RUBY_T_NONE = 0x00,
793
- dump_obj, // RUBY_T_OBJECT = 0x01,
794
- dump_class, // RUBY_T_CLASS = 0x02,
795
- dump_class, // RUBY_T_MODULE = 0x03,
796
- oj_dump_float, // RUBY_T_FLOAT = 0x04,
797
- dump_str, // RUBY_T_STRING = 0x05,
798
- dump_regexp, // RUBY_T_REGEXP = 0x06,
799
- dump_array, // RUBY_T_ARRAY = 0x07,
800
- dump_hash, // RUBY_T_HASH = 0x08,
801
- dump_struct, // RUBY_T_STRUCT = 0x09,
802
- oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
803
- NULL, // RUBY_T_FILE = 0x0b,
804
- dump_data, // RUBY_T_DATA = 0x0c,
805
- NULL, // RUBY_T_MATCH = 0x0d,
806
- dump_complex, // RUBY_T_COMPLEX = 0x0e,
807
- dump_rational, // RUBY_T_RATIONAL = 0x0f,
808
- NULL, // 0x10
809
- oj_dump_nil, // RUBY_T_NIL = 0x11,
810
- oj_dump_true, // RUBY_T_TRUE = 0x12,
811
- oj_dump_false, // RUBY_T_FALSE = 0x13,
812
- dump_sym, // RUBY_T_SYMBOL = 0x14,
813
- oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
773
+ static DumpFunc obj_funcs[] = {
774
+ NULL, // RUBY_T_NONE = 0x00,
775
+ dump_obj, // RUBY_T_OBJECT = 0x01,
776
+ dump_class, // RUBY_T_CLASS = 0x02,
777
+ dump_class, // RUBY_T_MODULE = 0x03,
778
+ oj_dump_float, // RUBY_T_FLOAT = 0x04,
779
+ dump_str, // RUBY_T_STRING = 0x05,
780
+ dump_regexp, // RUBY_T_REGEXP = 0x06,
781
+ dump_array, // RUBY_T_ARRAY = 0x07,
782
+ dump_hash, // RUBY_T_HASH = 0x08,
783
+ dump_struct, // RUBY_T_STRUCT = 0x09,
784
+ oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
785
+ NULL, // RUBY_T_FILE = 0x0b,
786
+ dump_data, // RUBY_T_DATA = 0x0c,
787
+ NULL, // RUBY_T_MATCH = 0x0d,
788
+ dump_complex, // RUBY_T_COMPLEX = 0x0e,
789
+ dump_rational, // RUBY_T_RATIONAL = 0x0f,
790
+ NULL, // 0x10
791
+ oj_dump_nil, // RUBY_T_NIL = 0x11,
792
+ oj_dump_true, // RUBY_T_TRUE = 0x12,
793
+ oj_dump_false, // RUBY_T_FALSE = 0x13,
794
+ dump_sym, // RUBY_T_SYMBOL = 0x14,
795
+ oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
814
796
  };
815
797
 
816
- void
817
- oj_dump_obj_val(VALUE obj, int depth, Out out) {
818
- int type = rb_type(obj);
798
+ void oj_dump_obj_val(VALUE obj, int depth, Out out) {
799
+ int type = rb_type(obj);
819
800
 
820
801
  if (Yes == out->opts->trace) {
821
- oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceIn);
802
+ oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceIn);
822
803
  }
823
804
  if (MAX_DEPTH < depth) {
824
- rb_raise(rb_eNoMemError, "Too deeply nested.\n");
805
+ rb_raise(rb_eNoMemError, "Too deeply nested.\n");
825
806
  }
826
807
  if (0 < type && type <= RUBY_T_FIXNUM) {
827
- DumpFunc f = obj_funcs[type];
808
+ DumpFunc f = obj_funcs[type];
828
809
 
829
- if (NULL != f) {
830
- f(obj, depth, out, false);
831
- if (Yes == out->opts->trace) {
832
- oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceOut);
833
- }
834
- return;
835
- }
810
+ if (NULL != f) {
811
+ f(obj, depth, out, false);
812
+ if (Yes == out->opts->trace) {
813
+ oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceOut);
814
+ }
815
+ return;
816
+ }
836
817
  }
837
818
  oj_dump_nil(Qnil, depth, out, false);
838
819
  if (Yes == out->opts->trace) {
839
- oj_trace("dump", Qnil, __FILE__, __LINE__, depth, TraceOut);
820
+ oj_trace("dump", Qnil, __FILE__, __LINE__, depth, TraceOut);
840
821
  }
841
822
  }