oj 3.9.1 → 3.16.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (171) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1452 -0
  3. data/README.md +21 -6
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +50 -68
  6. data/ext/oj/cache.c +329 -0
  7. data/ext/oj/cache.h +22 -0
  8. data/ext/oj/cache8.c +60 -62
  9. data/ext/oj/cache8.h +9 -36
  10. data/ext/oj/circarray.c +38 -42
  11. data/ext/oj/circarray.h +12 -13
  12. data/ext/oj/code.c +158 -179
  13. data/ext/oj/code.h +20 -22
  14. data/ext/oj/compat.c +145 -205
  15. data/ext/oj/custom.c +740 -880
  16. data/ext/oj/debug.c +126 -0
  17. data/ext/oj/dump.c +1145 -844
  18. data/ext/oj/dump.h +71 -57
  19. data/ext/oj/dump_compat.c +575 -655
  20. data/ext/oj/dump_leaf.c +96 -186
  21. data/ext/oj/dump_object.c +533 -660
  22. data/ext/oj/dump_strict.c +306 -340
  23. data/ext/oj/encode.h +4 -33
  24. data/ext/oj/encoder.c +43 -0
  25. data/ext/oj/err.c +28 -28
  26. data/ext/oj/err.h +39 -42
  27. data/ext/oj/extconf.rb +28 -7
  28. data/ext/oj/fast.c +1052 -1113
  29. data/ext/oj/intern.c +313 -0
  30. data/ext/oj/intern.h +22 -0
  31. data/ext/oj/mem.c +318 -0
  32. data/ext/oj/mem.h +53 -0
  33. data/ext/oj/mimic_json.c +471 -430
  34. data/ext/oj/object.c +532 -580
  35. data/ext/oj/odd.c +156 -142
  36. data/ext/oj/odd.h +25 -26
  37. data/ext/oj/oj.c +1346 -961
  38. data/ext/oj/oj.h +307 -290
  39. data/ext/oj/parse.c +954 -858
  40. data/ext/oj/parse.h +74 -72
  41. data/ext/oj/parser.c +1600 -0
  42. data/ext/oj/parser.h +103 -0
  43. data/ext/oj/rails.c +819 -836
  44. data/ext/oj/rails.h +8 -11
  45. data/ext/oj/reader.c +136 -147
  46. data/ext/oj/reader.h +69 -83
  47. data/ext/oj/resolve.c +41 -63
  48. data/ext/oj/resolve.h +4 -6
  49. data/ext/oj/rxclass.c +69 -72
  50. data/ext/oj/rxclass.h +12 -13
  51. data/ext/oj/saj.c +440 -485
  52. data/ext/oj/saj2.c +584 -0
  53. data/ext/oj/saj2.h +23 -0
  54. data/ext/oj/scp.c +79 -118
  55. data/ext/oj/simd.h +10 -0
  56. data/ext/oj/sparse.c +739 -709
  57. data/ext/oj/stream_writer.c +141 -175
  58. data/ext/oj/strict.c +103 -128
  59. data/ext/oj/string_writer.c +244 -261
  60. data/ext/oj/trace.c +34 -41
  61. data/ext/oj/trace.h +42 -15
  62. data/ext/oj/usual.c +1218 -0
  63. data/ext/oj/usual.h +69 -0
  64. data/ext/oj/util.c +107 -107
  65. data/ext/oj/util.h +4 -3
  66. data/ext/oj/val_stack.c +61 -78
  67. data/ext/oj/val_stack.h +80 -114
  68. data/ext/oj/validate.c +46 -0
  69. data/ext/oj/wab.c +316 -361
  70. data/lib/oj/active_support_helper.rb +1 -3
  71. data/lib/oj/bag.rb +8 -1
  72. data/lib/oj/easy_hash.rb +9 -9
  73. data/lib/oj/error.rb +1 -2
  74. data/lib/oj/json.rb +162 -150
  75. data/lib/oj/mimic.rb +54 -20
  76. data/lib/oj/saj.rb +20 -6
  77. data/lib/oj/schandler.rb +5 -4
  78. data/lib/oj/state.rb +12 -8
  79. data/lib/oj/version.rb +1 -2
  80. data/lib/oj.rb +2 -8
  81. data/pages/Compatibility.md +1 -1
  82. data/pages/Encoding.md +1 -1
  83. data/pages/InstallOptions.md +20 -0
  84. data/pages/JsonGem.md +15 -0
  85. data/pages/Modes.md +9 -3
  86. data/pages/Options.md +62 -12
  87. data/pages/Parser.md +309 -0
  88. data/pages/Rails.md +73 -22
  89. metadata +68 -192
  90. data/ext/oj/hash.c +0 -163
  91. data/ext/oj/hash.h +0 -46
  92. data/ext/oj/hash_test.c +0 -512
  93. data/test/_test_active.rb +0 -76
  94. data/test/_test_active_mimic.rb +0 -96
  95. data/test/_test_mimic_rails.rb +0 -126
  96. data/test/activerecord/result_test.rb +0 -27
  97. data/test/activesupport4/decoding_test.rb +0 -108
  98. data/test/activesupport4/encoding_test.rb +0 -531
  99. data/test/activesupport4/test_helper.rb +0 -41
  100. data/test/activesupport5/decoding_test.rb +0 -125
  101. data/test/activesupport5/encoding_test.rb +0 -485
  102. data/test/activesupport5/encoding_test_cases.rb +0 -90
  103. data/test/activesupport5/test_helper.rb +0 -50
  104. data/test/activesupport5/time_zone_test_helpers.rb +0 -24
  105. data/test/bar.rb +0 -25
  106. data/test/files.rb +0 -29
  107. data/test/foo.rb +0 -21
  108. data/test/helper.rb +0 -26
  109. data/test/isolated/shared.rb +0 -308
  110. data/test/isolated/test_mimic_after.rb +0 -13
  111. data/test/isolated/test_mimic_alone.rb +0 -12
  112. data/test/isolated/test_mimic_as_json.rb +0 -45
  113. data/test/isolated/test_mimic_before.rb +0 -13
  114. data/test/isolated/test_mimic_define.rb +0 -28
  115. data/test/isolated/test_mimic_rails_after.rb +0 -22
  116. data/test/isolated/test_mimic_rails_before.rb +0 -21
  117. data/test/isolated/test_mimic_redefine.rb +0 -15
  118. data/test/json_gem/json_addition_test.rb +0 -216
  119. data/test/json_gem/json_common_interface_test.rb +0 -148
  120. data/test/json_gem/json_encoding_test.rb +0 -107
  121. data/test/json_gem/json_ext_parser_test.rb +0 -20
  122. data/test/json_gem/json_fixtures_test.rb +0 -35
  123. data/test/json_gem/json_generator_test.rb +0 -383
  124. data/test/json_gem/json_generic_object_test.rb +0 -90
  125. data/test/json_gem/json_parser_test.rb +0 -470
  126. data/test/json_gem/json_string_matching_test.rb +0 -42
  127. data/test/json_gem/test_helper.rb +0 -18
  128. data/test/perf.rb +0 -107
  129. data/test/perf_compat.rb +0 -130
  130. data/test/perf_fast.rb +0 -164
  131. data/test/perf_file.rb +0 -64
  132. data/test/perf_object.rb +0 -138
  133. data/test/perf_saj.rb +0 -109
  134. data/test/perf_scp.rb +0 -151
  135. data/test/perf_simple.rb +0 -287
  136. data/test/perf_strict.rb +0 -145
  137. data/test/perf_wab.rb +0 -131
  138. data/test/sample/change.rb +0 -14
  139. data/test/sample/dir.rb +0 -19
  140. data/test/sample/doc.rb +0 -36
  141. data/test/sample/file.rb +0 -48
  142. data/test/sample/group.rb +0 -16
  143. data/test/sample/hasprops.rb +0 -16
  144. data/test/sample/layer.rb +0 -12
  145. data/test/sample/line.rb +0 -20
  146. data/test/sample/oval.rb +0 -10
  147. data/test/sample/rect.rb +0 -10
  148. data/test/sample/shape.rb +0 -35
  149. data/test/sample/text.rb +0 -20
  150. data/test/sample.rb +0 -54
  151. data/test/sample_json.rb +0 -37
  152. data/test/test_compat.rb +0 -509
  153. data/test/test_custom.rb +0 -503
  154. data/test/test_debian.rb +0 -53
  155. data/test/test_fast.rb +0 -470
  156. data/test/test_file.rb +0 -239
  157. data/test/test_gc.rb +0 -49
  158. data/test/test_hash.rb +0 -29
  159. data/test/test_integer_range.rb +0 -73
  160. data/test/test_null.rb +0 -376
  161. data/test/test_object.rb +0 -1018
  162. data/test/test_saj.rb +0 -186
  163. data/test/test_scp.rb +0 -433
  164. data/test/test_strict.rb +0 -410
  165. data/test/test_various.rb +0 -741
  166. data/test/test_wab.rb +0 -307
  167. data/test/test_writer.rb +0 -380
  168. data/test/tests.rb +0 -24
  169. data/test/tests_mimic.rb +0 -14
  170. data/test/tests_mimic_addition.rb +0 -7
  171. data/test/zoo.rb +0 -13
data/ext/oj/dump_strict.c CHANGED
@@ -1,433 +1,399 @@
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) {
24
+ static void raise_strict(VALUE obj) {
28
25
  rb_raise(rb_eTypeError, "Failed to dump %s Object to JSON in strict mode.\n", rb_class2name(rb_obj_class(obj)));
29
26
  }
30
27
 
31
28
  // 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;
29
+ static void dump_float(VALUE obj, int depth, Out out, bool as_ok) {
30
+ char buf[64];
31
+ char* b;
32
+ double d = rb_num2dbl(obj);
33
+ size_t cnt = 0;
38
34
 
39
35
  if (0.0 == d) {
40
- b = buf;
41
- *b++ = '0';
42
- *b++ = '.';
43
- *b++ = '0';
44
- *b++ = '\0';
45
- cnt = 3;
36
+ b = buf;
37
+ *b++ = '0';
38
+ *b++ = '.';
39
+ *b++ = '0';
40
+ *b++ = '\0';
41
+ cnt = 3;
46
42
  } 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
- }
43
+ NanDump nd = out->opts->dump_opts.nan_dump;
44
+
45
+ if (AutoNan == nd) {
46
+ nd = RaiseNan;
47
+ }
48
+ if (OJ_INFINITY == d) {
49
+ switch (nd) {
50
+ case RaiseNan:
51
+ case WordNan: raise_strict(obj); break;
52
+ case NullNan:
53
+ strcpy(buf, "null");
54
+ cnt = 4;
55
+ break;
56
+ case HugeNan:
57
+ default:
58
+ strcpy(buf, inf_val);
59
+ cnt = sizeof(inf_val) - 1;
60
+ break;
61
+ }
62
+ } else if (-OJ_INFINITY == d) {
63
+ switch (nd) {
64
+ case RaiseNan:
65
+ case WordNan: raise_strict(obj); break;
66
+ case NullNan:
67
+ strcpy(buf, "null");
68
+ cnt = 4;
69
+ break;
70
+ case HugeNan:
71
+ default:
72
+ strcpy(buf, ninf_val);
73
+ cnt = sizeof(ninf_val) - 1;
74
+ break;
75
+ }
76
+ } else if (isnan(d)) {
77
+ switch (nd) {
78
+ case RaiseNan:
79
+ case WordNan: raise_strict(obj); break;
80
+ case NullNan:
81
+ strcpy(buf, "null");
82
+ cnt = 4;
83
+ break;
84
+ case HugeNan:
85
+ default:
86
+ strcpy(buf, nan_val);
87
+ cnt = sizeof(nan_val) - 1;
88
+ break;
89
+ }
90
+ } else if (d == (double)(long long int)d) {
91
+ cnt = snprintf(buf, sizeof(buf), "%.1f", d);
92
+ } else if (0 == out->opts->float_prec) {
93
+ volatile VALUE rstr = oj_safe_string_convert(obj);
94
+
95
+ cnt = RSTRING_LEN(rstr);
96
+ if ((int)sizeof(buf) <= cnt) {
97
+ cnt = sizeof(buf) - 1;
98
+ }
99
+ memcpy(buf, RSTRING_PTR(rstr), cnt);
100
+ buf[cnt] = '\0';
101
+ } else {
102
+ cnt = oj_dump_float_printf(buf, sizeof(buf), obj, d, out->opts->float_fmt);
103
+ }
114
104
  }
115
105
  assure_size(out, cnt);
116
- for (b = buf; '\0' != *b; b++) {
117
- *out->cur++ = *b;
118
- }
106
+ APPEND_CHARS(out->cur, buf, cnt);
119
107
  *out->cur = '\0';
120
108
  }
121
109
 
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;
110
+ static void dump_array(VALUE a, int depth, Out out, bool as_ok) {
111
+ size_t size;
112
+ size_t i;
113
+ size_t cnt;
114
+ int d2 = depth + 1;
127
115
 
128
116
  if (Yes == out->opts->circular) {
129
- if (0 > oj_check_circular(a, out)) {
130
- oj_dump_nil(Qnil, 0, out, false);
131
- return;
132
- }
117
+ if (0 > oj_check_circular(a, out)) {
118
+ oj_dump_nil(Qnil, 0, out, false);
119
+ return;
120
+ }
133
121
  }
134
- cnt = (int)RARRAY_LEN(a);
122
+ cnt = RARRAY_LEN(a);
135
123
  *out->cur++ = '[';
136
- size = 2;
124
+ size = 2;
137
125
  assure_size(out, size);
138
126
  if (0 == cnt) {
139
- *out->cur++ = ']';
127
+ *out->cur++ = ']';
140
128
  } 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++ = ']';
129
+ if (out->opts->dump_opts.use) {
130
+ size = d2 * out->opts->dump_opts.indent_size + out->opts->dump_opts.array_size + 1;
131
+ } else {
132
+ size = d2 * out->indent + 2;
133
+ }
134
+ assure_size(out, size * cnt);
135
+ cnt--;
136
+ for (i = 0; i <= cnt; i++) {
137
+ if (out->opts->dump_opts.use) {
138
+ if (0 < out->opts->dump_opts.array_size) {
139
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
140
+ }
141
+ if (0 < out->opts->dump_opts.indent_size) {
142
+ int i;
143
+ for (i = d2; 0 < i; i--) {
144
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
145
+ }
146
+ }
147
+ } else {
148
+ fill_indent(out, d2);
149
+ }
150
+ if (NullMode == out->opts->mode) {
151
+ oj_dump_null_val(RARRAY_AREF(a, i), d2, out);
152
+ } else {
153
+ oj_dump_strict_val(RARRAY_AREF(a, i), d2, out);
154
+ }
155
+ if (i < cnt) {
156
+ *out->cur++ = ',';
157
+ }
158
+ }
159
+ size = depth * out->indent + 1;
160
+ assure_size(out, size);
161
+ if (out->opts->dump_opts.use) {
162
+ // printf("*** d2: %u indent: %u '%s'\n", d2, out->opts->dump_opts->indent_size,
163
+ // out->opts->dump_opts->indent);
164
+ if (0 < out->opts->dump_opts.array_size) {
165
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
166
+ }
167
+ if (0 < out->opts->dump_opts.indent_size) {
168
+ int i;
169
+
170
+ for (i = depth; 0 < i; i--) {
171
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
172
+ }
173
+ }
174
+ } else {
175
+ fill_indent(out, depth);
176
+ }
177
+ *out->cur++ = ']';
193
178
  }
194
179
  *out->cur = '\0';
195
180
  }
196
181
 
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
-
182
+ static int hash_cb(VALUE key, VALUE value, VALUE ov) {
183
+ Out out = (Out)ov;
184
+ int depth = out->depth;
185
+ long size;
186
+ int rtype = rb_type(key);
187
+
203
188
  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)));
189
+ rb_raise(rb_eTypeError,
190
+ "In :strict and :null mode all Hash keys must be Strings or Symbols, not %s.\n",
191
+ rb_class2name(rb_obj_class(key)));
205
192
  }
206
193
  if (out->omit_nil && Qnil == value) {
207
- return ST_CONTINUE;
194
+ return ST_CONTINUE;
208
195
  }
209
196
  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++ = ':';
197
+ size = depth * out->indent + 1;
198
+ assure_size(out, size);
199
+ fill_indent(out, depth);
200
+ if (rtype == T_STRING) {
201
+ oj_dump_str(key, 0, out, false);
202
+ } else {
203
+ oj_dump_sym(key, 0, out, false);
204
+ }
205
+ *out->cur++ = ':';
219
206
  } 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
- }
207
+ size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
208
+ assure_size(out, size);
209
+ if (0 < out->opts->dump_opts.hash_size) {
210
+ APPEND_CHARS(out->cur, out->opts->dump_opts.hash_nl, out->opts->dump_opts.hash_size);
211
+ }
212
+ if (0 < out->opts->dump_opts.indent_size) {
213
+ int i;
214
+ for (i = depth; 0 < i; i--) {
215
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
216
+ }
217
+ }
218
+ if (rtype == T_STRING) {
219
+ oj_dump_str(key, 0, out, false);
220
+ } else {
221
+ oj_dump_sym(key, 0, out, false);
222
+ }
223
+ size = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
224
+ assure_size(out, size);
225
+ if (0 < out->opts->dump_opts.before_size) {
226
+ APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
227
+ }
228
+ *out->cur++ = ':';
229
+ if (0 < out->opts->dump_opts.after_size) {
230
+ APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
231
+ }
249
232
  }
250
233
  if (NullMode == out->opts->mode) {
251
- oj_dump_null_val(value, depth, out);
234
+ oj_dump_null_val(value, depth, out);
252
235
  } else {
253
- oj_dump_strict_val(value, depth, out);
236
+ oj_dump_strict_val(value, depth, out);
254
237
  }
255
- out->depth = depth;
238
+ out->depth = depth;
256
239
  *out->cur++ = ',';
257
240
 
258
241
  return ST_CONTINUE;
259
242
  }
260
243
 
261
- static void
262
- dump_hash(VALUE obj, int depth, Out out, bool as_ok) {
263
- int cnt;
264
- size_t size;
244
+ static void dump_hash(VALUE obj, int depth, Out out, bool as_ok) {
245
+ int cnt;
246
+ size_t size;
265
247
 
266
248
  if (Yes == out->opts->circular) {
267
- if (0 > oj_check_circular(obj, out)) {
268
- oj_dump_nil(Qnil, 0, out, false);
269
- return;
270
- }
249
+ if (0 > oj_check_circular(obj, out)) {
250
+ oj_dump_nil(Qnil, 0, out, false);
251
+ return;
252
+ }
271
253
  }
272
- cnt = (int)RHASH_SIZE(obj);
254
+ cnt = (int)RHASH_SIZE(obj);
273
255
  size = depth * out->indent + 2;
274
256
  assure_size(out, 2);
275
257
  *out->cur++ = '{';
276
258
  if (0 == cnt) {
277
- *out->cur++ = '}';
259
+ *out->cur++ = '}';
278
260
  } 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++ = '}';
261
+ out->depth = depth + 1;
262
+ rb_hash_foreach(obj, hash_cb, (VALUE)out);
263
+ if (',' == *(out->cur - 1)) {
264
+ out->cur--; // backup to overwrite last comma
265
+ }
266
+ if (!out->opts->dump_opts.use) {
267
+ assure_size(out, size);
268
+ fill_indent(out, depth);
269
+ } else {
270
+ size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
271
+ assure_size(out, size);
272
+ if (0 < out->opts->dump_opts.hash_size) {
273
+ APPEND_CHARS(out->cur, out->opts->dump_opts.hash_nl, out->opts->dump_opts.hash_size);
274
+ }
275
+ if (0 < out->opts->dump_opts.indent_size) {
276
+ int i;
277
+
278
+ for (i = depth; 0 < i; i--) {
279
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
280
+ }
281
+ }
282
+ }
283
+ *out->cur++ = '}';
304
284
  }
305
285
  *out->cur = '\0';
306
286
  }
307
287
 
308
- static void
309
- dump_data_strict(VALUE obj, int depth, Out out, bool as_ok) {
310
- VALUE clas = rb_obj_class(obj);
288
+ static void dump_data_strict(VALUE obj, int depth, Out out, bool as_ok) {
289
+ VALUE clas = rb_obj_class(obj);
311
290
 
312
291
  if (oj_bigdecimal_class == clas) {
313
- volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
292
+ volatile VALUE rstr = oj_safe_string_convert(obj);
314
293
 
315
- oj_dump_raw(rb_string_value_ptr((VALUE*)&rstr), (int)RSTRING_LEN(rstr), out);
294
+ oj_dump_raw(RSTRING_PTR(rstr), RSTRING_LEN(rstr), out);
316
295
  } else {
317
- raise_strict(obj);
296
+ raise_strict(obj);
318
297
  }
319
298
  }
320
299
 
321
- static void
322
- dump_data_null(VALUE obj, int depth, Out out, bool as_ok) {
323
- VALUE clas = rb_obj_class(obj);
300
+ static void dump_data_null(VALUE obj, int depth, Out out, bool as_ok) {
301
+ VALUE clas = rb_obj_class(obj);
324
302
 
325
303
  if (oj_bigdecimal_class == clas) {
326
- volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
304
+ volatile VALUE rstr = oj_safe_string_convert(obj);
327
305
 
328
- oj_dump_raw(rb_string_value_ptr((VALUE*)&rstr), (int)RSTRING_LEN(rstr), out);
306
+ oj_dump_raw(RSTRING_PTR(rstr), RSTRING_LEN(rstr), out);
329
307
  } else {
330
- oj_dump_nil(Qnil, depth, out, false);
308
+ oj_dump_nil(Qnil, depth, out, false);
331
309
  }
332
310
  }
333
311
 
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,
312
+ static DumpFunc strict_funcs[] = {
313
+ NULL, // RUBY_T_NONE = 0x00,
314
+ dump_data_strict, // RUBY_T_OBJECT = 0x01,
315
+ NULL, // RUBY_T_CLASS = 0x02,
316
+ NULL, // RUBY_T_MODULE = 0x03,
317
+ dump_float, // RUBY_T_FLOAT = 0x04,
318
+ oj_dump_str, // RUBY_T_STRING = 0x05,
319
+ NULL, // RUBY_T_REGEXP = 0x06,
320
+ dump_array, // RUBY_T_ARRAY = 0x07,
321
+ dump_hash, // RUBY_T_HASH = 0x08,
322
+ NULL, // RUBY_T_STRUCT = 0x09,
323
+ oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
324
+ NULL, // RUBY_T_FILE = 0x0b,
325
+ dump_data_strict, // RUBY_T_DATA = 0x0c,
326
+ NULL, // RUBY_T_MATCH = 0x0d,
327
+ NULL, // RUBY_T_COMPLEX = 0x0e,
328
+ NULL, // RUBY_T_RATIONAL = 0x0f,
329
+ NULL, // 0x10
330
+ oj_dump_nil, // RUBY_T_NIL = 0x11,
331
+ oj_dump_true, // RUBY_T_TRUE = 0x12,
332
+ oj_dump_false, // RUBY_T_FALSE = 0x13,
333
+ oj_dump_sym, // RUBY_T_SYMBOL = 0x14,
334
+ oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
357
335
  };
358
336
 
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);
365
- }
337
+ void oj_dump_strict_val(VALUE obj, int depth, Out out) {
338
+ int type = rb_type(obj);
339
+
340
+ TRACE(out->opts->trace, "dump", obj, depth, TraceIn);
366
341
  if (MAX_DEPTH < depth) {
367
- rb_raise(rb_eNoMemError, "Too deeply nested.\n");
342
+ rb_raise(rb_eNoMemError, "Too deeply nested.\n");
368
343
  }
369
344
  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
- }
345
+ DumpFunc f = strict_funcs[type];
346
+
347
+ if (NULL != f) {
348
+ f(obj, depth, out, false);
349
+ TRACE(out->opts->trace, "dump", obj, depth, TraceOut);
350
+ return;
351
+ }
379
352
  }
380
353
  raise_strict(obj);
381
354
  }
382
355
 
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,
356
+ static DumpFunc null_funcs[] = {
357
+ NULL, // RUBY_T_NONE = 0x00,
358
+ dump_data_null, // RUBY_T_OBJECT = 0x01,
359
+ NULL, // RUBY_T_CLASS = 0x02,
360
+ NULL, // RUBY_T_MODULE = 0x03,
361
+ dump_float, // RUBY_T_FLOAT = 0x04,
362
+ oj_dump_str, // RUBY_T_STRING = 0x05,
363
+ NULL, // RUBY_T_REGEXP = 0x06,
364
+ dump_array, // RUBY_T_ARRAY = 0x07,
365
+ dump_hash, // RUBY_T_HASH = 0x08,
366
+ NULL, // RUBY_T_STRUCT = 0x09,
367
+ oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
368
+ NULL, // RUBY_T_FILE = 0x0b,
369
+ dump_data_null, // RUBY_T_DATA = 0x0c,
370
+ NULL, // RUBY_T_MATCH = 0x0d,
371
+ NULL, // RUBY_T_COMPLEX = 0x0e,
372
+ NULL, // RUBY_T_RATIONAL = 0x0f,
373
+ NULL, // 0x10
374
+ oj_dump_nil, // RUBY_T_NIL = 0x11,
375
+ oj_dump_true, // RUBY_T_TRUE = 0x12,
376
+ oj_dump_false, // RUBY_T_FALSE = 0x13,
377
+ oj_dump_sym, // RUBY_T_SYMBOL = 0x14,
378
+ oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
406
379
  };
407
380
 
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);
414
- }
381
+ void oj_dump_null_val(VALUE obj, int depth, Out out) {
382
+ int type = rb_type(obj);
383
+
384
+ TRACE(out->opts->trace, "dump", obj, depth, TraceOut);
415
385
  if (MAX_DEPTH < depth) {
416
- rb_raise(rb_eNoMemError, "Too deeply nested.\n");
386
+ rb_raise(rb_eNoMemError, "Too deeply nested.\n");
417
387
  }
418
388
  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
- }
389
+ DumpFunc f = null_funcs[type];
390
+
391
+ if (NULL != f) {
392
+ f(obj, depth, out, false);
393
+ TRACE(out->opts->trace, "dump", obj, depth, TraceOut);
394
+ return;
395
+ }
428
396
  }
429
397
  oj_dump_nil(Qnil, depth, out, false);
430
- if (Yes == out->opts->trace) {
431
- oj_trace("dump", Qnil, __FILE__, __LINE__, depth, TraceOut);
432
- }
398
+ TRACE(out->opts->trace, "dump", Qnil, depth, TraceOut);
433
399
  }