oj 3.10.6 → 3.12.0

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 +168 -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 +78 -95
  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 +1178 -905
  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 +317 -328
  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 +45 -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
@@ -1,254 +1,232 @@
1
- /* strwriter.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 "dump.h"
7
5
  #include "encode.h"
8
6
 
9
- extern VALUE Oj;
7
+ extern VALUE Oj;
10
8
 
11
- bool string_writer_optimized = false;
9
+ bool string_writer_optimized = false;
12
10
 
13
- static void
14
- key_check(StrWriter sw, const char *key) {
15
- DumpType type = sw->types[sw->depth];
11
+ static void key_check(StrWriter sw, const char *key) {
12
+ DumpType type = sw->types[sw->depth];
16
13
 
17
14
  if (0 == key && (ObjectNew == type || ObjectType == type)) {
18
- rb_raise(rb_eStandardError, "Can not push onto an Object without a key.");
15
+ rb_raise(rb_eStandardError, "Can not push onto an Object without a key.");
19
16
  }
20
17
  }
21
18
 
22
- static void
23
- push_type(StrWriter sw, DumpType type) {
19
+ static void push_type(StrWriter sw, DumpType type) {
24
20
  if (sw->types_end <= sw->types + sw->depth + 1) {
25
- size_t size = (sw->types_end - sw->types) * 2;
21
+ size_t size = (sw->types_end - sw->types) * 2;
26
22
 
27
- REALLOC_N(sw->types, char, size);
28
- sw->types_end = sw->types + size;
23
+ REALLOC_N(sw->types, char, size);
24
+ sw->types_end = sw->types + size;
29
25
  }
30
26
  sw->depth++;
31
27
  sw->types[sw->depth] = type;
32
28
  }
33
29
 
34
- static void
35
- maybe_comma(StrWriter sw) {
30
+ static void maybe_comma(StrWriter sw) {
36
31
  switch (sw->types[sw->depth]) {
37
- case ObjectNew:
38
- sw->types[sw->depth] = ObjectType;
39
- break;
40
- case ArrayNew:
41
- sw->types[sw->depth] = ArrayType;
42
- break;
32
+ case ObjectNew: sw->types[sw->depth] = ObjectType; break;
33
+ case ArrayNew: sw->types[sw->depth] = ArrayType; break;
43
34
  case ObjectType:
44
35
  case ArrayType:
45
- // Always have a few characters available in the out.buf.
46
- *sw->out.cur++ = ',';
47
- break;
36
+ // Always have a few characters available in the out.buf.
37
+ *sw->out.cur++ = ',';
38
+ break;
48
39
  }
49
40
  }
50
41
 
51
42
  // Used by stream writer also.
52
- void
53
- oj_str_writer_init(StrWriter sw, int buf_size) {
54
- sw->opts = oj_default_options;
55
- sw->depth = 0;
56
- sw->types = ALLOC_N(char, 256);
57
- sw->types_end = sw->types + 256;
58
- *sw->types = '\0';
43
+ void oj_str_writer_init(StrWriter sw, int buf_size) {
44
+ sw->opts = oj_default_options;
45
+ sw->depth = 0;
46
+ sw->types = ALLOC_N(char, 256);
47
+ sw->types_end = sw->types + 256;
48
+ *sw->types = '\0';
59
49
  sw->keyWritten = 0;
60
50
 
61
51
  if (0 == buf_size) {
62
- buf_size = 4096;
52
+ buf_size = 4096;
63
53
  } else if (buf_size < 1024) {
64
- buf_size = 1024;
54
+ buf_size = 1024;
65
55
  }
66
- sw->out.buf = ALLOC_N(char, buf_size);
67
- sw->out.end = sw->out.buf + buf_size - 10;
68
- sw->out.allocated = true;
69
- sw->out.cur = sw->out.buf;
70
- *sw->out.cur = '\0';
56
+ sw->out.buf = ALLOC_N(char, buf_size);
57
+ sw->out.end = sw->out.buf + buf_size - 10;
58
+ sw->out.allocated = true;
59
+ sw->out.cur = sw->out.buf;
60
+ *sw->out.cur = '\0';
71
61
  sw->out.circ_cache = NULL;
72
- sw->out.circ_cnt = 0;
73
- sw->out.hash_cnt = 0;
74
- sw->out.opts = &sw->opts;
75
- sw->out.indent = sw->opts.indent;
76
- sw->out.depth = 0;
77
- sw->out.argc = 0;
78
- sw->out.argv = NULL;
79
- sw->out.caller = 0;
80
- sw->out.ropts = NULL;
81
- sw->out.omit_nil = oj_default_options.dump_opts.omit_nil;
62
+ sw->out.circ_cnt = 0;
63
+ sw->out.hash_cnt = 0;
64
+ sw->out.opts = &sw->opts;
65
+ sw->out.indent = sw->opts.indent;
66
+ sw->out.depth = 0;
67
+ sw->out.argc = 0;
68
+ sw->out.argv = NULL;
69
+ sw->out.caller = 0;
70
+ sw->out.ropts = NULL;
71
+ sw->out.omit_nil = oj_default_options.dump_opts.omit_nil;
82
72
  }
83
73
 
84
- void
85
- oj_str_writer_push_key(StrWriter sw, const char *key) {
86
- DumpType type = sw->types[sw->depth];
87
- long size;
74
+ void oj_str_writer_push_key(StrWriter sw, const char *key) {
75
+ DumpType type = sw->types[sw->depth];
76
+ long size;
88
77
 
89
78
  if (sw->keyWritten) {
90
- rb_raise(rb_eStandardError, "Can not push more than one key before pushing a non-key.");
79
+ rb_raise(rb_eStandardError, "Can not push more than one key before pushing a non-key.");
91
80
  }
92
81
  if (ObjectNew != type && ObjectType != type) {
93
- rb_raise(rb_eStandardError, "Can only push a key onto an Object.");
82
+ rb_raise(rb_eStandardError, "Can only push a key onto an Object.");
94
83
  }
95
84
  size = sw->depth * sw->out.indent + 3;
96
85
  assure_size(&sw->out, size);
97
86
  maybe_comma(sw);
98
87
  if (0 < sw->depth) {
99
- fill_indent(&sw->out, sw->depth);
88
+ fill_indent(&sw->out, sw->depth);
100
89
  }
101
90
  oj_dump_cstr(key, strlen(key), 0, 0, &sw->out);
102
91
  *sw->out.cur++ = ':';
103
92
  sw->keyWritten = 1;
104
93
  }
105
94
 
106
- void
107
- oj_str_writer_push_object(StrWriter sw, const char *key) {
95
+ void oj_str_writer_push_object(StrWriter sw, const char *key) {
108
96
  if (sw->keyWritten) {
109
- sw->keyWritten = 0;
110
- assure_size(&sw->out, 1);
97
+ sw->keyWritten = 0;
98
+ assure_size(&sw->out, 1);
111
99
  } else {
112
- long size;
113
-
114
- key_check(sw, key);
115
- size = sw->depth * sw->out.indent + 3;
116
- assure_size(&sw->out, size);
117
- maybe_comma(sw);
118
- if (0 < sw->depth) {
119
- fill_indent(&sw->out, sw->depth);
120
- }
121
- if (0 != key) {
122
- oj_dump_cstr(key, strlen(key), 0, 0, &sw->out);
123
- *sw->out.cur++ = ':';
124
- }
100
+ long size;
101
+
102
+ key_check(sw, key);
103
+ size = sw->depth * sw->out.indent + 3;
104
+ assure_size(&sw->out, size);
105
+ maybe_comma(sw);
106
+ if (0 < sw->depth) {
107
+ fill_indent(&sw->out, sw->depth);
108
+ }
109
+ if (0 != key) {
110
+ oj_dump_cstr(key, strlen(key), 0, 0, &sw->out);
111
+ *sw->out.cur++ = ':';
112
+ }
125
113
  }
126
114
  *sw->out.cur++ = '{';
127
115
  push_type(sw, ObjectNew);
128
116
  }
129
117
 
130
- void
131
- oj_str_writer_push_array(StrWriter sw, const char *key) {
118
+ void oj_str_writer_push_array(StrWriter sw, const char *key) {
132
119
  if (sw->keyWritten) {
133
- sw->keyWritten = 0;
134
- assure_size(&sw->out, 1);
120
+ sw->keyWritten = 0;
121
+ assure_size(&sw->out, 1);
135
122
  } else {
136
- long size;
137
-
138
- key_check(sw, key);
139
- size = sw->depth * sw->out.indent + 3;
140
- assure_size(&sw->out, size);
141
- maybe_comma(sw);
142
- if (0 < sw->depth) {
143
- fill_indent(&sw->out, sw->depth);
144
- }
145
- if (0 != key) {
146
- oj_dump_cstr(key, strlen(key), 0, 0, &sw->out);
147
- *sw->out.cur++ = ':';
148
- }
123
+ long size;
124
+
125
+ key_check(sw, key);
126
+ size = sw->depth * sw->out.indent + 3;
127
+ assure_size(&sw->out, size);
128
+ maybe_comma(sw);
129
+ if (0 < sw->depth) {
130
+ fill_indent(&sw->out, sw->depth);
131
+ }
132
+ if (0 != key) {
133
+ oj_dump_cstr(key, strlen(key), 0, 0, &sw->out);
134
+ *sw->out.cur++ = ':';
135
+ }
149
136
  }
150
137
  *sw->out.cur++ = '[';
151
138
  push_type(sw, ArrayNew);
152
139
  }
153
140
 
154
- void
155
- oj_str_writer_push_value(StrWriter sw, VALUE val, const char *key) {
156
- Out out = &sw->out;
141
+ void oj_str_writer_push_value(StrWriter sw, VALUE val, const char *key) {
142
+ Out out = &sw->out;
157
143
 
158
144
  if (sw->keyWritten) {
159
- sw->keyWritten = 0;
145
+ sw->keyWritten = 0;
160
146
  } else {
161
- long size;
162
-
163
- key_check(sw, key);
164
- size = sw->depth * out->indent + 3;
165
- assure_size(out, size);
166
- maybe_comma(sw);
167
- if (0 < sw->depth) {
168
- fill_indent(&sw->out, sw->depth);
169
- }
170
- if (0 != key) {
171
- oj_dump_cstr(key, strlen(key), 0, 0, out);
172
- *out->cur++ = ':';
173
- }
147
+ long size;
148
+
149
+ key_check(sw, key);
150
+ size = sw->depth * out->indent + 3;
151
+ assure_size(out, size);
152
+ maybe_comma(sw);
153
+ if (0 < sw->depth) {
154
+ fill_indent(&sw->out, sw->depth);
155
+ }
156
+ if (0 != key) {
157
+ oj_dump_cstr(key, strlen(key), 0, 0, out);
158
+ *out->cur++ = ':';
159
+ }
174
160
  }
175
161
  switch (out->opts->mode) {
176
- case StrictMode: oj_dump_strict_val(val, sw->depth, out); break;
177
- case NullMode: oj_dump_null_val(val, sw->depth, out); break;
178
- case ObjectMode: oj_dump_obj_val(val, sw->depth, out); break;
179
- case CompatMode: oj_dump_compat_val(val, sw->depth, out, Yes == out->opts->to_json); break;
180
- case RailsMode: oj_dump_rails_val(val, sw->depth, out); break;
181
- case CustomMode: oj_dump_custom_val(val, sw->depth, out, true); break;
182
- default: oj_dump_custom_val(val, sw->depth, out, true); break;
162
+ case StrictMode: oj_dump_strict_val(val, sw->depth, out); break;
163
+ case NullMode: oj_dump_null_val(val, sw->depth, out); break;
164
+ case ObjectMode: oj_dump_obj_val(val, sw->depth, out); break;
165
+ case CompatMode: oj_dump_compat_val(val, sw->depth, out, Yes == out->opts->to_json); break;
166
+ case RailsMode: oj_dump_rails_val(val, sw->depth, out); break;
167
+ case CustomMode: oj_dump_custom_val(val, sw->depth, out, true); break;
168
+ default: oj_dump_custom_val(val, sw->depth, out, true); break;
183
169
  }
184
170
  }
185
171
 
186
- void
187
- oj_str_writer_push_json(StrWriter sw, const char *json, const char *key) {
172
+ void oj_str_writer_push_json(StrWriter sw, const char *json, const char *key) {
188
173
  if (sw->keyWritten) {
189
- sw->keyWritten = 0;
174
+ sw->keyWritten = 0;
190
175
  } else {
191
- long size;
192
-
193
- key_check(sw, key);
194
- size = sw->depth * sw->out.indent + 3;
195
- assure_size(&sw->out, size);
196
- maybe_comma(sw);
197
- if (0 < sw->depth) {
198
- fill_indent(&sw->out, sw->depth);
199
- }
200
- if (0 != key) {
201
- oj_dump_cstr(key, strlen(key), 0, 0, &sw->out);
202
- *sw->out.cur++ = ':';
203
- }
176
+ long size;
177
+
178
+ key_check(sw, key);
179
+ size = sw->depth * sw->out.indent + 3;
180
+ assure_size(&sw->out, size);
181
+ maybe_comma(sw);
182
+ if (0 < sw->depth) {
183
+ fill_indent(&sw->out, sw->depth);
184
+ }
185
+ if (0 != key) {
186
+ oj_dump_cstr(key, strlen(key), 0, 0, &sw->out);
187
+ *sw->out.cur++ = ':';
188
+ }
204
189
  }
205
190
  oj_dump_raw(json, strlen(json), &sw->out);
206
191
  }
207
192
 
208
- void
209
- oj_str_writer_pop(StrWriter sw) {
210
- long size;
211
- DumpType type = sw->types[sw->depth];
193
+ void oj_str_writer_pop(StrWriter sw) {
194
+ long size;
195
+ DumpType type = sw->types[sw->depth];
212
196
 
213
197
  if (sw->keyWritten) {
214
- sw->keyWritten = 0;
215
- rb_raise(rb_eStandardError, "Can not pop after writing a key but no value.");
198
+ sw->keyWritten = 0;
199
+ rb_raise(rb_eStandardError, "Can not pop after writing a key but no value.");
216
200
  }
217
201
  sw->depth--;
218
202
  if (0 > sw->depth) {
219
- rb_raise(rb_eStandardError, "Can not pop with no open array or object.");
203
+ rb_raise(rb_eStandardError, "Can not pop with no open array or object.");
220
204
  }
221
205
  size = sw->depth * sw->out.indent + 2;
222
206
  assure_size(&sw->out, size);
223
207
  fill_indent(&sw->out, sw->depth);
224
208
  switch (type) {
225
209
  case ObjectNew:
226
- case ObjectType:
227
- *sw->out.cur++ = '}';
228
- break;
210
+ case ObjectType: *sw->out.cur++ = '}'; break;
229
211
  case ArrayNew:
230
- case ArrayType:
231
- *sw->out.cur++ = ']';
232
- break;
212
+ case ArrayType: *sw->out.cur++ = ']'; break;
233
213
  }
234
214
  if (0 == sw->depth && 0 <= sw->out.indent) {
235
- *sw->out.cur++ = '\n';
215
+ *sw->out.cur++ = '\n';
236
216
  }
237
217
  }
238
218
 
239
- void
240
- oj_str_writer_pop_all(StrWriter sw) {
219
+ void oj_str_writer_pop_all(StrWriter sw) {
241
220
  while (0 < sw->depth) {
242
- oj_str_writer_pop(sw);
221
+ oj_str_writer_pop(sw);
243
222
  }
244
223
  }
245
224
 
246
- static void
247
- str_writer_free(void *ptr) {
248
- StrWriter sw;
225
+ static void str_writer_free(void *ptr) {
226
+ StrWriter sw;
249
227
 
250
228
  if (0 == ptr) {
251
- return;
229
+ return;
252
230
  }
253
231
  sw = (StrWriter)ptr;
254
232
  xfree(sw->out.buf);
@@ -272,16 +250,15 @@ str_writer_free(void *ptr) {
272
250
  * - *io* [_IO_] stream to write to
273
251
  * - *options* [_Hash_] formating options
274
252
  */
275
- static VALUE
276
- str_writer_new(int argc, VALUE *argv, VALUE self) {
277
- StrWriter sw = ALLOC(struct _strWriter);
253
+ static VALUE str_writer_new(int argc, VALUE *argv, VALUE self) {
254
+ StrWriter sw = ALLOC(struct _strWriter);
278
255
 
279
256
  oj_str_writer_init(sw, 0);
280
257
  if (1 == argc) {
281
- oj_parse_options(argv[0], &sw->opts);
258
+ oj_parse_options(argv[0], &sw->opts);
282
259
  }
283
- sw->out.argc = argc - 1;
284
- sw->out.argv = argv + 1;
260
+ sw->out.argc = argc - 1;
261
+ sw->out.argv = argv + 1;
285
262
  sw->out.indent = sw->opts.indent;
286
263
 
287
264
  return Data_Wrap_Struct(oj_string_writer_class, 0, str_writer_free, sw);
@@ -295,9 +272,8 @@ str_writer_new(int argc, VALUE *argv, VALUE self) {
295
272
  * the next push then that new key will be ignored.
296
273
  * - *key* [_String_] the key pending for the next push
297
274
  */
298
- static VALUE
299
- str_writer_push_key(VALUE self, VALUE key) {
300
- StrWriter sw = (StrWriter)DATA_PTR(self);
275
+ static VALUE str_writer_push_key(VALUE self, VALUE key) {
276
+ StrWriter sw = (StrWriter)DATA_PTR(self);
301
277
 
302
278
  rb_check_type(key, T_STRING);
303
279
  oj_str_writer_push_key(sw, StringValuePtr(key));
@@ -312,29 +288,24 @@ str_writer_push_key(VALUE self, VALUE key) {
312
288
  * until a pop() is called.
313
289
  * - *key* [_String_] the key if adding to an object in the JSON document
314
290
  */
315
- static VALUE
316
- str_writer_push_object(int argc, VALUE *argv, VALUE self) {
317
- StrWriter sw = (StrWriter)DATA_PTR(self);
291
+ static VALUE str_writer_push_object(int argc, VALUE *argv, VALUE self) {
292
+ StrWriter sw = (StrWriter)DATA_PTR(self);
318
293
 
319
294
  switch (argc) {
320
- case 0:
321
- oj_str_writer_push_object(sw, 0);
322
- break;
295
+ case 0: oj_str_writer_push_object(sw, 0); break;
323
296
  case 1:
324
- if (Qnil == argv[0]) {
325
- oj_str_writer_push_object(sw, 0);
326
- } else {
327
- rb_check_type(argv[0], T_STRING);
328
- oj_str_writer_push_object(sw, StringValuePtr(argv[0]));
329
- }
330
- break;
331
- default:
332
- rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'.");
333
- break;
297
+ if (Qnil == argv[0]) {
298
+ oj_str_writer_push_object(sw, 0);
299
+ } else {
300
+ rb_check_type(argv[0], T_STRING);
301
+ oj_str_writer_push_object(sw, StringValuePtr(argv[0]));
302
+ }
303
+ break;
304
+ default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'."); break;
334
305
  }
335
306
  if (rb_block_given_p()) {
336
- rb_yield(Qnil);
337
- oj_str_writer_pop(sw);
307
+ rb_yield(Qnil);
308
+ oj_str_writer_pop(sw);
338
309
  }
339
310
  return Qnil;
340
311
  }
@@ -346,29 +317,24 @@ str_writer_push_object(int argc, VALUE *argv, VALUE self) {
346
317
  * until a pop() is called.
347
318
  * - *key* [_String_] the key if adding to an object in the JSON document
348
319
  */
349
- static VALUE
350
- str_writer_push_array(int argc, VALUE *argv, VALUE self) {
351
- StrWriter sw = (StrWriter)DATA_PTR(self);
320
+ static VALUE str_writer_push_array(int argc, VALUE *argv, VALUE self) {
321
+ StrWriter sw = (StrWriter)DATA_PTR(self);
352
322
 
353
323
  switch (argc) {
354
- case 0:
355
- oj_str_writer_push_array(sw, 0);
356
- break;
324
+ case 0: oj_str_writer_push_array(sw, 0); break;
357
325
  case 1:
358
- if (Qnil == argv[0]) {
359
- oj_str_writer_push_array(sw, 0);
360
- } else {
361
- rb_check_type(argv[0], T_STRING);
362
- oj_str_writer_push_array(sw, StringValuePtr(argv[0]));
363
- }
364
- break;
365
- default:
366
- rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'.");
367
- break;
326
+ if (Qnil == argv[0]) {
327
+ oj_str_writer_push_array(sw, 0);
328
+ } else {
329
+ rb_check_type(argv[0], T_STRING);
330
+ oj_str_writer_push_array(sw, StringValuePtr(argv[0]));
331
+ }
332
+ break;
333
+ default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'."); break;
368
334
  }
369
335
  if (rb_block_given_p()) {
370
- rb_yield(Qnil);
371
- oj_str_writer_pop(sw);
336
+ rb_yield(Qnil);
337
+ oj_str_writer_pop(sw);
372
338
  }
373
339
  return Qnil;
374
340
  }
@@ -380,23 +346,18 @@ str_writer_push_array(int argc, VALUE *argv, VALUE self) {
380
346
  * - *value* [_Object_] value to add to the JSON document
381
347
  * - *key* [_String_] the key if adding to an object in the JSON document
382
348
  */
383
- static VALUE
384
- str_writer_push_value(int argc, VALUE *argv, VALUE self) {
349
+ static VALUE str_writer_push_value(int argc, VALUE *argv, VALUE self) {
385
350
  switch (argc) {
386
- case 1:
387
- oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, 0);
388
- break;
351
+ case 1: oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, 0); break;
389
352
  case 2:
390
- if (Qnil == argv[1]) {
391
- oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, 0);
392
- } else {
393
- rb_check_type(argv[1], T_STRING);
394
- oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, StringValuePtr(argv[1]));
395
- }
396
- break;
397
- default:
398
- rb_raise(rb_eArgError, "Wrong number of argument to 'push_value'.");
399
- break;
353
+ if (Qnil == argv[1]) {
354
+ oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, 0);
355
+ } else {
356
+ rb_check_type(argv[1], T_STRING);
357
+ oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, StringValuePtr(argv[1]));
358
+ }
359
+ break;
360
+ default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_value'."); break;
400
361
  }
401
362
  return Qnil;
402
363
  }
@@ -410,24 +371,21 @@ str_writer_push_value(int argc, VALUE *argv, VALUE self) {
410
371
  * - *value* [_Object_] value to add to the JSON document
411
372
  * - *key* [_String_] the key if adding to an object in the JSON document
412
373
  */
413
- static VALUE
414
- str_writer_push_json(int argc, VALUE *argv, VALUE self) {
374
+ static VALUE str_writer_push_json(int argc, VALUE *argv, VALUE self) {
415
375
  rb_check_type(argv[0], T_STRING);
416
376
  switch (argc) {
417
- case 1:
418
- oj_str_writer_push_json((StrWriter)DATA_PTR(self), StringValuePtr(*argv), 0);
419
- break;
377
+ case 1: oj_str_writer_push_json((StrWriter)DATA_PTR(self), StringValuePtr(*argv), 0); break;
420
378
  case 2:
421
- if (Qnil == argv[1]) {
422
- oj_str_writer_push_json((StrWriter)DATA_PTR(self), StringValuePtr(*argv), 0);
423
- } else {
424
- rb_check_type(argv[1], T_STRING);
425
- oj_str_writer_push_json((StrWriter)DATA_PTR(self), StringValuePtr(*argv), StringValuePtr(argv[1]));
426
- }
427
- break;
428
- default:
429
- rb_raise(rb_eArgError, "Wrong number of argument to 'push_json'.");
430
- break;
379
+ if (Qnil == argv[1]) {
380
+ oj_str_writer_push_json((StrWriter)DATA_PTR(self), StringValuePtr(*argv), 0);
381
+ } else {
382
+ rb_check_type(argv[1], T_STRING);
383
+ oj_str_writer_push_json((StrWriter)DATA_PTR(self),
384
+ StringValuePtr(*argv),
385
+ StringValuePtr(argv[1]));
386
+ }
387
+ break;
388
+ default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_json'."); break;
431
389
  }
432
390
  return Qnil;
433
391
  }
@@ -437,8 +395,7 @@ str_writer_push_json(int argc, VALUE *argv, VALUE self) {
437
395
  * Pops up a level in the JSON document closing the array or object that is
438
396
  * currently open.
439
397
  */
440
- static VALUE
441
- str_writer_pop(VALUE self) {
398
+ static VALUE str_writer_pop(VALUE self) {
442
399
  oj_str_writer_pop((StrWriter)DATA_PTR(self));
443
400
  return Qnil;
444
401
  }
@@ -449,8 +406,7 @@ str_writer_pop(VALUE self) {
449
406
  * Pops all level in the JSON document closing all the array or object that is
450
407
  * currently open.
451
408
  */
452
- static VALUE
453
- str_writer_pop_all(VALUE self) {
409
+ static VALUE str_writer_pop_all(VALUE self) {
454
410
  oj_str_writer_pop_all((StrWriter)DATA_PTR(self));
455
411
 
456
412
  return Qnil;
@@ -461,15 +417,14 @@ str_writer_pop_all(VALUE self) {
461
417
  *
462
418
  * Reset the writer back to the empty state.
463
419
  */
464
- static VALUE
465
- str_writer_reset(VALUE self) {
466
- StrWriter sw = (StrWriter)DATA_PTR(self);
420
+ static VALUE str_writer_reset(VALUE self) {
421
+ StrWriter sw = (StrWriter)DATA_PTR(self);
467
422
 
468
- sw->depth = 0;
469
- *sw->types = '\0';
423
+ sw->depth = 0;
424
+ *sw->types = '\0';
470
425
  sw->keyWritten = 0;
471
- sw->out.cur = sw->out.buf;
472
- *sw->out.cur = '\0';
426
+ sw->out.cur = sw->out.buf;
427
+ *sw->out.cur = '\0';
473
428
 
474
429
  return Qnil;
475
430
  }
@@ -481,10 +436,9 @@ str_writer_reset(VALUE self) {
481
436
  *
482
437
  * *return* [_String_]
483
438
  */
484
- static VALUE
485
- str_writer_to_s(VALUE self) {
486
- StrWriter sw = (StrWriter)DATA_PTR(self);
487
- VALUE rstr = rb_str_new(sw->out.buf, sw->out.cur - sw->out.buf);
439
+ static VALUE str_writer_to_s(VALUE self) {
440
+ StrWriter sw = (StrWriter)DATA_PTR(self);
441
+ VALUE rstr = rb_str_new(sw->out.buf, sw->out.cur - sw->out.buf);
488
442
 
489
443
  return oj_encode(rstr);
490
444
  }
@@ -499,10 +453,9 @@ str_writer_to_s(VALUE self) {
499
453
  *
500
454
  * *return* [_Hash_|_Array_|_String_|_Integer_|_Float_|_True_|_False_|_nil|)
501
455
  */
502
- static VALUE
503
- str_writer_as_json(VALUE self) {
456
+ static VALUE str_writer_as_json(VALUE self) {
504
457
  if (string_writer_optimized) {
505
- return self;
458
+ return self;
506
459
  }
507
460
  return rb_hash_new();
508
461
  }
@@ -516,8 +469,7 @@ str_writer_as_json(VALUE self) {
516
469
  * calling to_s() will return the JSON document. Note tha calling to_s() before
517
470
  * construction is complete will return the document in it's current state.
518
471
  */
519
- void
520
- oj_string_writer_init() {
472
+ void oj_string_writer_init() {
521
473
  oj_string_writer_class = rb_define_class_under(Oj, "StringWriter", rb_cObject);
522
474
  rb_define_module_function(oj_string_writer_class, "new", str_writer_new, -1);
523
475
  rb_define_method(oj_string_writer_class, "push_key", str_writer_push_key, 1);