oj 3.10.18 → 3.11.4

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