oj 3.9.1 → 3.16.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (171) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1452 -0
  3. data/README.md +21 -6
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +50 -68
  6. data/ext/oj/cache.c +329 -0
  7. data/ext/oj/cache.h +22 -0
  8. data/ext/oj/cache8.c +60 -62
  9. data/ext/oj/cache8.h +9 -36
  10. data/ext/oj/circarray.c +38 -42
  11. data/ext/oj/circarray.h +12 -13
  12. data/ext/oj/code.c +158 -179
  13. data/ext/oj/code.h +20 -22
  14. data/ext/oj/compat.c +145 -205
  15. data/ext/oj/custom.c +740 -880
  16. data/ext/oj/debug.c +126 -0
  17. data/ext/oj/dump.c +1145 -844
  18. data/ext/oj/dump.h +71 -57
  19. data/ext/oj/dump_compat.c +575 -655
  20. data/ext/oj/dump_leaf.c +96 -186
  21. data/ext/oj/dump_object.c +533 -660
  22. data/ext/oj/dump_strict.c +306 -340
  23. data/ext/oj/encode.h +4 -33
  24. data/ext/oj/encoder.c +43 -0
  25. data/ext/oj/err.c +28 -28
  26. data/ext/oj/err.h +39 -42
  27. data/ext/oj/extconf.rb +28 -7
  28. data/ext/oj/fast.c +1052 -1113
  29. data/ext/oj/intern.c +313 -0
  30. data/ext/oj/intern.h +22 -0
  31. data/ext/oj/mem.c +318 -0
  32. data/ext/oj/mem.h +53 -0
  33. data/ext/oj/mimic_json.c +471 -430
  34. data/ext/oj/object.c +532 -580
  35. data/ext/oj/odd.c +156 -142
  36. data/ext/oj/odd.h +25 -26
  37. data/ext/oj/oj.c +1346 -961
  38. data/ext/oj/oj.h +307 -290
  39. data/ext/oj/parse.c +954 -858
  40. data/ext/oj/parse.h +74 -72
  41. data/ext/oj/parser.c +1600 -0
  42. data/ext/oj/parser.h +103 -0
  43. data/ext/oj/rails.c +819 -836
  44. data/ext/oj/rails.h +8 -11
  45. data/ext/oj/reader.c +136 -147
  46. data/ext/oj/reader.h +69 -83
  47. data/ext/oj/resolve.c +41 -63
  48. data/ext/oj/resolve.h +4 -6
  49. data/ext/oj/rxclass.c +69 -72
  50. data/ext/oj/rxclass.h +12 -13
  51. data/ext/oj/saj.c +440 -485
  52. data/ext/oj/saj2.c +584 -0
  53. data/ext/oj/saj2.h +23 -0
  54. data/ext/oj/scp.c +79 -118
  55. data/ext/oj/simd.h +10 -0
  56. data/ext/oj/sparse.c +739 -709
  57. data/ext/oj/stream_writer.c +141 -175
  58. data/ext/oj/strict.c +103 -128
  59. data/ext/oj/string_writer.c +244 -261
  60. data/ext/oj/trace.c +34 -41
  61. data/ext/oj/trace.h +42 -15
  62. data/ext/oj/usual.c +1218 -0
  63. data/ext/oj/usual.h +69 -0
  64. data/ext/oj/util.c +107 -107
  65. data/ext/oj/util.h +4 -3
  66. data/ext/oj/val_stack.c +61 -78
  67. data/ext/oj/val_stack.h +80 -114
  68. data/ext/oj/validate.c +46 -0
  69. data/ext/oj/wab.c +316 -361
  70. data/lib/oj/active_support_helper.rb +1 -3
  71. data/lib/oj/bag.rb +8 -1
  72. data/lib/oj/easy_hash.rb +9 -9
  73. data/lib/oj/error.rb +1 -2
  74. data/lib/oj/json.rb +162 -150
  75. data/lib/oj/mimic.rb +54 -20
  76. data/lib/oj/saj.rb +20 -6
  77. data/lib/oj/schandler.rb +5 -4
  78. data/lib/oj/state.rb +12 -8
  79. data/lib/oj/version.rb +1 -2
  80. data/lib/oj.rb +2 -8
  81. data/pages/Compatibility.md +1 -1
  82. data/pages/Encoding.md +1 -1
  83. data/pages/InstallOptions.md +20 -0
  84. data/pages/JsonGem.md +15 -0
  85. data/pages/Modes.md +9 -3
  86. data/pages/Options.md +62 -12
  87. data/pages/Parser.md +309 -0
  88. data/pages/Rails.md +73 -22
  89. metadata +68 -192
  90. data/ext/oj/hash.c +0 -163
  91. data/ext/oj/hash.h +0 -46
  92. data/ext/oj/hash_test.c +0 -512
  93. data/test/_test_active.rb +0 -76
  94. data/test/_test_active_mimic.rb +0 -96
  95. data/test/_test_mimic_rails.rb +0 -126
  96. data/test/activerecord/result_test.rb +0 -27
  97. data/test/activesupport4/decoding_test.rb +0 -108
  98. data/test/activesupport4/encoding_test.rb +0 -531
  99. data/test/activesupport4/test_helper.rb +0 -41
  100. data/test/activesupport5/decoding_test.rb +0 -125
  101. data/test/activesupport5/encoding_test.rb +0 -485
  102. data/test/activesupport5/encoding_test_cases.rb +0 -90
  103. data/test/activesupport5/test_helper.rb +0 -50
  104. data/test/activesupport5/time_zone_test_helpers.rb +0 -24
  105. data/test/bar.rb +0 -25
  106. data/test/files.rb +0 -29
  107. data/test/foo.rb +0 -21
  108. data/test/helper.rb +0 -26
  109. data/test/isolated/shared.rb +0 -308
  110. data/test/isolated/test_mimic_after.rb +0 -13
  111. data/test/isolated/test_mimic_alone.rb +0 -12
  112. data/test/isolated/test_mimic_as_json.rb +0 -45
  113. data/test/isolated/test_mimic_before.rb +0 -13
  114. data/test/isolated/test_mimic_define.rb +0 -28
  115. data/test/isolated/test_mimic_rails_after.rb +0 -22
  116. data/test/isolated/test_mimic_rails_before.rb +0 -21
  117. data/test/isolated/test_mimic_redefine.rb +0 -15
  118. data/test/json_gem/json_addition_test.rb +0 -216
  119. data/test/json_gem/json_common_interface_test.rb +0 -148
  120. data/test/json_gem/json_encoding_test.rb +0 -107
  121. data/test/json_gem/json_ext_parser_test.rb +0 -20
  122. data/test/json_gem/json_fixtures_test.rb +0 -35
  123. data/test/json_gem/json_generator_test.rb +0 -383
  124. data/test/json_gem/json_generic_object_test.rb +0 -90
  125. data/test/json_gem/json_parser_test.rb +0 -470
  126. data/test/json_gem/json_string_matching_test.rb +0 -42
  127. data/test/json_gem/test_helper.rb +0 -18
  128. data/test/perf.rb +0 -107
  129. data/test/perf_compat.rb +0 -130
  130. data/test/perf_fast.rb +0 -164
  131. data/test/perf_file.rb +0 -64
  132. data/test/perf_object.rb +0 -138
  133. data/test/perf_saj.rb +0 -109
  134. data/test/perf_scp.rb +0 -151
  135. data/test/perf_simple.rb +0 -287
  136. data/test/perf_strict.rb +0 -145
  137. data/test/perf_wab.rb +0 -131
  138. data/test/sample/change.rb +0 -14
  139. data/test/sample/dir.rb +0 -19
  140. data/test/sample/doc.rb +0 -36
  141. data/test/sample/file.rb +0 -48
  142. data/test/sample/group.rb +0 -16
  143. data/test/sample/hasprops.rb +0 -16
  144. data/test/sample/layer.rb +0 -12
  145. data/test/sample/line.rb +0 -20
  146. data/test/sample/oval.rb +0 -10
  147. data/test/sample/rect.rb +0 -10
  148. data/test/sample/shape.rb +0 -35
  149. data/test/sample/text.rb +0 -20
  150. data/test/sample.rb +0 -54
  151. data/test/sample_json.rb +0 -37
  152. data/test/test_compat.rb +0 -509
  153. data/test/test_custom.rb +0 -503
  154. data/test/test_debian.rb +0 -53
  155. data/test/test_fast.rb +0 -470
  156. data/test/test_file.rb +0 -239
  157. data/test/test_gc.rb +0 -49
  158. data/test/test_hash.rb +0 -29
  159. data/test/test_integer_range.rb +0 -73
  160. data/test/test_null.rb +0 -376
  161. data/test/test_object.rb +0 -1018
  162. data/test/test_saj.rb +0 -186
  163. data/test/test_scp.rb +0 -433
  164. data/test/test_strict.rb +0 -410
  165. data/test/test_various.rb +0 -741
  166. data/test/test_wab.rb +0 -307
  167. data/test/test_writer.rb +0 -380
  168. data/test/tests.rb +0 -24
  169. data/test/tests_mimic.rb +0 -14
  170. data/test/tests_mimic_addition.rb +0 -7
  171. data/test/zoo.rb +0 -13
@@ -1,71 +1,66 @@
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
7
  #include "encode.h"
8
+ #include "mem.h"
11
9
 
12
- extern VALUE Oj;
10
+ extern VALUE Oj;
13
11
 
14
- static void
15
- stream_writer_free(void *ptr) {
16
- StreamWriter sw;
12
+ static void stream_writer_free(void *ptr) {
13
+ StreamWriter sw;
17
14
 
18
15
  if (0 == ptr) {
19
- return;
16
+ return;
20
17
  }
21
18
  sw = (StreamWriter)ptr;
22
- xfree(sw->sw.out.buf);
23
- xfree(sw->sw.types);
24
- xfree(ptr);
19
+ OJ_R_FREE(sw->sw.out.buf);
20
+ OJ_R_FREE(sw->sw.types);
21
+ OJ_R_FREE(ptr);
25
22
  }
26
23
 
27
- static void
28
- stream_writer_reset_buf(StreamWriter sw) {
29
- sw->sw.out.cur = sw->sw.out.buf;
24
+ static const rb_data_type_t oj_stream_writer_type = {
25
+ "Oj/stream_writer",
26
+ {
27
+ NULL,
28
+ stream_writer_free,
29
+ NULL,
30
+ },
31
+ 0,
32
+ 0,
33
+ };
34
+
35
+ static void stream_writer_reset_buf(StreamWriter sw) {
36
+ sw->sw.out.cur = sw->sw.out.buf;
30
37
  *sw->sw.out.cur = '\0';
31
38
  }
32
39
 
33
- static void
34
- stream_writer_write(StreamWriter sw) {
35
- ssize_t size = sw->sw.out.cur - sw->sw.out.buf;
40
+ static void stream_writer_write(StreamWriter sw) {
41
+ ssize_t size = sw->sw.out.cur - sw->sw.out.buf;
36
42
 
37
43
  switch (sw->type) {
38
44
  case STRING_IO:
39
- case STREAM_IO: {
40
- volatile VALUE rs = rb_str_new(sw->sw.out.buf, size);
41
-
42
- // Oddly enough, when pushing ASCII characters with UTF-8 encoding or
43
- // even ASCII-8BIT does not change the output encoding. Pushing any
44
- // non-ASCII no matter what the encoding changes the output encoding
45
- // to ASCII-8BIT if it the string is not forced to UTF-8 here.
46
- rs = oj_encode(rs);
47
- rb_funcall(sw->stream, oj_write_id, 1, rs);
48
- break;
45
+ case STREAM_IO:
46
+ case FILE_IO: {
47
+ volatile VALUE rs = rb_utf8_str_new(sw->sw.out.buf, size);
48
+ rb_funcall(sw->stream, oj_write_id, 1, rs);
49
+ break;
49
50
  }
50
- case FILE_IO:
51
- if (size != write(sw->fd, sw->sw.out.buf, size)) {
52
- rb_raise(rb_eIOError, "Write failed. [_%d_:%s]\n", errno, strerror(errno));
53
- }
54
- break;
55
- default:
56
- rb_raise(rb_eArgError, "expected an IO Object.");
51
+ default: rb_raise(rb_eArgError, "expected an IO Object.");
57
52
  }
58
53
  stream_writer_reset_buf(sw);
59
54
  }
60
55
 
61
- static VALUE buffer_size_sym = Qundef;
56
+ static VALUE buffer_size_sym = Qundef;
62
57
 
63
58
  /* Document-method: new
64
59
  * call-seq: new(io, options)
65
60
  *
66
- * Creates a new StreamWriter. Options are supported according the the
67
- * specified mode or the mode in the default options. Note that if mimic_JSON
68
- * or Oj.optimize_rails has not been called then the behavior of the modes may
61
+ * Creates a new StreamWriter. Options are supported according the specified
62
+ * mode or the mode in the default options. Note that if mimic_JSON or
63
+ * Oj.optimize_rails has not been called then the behavior of the modes may
69
64
  * not be the same as if they were.
70
65
  *
71
66
  * In addition to the regular dump options for the various modes a
@@ -74,66 +69,59 @@ static VALUE buffer_size_sym = Qundef;
74
69
  * should be and also a hint on when to flush.
75
70
  *
76
71
  * - *io* [_IO_] stream to write to
77
- * - *options* [_Hash_] formating options
72
+ * - *options* [_Hash_] formatting options
78
73
  */
79
- static VALUE
80
- stream_writer_new(int argc, VALUE *argv, VALUE self) {
81
- StreamWriterType type = STREAM_IO;
82
- int fd = 0;
83
- VALUE stream = argv[0];
84
- VALUE clas = rb_obj_class(stream);
85
- StreamWriter sw;
74
+ static VALUE 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;
86
80
  #if !IS_WINDOWS
87
- VALUE s;
81
+ VALUE s;
88
82
  #endif
89
-
83
+
90
84
  if (oj_stringio_class == clas) {
91
- type = STRING_IO;
85
+ type = STRING_IO;
92
86
  #if !IS_WINDOWS
93
- } else if (rb_respond_to(stream, oj_fileno_id) &&
94
- Qnil != (s = rb_funcall(stream, oj_fileno_id, 0)) &&
95
- 0 != (fd = FIX2INT(s))) {
96
- type = FILE_IO;
87
+ } else if (rb_respond_to(stream, oj_fileno_id) && Qnil != (s = rb_funcall(stream, oj_fileno_id, 0)) &&
88
+ 0 != (fd = FIX2INT(s))) {
89
+ type = FILE_IO;
97
90
  #endif
98
91
  } else if (rb_respond_to(stream, oj_write_id)) {
99
- type = STREAM_IO;
92
+ type = STREAM_IO;
100
93
  } else {
101
- rb_raise(rb_eArgError, "expected an IO Object.");
94
+ rb_raise(rb_eArgError, "expected an IO Object.");
102
95
  }
103
- sw = ALLOC(struct _streamWriter);
96
+ sw = OJ_R_ALLOC(struct _streamWriter);
104
97
  if (2 == argc && T_HASH == rb_type(argv[1])) {
105
- volatile VALUE v;
106
- int buf_size = 0;
107
-
108
- if (Qundef == buffer_size_sym) {
109
- buffer_size_sym = ID2SYM(rb_intern("buffer_size")); rb_gc_register_address(&buffer_size_sym);
110
-
111
- }
112
- if (Qnil != (v = rb_hash_lookup(argv[1], buffer_size_sym))) {
113
- #ifdef RUBY_INTEGER_UNIFICATION
114
- if (rb_cInteger != rb_obj_class(v)) {
115
- rb_raise(rb_eArgError, ":buffer size must be a Integer.");
116
- }
117
- #else
118
- if (T_FIXNUM != rb_type(v)) {
119
- rb_raise(rb_eArgError, ":buffer size must be a Integer.");
120
- }
121
- #endif
122
- buf_size = FIX2INT(v);
123
- }
124
- oj_str_writer_init(&sw->sw, buf_size);
125
- oj_parse_options(argv[1], &sw->sw.opts);
126
- sw->flush_limit = buf_size;
98
+ volatile VALUE v;
99
+ int buf_size = 0;
100
+
101
+ if (Qundef == buffer_size_sym) {
102
+ buffer_size_sym = ID2SYM(rb_intern("buffer_size"));
103
+ rb_gc_register_address(&buffer_size_sym);
104
+ }
105
+ if (Qnil != (v = rb_hash_lookup(argv[1], buffer_size_sym))) {
106
+ if (rb_cInteger != rb_obj_class(v)) {
107
+ OJ_R_FREE(sw);
108
+ rb_raise(rb_eArgError, ":buffer size must be a Integer.");
109
+ }
110
+ buf_size = FIX2INT(v);
111
+ }
112
+ oj_str_writer_init(&sw->sw, buf_size);
113
+ oj_parse_options(argv[1], &sw->sw.opts);
114
+ sw->flush_limit = buf_size;
127
115
  } else {
128
- oj_str_writer_init(&sw->sw, 4096);
129
- sw->flush_limit = 0;
116
+ oj_str_writer_init(&sw->sw, 4096);
117
+ sw->flush_limit = 0;
130
118
  }
131
119
  sw->sw.out.indent = sw->sw.opts.indent;
132
- sw->stream = stream;
133
- sw->type = type;
134
- sw->fd = fd;
120
+ sw->stream = stream;
121
+ sw->type = type;
122
+ sw->fd = fd;
135
123
 
136
- return Data_Wrap_Struct(oj_stream_writer_class, 0, stream_writer_free, sw);
124
+ return TypedData_Wrap_Struct(oj_stream_writer_class, &oj_stream_writer_type, sw);
137
125
  }
138
126
 
139
127
  /* Document-method: push_key
@@ -145,14 +133,13 @@ stream_writer_new(int argc, VALUE *argv, VALUE self) {
145
133
  *
146
134
  * - *key* [_String_] the key pending for the next push
147
135
  */
148
- static VALUE
149
- stream_writer_push_key(VALUE self, VALUE key) {
150
- StreamWriter sw = (StreamWriter)DATA_PTR(self);
136
+ static VALUE stream_writer_push_key(VALUE self, VALUE key) {
137
+ StreamWriter sw;
138
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
151
139
 
152
- rb_check_type(key, T_STRING);
153
140
  oj_str_writer_push_key(&sw->sw, StringValuePtr(key));
154
141
  if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
155
- stream_writer_write(sw);
142
+ stream_writer_write(sw);
156
143
  }
157
144
  return Qnil;
158
145
  }
@@ -165,28 +152,23 @@ stream_writer_push_key(VALUE self, VALUE key) {
165
152
  *
166
153
  * - *key* [_String_] the key if adding to an object in the JSON document
167
154
  */
168
- static VALUE
169
- stream_writer_push_object(int argc, VALUE *argv, VALUE self) {
170
- StreamWriter sw = (StreamWriter)DATA_PTR(self);
155
+ static VALUE stream_writer_push_object(int argc, VALUE *argv, VALUE self) {
156
+ StreamWriter sw;
157
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
171
158
 
172
159
  switch (argc) {
173
- case 0:
174
- oj_str_writer_push_object(&sw->sw, 0);
175
- break;
160
+ case 0: oj_str_writer_push_object(&sw->sw, 0); break;
176
161
  case 1:
177
- if (Qnil == argv[0]) {
178
- oj_str_writer_push_object(&sw->sw, 0);
179
- } else {
180
- rb_check_type(argv[0], T_STRING);
181
- oj_str_writer_push_object(&sw->sw, StringValuePtr(argv[0]));
182
- }
183
- break;
184
- default:
185
- rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'.");
186
- break;
162
+ if (Qnil == argv[0]) {
163
+ oj_str_writer_push_object(&sw->sw, 0);
164
+ } else {
165
+ oj_str_writer_push_object(&sw->sw, StringValuePtr(argv[0]));
166
+ }
167
+ break;
168
+ default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'."); break;
187
169
  }
188
170
  if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
189
- stream_writer_write(sw);
171
+ stream_writer_write(sw);
190
172
  }
191
173
  return Qnil;
192
174
  }
@@ -199,28 +181,23 @@ stream_writer_push_object(int argc, VALUE *argv, VALUE self) {
199
181
  *
200
182
  * - *key* [_String_] the key if adding to an object in the JSON document
201
183
  */
202
- static VALUE
203
- stream_writer_push_array(int argc, VALUE *argv, VALUE self) {
204
- StreamWriter sw = (StreamWriter)DATA_PTR(self);
184
+ static VALUE stream_writer_push_array(int argc, VALUE *argv, VALUE self) {
185
+ StreamWriter sw;
186
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
205
187
 
206
188
  switch (argc) {
207
- case 0:
208
- oj_str_writer_push_array(&sw->sw, 0);
209
- break;
189
+ case 0: oj_str_writer_push_array(&sw->sw, 0); break;
210
190
  case 1:
211
- if (Qnil == argv[0]) {
212
- oj_str_writer_push_array(&sw->sw, 0);
213
- } else {
214
- rb_check_type(argv[0], T_STRING);
215
- oj_str_writer_push_array(&sw->sw, StringValuePtr(argv[0]));
216
- }
217
- break;
218
- default:
219
- rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'.");
220
- break;
191
+ if (Qnil == argv[0]) {
192
+ oj_str_writer_push_array(&sw->sw, 0);
193
+ } else {
194
+ oj_str_writer_push_array(&sw->sw, StringValuePtr(argv[0]));
195
+ }
196
+ break;
197
+ default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'."); break;
221
198
  }
222
199
  if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
223
- stream_writer_write(sw);
200
+ stream_writer_write(sw);
224
201
  }
225
202
  return Qnil;
226
203
  }
@@ -232,28 +209,23 @@ stream_writer_push_array(int argc, VALUE *argv, VALUE self) {
232
209
  * - *value* [_Object_] value to add to the JSON document
233
210
  * - *key* [_String_] the key if adding to an object in the JSON document
234
211
  */
235
- static VALUE
236
- stream_writer_push_value(int argc, VALUE *argv, VALUE self) {
237
- StreamWriter sw = (StreamWriter)DATA_PTR(self);
212
+ static VALUE stream_writer_push_value(int argc, VALUE *argv, VALUE self) {
213
+ StreamWriter sw;
214
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
238
215
 
239
216
  switch (argc) {
240
- case 1:
241
- oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, 0);
242
- break;
217
+ case 1: oj_str_writer_push_value((StrWriter)sw, *argv, 0); break;
243
218
  case 2:
244
- if (Qnil == argv[1]) {
245
- oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, 0);
246
- } else {
247
- rb_check_type(argv[1], T_STRING);
248
- oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, StringValuePtr(argv[1]));
249
- }
250
- break;
251
- default:
252
- rb_raise(rb_eArgError, "Wrong number of argument to 'push_value'.");
253
- break;
219
+ if (Qnil == argv[1]) {
220
+ oj_str_writer_push_value((StrWriter)sw, *argv, 0);
221
+ } else {
222
+ oj_str_writer_push_value((StrWriter)sw, *argv, StringValuePtr(argv[1]));
223
+ }
224
+ break;
225
+ default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_value'."); break;
254
226
  }
255
227
  if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
256
- stream_writer_write(sw);
228
+ stream_writer_write(sw);
257
229
  }
258
230
  return Qnil;
259
231
  }
@@ -267,29 +239,23 @@ stream_writer_push_value(int argc, VALUE *argv, VALUE self) {
267
239
  * - *value* [_Object_] value to add to the JSON document
268
240
  * - *key* [_String_] the key if adding to an object in the JSON document
269
241
  */
270
- static VALUE
271
- stream_writer_push_json(int argc, VALUE *argv, VALUE self) {
272
- StreamWriter sw = (StreamWriter)DATA_PTR(self);
242
+ static VALUE stream_writer_push_json(int argc, VALUE *argv, VALUE self) {
243
+ StreamWriter sw;
244
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
273
245
 
274
- rb_check_type(argv[0], T_STRING);
275
246
  switch (argc) {
276
- case 1:
277
- oj_str_writer_push_json((StrWriter)DATA_PTR(self), StringValuePtr(*argv), 0);
278
- break;
247
+ case 1: oj_str_writer_push_json((StrWriter)sw, StringValuePtr(*argv), 0); break;
279
248
  case 2:
280
- if (Qnil == argv[1]) {
281
- oj_str_writer_push_json((StrWriter)DATA_PTR(self), StringValuePtr(*argv), 0);
282
- } else {
283
- rb_check_type(argv[1], T_STRING);
284
- oj_str_writer_push_json((StrWriter)DATA_PTR(self), StringValuePtr(*argv), StringValuePtr(argv[1]));
285
- }
286
- break;
287
- default:
288
- rb_raise(rb_eArgError, "Wrong number of argument to 'push_json'.");
289
- break;
249
+ if (Qnil == argv[1]) {
250
+ oj_str_writer_push_json((StrWriter)sw, StringValuePtr(*argv), 0);
251
+ } else {
252
+ oj_str_writer_push_json((StrWriter)sw, StringValuePtr(*argv), StringValuePtr(argv[1]));
253
+ }
254
+ break;
255
+ default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_json'."); break;
290
256
  }
291
257
  if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
292
- stream_writer_write(sw);
258
+ stream_writer_write(sw);
293
259
  }
294
260
  return Qnil;
295
261
  }
@@ -300,13 +266,13 @@ stream_writer_push_json(int argc, VALUE *argv, VALUE self) {
300
266
  * Pops up a level in the JSON document closing the array or object that is
301
267
  * currently open.
302
268
  */
303
- static VALUE
304
- stream_writer_pop(VALUE self) {
305
- StreamWriter sw = (StreamWriter)DATA_PTR(self);
269
+ static VALUE stream_writer_pop(VALUE self) {
270
+ StreamWriter sw;
271
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
306
272
 
307
273
  oj_str_writer_pop(&sw->sw);
308
274
  if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
309
- stream_writer_write(sw);
275
+ stream_writer_write(sw);
310
276
  }
311
277
  return Qnil;
312
278
  }
@@ -317,9 +283,9 @@ stream_writer_pop(VALUE self) {
317
283
  * Pops all level in the JSON document closing all the array or object that is
318
284
  * currently open.
319
285
  */
320
- static VALUE
321
- stream_writer_pop_all(VALUE self) {
322
- StreamWriter sw = (StreamWriter)DATA_PTR(self);
286
+ static VALUE stream_writer_pop_all(VALUE self) {
287
+ StreamWriter sw;
288
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
323
289
 
324
290
  oj_str_writer_pop_all(&sw->sw);
325
291
  stream_writer_write(sw);
@@ -332,23 +298,25 @@ stream_writer_pop_all(VALUE self) {
332
298
  *
333
299
  * Flush any remaining characters in the buffer.
334
300
  */
335
- static VALUE
336
- stream_writer_flush(VALUE self) {
337
- stream_writer_write((StreamWriter)DATA_PTR(self));
301
+ static VALUE stream_writer_flush(VALUE self) {
302
+ StreamWriter sw;
303
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
304
+ stream_writer_write(sw);
338
305
 
339
306
  return Qnil;
340
307
  }
341
308
 
342
309
  /* Document-class: Oj::StreamWriter
343
- *
310
+ *
344
311
  * Supports building a JSON document one element at a time. Build the IO stream
345
312
  * document by pushing values into the document. Pushing an array or an object
346
313
  * will create that element in the JSON document and subsequent pushes will add
347
314
  * the elements to that array or object until a pop() is called.
348
315
  */
349
- void
350
- oj_stream_writer_init() {
316
+ void oj_stream_writer_init(void) {
351
317
  oj_stream_writer_class = rb_define_class_under(Oj, "StreamWriter", rb_cObject);
318
+ rb_gc_register_address(&oj_stream_writer_class);
319
+ rb_undef_alloc_func(oj_stream_writer_class);
352
320
  rb_define_module_function(oj_stream_writer_class, "new", stream_writer_new, -1);
353
321
  rb_define_method(oj_stream_writer_class, "push_key", stream_writer_push_key, 1);
354
322
  rb_define_method(oj_stream_writer_class, "push_object", stream_writer_push_object, -1);
@@ -359,5 +327,3 @@ oj_stream_writer_init() {
359
327
  rb_define_method(oj_stream_writer_class, "pop_all", stream_writer_pop_all, 0);
360
328
  rb_define_method(oj_stream_writer_class, "flush", stream_writer_flush, 0);
361
329
  }
362
-
363
-