oj 3.7.4 → 3.13.21

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1352 -0
  3. data/README.md +29 -8
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +53 -72
  6. data/ext/oj/cache.c +326 -0
  7. data/ext/oj/cache.h +21 -0
  8. data/ext/oj/cache8.c +61 -64
  9. data/ext/oj/cache8.h +12 -39
  10. data/ext/oj/circarray.c +37 -43
  11. data/ext/oj/circarray.h +16 -17
  12. data/ext/oj/code.c +165 -179
  13. data/ext/oj/code.h +27 -29
  14. data/ext/oj/compat.c +174 -194
  15. data/ext/oj/custom.c +809 -866
  16. data/ext/oj/debug.c +132 -0
  17. data/ext/oj/dump.c +848 -863
  18. data/ext/oj/dump.h +81 -67
  19. data/ext/oj/dump_compat.c +85 -123
  20. data/ext/oj/dump_leaf.c +100 -188
  21. data/ext/oj/dump_object.c +527 -656
  22. data/ext/oj/dump_strict.c +315 -338
  23. data/ext/oj/encode.h +7 -34
  24. data/ext/oj/encoder.c +43 -0
  25. data/ext/oj/err.c +40 -29
  26. data/ext/oj/err.h +48 -48
  27. data/ext/oj/extconf.rb +17 -4
  28. data/ext/oj/fast.c +1070 -1087
  29. data/ext/oj/intern.c +301 -0
  30. data/ext/oj/intern.h +26 -0
  31. data/ext/oj/mimic_json.c +469 -436
  32. data/ext/oj/object.c +525 -593
  33. data/ext/oj/odd.c +154 -138
  34. data/ext/oj/odd.h +37 -38
  35. data/ext/oj/oj.c +1325 -986
  36. data/ext/oj/oj.h +333 -316
  37. data/ext/oj/parse.c +1002 -846
  38. data/ext/oj/parse.h +92 -87
  39. data/ext/oj/parser.c +1557 -0
  40. data/ext/oj/parser.h +91 -0
  41. data/ext/oj/rails.c +888 -878
  42. data/ext/oj/rails.h +11 -14
  43. data/ext/oj/reader.c +141 -147
  44. data/ext/oj/reader.h +73 -89
  45. data/ext/oj/resolve.c +41 -62
  46. data/ext/oj/resolve.h +7 -9
  47. data/ext/oj/rxclass.c +71 -75
  48. data/ext/oj/rxclass.h +18 -19
  49. data/ext/oj/saj.c +443 -486
  50. data/ext/oj/saj2.c +602 -0
  51. data/ext/oj/scp.c +88 -113
  52. data/ext/oj/sparse.c +787 -709
  53. data/ext/oj/stream_writer.c +133 -159
  54. data/ext/oj/strict.c +127 -118
  55. data/ext/oj/string_writer.c +230 -249
  56. data/ext/oj/trace.c +34 -41
  57. data/ext/oj/trace.h +19 -19
  58. data/ext/oj/usual.c +1254 -0
  59. data/ext/oj/util.c +136 -0
  60. data/ext/oj/util.h +20 -0
  61. data/ext/oj/val_stack.c +59 -67
  62. data/ext/oj/val_stack.h +91 -129
  63. data/ext/oj/validate.c +46 -0
  64. data/ext/oj/wab.c +342 -353
  65. data/lib/oj/bag.rb +1 -0
  66. data/lib/oj/easy_hash.rb +5 -4
  67. data/lib/oj/error.rb +1 -1
  68. data/lib/oj/json.rb +1 -1
  69. data/lib/oj/mimic.rb +48 -14
  70. data/lib/oj/saj.rb +20 -6
  71. data/lib/oj/state.rb +8 -7
  72. data/lib/oj/version.rb +2 -2
  73. data/lib/oj.rb +0 -8
  74. data/pages/Compatibility.md +1 -1
  75. data/pages/JsonGem.md +15 -0
  76. data/pages/Modes.md +53 -46
  77. data/pages/Options.md +72 -11
  78. data/pages/Parser.md +309 -0
  79. data/pages/Rails.md +73 -22
  80. data/pages/Security.md +1 -1
  81. data/test/activerecord/result_test.rb +7 -2
  82. data/test/activesupport5/abstract_unit.rb +45 -0
  83. data/test/activesupport5/decoding_test.rb +68 -60
  84. data/test/activesupport5/encoding_test.rb +111 -96
  85. data/test/activesupport5/encoding_test_cases.rb +33 -25
  86. data/test/activesupport5/test_helper.rb +43 -21
  87. data/test/activesupport5/time_zone_test_helpers.rb +18 -3
  88. data/test/activesupport6/abstract_unit.rb +44 -0
  89. data/test/activesupport6/decoding_test.rb +133 -0
  90. data/test/activesupport6/encoding_test.rb +507 -0
  91. data/test/activesupport6/encoding_test_cases.rb +98 -0
  92. data/test/activesupport6/test_common.rb +17 -0
  93. data/test/activesupport6/test_helper.rb +163 -0
  94. data/test/activesupport6/time_zone_test_helpers.rb +39 -0
  95. data/test/activesupport7/abstract_unit.rb +49 -0
  96. data/test/activesupport7/decoding_test.rb +125 -0
  97. data/test/activesupport7/encoding_test.rb +486 -0
  98. data/test/activesupport7/encoding_test_cases.rb +104 -0
  99. data/test/activesupport7/time_zone_test_helpers.rb +47 -0
  100. data/test/bar.rb +6 -12
  101. data/test/baz.rb +16 -0
  102. data/test/bug.rb +16 -0
  103. data/test/foo.rb +69 -75
  104. data/test/helper.rb +16 -0
  105. data/test/json_gem/json_common_interface_test.rb +8 -3
  106. data/test/json_gem/json_generator_test.rb +18 -4
  107. data/test/json_gem/json_parser_test.rb +9 -0
  108. data/test/json_gem/test_helper.rb +12 -0
  109. data/test/mem.rb +33 -0
  110. data/test/perf.rb +1 -1
  111. data/test/perf_dump.rb +50 -0
  112. data/test/perf_once.rb +58 -0
  113. data/test/perf_parser.rb +189 -0
  114. data/test/perf_scp.rb +11 -10
  115. data/test/perf_strict.rb +17 -23
  116. data/test/prec.rb +23 -0
  117. data/test/sample_json.rb +1 -1
  118. data/test/test_compat.rb +46 -10
  119. data/test/test_custom.rb +147 -8
  120. data/test/test_fast.rb +62 -2
  121. data/test/test_file.rb +25 -2
  122. data/test/test_gc.rb +13 -0
  123. data/test/test_generate.rb +21 -0
  124. data/test/test_hash.rb +11 -1
  125. data/test/test_integer_range.rb +7 -2
  126. data/test/test_object.rb +85 -9
  127. data/test/test_parser.rb +27 -0
  128. data/test/test_parser_saj.rb +335 -0
  129. data/test/test_parser_usual.rb +217 -0
  130. data/test/test_rails.rb +35 -0
  131. data/test/test_saj.rb +1 -1
  132. data/test/test_scp.rb +5 -5
  133. data/test/test_strict.rb +26 -1
  134. data/test/test_various.rb +87 -65
  135. data/test/test_wab.rb +2 -0
  136. data/test/test_writer.rb +19 -2
  137. data/test/tests.rb +1 -1
  138. data/test/zoo.rb +13 -0
  139. metadata +60 -110
  140. data/ext/oj/hash.c +0 -163
  141. data/ext/oj/hash.h +0 -46
  142. data/ext/oj/hash_test.c +0 -512
data/ext/oj/dump_strict.c CHANGED
@@ -1,433 +1,410 @@
1
- /* dump_strict.c
2
- * Copyright (c) 2012, 2017, Peter Ohler
3
- * All rights reserved.
4
- */
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.
5
3
 
6
- #include <stdlib.h>
7
- #include <time.h>
4
+ #include <errno.h>
5
+ #include <math.h>
8
6
  #include <stdio.h>
7
+ #include <stdlib.h>
9
8
  #include <string.h>
10
- #include <math.h>
9
+ #include <time.h>
11
10
  #include <unistd.h>
12
- #include <errno.h>
13
11
 
14
12
  #include "dump.h"
15
13
  #include "trace.h"
16
14
 
17
15
  // Workaround in case INFINITY is not defined in math.h or if the OS is CentOS
18
- #define OJ_INFINITY (1.0/0.0)
16
+ #define OJ_INFINITY (1.0 / 0.0)
19
17
 
20
- typedef unsigned long ulong;
18
+ typedef unsigned long ulong;
21
19
 
22
- static const char inf_val[] = INF_VAL;
23
- static const char ninf_val[] = NINF_VAL;
24
- static const char nan_val[] = NAN_VAL;
20
+ static const char inf_val[] = INF_VAL;
21
+ static const char ninf_val[] = NINF_VAL;
22
+ static const char nan_val[] = NAN_VAL;
25
23
 
26
- static void
27
- raise_strict(VALUE obj) {
28
- rb_raise(rb_eTypeError, "Failed to dump %s Object to JSON in strict mode.\n", rb_class2name(rb_obj_class(obj)));
24
+ static void raise_strict(VALUE obj) {
25
+ rb_raise(rb_eTypeError,
26
+ "Failed to dump %s Object to JSON in strict mode.\n",
27
+ rb_class2name(rb_obj_class(obj)));
29
28
  }
30
29
 
31
30
  // Removed dependencies on math due to problems with CentOS 5.4.
32
- static void
33
- dump_float(VALUE obj, int depth, Out out, bool as_ok) {
34
- char buf[64];
35
- char *b;
36
- double d = rb_num2dbl(obj);
37
- int cnt = 0;
31
+ static void dump_float(VALUE obj, int depth, Out out, bool as_ok) {
32
+ char buf[64];
33
+ char * b;
34
+ double d = rb_num2dbl(obj);
35
+ int cnt = 0;
38
36
 
39
37
  if (0.0 == d) {
40
- b = buf;
41
- *b++ = '0';
42
- *b++ = '.';
43
- *b++ = '0';
44
- *b++ = '\0';
45
- cnt = 3;
38
+ b = buf;
39
+ *b++ = '0';
40
+ *b++ = '.';
41
+ *b++ = '0';
42
+ *b++ = '\0';
43
+ cnt = 3;
46
44
  } else {
47
- NanDump nd = out->opts->dump_opts.nan_dump;
48
-
49
- if (AutoNan == nd) {
50
- nd = RaiseNan;
51
- }
52
- if (OJ_INFINITY == d) {
53
- switch (nd) {
54
- case RaiseNan:
55
- case WordNan:
56
- raise_strict(obj);
57
- break;
58
- case NullNan:
59
- strcpy(buf, "null");
60
- cnt = 4;
61
- break;
62
- case HugeNan:
63
- default:
64
- strcpy(buf, inf_val);
65
- cnt = sizeof(inf_val) - 1;
66
- break;
67
- }
68
- } else if (-OJ_INFINITY == d) {
69
- switch (nd) {
70
- case RaiseNan:
71
- case WordNan:
72
- raise_strict(obj);
73
- break;
74
- case NullNan:
75
- strcpy(buf, "null");
76
- cnt = 4;
77
- break;
78
- case HugeNan:
79
- default:
80
- strcpy(buf, ninf_val);
81
- cnt = sizeof(ninf_val) - 1;
82
- break;
83
- }
84
- } else if (isnan(d)) {
85
- switch (nd) {
86
- case RaiseNan:
87
- case WordNan:
88
- raise_strict(obj);
89
- break;
90
- case NullNan:
91
- strcpy(buf, "null");
92
- cnt = 4;
93
- break;
94
- case HugeNan:
95
- default:
96
- strcpy(buf, nan_val);
97
- cnt = sizeof(nan_val) - 1;
98
- break;
99
- }
100
- } else if (d == (double)(long long int)d) {
101
- cnt = snprintf(buf, sizeof(buf), "%.1f", d);
102
- } else if (0 == out->opts->float_prec) {
103
- volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
104
-
105
- cnt = (int)RSTRING_LEN(rstr);
106
- if ((int)sizeof(buf) <= cnt) {
107
- cnt = sizeof(buf) - 1;
108
- }
109
- strncpy(buf, rb_string_value_ptr((VALUE*)&rstr), cnt);
110
- buf[cnt] = '\0';
111
- } else {
112
- cnt = oj_dump_float_printf(buf, sizeof(buf), obj, d, out->opts->float_fmt);
113
- }
45
+ NanDump nd = out->opts->dump_opts.nan_dump;
46
+
47
+ if (AutoNan == nd) {
48
+ nd = RaiseNan;
49
+ }
50
+ if (OJ_INFINITY == d) {
51
+ switch (nd) {
52
+ case RaiseNan:
53
+ case WordNan: raise_strict(obj); break;
54
+ case NullNan:
55
+ strcpy(buf, "null");
56
+ cnt = 4;
57
+ break;
58
+ case HugeNan:
59
+ default:
60
+ strcpy(buf, inf_val);
61
+ cnt = sizeof(inf_val) - 1;
62
+ break;
63
+ }
64
+ } else if (-OJ_INFINITY == d) {
65
+ switch (nd) {
66
+ case RaiseNan:
67
+ case WordNan: raise_strict(obj); break;
68
+ case NullNan:
69
+ strcpy(buf, "null");
70
+ cnt = 4;
71
+ break;
72
+ case HugeNan:
73
+ default:
74
+ strcpy(buf, ninf_val);
75
+ cnt = sizeof(ninf_val) - 1;
76
+ break;
77
+ }
78
+ } else if (isnan(d)) {
79
+ switch (nd) {
80
+ case RaiseNan:
81
+ case WordNan: raise_strict(obj); break;
82
+ case NullNan:
83
+ strcpy(buf, "null");
84
+ cnt = 4;
85
+ break;
86
+ case HugeNan:
87
+ default:
88
+ strcpy(buf, nan_val);
89
+ cnt = sizeof(nan_val) - 1;
90
+ break;
91
+ }
92
+ } else if (d == (double)(long long int)d) {
93
+ cnt = snprintf(buf, sizeof(buf), "%.1f", d);
94
+ } else if (0 == out->opts->float_prec) {
95
+ volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
96
+
97
+ cnt = (int)RSTRING_LEN(rstr);
98
+ if ((int)sizeof(buf) <= cnt) {
99
+ cnt = sizeof(buf) - 1;
100
+ }
101
+ memcpy(buf, RSTRING_PTR(rstr), cnt);
102
+ buf[cnt] = '\0';
103
+ } else {
104
+ cnt = oj_dump_float_printf(buf, sizeof(buf), obj, d, out->opts->float_fmt);
105
+ }
114
106
  }
115
107
  assure_size(out, cnt);
116
- for (b = buf; '\0' != *b; b++) {
117
- *out->cur++ = *b;
118
- }
108
+ APPEND_CHARS(out->cur, buf, cnt);
119
109
  *out->cur = '\0';
120
110
  }
121
111
 
122
- static void
123
- dump_array(VALUE a, int depth, Out out, bool as_ok) {
124
- size_t size;
125
- int i, cnt;
126
- int d2 = depth + 1;
112
+ static void dump_array(VALUE a, int depth, Out out, bool as_ok) {
113
+ size_t size;
114
+ int i, cnt;
115
+ int d2 = depth + 1;
127
116
 
128
117
  if (Yes == out->opts->circular) {
129
- if (0 > oj_check_circular(a, out)) {
130
- oj_dump_nil(Qnil, 0, out, false);
131
- return;
132
- }
118
+ if (0 > oj_check_circular(a, out)) {
119
+ oj_dump_nil(Qnil, 0, out, false);
120
+ return;
121
+ }
133
122
  }
134
- cnt = (int)RARRAY_LEN(a);
123
+ cnt = (int)RARRAY_LEN(a);
135
124
  *out->cur++ = '[';
136
- size = 2;
125
+ size = 2;
137
126
  assure_size(out, size);
138
127
  if (0 == cnt) {
139
- *out->cur++ = ']';
128
+ *out->cur++ = ']';
140
129
  } else {
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
- if (NullMode == out->opts->mode) {
165
- oj_dump_null_val(rb_ary_entry(a, i), d2, out);
166
- } else {
167
- oj_dump_strict_val(rb_ary_entry(a, i), d2, out);
168
- }
169
- if (i < cnt) {
170
- *out->cur++ = ',';
171
- }
172
- }
173
- size = depth * out->indent + 1;
174
- assure_size(out, size);
175
- if (out->opts->dump_opts.use) {
176
- //printf("*** d2: %u indent: %u '%s'\n", d2, out->opts->dump_opts->indent_size, out->opts->dump_opts->indent);
177
- if (0 < out->opts->dump_opts.array_size) {
178
- strcpy(out->cur, out->opts->dump_opts.array_nl);
179
- out->cur += out->opts->dump_opts.array_size;
180
- }
181
- if (0 < out->opts->dump_opts.indent_size) {
182
- int i;
183
-
184
- for (i = depth; 0 < i; i--) {
185
- strcpy(out->cur, out->opts->dump_opts.indent_str);
186
- out->cur += out->opts->dump_opts.indent_size;
187
- }
188
- }
189
- } else {
190
- fill_indent(out, depth);
191
- }
192
- *out->cur++ = ']';
130
+ if (out->opts->dump_opts.use) {
131
+ size = d2 * out->opts->dump_opts.indent_size + out->opts->dump_opts.array_size + 1;
132
+ } else {
133
+ size = d2 * out->indent + 2;
134
+ }
135
+ assure_size(out, size * cnt);
136
+ cnt--;
137
+ for (i = 0; i <= cnt; i++) {
138
+ if (out->opts->dump_opts.use) {
139
+ if (0 < out->opts->dump_opts.array_size) {
140
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
141
+ }
142
+ if (0 < out->opts->dump_opts.indent_size) {
143
+ int i;
144
+ for (i = d2; 0 < i; i--) {
145
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
146
+ }
147
+ }
148
+ } else {
149
+ fill_indent(out, d2);
150
+ }
151
+ if (NullMode == out->opts->mode) {
152
+ oj_dump_null_val(RARRAY_AREF(a, i), d2, out);
153
+ } else {
154
+ oj_dump_strict_val(RARRAY_AREF(a, i), d2, out);
155
+ }
156
+ if (i < cnt) {
157
+ *out->cur++ = ',';
158
+ }
159
+ }
160
+ size = depth * out->indent + 1;
161
+ assure_size(out, size);
162
+ if (out->opts->dump_opts.use) {
163
+ // printf("*** d2: %u indent: %u '%s'\n", d2, out->opts->dump_opts->indent_size,
164
+ // out->opts->dump_opts->indent);
165
+ if (0 < out->opts->dump_opts.array_size) {
166
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
167
+ }
168
+ if (0 < out->opts->dump_opts.indent_size) {
169
+ int i;
170
+
171
+ for (i = depth; 0 < i; i--) {
172
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
173
+ }
174
+ }
175
+ } else {
176
+ fill_indent(out, depth);
177
+ }
178
+ *out->cur++ = ']';
193
179
  }
194
180
  *out->cur = '\0';
195
181
  }
196
182
 
197
- static int
198
- hash_cb(VALUE key, VALUE value, Out out) {
199
- int depth = out->depth;
200
- long size;
201
- int rtype = rb_type(key);
202
-
183
+ static int hash_cb(VALUE key, VALUE value, VALUE ov) {
184
+ Out out = (Out)ov;
185
+ int depth = out->depth;
186
+ long size;
187
+ int rtype = rb_type(key);
188
+
203
189
  if (rtype != T_STRING && rtype != T_SYMBOL) {
204
- rb_raise(rb_eTypeError, "In :strict and :null mode all Hash keys must be Strings or Symbols, not %s.\n", rb_class2name(rb_obj_class(key)));
190
+ rb_raise(rb_eTypeError,
191
+ "In :strict and :null mode all Hash keys must be Strings or Symbols, not %s.\n",
192
+ rb_class2name(rb_obj_class(key)));
205
193
  }
206
194
  if (out->omit_nil && Qnil == value) {
207
- return ST_CONTINUE;
195
+ return ST_CONTINUE;
208
196
  }
209
197
  if (!out->opts->dump_opts.use) {
210
- size = depth * out->indent + 1;
211
- assure_size(out, size);
212
- fill_indent(out, depth);
213
- if (rtype == T_STRING) {
214
- oj_dump_str(key, 0, out, false);
215
- } else {
216
- oj_dump_sym(key, 0, out, false);
217
- }
218
- *out->cur++ = ':';
198
+ size = depth * out->indent + 1;
199
+ assure_size(out, size);
200
+ fill_indent(out, depth);
201
+ if (rtype == T_STRING) {
202
+ oj_dump_str(key, 0, out, false);
203
+ } else {
204
+ oj_dump_sym(key, 0, out, false);
205
+ }
206
+ *out->cur++ = ':';
219
207
  } else {
220
- size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
221
- assure_size(out, size);
222
- if (0 < out->opts->dump_opts.hash_size) {
223
- strcpy(out->cur, out->opts->dump_opts.hash_nl);
224
- out->cur += out->opts->dump_opts.hash_size;
225
- }
226
- if (0 < out->opts->dump_opts.indent_size) {
227
- int i;
228
- for (i = depth; 0 < i; i--) {
229
- strcpy(out->cur, out->opts->dump_opts.indent_str);
230
- out->cur += out->opts->dump_opts.indent_size;
231
- }
232
- }
233
- if (rtype == T_STRING) {
234
- oj_dump_str(key, 0, out, false);
235
- } else {
236
- oj_dump_sym(key, 0, out, false);
237
- }
238
- size = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
239
- assure_size(out, size);
240
- if (0 < out->opts->dump_opts.before_size) {
241
- strcpy(out->cur, out->opts->dump_opts.before_sep);
242
- out->cur += out->opts->dump_opts.before_size;
243
- }
244
- *out->cur++ = ':';
245
- if (0 < out->opts->dump_opts.after_size) {
246
- strcpy(out->cur, out->opts->dump_opts.after_sep);
247
- out->cur += out->opts->dump_opts.after_size;
248
- }
208
+ size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
209
+ assure_size(out, size);
210
+ if (0 < out->opts->dump_opts.hash_size) {
211
+ APPEND_CHARS(out->cur, out->opts->dump_opts.hash_nl, out->opts->dump_opts.hash_size);
212
+ }
213
+ if (0 < out->opts->dump_opts.indent_size) {
214
+ int i;
215
+ for (i = depth; 0 < i; i--) {
216
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
217
+ }
218
+ }
219
+ if (rtype == T_STRING) {
220
+ oj_dump_str(key, 0, out, false);
221
+ } else {
222
+ oj_dump_sym(key, 0, out, false);
223
+ }
224
+ size = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
225
+ assure_size(out, size);
226
+ if (0 < out->opts->dump_opts.before_size) {
227
+ APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
228
+ }
229
+ *out->cur++ = ':';
230
+ if (0 < out->opts->dump_opts.after_size) {
231
+ APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
232
+ }
249
233
  }
250
234
  if (NullMode == out->opts->mode) {
251
- oj_dump_null_val(value, depth, out);
235
+ oj_dump_null_val(value, depth, out);
252
236
  } else {
253
- oj_dump_strict_val(value, depth, out);
237
+ oj_dump_strict_val(value, depth, out);
254
238
  }
255
- out->depth = depth;
239
+ out->depth = depth;
256
240
  *out->cur++ = ',';
257
241
 
258
242
  return ST_CONTINUE;
259
243
  }
260
244
 
261
- static void
262
- dump_hash(VALUE obj, int depth, Out out, bool as_ok) {
263
- int cnt;
264
- size_t size;
245
+ static void dump_hash(VALUE obj, int depth, Out out, bool as_ok) {
246
+ int cnt;
247
+ size_t size;
265
248
 
266
249
  if (Yes == out->opts->circular) {
267
- if (0 > oj_check_circular(obj, out)) {
268
- oj_dump_nil(Qnil, 0, out, false);
269
- return;
270
- }
250
+ if (0 > oj_check_circular(obj, out)) {
251
+ oj_dump_nil(Qnil, 0, out, false);
252
+ return;
253
+ }
271
254
  }
272
- cnt = (int)RHASH_SIZE(obj);
255
+ cnt = (int)RHASH_SIZE(obj);
273
256
  size = depth * out->indent + 2;
274
257
  assure_size(out, 2);
275
258
  *out->cur++ = '{';
276
259
  if (0 == cnt) {
277
- *out->cur++ = '}';
260
+ *out->cur++ = '}';
278
261
  } else {
279
- out->depth = depth + 1;
280
- rb_hash_foreach(obj, hash_cb, (VALUE)out);
281
- if (',' == *(out->cur - 1)) {
282
- out->cur--; // backup to overwrite last comma
283
- }
284
- if (!out->opts->dump_opts.use) {
285
- assure_size(out, size);
286
- fill_indent(out, depth);
287
- } else {
288
- size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
289
- assure_size(out, size);
290
- if (0 < out->opts->dump_opts.hash_size) {
291
- strcpy(out->cur, out->opts->dump_opts.hash_nl);
292
- out->cur += out->opts->dump_opts.hash_size;
293
- }
294
- if (0 < out->opts->dump_opts.indent_size) {
295
- int i;
296
-
297
- for (i = depth; 0 < i; i--) {
298
- strcpy(out->cur, out->opts->dump_opts.indent_str);
299
- out->cur += out->opts->dump_opts.indent_size;
300
- }
301
- }
302
- }
303
- *out->cur++ = '}';
262
+ out->depth = depth + 1;
263
+ rb_hash_foreach(obj, hash_cb, (VALUE)out);
264
+ if (',' == *(out->cur - 1)) {
265
+ out->cur--; // backup to overwrite last comma
266
+ }
267
+ if (!out->opts->dump_opts.use) {
268
+ assure_size(out, size);
269
+ fill_indent(out, depth);
270
+ } else {
271
+ size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
272
+ assure_size(out, size);
273
+ if (0 < out->opts->dump_opts.hash_size) {
274
+ APPEND_CHARS(out->cur, out->opts->dump_opts.hash_nl, out->opts->dump_opts.hash_size);
275
+ }
276
+ if (0 < out->opts->dump_opts.indent_size) {
277
+ int i;
278
+
279
+ for (i = depth; 0 < i; i--) {
280
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
281
+ }
282
+ }
283
+ }
284
+ *out->cur++ = '}';
304
285
  }
305
286
  *out->cur = '\0';
306
287
  }
307
288
 
308
- static void
309
- dump_data_strict(VALUE obj, int depth, Out out, bool as_ok) {
310
- VALUE clas = rb_obj_class(obj);
289
+ static void dump_data_strict(VALUE obj, int depth, Out out, bool as_ok) {
290
+ VALUE clas = rb_obj_class(obj);
311
291
 
312
292
  if (oj_bigdecimal_class == clas) {
313
- volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
293
+ volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
314
294
 
315
- oj_dump_raw(rb_string_value_ptr((VALUE*)&rstr), (int)RSTRING_LEN(rstr), out);
295
+ oj_dump_raw(RSTRING_PTR(rstr), (int)RSTRING_LEN(rstr), out);
316
296
  } else {
317
- raise_strict(obj);
297
+ raise_strict(obj);
318
298
  }
319
299
  }
320
300
 
321
- static void
322
- dump_data_null(VALUE obj, int depth, Out out, bool as_ok) {
323
- VALUE clas = rb_obj_class(obj);
301
+ static void dump_data_null(VALUE obj, int depth, Out out, bool as_ok) {
302
+ VALUE clas = rb_obj_class(obj);
324
303
 
325
304
  if (oj_bigdecimal_class == clas) {
326
- volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
305
+ volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
327
306
 
328
- oj_dump_raw(rb_string_value_ptr((VALUE*)&rstr), (int)RSTRING_LEN(rstr), out);
307
+ oj_dump_raw(RSTRING_PTR(rstr), (int)RSTRING_LEN(rstr), out);
329
308
  } else {
330
- oj_dump_nil(Qnil, depth, out, false);
309
+ oj_dump_nil(Qnil, depth, out, false);
331
310
  }
332
311
  }
333
312
 
334
- static DumpFunc strict_funcs[] = {
335
- NULL, // RUBY_T_NONE = 0x00,
336
- dump_data_strict, // RUBY_T_OBJECT = 0x01,
337
- NULL, // RUBY_T_CLASS = 0x02,
338
- NULL, // RUBY_T_MODULE = 0x03,
339
- dump_float, // RUBY_T_FLOAT = 0x04,
340
- oj_dump_str, // RUBY_T_STRING = 0x05,
341
- NULL, // RUBY_T_REGEXP = 0x06,
342
- dump_array, // RUBY_T_ARRAY = 0x07,
343
- dump_hash, // RUBY_T_HASH = 0x08,
344
- NULL, // RUBY_T_STRUCT = 0x09,
345
- oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
346
- NULL, // RUBY_T_FILE = 0x0b,
347
- dump_data_strict, // RUBY_T_DATA = 0x0c,
348
- NULL, // RUBY_T_MATCH = 0x0d,
349
- NULL, // RUBY_T_COMPLEX = 0x0e,
350
- NULL, // RUBY_T_RATIONAL = 0x0f,
351
- NULL, // 0x10
352
- oj_dump_nil, // RUBY_T_NIL = 0x11,
353
- oj_dump_true, // RUBY_T_TRUE = 0x12,
354
- oj_dump_false, // RUBY_T_FALSE = 0x13,
355
- oj_dump_sym, // RUBY_T_SYMBOL = 0x14,
356
- oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
313
+ static DumpFunc strict_funcs[] = {
314
+ NULL, // RUBY_T_NONE = 0x00,
315
+ dump_data_strict, // RUBY_T_OBJECT = 0x01,
316
+ NULL, // RUBY_T_CLASS = 0x02,
317
+ NULL, // RUBY_T_MODULE = 0x03,
318
+ dump_float, // RUBY_T_FLOAT = 0x04,
319
+ oj_dump_str, // RUBY_T_STRING = 0x05,
320
+ NULL, // RUBY_T_REGEXP = 0x06,
321
+ dump_array, // RUBY_T_ARRAY = 0x07,
322
+ dump_hash, // RUBY_T_HASH = 0x08,
323
+ NULL, // RUBY_T_STRUCT = 0x09,
324
+ oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
325
+ NULL, // RUBY_T_FILE = 0x0b,
326
+ dump_data_strict, // RUBY_T_DATA = 0x0c,
327
+ NULL, // RUBY_T_MATCH = 0x0d,
328
+ NULL, // RUBY_T_COMPLEX = 0x0e,
329
+ NULL, // RUBY_T_RATIONAL = 0x0f,
330
+ NULL, // 0x10
331
+ oj_dump_nil, // RUBY_T_NIL = 0x11,
332
+ oj_dump_true, // RUBY_T_TRUE = 0x12,
333
+ oj_dump_false, // RUBY_T_FALSE = 0x13,
334
+ oj_dump_sym, // RUBY_T_SYMBOL = 0x14,
335
+ oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
357
336
  };
358
337
 
359
- void
360
- oj_dump_strict_val(VALUE obj, int depth, Out out) {
361
- int type = rb_type(obj);
362
-
363
- if (Yes == out->opts->trace) {
364
- oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceIn);
338
+ void oj_dump_strict_val(VALUE obj, int depth, Out out) {
339
+ int type = rb_type(obj);
340
+
341
+ if (RB_UNLIKELY(Yes == out->opts->trace)) {
342
+ oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceIn);
365
343
  }
366
344
  if (MAX_DEPTH < depth) {
367
- rb_raise(rb_eNoMemError, "Too deeply nested.\n");
345
+ rb_raise(rb_eNoMemError, "Too deeply nested.\n");
368
346
  }
369
347
  if (0 < type && type <= RUBY_T_FIXNUM) {
370
- DumpFunc f = strict_funcs[type];
371
-
372
- if (NULL != f) {
373
- f(obj, depth, out, false);
374
- if (Yes == out->opts->trace) {
375
- oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceOut);
376
- }
377
- return;
378
- }
348
+ DumpFunc f = strict_funcs[type];
349
+
350
+ if (NULL != f) {
351
+ f(obj, depth, out, false);
352
+ if (RB_UNLIKELY(Yes == out->opts->trace)) {
353
+ oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceOut);
354
+ }
355
+ return;
356
+ }
379
357
  }
380
358
  raise_strict(obj);
381
359
  }
382
360
 
383
- static DumpFunc null_funcs[] = {
384
- NULL, // RUBY_T_NONE = 0x00,
385
- dump_data_null, // RUBY_T_OBJECT = 0x01,
386
- NULL, // RUBY_T_CLASS = 0x02,
387
- NULL, // RUBY_T_MODULE = 0x03,
388
- dump_float, // RUBY_T_FLOAT = 0x04,
389
- oj_dump_str, // RUBY_T_STRING = 0x05,
390
- NULL, // RUBY_T_REGEXP = 0x06,
391
- dump_array, // RUBY_T_ARRAY = 0x07,
392
- dump_hash, // RUBY_T_HASH = 0x08,
393
- NULL, // RUBY_T_STRUCT = 0x09,
394
- oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
395
- NULL, // RUBY_T_FILE = 0x0b,
396
- dump_data_null, // RUBY_T_DATA = 0x0c,
397
- NULL, // RUBY_T_MATCH = 0x0d,
398
- NULL, // RUBY_T_COMPLEX = 0x0e,
399
- NULL, // RUBY_T_RATIONAL = 0x0f,
400
- NULL, // 0x10
401
- oj_dump_nil, // RUBY_T_NIL = 0x11,
402
- oj_dump_true, // RUBY_T_TRUE = 0x12,
403
- oj_dump_false, // RUBY_T_FALSE = 0x13,
404
- oj_dump_sym, // RUBY_T_SYMBOL = 0x14,
405
- oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
361
+ static DumpFunc null_funcs[] = {
362
+ NULL, // RUBY_T_NONE = 0x00,
363
+ dump_data_null, // RUBY_T_OBJECT = 0x01,
364
+ NULL, // RUBY_T_CLASS = 0x02,
365
+ NULL, // RUBY_T_MODULE = 0x03,
366
+ dump_float, // RUBY_T_FLOAT = 0x04,
367
+ oj_dump_str, // RUBY_T_STRING = 0x05,
368
+ NULL, // RUBY_T_REGEXP = 0x06,
369
+ dump_array, // RUBY_T_ARRAY = 0x07,
370
+ dump_hash, // RUBY_T_HASH = 0x08,
371
+ NULL, // RUBY_T_STRUCT = 0x09,
372
+ oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
373
+ NULL, // RUBY_T_FILE = 0x0b,
374
+ dump_data_null, // RUBY_T_DATA = 0x0c,
375
+ NULL, // RUBY_T_MATCH = 0x0d,
376
+ NULL, // RUBY_T_COMPLEX = 0x0e,
377
+ NULL, // RUBY_T_RATIONAL = 0x0f,
378
+ NULL, // 0x10
379
+ oj_dump_nil, // RUBY_T_NIL = 0x11,
380
+ oj_dump_true, // RUBY_T_TRUE = 0x12,
381
+ oj_dump_false, // RUBY_T_FALSE = 0x13,
382
+ oj_dump_sym, // RUBY_T_SYMBOL = 0x14,
383
+ oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
406
384
  };
407
385
 
408
- void
409
- oj_dump_null_val(VALUE obj, int depth, Out out) {
410
- int type = rb_type(obj);
411
-
412
- if (Yes == out->opts->trace) {
413
- oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceOut);
386
+ void oj_dump_null_val(VALUE obj, int depth, Out out) {
387
+ int type = rb_type(obj);
388
+
389
+ if (RB_UNLIKELY(Yes == out->opts->trace)) {
390
+ oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceOut);
414
391
  }
415
392
  if (MAX_DEPTH < depth) {
416
- rb_raise(rb_eNoMemError, "Too deeply nested.\n");
393
+ rb_raise(rb_eNoMemError, "Too deeply nested.\n");
417
394
  }
418
395
  if (0 < type && type <= RUBY_T_FIXNUM) {
419
- DumpFunc f = null_funcs[type];
420
-
421
- if (NULL != f) {
422
- f(obj, depth, out, false);
423
- if (Yes == out->opts->trace) {
424
- oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceOut);
425
- }
426
- return;
427
- }
396
+ DumpFunc f = null_funcs[type];
397
+
398
+ if (NULL != f) {
399
+ f(obj, depth, out, false);
400
+ if (RB_UNLIKELY(Yes == out->opts->trace)) {
401
+ oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceOut);
402
+ }
403
+ return;
404
+ }
428
405
  }
429
406
  oj_dump_nil(Qnil, depth, out, false);
430
- if (Yes == out->opts->trace) {
431
- oj_trace("dump", Qnil, __FILE__, __LINE__, depth, TraceOut);
407
+ if (RB_UNLIKELY(Yes == out->opts->trace)) {
408
+ oj_trace("dump", Qnil, __FILE__, __LINE__, depth, TraceOut);
432
409
  }
433
410
  }