oj 3.10.6 → 3.12.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -1
- data/ext/oj/buf.h +36 -68
- data/ext/oj/cache8.c +59 -62
- data/ext/oj/cache8.h +9 -36
- data/ext/oj/circarray.c +36 -42
- data/ext/oj/circarray.h +12 -13
- data/ext/oj/code.c +172 -179
- data/ext/oj/code.h +22 -24
- data/ext/oj/compat.c +176 -181
- data/ext/oj/custom.c +800 -864
- data/ext/oj/dump.c +774 -776
- data/ext/oj/dump.h +50 -55
- data/ext/oj/dump_compat.c +2 -4
- data/ext/oj/dump_leaf.c +118 -162
- data/ext/oj/dump_object.c +610 -632
- data/ext/oj/dump_strict.c +319 -331
- data/ext/oj/encode.h +4 -33
- data/ext/oj/err.c +40 -29
- data/ext/oj/err.h +25 -44
- data/ext/oj/extconf.rb +2 -1
- data/ext/oj/fast.c +1054 -1081
- data/ext/oj/hash.c +102 -97
- data/ext/oj/hash.h +10 -35
- data/ext/oj/hash_test.c +451 -472
- data/ext/oj/mimic_json.c +415 -402
- data/ext/oj/object.c +588 -532
- data/ext/oj/odd.c +124 -132
- data/ext/oj/odd.h +28 -29
- data/ext/oj/oj.c +1186 -906
- data/ext/oj/oj.h +289 -298
- data/ext/oj/parse.c +946 -870
- data/ext/oj/parse.h +81 -79
- data/ext/oj/rails.c +837 -842
- data/ext/oj/rails.h +8 -11
- data/ext/oj/reader.c +139 -147
- data/ext/oj/reader.h +68 -84
- data/ext/oj/resolve.c +44 -47
- data/ext/oj/resolve.h +4 -6
- data/ext/oj/rxclass.c +69 -73
- data/ext/oj/rxclass.h +13 -14
- data/ext/oj/saj.c +453 -484
- data/ext/oj/scp.c +88 -113
- data/ext/oj/sparse.c +783 -714
- data/ext/oj/stream_writer.c +123 -157
- data/ext/oj/strict.c +133 -106
- data/ext/oj/string_writer.c +199 -247
- data/ext/oj/trace.c +34 -41
- data/ext/oj/trace.h +15 -15
- data/ext/oj/util.c +104 -104
- data/ext/oj/util.h +4 -3
- data/ext/oj/val_stack.c +48 -76
- data/ext/oj/val_stack.h +80 -115
- data/ext/oj/wab.c +321 -325
- data/lib/oj.rb +0 -8
- data/lib/oj/bag.rb +1 -0
- data/lib/oj/easy_hash.rb +5 -4
- data/lib/oj/mimic.rb +47 -13
- data/lib/oj/version.rb +1 -1
- data/pages/Modes.md +1 -0
- data/pages/Options.md +23 -11
- data/test/activerecord/result_test.rb +7 -2
- data/test/foo.rb +8 -40
- data/test/helper.rb +10 -0
- data/test/json_gem/json_common_interface_test.rb +8 -3
- data/test/json_gem/json_generator_test.rb +15 -3
- data/test/json_gem/test_helper.rb +8 -0
- data/test/perf.rb +1 -1
- data/test/perf_scp.rb +11 -10
- data/test/perf_strict.rb +17 -23
- data/test/prec.rb +23 -0
- data/test/sample_json.rb +1 -1
- data/test/test_compat.rb +16 -3
- data/test/test_custom.rb +11 -0
- data/test/test_fast.rb +32 -2
- data/test/test_generate.rb +21 -0
- data/test/test_hash.rb +10 -0
- data/test/test_rails.rb +9 -0
- data/test/test_scp.rb +1 -1
- data/test/test_various.rb +4 -2
- metadata +89 -85
data/ext/oj/dump.h
CHANGED
@@ -1,7 +1,5 @@
|
|
1
|
-
|
2
|
-
|
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
|
19
|
-
extern void
|
20
|
-
extern void
|
21
|
-
extern void
|
22
|
-
extern void
|
23
|
-
extern void
|
24
|
-
extern void
|
25
|
-
extern void
|
26
|
-
extern void
|
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
|
29
|
-
extern void
|
30
|
-
extern void
|
31
|
-
extern void
|
32
|
-
extern void
|
33
|
-
extern void
|
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*
|
33
|
+
extern const char *oj_nan_str(VALUE obj, int opt, int mode, bool plus, int *lenp);
|
36
34
|
|
37
|
-
extern void
|
38
|
-
extern long
|
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
|
41
|
-
extern void
|
42
|
-
extern void
|
43
|
-
extern void
|
44
|
-
extern void
|
45
|
-
extern void
|
46
|
-
extern void
|
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
|
46
|
+
extern void oj_dump_raw_json(VALUE obj, int depth, Out out);
|
49
47
|
|
50
|
-
extern VALUE
|
51
|
-
extern VALUE
|
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
|
51
|
+
extern int oj_dump_float_printf(char *buf, size_t blen, VALUE obj, double d, const char *format);
|
54
52
|
|
55
|
-
extern bool
|
56
|
-
extern time_t
|
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
|
-
|
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
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
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
|
-
|
78
|
-
char
|
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
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
78
|
+
for (; 0 < num; num /= 10, b--) {
|
79
|
+
*b = (num % 10) + '0';
|
80
|
+
}
|
81
|
+
b++;
|
87
82
|
} else {
|
88
|
-
|
83
|
+
*b = '0';
|
89
84
|
}
|
90
85
|
for (; '\0' != *b; b++) {
|
91
|
-
|
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
|
-
|
2
|
-
|
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
|
-
|
2
|
-
|
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
|
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
|
-
|
18
|
+
size += len;
|
22
19
|
}
|
23
20
|
if (out->allocated) {
|
24
|
-
|
21
|
+
buf = REALLOC_N(out->buf, char, (size + BUFFER_EXTRA));
|
25
22
|
} else {
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
57
|
-
|
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
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
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
|
-
|
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
|
-
|
103
|
-
|
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
|
-
|
85
|
+
grow(out, size);
|
109
86
|
}
|
110
87
|
*out->cur++ = '[';
|
111
88
|
if (0 == leaf->elements) {
|
112
|
-
|
89
|
+
*out->cur++ = ']';
|
113
90
|
} else {
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
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
|
-
|
141
|
-
|
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
|
-
|
122
|
+
grow(out, size);
|
147
123
|
}
|
148
124
|
*out->cur++ = '{';
|
149
125
|
if (0 == leaf->elements) {
|
150
|
-
|
126
|
+
*out->cur++ = '}';
|
151
127
|
} else {
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
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
|
-
|
184
|
-
|
185
|
-
case
|
186
|
-
|
187
|
-
|
188
|
-
case
|
189
|
-
|
190
|
-
|
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
|
-
|
216
|
-
|
217
|
-
|
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
|
176
|
+
out->cur = out->buf;
|
220
177
|
out->circ_cnt = 0;
|
221
|
-
out->opts
|
178
|
+
out->opts = copts;
|
222
179
|
out->hash_cnt = 0;
|
223
|
-
out->indent
|
180
|
+
out->indent = copts->indent;
|
224
181
|
dump_leaf(leaf, 0, out);
|
225
182
|
}
|
226
183
|
|
227
|
-
void
|
228
|
-
|
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
|
232
|
-
FILE
|
187
|
+
size_t size;
|
188
|
+
FILE * f;
|
233
189
|
|
234
|
-
out.buf
|
235
|
-
out.end
|
190
|
+
out.buf = buf;
|
191
|
+
out.end = buf + sizeof(buf) - BUFFER_EXTRA;
|
236
192
|
out.allocated = false;
|
237
|
-
out.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
|
-
|
197
|
+
rb_raise(rb_eIOError, "%s\n", strerror(errno));
|
242
198
|
}
|
243
199
|
if (size != fwrite(out.buf, 1, size, f)) {
|
244
|
-
|
200
|
+
int err = ferror(f);
|
245
201
|
|
246
|
-
|
202
|
+
rb_raise(rb_eIOError, "Write failed. [%d:%s]\n", err, strerror(err));
|
247
203
|
}
|
248
204
|
if (out.allocated) {
|
249
|
-
|
205
|
+
xfree(out.buf);
|
250
206
|
}
|
251
207
|
fclose(f);
|
252
208
|
}
|