oj 3.7.4 → 3.13.21

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1352 -0
  3. data/README.md +29 -8
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +53 -72
  6. data/ext/oj/cache.c +326 -0
  7. data/ext/oj/cache.h +21 -0
  8. data/ext/oj/cache8.c +61 -64
  9. data/ext/oj/cache8.h +12 -39
  10. data/ext/oj/circarray.c +37 -43
  11. data/ext/oj/circarray.h +16 -17
  12. data/ext/oj/code.c +165 -179
  13. data/ext/oj/code.h +27 -29
  14. data/ext/oj/compat.c +174 -194
  15. data/ext/oj/custom.c +809 -866
  16. data/ext/oj/debug.c +132 -0
  17. data/ext/oj/dump.c +848 -863
  18. data/ext/oj/dump.h +81 -67
  19. data/ext/oj/dump_compat.c +85 -123
  20. data/ext/oj/dump_leaf.c +100 -188
  21. data/ext/oj/dump_object.c +527 -656
  22. data/ext/oj/dump_strict.c +315 -338
  23. data/ext/oj/encode.h +7 -34
  24. data/ext/oj/encoder.c +43 -0
  25. data/ext/oj/err.c +40 -29
  26. data/ext/oj/err.h +48 -48
  27. data/ext/oj/extconf.rb +17 -4
  28. data/ext/oj/fast.c +1070 -1087
  29. data/ext/oj/intern.c +301 -0
  30. data/ext/oj/intern.h +26 -0
  31. data/ext/oj/mimic_json.c +469 -436
  32. data/ext/oj/object.c +525 -593
  33. data/ext/oj/odd.c +154 -138
  34. data/ext/oj/odd.h +37 -38
  35. data/ext/oj/oj.c +1325 -986
  36. data/ext/oj/oj.h +333 -316
  37. data/ext/oj/parse.c +1002 -846
  38. data/ext/oj/parse.h +92 -87
  39. data/ext/oj/parser.c +1557 -0
  40. data/ext/oj/parser.h +91 -0
  41. data/ext/oj/rails.c +888 -878
  42. data/ext/oj/rails.h +11 -14
  43. data/ext/oj/reader.c +141 -147
  44. data/ext/oj/reader.h +73 -89
  45. data/ext/oj/resolve.c +41 -62
  46. data/ext/oj/resolve.h +7 -9
  47. data/ext/oj/rxclass.c +71 -75
  48. data/ext/oj/rxclass.h +18 -19
  49. data/ext/oj/saj.c +443 -486
  50. data/ext/oj/saj2.c +602 -0
  51. data/ext/oj/scp.c +88 -113
  52. data/ext/oj/sparse.c +787 -709
  53. data/ext/oj/stream_writer.c +133 -159
  54. data/ext/oj/strict.c +127 -118
  55. data/ext/oj/string_writer.c +230 -249
  56. data/ext/oj/trace.c +34 -41
  57. data/ext/oj/trace.h +19 -19
  58. data/ext/oj/usual.c +1254 -0
  59. data/ext/oj/util.c +136 -0
  60. data/ext/oj/util.h +20 -0
  61. data/ext/oj/val_stack.c +59 -67
  62. data/ext/oj/val_stack.h +91 -129
  63. data/ext/oj/validate.c +46 -0
  64. data/ext/oj/wab.c +342 -353
  65. data/lib/oj/bag.rb +1 -0
  66. data/lib/oj/easy_hash.rb +5 -4
  67. data/lib/oj/error.rb +1 -1
  68. data/lib/oj/json.rb +1 -1
  69. data/lib/oj/mimic.rb +48 -14
  70. data/lib/oj/saj.rb +20 -6
  71. data/lib/oj/state.rb +8 -7
  72. data/lib/oj/version.rb +2 -2
  73. data/lib/oj.rb +0 -8
  74. data/pages/Compatibility.md +1 -1
  75. data/pages/JsonGem.md +15 -0
  76. data/pages/Modes.md +53 -46
  77. data/pages/Options.md +72 -11
  78. data/pages/Parser.md +309 -0
  79. data/pages/Rails.md +73 -22
  80. data/pages/Security.md +1 -1
  81. data/test/activerecord/result_test.rb +7 -2
  82. data/test/activesupport5/abstract_unit.rb +45 -0
  83. data/test/activesupport5/decoding_test.rb +68 -60
  84. data/test/activesupport5/encoding_test.rb +111 -96
  85. data/test/activesupport5/encoding_test_cases.rb +33 -25
  86. data/test/activesupport5/test_helper.rb +43 -21
  87. data/test/activesupport5/time_zone_test_helpers.rb +18 -3
  88. data/test/activesupport6/abstract_unit.rb +44 -0
  89. data/test/activesupport6/decoding_test.rb +133 -0
  90. data/test/activesupport6/encoding_test.rb +507 -0
  91. data/test/activesupport6/encoding_test_cases.rb +98 -0
  92. data/test/activesupport6/test_common.rb +17 -0
  93. data/test/activesupport6/test_helper.rb +163 -0
  94. data/test/activesupport6/time_zone_test_helpers.rb +39 -0
  95. data/test/activesupport7/abstract_unit.rb +49 -0
  96. data/test/activesupport7/decoding_test.rb +125 -0
  97. data/test/activesupport7/encoding_test.rb +486 -0
  98. data/test/activesupport7/encoding_test_cases.rb +104 -0
  99. data/test/activesupport7/time_zone_test_helpers.rb +47 -0
  100. data/test/bar.rb +6 -12
  101. data/test/baz.rb +16 -0
  102. data/test/bug.rb +16 -0
  103. data/test/foo.rb +69 -75
  104. data/test/helper.rb +16 -0
  105. data/test/json_gem/json_common_interface_test.rb +8 -3
  106. data/test/json_gem/json_generator_test.rb +18 -4
  107. data/test/json_gem/json_parser_test.rb +9 -0
  108. data/test/json_gem/test_helper.rb +12 -0
  109. data/test/mem.rb +33 -0
  110. data/test/perf.rb +1 -1
  111. data/test/perf_dump.rb +50 -0
  112. data/test/perf_once.rb +58 -0
  113. data/test/perf_parser.rb +189 -0
  114. data/test/perf_scp.rb +11 -10
  115. data/test/perf_strict.rb +17 -23
  116. data/test/prec.rb +23 -0
  117. data/test/sample_json.rb +1 -1
  118. data/test/test_compat.rb +46 -10
  119. data/test/test_custom.rb +147 -8
  120. data/test/test_fast.rb +62 -2
  121. data/test/test_file.rb +25 -2
  122. data/test/test_gc.rb +13 -0
  123. data/test/test_generate.rb +21 -0
  124. data/test/test_hash.rb +11 -1
  125. data/test/test_integer_range.rb +7 -2
  126. data/test/test_object.rb +85 -9
  127. data/test/test_parser.rb +27 -0
  128. data/test/test_parser_saj.rb +335 -0
  129. data/test/test_parser_usual.rb +217 -0
  130. data/test/test_rails.rb +35 -0
  131. data/test/test_saj.rb +1 -1
  132. data/test/test_scp.rb +5 -5
  133. data/test/test_strict.rb +26 -1
  134. data/test/test_various.rb +87 -65
  135. data/test/test_wab.rb +2 -0
  136. data/test/test_writer.rb +19 -2
  137. data/test/tests.rb +1 -1
  138. data/test/zoo.rb +13 -0
  139. metadata +60 -110
  140. data/ext/oj/hash.c +0 -163
  141. data/ext/oj/hash.h +0 -46
  142. data/ext/oj/hash_test.c +0 -512
@@ -1,22 +1,18 @@
1
- /* stream_writer.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
-
8
5
  #include <ruby.h>
9
6
 
10
- #include "oj.h"
7
+ #include "encode.h"
11
8
 
12
- extern VALUE Oj;
9
+ extern VALUE Oj;
13
10
 
14
- static void
15
- stream_writer_free(void *ptr) {
16
- StreamWriter sw;
11
+ static void stream_writer_free(void *ptr) {
12
+ StreamWriter sw;
17
13
 
18
14
  if (0 == ptr) {
19
- return;
15
+ return;
20
16
  }
21
17
  sw = (StreamWriter)ptr;
22
18
  xfree(sw->sw.out.buf);
@@ -24,42 +20,45 @@ stream_writer_free(void *ptr) {
24
20
  xfree(ptr);
25
21
  }
26
22
 
27
- static void
28
- stream_writer_reset_buf(StreamWriter sw) {
29
- sw->sw.out.cur = sw->sw.out.buf;
23
+ static void stream_writer_reset_buf(StreamWriter sw) {
24
+ sw->sw.out.cur = sw->sw.out.buf;
30
25
  *sw->sw.out.cur = '\0';
31
26
  }
32
27
 
33
- static void
34
- stream_writer_write(StreamWriter sw) {
35
- ssize_t size = sw->sw.out.cur - sw->sw.out.buf;
28
+ static void stream_writer_write(StreamWriter sw) {
29
+ ssize_t size = sw->sw.out.cur - sw->sw.out.buf;
36
30
 
37
31
  switch (sw->type) {
38
32
  case STRING_IO:
39
- rb_funcall(sw->stream, oj_write_id, 1, rb_str_new(sw->sw.out.buf, size));
40
- break;
41
- case STREAM_IO:
42
- rb_funcall(sw->stream, oj_write_id, 1, rb_str_new(sw->sw.out.buf, size));
43
- break;
33
+ case STREAM_IO: {
34
+ volatile VALUE rs = rb_str_new(sw->sw.out.buf, size);
35
+
36
+ // Oddly enough, when pushing ASCII characters with UTF-8 encoding or
37
+ // even ASCII-8BIT does not change the output encoding. Pushing any
38
+ // non-ASCII no matter what the encoding changes the output encoding
39
+ // to ASCII-8BIT if it the string is not forced to UTF-8 here.
40
+ rs = oj_encode(rs);
41
+ rb_funcall(sw->stream, oj_write_id, 1, rs);
42
+ break;
43
+ }
44
44
  case FILE_IO:
45
- if (size != write(sw->fd, sw->sw.out.buf, size)) {
46
- rb_raise(rb_eIOError, "Write failed. [_%d_:%s]\n", errno, strerror(errno));
47
- }
48
- break;
49
- default:
50
- rb_raise(rb_eArgError, "expected an IO Object.");
45
+ if (size != write(sw->fd, sw->sw.out.buf, size)) {
46
+ rb_raise(rb_eIOError, "Write failed. [_%d_:%s]\n", errno, strerror(errno));
47
+ }
48
+ break;
49
+ default: rb_raise(rb_eArgError, "expected an IO Object.");
51
50
  }
52
51
  stream_writer_reset_buf(sw);
53
52
  }
54
53
 
55
- static VALUE buffer_size_sym = Qundef;
54
+ static VALUE buffer_size_sym = Qundef;
56
55
 
57
56
  /* Document-method: new
58
57
  * call-seq: new(io, options)
59
58
  *
60
- * Creates a new StreamWriter. Options are supported according the the
61
- * specified mode or the mode in the default options. Note that if mimic_JSON
62
- * or Oj.optimize_rails has not been called then the behavior of the modes may
59
+ * Creates a new StreamWriter. Options are supported according the specified
60
+ * mode or the mode in the default options. Note that if mimic_JSON or
61
+ * Oj.optimize_rails has not been called then the behavior of the modes may
63
62
  * not be the same as if they were.
64
63
  *
65
64
  * In addition to the regular dump options for the various modes a
@@ -68,64 +67,62 @@ static VALUE buffer_size_sym = Qundef;
68
67
  * should be and also a hint on when to flush.
69
68
  *
70
69
  * - *io* [_IO_] stream to write to
71
- * - *options* [_Hash_] formating options
70
+ * - *options* [_Hash_] formatting options
72
71
  */
73
- static VALUE
74
- stream_writer_new(int argc, VALUE *argv, VALUE self) {
75
- StreamWriterType type = STREAM_IO;
76
- int fd = 0;
77
- VALUE stream = argv[0];
78
- VALUE clas = rb_obj_class(stream);
79
- StreamWriter sw;
72
+ static VALUE stream_writer_new(int argc, VALUE *argv, VALUE self) {
73
+ StreamWriterType type = STREAM_IO;
74
+ int fd = 0;
75
+ VALUE stream = argv[0];
76
+ VALUE clas = rb_obj_class(stream);
77
+ StreamWriter sw;
80
78
  #if !IS_WINDOWS
81
- VALUE s;
79
+ VALUE s;
82
80
  #endif
83
-
81
+
84
82
  if (oj_stringio_class == clas) {
85
- type = STRING_IO;
83
+ type = STRING_IO;
86
84
  #if !IS_WINDOWS
87
85
  } else if (rb_respond_to(stream, oj_fileno_id) &&
88
- Qnil != (s = rb_funcall(stream, oj_fileno_id, 0)) &&
89
- 0 != (fd = FIX2INT(s))) {
90
- type = FILE_IO;
86
+ Qnil != (s = rb_funcall(stream, oj_fileno_id, 0)) && 0 != (fd = FIX2INT(s))) {
87
+ type = FILE_IO;
91
88
  #endif
92
89
  } else if (rb_respond_to(stream, oj_write_id)) {
93
- type = STREAM_IO;
90
+ type = STREAM_IO;
94
91
  } else {
95
- rb_raise(rb_eArgError, "expected an IO Object.");
92
+ rb_raise(rb_eArgError, "expected an IO Object.");
96
93
  }
97
- sw = ALLOC(struct _StreamWriter);
94
+ sw = ALLOC(struct _streamWriter);
98
95
  if (2 == argc && T_HASH == rb_type(argv[1])) {
99
- volatile VALUE v;
100
- int buf_size = 0;
101
-
102
- if (Qundef == buffer_size_sym) {
103
- buffer_size_sym = ID2SYM(rb_intern("buffer_size")); rb_gc_register_address(&buffer_size_sym);
104
-
105
- }
106
- if (Qnil != (v = rb_hash_lookup(argv[1], buffer_size_sym))) {
96
+ volatile VALUE v;
97
+ int buf_size = 0;
98
+
99
+ if (Qundef == buffer_size_sym) {
100
+ buffer_size_sym = ID2SYM(rb_intern("buffer_size"));
101
+ rb_gc_register_address(&buffer_size_sym);
102
+ }
103
+ if (Qnil != (v = rb_hash_lookup(argv[1], buffer_size_sym))) {
107
104
  #ifdef RUBY_INTEGER_UNIFICATION
108
- if (rb_cInteger != rb_obj_class(v)) {
109
- rb_raise(rb_eArgError, ":buffer size must be a Integer.");
110
- }
105
+ if (rb_cInteger != rb_obj_class(v)) {
106
+ rb_raise(rb_eArgError, ":buffer size must be a Integer.");
107
+ }
111
108
  #else
112
- if (T_FIXNUM != rb_type(v)) {
113
- rb_raise(rb_eArgError, ":buffer size must be a Integer.");
114
- }
109
+ if (T_FIXNUM != rb_type(v)) {
110
+ rb_raise(rb_eArgError, ":buffer size must be a Integer.");
111
+ }
115
112
  #endif
116
- buf_size = FIX2INT(v);
117
- }
118
- oj_str_writer_init(&sw->sw, buf_size);
119
- oj_parse_options(argv[1], &sw->sw.opts);
120
- sw->flush_limit = buf_size;
113
+ buf_size = FIX2INT(v);
114
+ }
115
+ oj_str_writer_init(&sw->sw, buf_size);
116
+ oj_parse_options(argv[1], &sw->sw.opts);
117
+ sw->flush_limit = buf_size;
121
118
  } else {
122
- oj_str_writer_init(&sw->sw, 4096);
123
- sw->flush_limit = 0;
119
+ oj_str_writer_init(&sw->sw, 4096);
120
+ sw->flush_limit = 0;
124
121
  }
125
122
  sw->sw.out.indent = sw->sw.opts.indent;
126
- sw->stream = stream;
127
- sw->type = type;
128
- sw->fd = fd;
123
+ sw->stream = stream;
124
+ sw->type = type;
125
+ sw->fd = fd;
129
126
 
130
127
  return Data_Wrap_Struct(oj_stream_writer_class, 0, stream_writer_free, sw);
131
128
  }
@@ -139,14 +136,13 @@ stream_writer_new(int argc, VALUE *argv, VALUE self) {
139
136
  *
140
137
  * - *key* [_String_] the key pending for the next push
141
138
  */
142
- static VALUE
143
- stream_writer_push_key(VALUE self, VALUE key) {
144
- StreamWriter sw = (StreamWriter)DATA_PTR(self);
139
+ static VALUE stream_writer_push_key(VALUE self, VALUE key) {
140
+ StreamWriter sw = (StreamWriter)DATA_PTR(self);
145
141
 
146
142
  rb_check_type(key, T_STRING);
147
143
  oj_str_writer_push_key(&sw->sw, StringValuePtr(key));
148
144
  if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
149
- stream_writer_write(sw);
145
+ stream_writer_write(sw);
150
146
  }
151
147
  return Qnil;
152
148
  }
@@ -159,28 +155,23 @@ stream_writer_push_key(VALUE self, VALUE key) {
159
155
  *
160
156
  * - *key* [_String_] the key if adding to an object in the JSON document
161
157
  */
162
- static VALUE
163
- stream_writer_push_object(int argc, VALUE *argv, VALUE self) {
164
- StreamWriter sw = (StreamWriter)DATA_PTR(self);
158
+ static VALUE stream_writer_push_object(int argc, VALUE *argv, VALUE self) {
159
+ StreamWriter sw = (StreamWriter)DATA_PTR(self);
165
160
 
166
161
  switch (argc) {
167
- case 0:
168
- oj_str_writer_push_object(&sw->sw, 0);
169
- break;
162
+ case 0: oj_str_writer_push_object(&sw->sw, 0); break;
170
163
  case 1:
171
- if (Qnil == argv[0]) {
172
- oj_str_writer_push_object(&sw->sw, 0);
173
- } else {
174
- rb_check_type(argv[0], T_STRING);
175
- oj_str_writer_push_object(&sw->sw, StringValuePtr(argv[0]));
176
- }
177
- break;
178
- default:
179
- rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'.");
180
- break;
164
+ if (Qnil == argv[0]) {
165
+ oj_str_writer_push_object(&sw->sw, 0);
166
+ } else {
167
+ rb_check_type(argv[0], T_STRING);
168
+ oj_str_writer_push_object(&sw->sw, StringValuePtr(argv[0]));
169
+ }
170
+ break;
171
+ default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'."); break;
181
172
  }
182
173
  if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
183
- stream_writer_write(sw);
174
+ stream_writer_write(sw);
184
175
  }
185
176
  return Qnil;
186
177
  }
@@ -193,28 +184,23 @@ stream_writer_push_object(int argc, VALUE *argv, VALUE self) {
193
184
  *
194
185
  * - *key* [_String_] the key if adding to an object in the JSON document
195
186
  */
196
- static VALUE
197
- stream_writer_push_array(int argc, VALUE *argv, VALUE self) {
198
- StreamWriter sw = (StreamWriter)DATA_PTR(self);
187
+ static VALUE stream_writer_push_array(int argc, VALUE *argv, VALUE self) {
188
+ StreamWriter sw = (StreamWriter)DATA_PTR(self);
199
189
 
200
190
  switch (argc) {
201
- case 0:
202
- oj_str_writer_push_array(&sw->sw, 0);
203
- break;
191
+ case 0: oj_str_writer_push_array(&sw->sw, 0); break;
204
192
  case 1:
205
- if (Qnil == argv[0]) {
206
- oj_str_writer_push_array(&sw->sw, 0);
207
- } else {
208
- rb_check_type(argv[0], T_STRING);
209
- oj_str_writer_push_array(&sw->sw, StringValuePtr(argv[0]));
210
- }
211
- break;
212
- default:
213
- rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'.");
214
- break;
193
+ if (Qnil == argv[0]) {
194
+ oj_str_writer_push_array(&sw->sw, 0);
195
+ } else {
196
+ rb_check_type(argv[0], T_STRING);
197
+ oj_str_writer_push_array(&sw->sw, StringValuePtr(argv[0]));
198
+ }
199
+ break;
200
+ default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'."); break;
215
201
  }
216
202
  if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
217
- stream_writer_write(sw);
203
+ stream_writer_write(sw);
218
204
  }
219
205
  return Qnil;
220
206
  }
@@ -226,28 +212,23 @@ stream_writer_push_array(int argc, VALUE *argv, VALUE self) {
226
212
  * - *value* [_Object_] value to add to the JSON document
227
213
  * - *key* [_String_] the key if adding to an object in the JSON document
228
214
  */
229
- static VALUE
230
- stream_writer_push_value(int argc, VALUE *argv, VALUE self) {
231
- StreamWriter sw = (StreamWriter)DATA_PTR(self);
215
+ static VALUE stream_writer_push_value(int argc, VALUE *argv, VALUE self) {
216
+ StreamWriter sw = (StreamWriter)DATA_PTR(self);
232
217
 
233
218
  switch (argc) {
234
- case 1:
235
- oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, 0);
236
- break;
219
+ case 1: oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, 0); break;
237
220
  case 2:
238
- if (Qnil == argv[1]) {
239
- oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, 0);
240
- } else {
241
- rb_check_type(argv[1], T_STRING);
242
- oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, StringValuePtr(argv[1]));
243
- }
244
- break;
245
- default:
246
- rb_raise(rb_eArgError, "Wrong number of argument to 'push_value'.");
247
- break;
221
+ if (Qnil == argv[1]) {
222
+ oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, 0);
223
+ } else {
224
+ rb_check_type(argv[1], T_STRING);
225
+ oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, StringValuePtr(argv[1]));
226
+ }
227
+ break;
228
+ default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_value'."); break;
248
229
  }
249
230
  if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
250
- stream_writer_write(sw);
231
+ stream_writer_write(sw);
251
232
  }
252
233
  return Qnil;
253
234
  }
@@ -261,29 +242,26 @@ stream_writer_push_value(int argc, VALUE *argv, VALUE self) {
261
242
  * - *value* [_Object_] value to add to the JSON document
262
243
  * - *key* [_String_] the key if adding to an object in the JSON document
263
244
  */
264
- static VALUE
265
- stream_writer_push_json(int argc, VALUE *argv, VALUE self) {
266
- StreamWriter sw = (StreamWriter)DATA_PTR(self);
245
+ static VALUE stream_writer_push_json(int argc, VALUE *argv, VALUE self) {
246
+ StreamWriter sw = (StreamWriter)DATA_PTR(self);
267
247
 
268
248
  rb_check_type(argv[0], T_STRING);
269
249
  switch (argc) {
270
- case 1:
271
- oj_str_writer_push_json((StrWriter)DATA_PTR(self), StringValuePtr(*argv), 0);
272
- break;
250
+ case 1: oj_str_writer_push_json((StrWriter)DATA_PTR(self), StringValuePtr(*argv), 0); break;
273
251
  case 2:
274
- if (Qnil == argv[1]) {
275
- oj_str_writer_push_json((StrWriter)DATA_PTR(self), StringValuePtr(*argv), 0);
276
- } else {
277
- rb_check_type(argv[1], T_STRING);
278
- oj_str_writer_push_json((StrWriter)DATA_PTR(self), StringValuePtr(*argv), StringValuePtr(argv[1]));
279
- }
280
- break;
281
- default:
282
- rb_raise(rb_eArgError, "Wrong number of argument to 'push_json'.");
283
- break;
252
+ if (Qnil == argv[1]) {
253
+ oj_str_writer_push_json((StrWriter)DATA_PTR(self), StringValuePtr(*argv), 0);
254
+ } else {
255
+ rb_check_type(argv[1], T_STRING);
256
+ oj_str_writer_push_json((StrWriter)DATA_PTR(self),
257
+ StringValuePtr(*argv),
258
+ StringValuePtr(argv[1]));
259
+ }
260
+ break;
261
+ default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_json'."); break;
284
262
  }
285
263
  if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
286
- stream_writer_write(sw);
264
+ stream_writer_write(sw);
287
265
  }
288
266
  return Qnil;
289
267
  }
@@ -294,13 +272,12 @@ stream_writer_push_json(int argc, VALUE *argv, VALUE self) {
294
272
  * Pops up a level in the JSON document closing the array or object that is
295
273
  * currently open.
296
274
  */
297
- static VALUE
298
- stream_writer_pop(VALUE self) {
299
- StreamWriter sw = (StreamWriter)DATA_PTR(self);
275
+ static VALUE stream_writer_pop(VALUE self) {
276
+ StreamWriter sw = (StreamWriter)DATA_PTR(self);
300
277
 
301
278
  oj_str_writer_pop(&sw->sw);
302
279
  if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
303
- stream_writer_write(sw);
280
+ stream_writer_write(sw);
304
281
  }
305
282
  return Qnil;
306
283
  }
@@ -311,9 +288,8 @@ stream_writer_pop(VALUE self) {
311
288
  * Pops all level in the JSON document closing all the array or object that is
312
289
  * currently open.
313
290
  */
314
- static VALUE
315
- stream_writer_pop_all(VALUE self) {
316
- StreamWriter sw = (StreamWriter)DATA_PTR(self);
291
+ static VALUE stream_writer_pop_all(VALUE self) {
292
+ StreamWriter sw = (StreamWriter)DATA_PTR(self);
317
293
 
318
294
  oj_str_writer_pop_all(&sw->sw);
319
295
  stream_writer_write(sw);
@@ -326,23 +302,23 @@ stream_writer_pop_all(VALUE self) {
326
302
  *
327
303
  * Flush any remaining characters in the buffer.
328
304
  */
329
- static VALUE
330
- stream_writer_flush(VALUE self) {
305
+ static VALUE stream_writer_flush(VALUE self) {
331
306
  stream_writer_write((StreamWriter)DATA_PTR(self));
332
307
 
333
308
  return Qnil;
334
309
  }
335
310
 
336
311
  /* Document-class: Oj::StreamWriter
337
- *
312
+ *
338
313
  * Supports building a JSON document one element at a time. Build the IO stream
339
314
  * document by pushing values into the document. Pushing an array or an object
340
315
  * will create that element in the JSON document and subsequent pushes will add
341
316
  * the elements to that array or object until a pop() is called.
342
317
  */
343
- void
344
- oj_stream_writer_init() {
318
+ void oj_stream_writer_init(void) {
345
319
  oj_stream_writer_class = rb_define_class_under(Oj, "StreamWriter", rb_cObject);
320
+ rb_gc_register_address(&oj_stream_writer_class);
321
+ rb_undef_alloc_func(oj_stream_writer_class);
346
322
  rb_define_module_function(oj_stream_writer_class, "new", stream_writer_new, -1);
347
323
  rb_define_method(oj_stream_writer_class, "push_key", stream_writer_push_key, 1);
348
324
  rb_define_method(oj_stream_writer_class, "push_object", stream_writer_push_object, -1);
@@ -353,5 +329,3 @@ oj_stream_writer_init() {
353
329
  rb_define_method(oj_stream_writer_class, "pop_all", stream_writer_pop_all, 0);
354
330
  rb_define_method(oj_stream_writer_class, "flush", stream_writer_flush, 0);
355
331
  }
356
-
357
-