oj 3.10.6 → 3.12.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +6 -1
  3. data/ext/oj/buf.h +36 -68
  4. data/ext/oj/cache8.c +59 -62
  5. data/ext/oj/cache8.h +9 -36
  6. data/ext/oj/circarray.c +36 -42
  7. data/ext/oj/circarray.h +12 -13
  8. data/ext/oj/code.c +172 -179
  9. data/ext/oj/code.h +22 -24
  10. data/ext/oj/compat.c +176 -181
  11. data/ext/oj/custom.c +800 -864
  12. data/ext/oj/dump.c +774 -776
  13. data/ext/oj/dump.h +50 -55
  14. data/ext/oj/dump_compat.c +2 -4
  15. data/ext/oj/dump_leaf.c +118 -162
  16. data/ext/oj/dump_object.c +610 -632
  17. data/ext/oj/dump_strict.c +319 -331
  18. data/ext/oj/encode.h +4 -33
  19. data/ext/oj/err.c +40 -29
  20. data/ext/oj/err.h +25 -44
  21. data/ext/oj/extconf.rb +2 -1
  22. data/ext/oj/fast.c +1054 -1081
  23. data/ext/oj/hash.c +102 -97
  24. data/ext/oj/hash.h +10 -35
  25. data/ext/oj/hash_test.c +451 -472
  26. data/ext/oj/mimic_json.c +415 -402
  27. data/ext/oj/object.c +588 -532
  28. data/ext/oj/odd.c +124 -132
  29. data/ext/oj/odd.h +28 -29
  30. data/ext/oj/oj.c +1186 -906
  31. data/ext/oj/oj.h +289 -298
  32. data/ext/oj/parse.c +946 -870
  33. data/ext/oj/parse.h +81 -79
  34. data/ext/oj/rails.c +837 -842
  35. data/ext/oj/rails.h +8 -11
  36. data/ext/oj/reader.c +139 -147
  37. data/ext/oj/reader.h +68 -84
  38. data/ext/oj/resolve.c +44 -47
  39. data/ext/oj/resolve.h +4 -6
  40. data/ext/oj/rxclass.c +69 -73
  41. data/ext/oj/rxclass.h +13 -14
  42. data/ext/oj/saj.c +453 -484
  43. data/ext/oj/scp.c +88 -113
  44. data/ext/oj/sparse.c +783 -714
  45. data/ext/oj/stream_writer.c +123 -157
  46. data/ext/oj/strict.c +133 -106
  47. data/ext/oj/string_writer.c +199 -247
  48. data/ext/oj/trace.c +34 -41
  49. data/ext/oj/trace.h +15 -15
  50. data/ext/oj/util.c +104 -104
  51. data/ext/oj/util.h +4 -3
  52. data/ext/oj/val_stack.c +48 -76
  53. data/ext/oj/val_stack.h +80 -115
  54. data/ext/oj/wab.c +321 -325
  55. data/lib/oj.rb +0 -8
  56. data/lib/oj/bag.rb +1 -0
  57. data/lib/oj/easy_hash.rb +5 -4
  58. data/lib/oj/mimic.rb +47 -13
  59. data/lib/oj/version.rb +1 -1
  60. data/pages/Modes.md +1 -0
  61. data/pages/Options.md +23 -11
  62. data/test/activerecord/result_test.rb +7 -2
  63. data/test/foo.rb +8 -40
  64. data/test/helper.rb +10 -0
  65. data/test/json_gem/json_common_interface_test.rb +8 -3
  66. data/test/json_gem/json_generator_test.rb +15 -3
  67. data/test/json_gem/test_helper.rb +8 -0
  68. data/test/perf.rb +1 -1
  69. data/test/perf_scp.rb +11 -10
  70. data/test/perf_strict.rb +17 -23
  71. data/test/prec.rb +23 -0
  72. data/test/sample_json.rb +1 -1
  73. data/test/test_compat.rb +16 -3
  74. data/test/test_custom.rb +11 -0
  75. data/test/test_fast.rb +32 -2
  76. data/test/test_generate.rb +21 -0
  77. data/test/test_hash.rb +10 -0
  78. data/test/test_rails.rb +9 -0
  79. data/test/test_scp.rb +1 -1
  80. data/test/test_various.rb +4 -2
  81. metadata +89 -85
data/ext/oj/dump.h CHANGED
@@ -1,7 +1,5 @@
1
- /* dump.h
2
- * Copyright (c) 2011, Peter Ohler
3
- * All rights reserved.
4
- */
1
+ // Copyright (c) 2011 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
5
3
 
6
4
  #ifndef OJ_DUMP_H
7
5
  #define OJ_DUMP_H
@@ -15,80 +13,77 @@
15
13
  // Extra padding at end of buffer.
16
14
  #define BUFFER_EXTRA 64
17
15
 
18
- extern void oj_dump_nil(VALUE obj, int depth, Out out, bool as_ok);
19
- extern void oj_dump_true(VALUE obj, int depth, Out out, bool as_ok);
20
- extern void oj_dump_false(VALUE obj, int depth, Out out, bool as_ok);
21
- extern void oj_dump_fixnum(VALUE obj, int depth, Out out, bool as_ok);
22
- extern void oj_dump_bignum(VALUE obj, int depth, Out out, bool as_ok);
23
- extern void oj_dump_float(VALUE obj, int depth, Out out, bool as_ok);
24
- extern void oj_dump_str(VALUE obj, int depth, Out out, bool as_ok);
25
- extern void oj_dump_sym(VALUE obj, int depth, Out out, bool as_ok);
26
- extern void oj_dump_class(VALUE obj, int depth, Out out, bool as_ok);
16
+ extern void oj_dump_nil(VALUE obj, int depth, Out out, bool as_ok);
17
+ extern void oj_dump_true(VALUE obj, int depth, Out out, bool as_ok);
18
+ extern void oj_dump_false(VALUE obj, int depth, Out out, bool as_ok);
19
+ extern void oj_dump_fixnum(VALUE obj, int depth, Out out, bool as_ok);
20
+ extern void oj_dump_bignum(VALUE obj, int depth, Out out, bool as_ok);
21
+ extern void oj_dump_float(VALUE obj, int depth, Out out, bool as_ok);
22
+ extern void oj_dump_str(VALUE obj, int depth, Out out, bool as_ok);
23
+ extern void oj_dump_sym(VALUE obj, int depth, Out out, bool as_ok);
24
+ extern void oj_dump_class(VALUE obj, int depth, Out out, bool as_ok);
27
25
 
28
- extern void oj_dump_raw(const char *str, size_t cnt, Out out);
29
- extern void oj_dump_cstr(const char *str, size_t cnt, bool is_sym, bool escape1, Out out);
30
- extern void oj_dump_ruby_time(VALUE obj, Out out);
31
- extern void oj_dump_xml_time(VALUE obj, Out out);
32
- extern void oj_dump_time(VALUE obj, Out out, int withZone);
33
- extern void oj_dump_obj_to_s(VALUE obj, Out out);
26
+ extern void oj_dump_raw(const char *str, size_t cnt, Out out);
27
+ extern void oj_dump_cstr(const char *str, size_t cnt, bool is_sym, bool escape1, Out out);
28
+ extern void oj_dump_ruby_time(VALUE obj, Out out);
29
+ extern void oj_dump_xml_time(VALUE obj, Out out);
30
+ extern void oj_dump_time(VALUE obj, Out out, int withZone);
31
+ extern void oj_dump_obj_to_s(VALUE obj, Out out);
34
32
 
35
- extern const char* oj_nan_str(VALUE obj, int opt, int mode, bool plus, int *lenp);
33
+ extern const char *oj_nan_str(VALUE obj, int opt, int mode, bool plus, int *lenp);
36
34
 
37
- extern void oj_grow_out(Out out, size_t len);
38
- extern long oj_check_circular(VALUE obj, Out out);
35
+ extern void oj_grow_out(Out out, size_t len);
36
+ extern long oj_check_circular(VALUE obj, Out out);
39
37
 
40
- extern void oj_dump_strict_val(VALUE obj, int depth, Out out);
41
- extern void oj_dump_null_val(VALUE obj, int depth, Out out);
42
- extern void oj_dump_obj_val(VALUE obj, int depth, Out out);
43
- extern void oj_dump_compat_val(VALUE obj, int depth, Out out, bool as_ok);
44
- extern void oj_dump_rails_val(VALUE obj, int depth, Out out);
45
- extern void oj_dump_custom_val(VALUE obj, int depth, Out out, bool as_ok);
46
- extern void oj_dump_wab_val(VALUE obj, int depth, Out out);
38
+ extern void oj_dump_strict_val(VALUE obj, int depth, Out out);
39
+ extern void oj_dump_null_val(VALUE obj, int depth, Out out);
40
+ extern void oj_dump_obj_val(VALUE obj, int depth, Out out);
41
+ extern void oj_dump_compat_val(VALUE obj, int depth, Out out, bool as_ok);
42
+ extern void oj_dump_rails_val(VALUE obj, int depth, Out out);
43
+ extern void oj_dump_custom_val(VALUE obj, int depth, Out out, bool as_ok);
44
+ extern void oj_dump_wab_val(VALUE obj, int depth, Out out);
47
45
 
48
- extern void oj_dump_raw_json(VALUE obj, int depth, Out out);
46
+ extern void oj_dump_raw_json(VALUE obj, int depth, Out out);
49
47
 
50
- extern VALUE oj_add_to_json(int argc, VALUE *argv, VALUE self);
51
- extern VALUE oj_remove_to_json(int argc, VALUE *argv, VALUE self);
48
+ extern VALUE oj_add_to_json(int argc, VALUE *argv, VALUE self);
49
+ extern VALUE oj_remove_to_json(int argc, VALUE *argv, VALUE self);
52
50
 
53
- extern int oj_dump_float_printf(char *buf, size_t blen, VALUE obj, double d, const char *format);
51
+ extern int oj_dump_float_printf(char *buf, size_t blen, VALUE obj, double d, const char *format);
54
52
 
55
- extern bool oj_dump_ignore(Options opts, VALUE obj);
56
- extern time_t oj_sec_from_time_hard_way(VALUE obj);
53
+ extern bool oj_dump_ignore(Options opts, VALUE obj);
54
+ extern time_t oj_sec_from_time_hard_way(VALUE obj);
57
55
 
58
- inline static void
59
- assure_size(Out out, size_t len) {
56
+ inline static void assure_size(Out out, size_t len) {
60
57
  if (out->end - out->cur <= (long)len) {
61
- oj_grow_out(out, len);
58
+ oj_grow_out(out, len);
62
59
  }
63
60
  }
64
61
 
65
- inline static void
66
- fill_indent(Out out, int cnt) {
62
+ inline static void fill_indent(Out out, int cnt) {
67
63
  if (0 < out->indent) {
68
- cnt *= out->indent;
69
- *out->cur++ = '\n';
70
- for (; 0 < cnt; cnt--) {
71
- *out->cur++ = ' ';
72
- }
64
+ cnt *= out->indent;
65
+ *out->cur++ = '\n';
66
+ for (; 0 < cnt; cnt--) {
67
+ *out->cur++ = ' ';
68
+ }
73
69
  }
74
70
  }
75
71
 
76
- inline static void
77
- dump_ulong(unsigned long num, Out out) {
78
- char buf[32];
79
- char *b = buf + sizeof(buf) - 1;
72
+ inline static void dump_ulong(unsigned long num, Out out) {
73
+ char buf[32];
74
+ char *b = buf + sizeof(buf) - 1;
80
75
 
81
76
  *b-- = '\0';
82
77
  if (0 < num) {
83
- for (; 0 < num; num /= 10, b--) {
84
- *b = (num % 10) + '0';
85
- }
86
- b++;
78
+ for (; 0 < num; num /= 10, b--) {
79
+ *b = (num % 10) + '0';
80
+ }
81
+ b++;
87
82
  } else {
88
- *b = '0';
83
+ *b = '0';
89
84
  }
90
85
  for (; '\0' != *b; b++) {
91
- *out->cur++ = *b;
86
+ *out->cur++ = *b;
92
87
  }
93
88
  *out->cur = '\0';
94
89
  }
data/ext/oj/dump_compat.c CHANGED
@@ -1,7 +1,5 @@
1
- /* dump_object.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
4
  #include "code.h"
7
5
  #include "dump.h"
data/ext/oj/dump_leaf.c CHANGED
@@ -1,252 +1,208 @@
1
- /* dump_leaf.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
4
  #include <errno.h>
7
5
 
8
- #include "oj.h"
9
6
  #include "dump.h"
7
+ #include "oj.h"
8
+
9
+ static void dump_leaf(Leaf leaf, int depth, Out out);
10
10
 
11
- static void dump_leaf(Leaf leaf, int depth, Out out);
11
+ static void grow(Out out, size_t len) {
12
+ size_t size = out->end - out->buf;
13
+ long pos = out->cur - out->buf;
14
+ char * buf;
12
15
 
13
- static void
14
- grow(Out out, size_t len) {
15
- size_t size = out->end - out->buf;
16
- long pos = out->cur - out->buf;
17
- char *buf;
18
-
19
16
  size *= 2;
20
17
  if (size <= len * 2 + pos) {
21
- size += len;
18
+ size += len;
22
19
  }
23
20
  if (out->allocated) {
24
- buf = REALLOC_N(out->buf, char, (size + BUFFER_EXTRA));
21
+ buf = REALLOC_N(out->buf, char, (size + BUFFER_EXTRA));
25
22
  } else {
26
- buf = ALLOC_N(char, (size + BUFFER_EXTRA));
27
- out->allocated = true;
28
- memcpy(buf, out->buf, out->end - out->buf + BUFFER_EXTRA);
23
+ buf = ALLOC_N(char, (size + BUFFER_EXTRA));
24
+ out->allocated = true;
25
+ memcpy(buf, out->buf, out->end - out->buf + BUFFER_EXTRA);
29
26
  }
30
27
  if (0 == buf) {
31
- rb_raise(rb_eNoMemError, "Failed to create string. [%d:%s]\n", ENOSPC, strerror(ENOSPC));
28
+ rb_raise(rb_eNoMemError, "Failed to create string. [%d:%s]\n", ENOSPC, strerror(ENOSPC));
32
29
  }
33
30
  out->buf = buf;
34
31
  out->end = buf + size;
35
32
  out->cur = out->buf + pos;
36
33
  }
37
34
 
38
-
39
- inline static void
40
- dump_chars(const char *s, size_t size, Out out) {
35
+ inline static void dump_chars(const char *s, size_t size, Out out) {
41
36
  if (out->end - out->cur <= (long)size) {
42
- grow(out, size);
37
+ grow(out, size);
43
38
  }
44
39
  memcpy(out->cur, s, size);
45
40
  out->cur += size;
46
41
  *out->cur = '\0';
47
42
  }
48
43
 
49
- static void
50
- dump_leaf_str(Leaf leaf, Out out) {
44
+ static void dump_leaf_str(Leaf leaf, Out out) {
51
45
  switch (leaf->value_type) {
52
- case STR_VAL:
53
- oj_dump_cstr(leaf->str, strlen(leaf->str), 0, 0, out);
54
- break;
46
+ case STR_VAL: oj_dump_cstr(leaf->str, strlen(leaf->str), 0, 0, out); break;
55
47
  case RUBY_VAL:
56
- oj_dump_cstr(rb_string_value_cstr(&leaf->value), (int)RSTRING_LEN(leaf->value), 0, 0, out);
57
- break;
48
+ oj_dump_cstr(rb_string_value_cstr(&leaf->value), (int)RSTRING_LEN(leaf->value), 0, 0, out);
49
+ break;
58
50
  case COL_VAL:
59
- default:
60
- rb_raise(rb_eTypeError, "Unexpected value type %02x.\n", leaf->value_type);
61
- break;
51
+ default: rb_raise(rb_eTypeError, "Unexpected value type %02x.\n", leaf->value_type); break;
62
52
  }
63
53
  }
64
54
 
65
- static void
66
- dump_leaf_fixnum(Leaf leaf, Out out) {
55
+ static void dump_leaf_fixnum(Leaf leaf, Out out) {
67
56
  switch (leaf->value_type) {
68
- case STR_VAL:
69
- dump_chars(leaf->str, strlen(leaf->str), out);
70
- break;
57
+ case STR_VAL: dump_chars(leaf->str, strlen(leaf->str), out); break;
71
58
  case RUBY_VAL:
72
- if (T_BIGNUM == rb_type(leaf->value)) {
73
- oj_dump_bignum(leaf->value, 0, out, false);
74
- } else {
75
- oj_dump_fixnum(leaf->value, 0, out, false);
76
- }
77
- break;
59
+ if (T_BIGNUM == rb_type(leaf->value)) {
60
+ oj_dump_bignum(leaf->value, 0, out, false);
61
+ } else {
62
+ oj_dump_fixnum(leaf->value, 0, out, false);
63
+ }
64
+ break;
78
65
  case COL_VAL:
79
- default:
80
- rb_raise(rb_eTypeError, "Unexpected value type %02x.\n", leaf->value_type);
81
- break;
66
+ default: rb_raise(rb_eTypeError, "Unexpected value type %02x.\n", leaf->value_type); break;
82
67
  }
83
68
  }
84
69
 
85
- static void
86
- dump_leaf_float(Leaf leaf, Out out) {
70
+ static void dump_leaf_float(Leaf leaf, Out out) {
87
71
  switch (leaf->value_type) {
88
- case STR_VAL:
89
- dump_chars(leaf->str, strlen(leaf->str), out);
90
- break;
91
- case RUBY_VAL:
92
- oj_dump_float(leaf->value, 0, out, false);
93
- break;
72
+ case STR_VAL: dump_chars(leaf->str, strlen(leaf->str), out); break;
73
+ case RUBY_VAL: oj_dump_float(leaf->value, 0, out, false); break;
94
74
  case COL_VAL:
95
- default:
96
- rb_raise(rb_eTypeError, "Unexpected value type %02x.\n", leaf->value_type);
97
- break;
75
+ default: rb_raise(rb_eTypeError, "Unexpected value type %02x.\n", leaf->value_type); break;
98
76
  }
99
77
  }
100
78
 
101
- static void
102
- dump_leaf_array(Leaf leaf, int depth, Out out) {
103
- size_t size;
104
- int d2 = depth + 1;
79
+ static void dump_leaf_array(Leaf leaf, int depth, Out out) {
80
+ size_t size;
81
+ int d2 = depth + 1;
105
82
 
106
83
  size = 2;
107
84
  if (out->end - out->cur <= (long)size) {
108
- grow(out, size);
85
+ grow(out, size);
109
86
  }
110
87
  *out->cur++ = '[';
111
88
  if (0 == leaf->elements) {
112
- *out->cur++ = ']';
89
+ *out->cur++ = ']';
113
90
  } else {
114
- Leaf first = leaf->elements->next;
115
- Leaf e = first;
116
-
117
- size = d2 * out->indent + 2;
118
- do {
119
- if (out->end - out->cur <= (long)size) {
120
- grow(out, size);
121
- }
122
- fill_indent(out, d2);
123
- dump_leaf(e, d2, out);
124
- if (e->next != first) {
125
- *out->cur++ = ',';
126
- }
127
- e = e->next;
128
- } while (e != first);
129
- size = depth * out->indent + 1;
130
- if (out->end - out->cur <= (long)size) {
131
- grow(out, size);
132
- }
133
- fill_indent(out, depth);
134
- *out->cur++ = ']';
91
+ Leaf first = leaf->elements->next;
92
+ Leaf e = first;
93
+
94
+ size = d2 * out->indent + 2;
95
+ do {
96
+ if (out->end - out->cur <= (long)size) {
97
+ grow(out, size);
98
+ }
99
+ fill_indent(out, d2);
100
+ dump_leaf(e, d2, out);
101
+ if (e->next != first) {
102
+ *out->cur++ = ',';
103
+ }
104
+ e = e->next;
105
+ } while (e != first);
106
+ size = depth * out->indent + 1;
107
+ if (out->end - out->cur <= (long)size) {
108
+ grow(out, size);
109
+ }
110
+ fill_indent(out, depth);
111
+ *out->cur++ = ']';
135
112
  }
136
113
  *out->cur = '\0';
137
114
  }
138
115
 
139
- static void
140
- dump_leaf_hash(Leaf leaf, int depth, Out out) {
141
- size_t size;
142
- int d2 = depth + 1;
116
+ static void dump_leaf_hash(Leaf leaf, int depth, Out out) {
117
+ size_t size;
118
+ int d2 = depth + 1;
143
119
 
144
120
  size = 2;
145
121
  if (out->end - out->cur <= (long)size) {
146
- grow(out, size);
122
+ grow(out, size);
147
123
  }
148
124
  *out->cur++ = '{';
149
125
  if (0 == leaf->elements) {
150
- *out->cur++ = '}';
126
+ *out->cur++ = '}';
151
127
  } else {
152
- Leaf first = leaf->elements->next;
153
- Leaf e = first;
154
-
155
- size = d2 * out->indent + 2;
156
- do {
157
- if (out->end - out->cur <= (long)size) {
158
- grow(out, size);
159
- }
160
- fill_indent(out, d2);
161
- oj_dump_cstr(e->key, strlen(e->key), 0, 0, out);
162
- *out->cur++ = ':';
163
- dump_leaf(e, d2, out);
164
- if (e->next != first) {
165
- *out->cur++ = ',';
166
- }
167
- e = e->next;
168
- } while (e != first);
169
- size = depth * out->indent + 1;
170
- if (out->end - out->cur <= (long)size) {
171
- grow(out, size);
172
- }
173
- fill_indent(out, depth);
174
- *out->cur++ = '}';
128
+ Leaf first = leaf->elements->next;
129
+ Leaf e = first;
130
+
131
+ size = d2 * out->indent + 2;
132
+ do {
133
+ if (out->end - out->cur <= (long)size) {
134
+ grow(out, size);
135
+ }
136
+ fill_indent(out, d2);
137
+ oj_dump_cstr(e->key, strlen(e->key), 0, 0, out);
138
+ *out->cur++ = ':';
139
+ dump_leaf(e, d2, out);
140
+ if (e->next != first) {
141
+ *out->cur++ = ',';
142
+ }
143
+ e = e->next;
144
+ } while (e != first);
145
+ size = depth * out->indent + 1;
146
+ if (out->end - out->cur <= (long)size) {
147
+ grow(out, size);
148
+ }
149
+ fill_indent(out, depth);
150
+ *out->cur++ = '}';
175
151
  }
176
152
  *out->cur = '\0';
177
153
  }
178
154
 
179
- static void
180
- dump_leaf(Leaf leaf, int depth, Out out) {
155
+ static void dump_leaf(Leaf leaf, int depth, Out out) {
181
156
  switch (leaf->rtype) {
182
- case T_NIL:
183
- oj_dump_nil(Qnil, 0, out, false);
184
- break;
185
- case T_TRUE:
186
- oj_dump_true(Qtrue, 0, out, false);
187
- break;
188
- case T_FALSE:
189
- oj_dump_false(Qfalse, 0, out, false);
190
- break;
191
- case T_STRING:
192
- dump_leaf_str(leaf, out);
193
- break;
194
- case T_FIXNUM:
195
- dump_leaf_fixnum(leaf, out);
196
- break;
197
- case T_FLOAT:
198
- dump_leaf_float(leaf, out);
199
- break;
200
- case T_ARRAY:
201
- dump_leaf_array(leaf, depth, out);
202
- break;
203
- case T_HASH:
204
- dump_leaf_hash(leaf, depth, out);
205
- break;
206
- default:
207
- rb_raise(rb_eTypeError, "Unexpected type %02x.\n", leaf->rtype);
208
- break;
157
+ case T_NIL: oj_dump_nil(Qnil, 0, out, false); break;
158
+ case T_TRUE: oj_dump_true(Qtrue, 0, out, false); break;
159
+ case T_FALSE: oj_dump_false(Qfalse, 0, out, false); break;
160
+ case T_STRING: dump_leaf_str(leaf, out); break;
161
+ case T_FIXNUM: dump_leaf_fixnum(leaf, out); break;
162
+ case T_FLOAT: dump_leaf_float(leaf, out); break;
163
+ case T_ARRAY: dump_leaf_array(leaf, depth, out); break;
164
+ case T_HASH: dump_leaf_hash(leaf, depth, out); break;
165
+ default: rb_raise(rb_eTypeError, "Unexpected type %02x.\n", leaf->rtype); break;
209
166
  }
210
167
  }
211
168
 
212
- void
213
- oj_dump_leaf_to_json(Leaf leaf, Options copts, Out out) {
169
+ void oj_dump_leaf_to_json(Leaf leaf, Options copts, Out out) {
214
170
  if (0 == out->buf) {
215
- out->buf = ALLOC_N(char, 4096);
216
- out->end = out->buf + 4095 - BUFFER_EXTRA; // 1 less than end plus extra for possible errors
217
- out->allocated = true;
171
+ out->buf = ALLOC_N(char, 4096);
172
+ out->end = out->buf + 4095 -
173
+ BUFFER_EXTRA; // 1 less than end plus extra for possible errors
174
+ out->allocated = true;
218
175
  }
219
- out->cur = out->buf;
176
+ out->cur = out->buf;
220
177
  out->circ_cnt = 0;
221
- out->opts = copts;
178
+ out->opts = copts;
222
179
  out->hash_cnt = 0;
223
- out->indent = copts->indent;
180
+ out->indent = copts->indent;
224
181
  dump_leaf(leaf, 0, out);
225
182
  }
226
183
 
227
- void
228
- oj_write_leaf_to_file(Leaf leaf, const char *path, Options copts) {
229
- char buf[4096];
184
+ void oj_write_leaf_to_file(Leaf leaf, const char *path, Options copts) {
185
+ char buf[4096];
230
186
  struct _out out;
231
- size_t size;
232
- FILE *f;
187
+ size_t size;
188
+ FILE * f;
233
189
 
234
- out.buf = buf;
235
- out.end = buf + sizeof(buf) - BUFFER_EXTRA;
190
+ out.buf = buf;
191
+ out.end = buf + sizeof(buf) - BUFFER_EXTRA;
236
192
  out.allocated = false;
237
- out.omit_nil = copts->dump_opts.omit_nil;
193
+ out.omit_nil = copts->dump_opts.omit_nil;
238
194
  oj_dump_leaf_to_json(leaf, copts, &out);
239
195
  size = out.cur - out.buf;
240
196
  if (0 == (f = fopen(path, "w"))) {
241
- rb_raise(rb_eIOError, "%s\n", strerror(errno));
197
+ rb_raise(rb_eIOError, "%s\n", strerror(errno));
242
198
  }
243
199
  if (size != fwrite(out.buf, 1, size, f)) {
244
- int err = ferror(f);
200
+ int err = ferror(f);
245
201
 
246
- rb_raise(rb_eIOError, "Write failed. [%d:%s]\n", err, strerror(err));
202
+ rb_raise(rb_eIOError, "Write failed. [%d:%s]\n", err, strerror(err));
247
203
  }
248
204
  if (out.allocated) {
249
- xfree(out.buf);
205
+ xfree(out.buf);
250
206
  }
251
207
  fclose(f);
252
208
  }