oj 3.15.0 → 3.16.3

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.
data/ext/oj/parse.h CHANGED
@@ -16,22 +16,24 @@
16
16
  #include "val_stack.h"
17
17
 
18
18
  struct _rxClass;
19
+ struct _parseInfo;
19
20
 
20
21
  typedef struct _numInfo {
21
- int64_t i;
22
- int64_t num;
23
- int64_t div;
24
- int64_t di;
25
- const char *str;
26
- size_t len;
27
- long exp;
28
- int big;
29
- int infinity;
30
- int nan;
31
- int neg;
32
- int has_exp;
33
- int no_big;
34
- int bigdec_load;
22
+ int64_t i;
23
+ int64_t num;
24
+ int64_t div;
25
+ int64_t di;
26
+ const char *str;
27
+ size_t len;
28
+ long exp;
29
+ struct _parseInfo *pi;
30
+ int big;
31
+ int infinity;
32
+ int nan;
33
+ int neg;
34
+ int has_exp;
35
+ int no_big;
36
+ int bigdec_load;
35
37
  } *NumInfo;
36
38
 
37
39
  typedef struct _parseInfo {
data/ext/oj/parser.c CHANGED
@@ -1164,6 +1164,17 @@ static void parser_mark(void *ptr) {
1164
1164
  }
1165
1165
  }
1166
1166
 
1167
+ static const rb_data_type_t oj_parser_type = {
1168
+ "Oj/parser",
1169
+ {
1170
+ parser_mark,
1171
+ parser_free,
1172
+ NULL,
1173
+ },
1174
+ 0,
1175
+ 0,
1176
+ };
1177
+
1167
1178
  extern void oj_set_parser_validator(ojParser p);
1168
1179
  extern void oj_set_parser_saj(ojParser p);
1169
1180
  extern void oj_set_parser_usual(ojParser p);
@@ -1255,7 +1266,7 @@ static VALUE parser_new(int argc, VALUE *argv, VALUE self) {
1255
1266
  rb_hash_foreach(ropts, opt_cb, (VALUE)p);
1256
1267
  }
1257
1268
  }
1258
- return Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
1269
+ return TypedData_Wrap_Struct(parser_class, &oj_parser_type, p);
1259
1270
  }
1260
1271
 
1261
1272
  // Create a new parser without setting the delegate. The parser is
@@ -1275,7 +1286,7 @@ VALUE oj_parser_new(void) {
1275
1286
  buf_init(&p->buf);
1276
1287
  p->map = value_map;
1277
1288
 
1278
- return Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
1289
+ return TypedData_Wrap_Struct(parser_class, &oj_parser_type, p);
1279
1290
  }
1280
1291
 
1281
1292
  // Set set the options from a hash (ropts).
@@ -1294,53 +1305,41 @@ void oj_parser_set_option(ojParser p, VALUE ropts) {
1294
1305
  * - no options
1295
1306
  *
1296
1307
  * - *:saj*
1297
- * - _cache_keys=_ sets the value of the _cache_keys_ flag.
1298
- * - _cache_keys_ returns the value of the _cache_keys_ flag.
1299
- * - _cache_strings=_ sets the value of the _cache_strings_ to an positive integer less than 35. Strings shorter than
1300
- * that length are cached.
1301
- * - _cache_strings_ returns the value of the _cache_strings_ integer value.
1302
- * - _handler=_ sets the SAJ handler
1303
- * - _handler_ returns the SAJ handler
1308
+ * - _cache_keys_ is a flag indicating hash keys should be cached.
1309
+ * - _cache_strings_ is a positive integer less than 35. Strings shorter than that length are cached.
1310
+ * - _handler_ is the SAJ handler
1304
1311
  *
1305
1312
  * - *:usual*
1306
- * - _cache_keys=_ sets the value of the _cache_keys_ flag.
1307
- * - _cache_keys_ returns the value of the _cache_keys_ flag.
1308
- * - _cache_strings=_ sets the value of the _cache_strings_ to a positive integer less than 35. Strings shorter than
1309
- * that length are cached.
1310
- * - _cache_strings_ returns the value of the _cache_strings_ integer value.
1311
- * - _cache_expunge=_ sets the value of the _cache_expunge_ where 0 never expunges, 1 expunges slowly, 2 expunges
1312
- * faster, and 3 or higher expunges agressively.
1313
- * - _cache_expunge_ returns the value of the _cache_expunge_ integer value.
1314
- * - _capacity=_ sets the capacity of the parser. The parser grows automatically but can be updated directly with this
1315
- * call.
1316
- * - _capacity_ returns the current capacity of the parser's internal stack.
1317
- * - _create_id_ returns the value _create_id_ or _nil_ if there is no _create_id_.
1318
- * - _create_id=_ sets the value _create_id_ or if _nil_ unsets it. Parsed JSON objects that include the specified
1319
- * element use the element value as the name of the class to create an object from instead of a Hash.
1320
- * - _decimal=_ sets the approach to how decimals are parser. If _:auto_ then the decimals with significant digits are
1321
- * 16 or less are Floats and long ones are BigDecimal. _:ruby_ uses a call to Ruby to convert a string to a Float.
1322
- * _:float_ always generates a Float. _:bigdecimal_ always results in a BigDecimal.
1323
- * - _decimal_ returns the value of the decimal conversion option which can be :auto (default), :ruby, :float, or
1324
- * :bigdecimal.
1325
- * - _ignore_json_create_ returns the value of the _ignore_json_create_ flag.
1326
- * - _ignore_json_create=_ sets the value of the _ignore_json_create_ flag. When set the class json_create method is
1327
- * ignored on parsing in favor of creating an instance and populating directly.
1328
- * - _missing_class_ return the value of the _missing_class_ indicator.
1329
- * - _missing_class=_ sets the value of the _missing_class_ flag. Valid values are _:auto_ which creates any missing
1330
- * classes on parse, :ignore which ignores and continues as a Hash (default), and :raise which raises an exception if
1331
- * the class is not found.
1332
- * - _omit_null=_ sets the _omit_null_ flag. If true then null values in a map or object are omitted from the
1333
- * resulting Hash or Object.
1334
- * - _omit_null_ returns the value of the _omit_null_ flag.
1335
- * - _symbol_keys=_ sets the flag that indicates Hash keys should be parsed to Symbols versus Strings.
1336
- * - _symbol_keys_ returns the value of the _symbol_keys_ flag.
1313
+ * - _cache_keys_ is a flag indicating hash keys should be cached.
1314
+ * - _cache_strings_ is a positive integer less than 35. Strings shorter than that length are cached.
1315
+ * - _cache_expunge_ dictates when the cache will be expunged where 0 never expunges,
1316
+ * 1 expunges slowly, 2 expunges faster, and 3 or higher expunges agressively.
1317
+ * - _capacity_ is the capacity of the parser's internal stack. The parser grows automatically
1318
+ * but can be updated directly with this call.
1319
+ * - _create_id_ if non-nil is the key that is used to specify the type of object to create
1320
+ * when parsing. Parsed JSON objects that include the specified element use the element
1321
+ * value as the name of the class to create an object from instead of a Hash.
1322
+ * - _decimal_ is the approach to how decimals are parsed. If _:auto_ then
1323
+ * the decimals with significant digits are 16 or less are Floats and long
1324
+ * ones are BigDecimal. _:ruby_ uses a call to Ruby to convert a string to a Float.
1325
+ * _:float_ always generates a Float. _:bigdecimal_ always results in a BigDecimal.
1326
+ * - _ignore_json_create_ is a flag that when set the class json_create method is
1327
+ * ignored on parsing in favor of creating an instance and populating directly.
1328
+ * - _missing_class_ is an indicator that determines how unknown class names are handled.
1329
+ * Valid values are _:auto_ which creates any missing classes on parse, :ignore which ignores
1330
+ * and continues as a Hash (default), and :raise which raises an exception if the class is not found.
1331
+ * - _omit_null_ is a flag that if true then null values in a map or object are omitted
1332
+ * from the resulting Hash or Object.
1333
+ * - _symbol_keys_ is a flag that indicates Hash keys should be parsed to Symbols versus Strings.
1337
1334
  */
1338
1335
  static VALUE parser_missing(int argc, VALUE *argv, VALUE self) {
1339
- ojParser p = (ojParser)DATA_PTR(self);
1336
+ ojParser p;
1340
1337
  const char *key = NULL;
1341
1338
  volatile VALUE rkey = *argv;
1342
1339
  volatile VALUE rv = Qnil;
1343
1340
 
1341
+ TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
1342
+
1344
1343
  #if HAVE_RB_EXT_RACTOR_SAFE
1345
1344
  // This doesn't seem to do anything.
1346
1345
  rb_ext_ractor_safe(true);
@@ -1366,9 +1365,11 @@ static VALUE parser_missing(int argc, VALUE *argv, VALUE self) {
1366
1365
  * Returns the result according to the delegate of the parser.
1367
1366
  */
1368
1367
  static VALUE parser_parse(VALUE self, VALUE json) {
1369
- ojParser p = (ojParser)DATA_PTR(self);
1368
+ ojParser p;
1370
1369
  const byte *ptr = (const byte *)StringValuePtr(json);
1371
1370
 
1371
+ TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
1372
+
1372
1373
  parser_reset(p);
1373
1374
  p->start(p);
1374
1375
  parse(p, ptr);
@@ -1382,9 +1383,11 @@ static VALUE load_rescue(VALUE self, VALUE x) {
1382
1383
  }
1383
1384
 
1384
1385
  static VALUE load(VALUE self) {
1385
- ojParser p = (ojParser)DATA_PTR(self);
1386
+ ojParser p;
1386
1387
  volatile VALUE rbuf = rb_str_new2("");
1387
1388
 
1389
+ TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
1390
+
1388
1391
  p->start(p);
1389
1392
  while (true) {
1390
1393
  rb_funcall(p->reader, oj_readpartial_id, 2, INT2NUM(16385), rbuf);
@@ -1403,7 +1406,9 @@ static VALUE load(VALUE self) {
1403
1406
  * Returns the result according to the delegate of the parser.
1404
1407
  */
1405
1408
  static VALUE parser_load(VALUE self, VALUE reader) {
1406
- ojParser p = (ojParser)DATA_PTR(self);
1409
+ ojParser p;
1410
+
1411
+ TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
1407
1412
 
1408
1413
  parser_reset(p);
1409
1414
  p->reader = reader;
@@ -1420,10 +1425,12 @@ static VALUE parser_load(VALUE self, VALUE reader) {
1420
1425
  * Returns the result according to the delegate of the parser.
1421
1426
  */
1422
1427
  static VALUE parser_file(VALUE self, VALUE filename) {
1423
- ojParser p = (ojParser)DATA_PTR(self);
1428
+ ojParser p;
1424
1429
  const char *path;
1425
1430
  int fd;
1426
1431
 
1432
+ TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
1433
+
1427
1434
  path = StringValuePtr(filename);
1428
1435
 
1429
1436
  parser_reset(p);
@@ -1467,7 +1474,9 @@ static VALUE parser_file(VALUE self, VALUE filename) {
1467
1474
  * Returns the current state of the just_one [_Boolean_] option.
1468
1475
  */
1469
1476
  static VALUE parser_just_one(VALUE self) {
1470
- ojParser p = (ojParser)DATA_PTR(self);
1477
+ ojParser p;
1478
+
1479
+ TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
1471
1480
 
1472
1481
  return p->just_one ? Qtrue : Qfalse;
1473
1482
  }
@@ -1481,7 +1490,9 @@ static VALUE parser_just_one(VALUE self) {
1481
1490
  * Returns the current state of the just_one [_Boolean_] option.
1482
1491
  */
1483
1492
  static VALUE parser_just_one_set(VALUE self, VALUE v) {
1484
- ojParser p = (ojParser)DATA_PTR(self);
1493
+ ojParser p;
1494
+
1495
+ TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
1485
1496
 
1486
1497
  p->just_one = (Qtrue == v);
1487
1498
 
@@ -1505,7 +1516,7 @@ static VALUE parser_usual(VALUE self) {
1505
1516
  buf_init(&p->buf);
1506
1517
  p->map = value_map;
1507
1518
  oj_set_parser_usual(p);
1508
- usual_parser = Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
1519
+ usual_parser = TypedData_Wrap_Struct(parser_class, &oj_parser_type, p);
1509
1520
  rb_gc_register_address(&usual_parser);
1510
1521
  }
1511
1522
  return usual_parser;
@@ -1528,7 +1539,7 @@ static VALUE parser_saj(VALUE self) {
1528
1539
  buf_init(&p->buf);
1529
1540
  p->map = value_map;
1530
1541
  oj_set_parser_saj(p);
1531
- saj_parser = Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
1542
+ saj_parser = TypedData_Wrap_Struct(parser_class, &oj_parser_type, p);
1532
1543
  rb_gc_register_address(&saj_parser);
1533
1544
  }
1534
1545
  return saj_parser;
@@ -1550,7 +1561,7 @@ static VALUE parser_validate(VALUE self) {
1550
1561
  buf_init(&p->buf);
1551
1562
  p->map = value_map;
1552
1563
  oj_set_parser_validator(p);
1553
- validate_parser = Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
1564
+ validate_parser = TypedData_Wrap_Struct(parser_class, &oj_parser_type, p);
1554
1565
  rb_gc_register_address(&validate_parser);
1555
1566
  }
1556
1567
  return validate_parser;
data/ext/oj/parser.h CHANGED
@@ -32,9 +32,9 @@ typedef struct _num {
32
32
  long double dub;
33
33
  int64_t fixnum; // holds all digits
34
34
  uint32_t len;
35
- int16_t div; // 10^div
35
+ int16_t div; // 10^div
36
36
  int16_t exp;
37
- uint8_t shift; // shift of fixnum to get decimal
37
+ uint8_t shift; // shift of fixnum to get decimal
38
38
  bool neg;
39
39
  bool exp_neg;
40
40
  // for numbers as strings, reuse buf
data/ext/oj/rails.c CHANGED
@@ -639,6 +639,17 @@ static void encoder_mark(void *ptr) {
639
639
  }
640
640
  }
641
641
 
642
+ static const rb_data_type_t oj_encoder_type = {
643
+ "Oj/encoder",
644
+ {
645
+ encoder_mark,
646
+ encoder_free,
647
+ NULL,
648
+ },
649
+ 0,
650
+ 0,
651
+ };
652
+
642
653
  /* Document-method: new
643
654
  * call-seq: new(options=nil)
644
655
  *
@@ -656,7 +667,7 @@ static VALUE encoder_new(int argc, VALUE *argv, VALUE self) {
656
667
  oj_parse_options(*argv, &e->opts);
657
668
  e->arg = *argv;
658
669
  }
659
- return Data_Wrap_Struct(encoder_class, encoder_mark, encoder_free, e);
670
+ return TypedData_Wrap_Struct(encoder_class, &oj_encoder_type, e);
660
671
  }
661
672
 
662
673
  static VALUE resolve_classpath(const char *name) {
@@ -748,7 +759,8 @@ static void optimize(int argc, VALUE *argv, ROptTable rot, bool on) {
748
759
  * - *classes* [_Class_] a list of classes to optimize
749
760
  */
750
761
  static VALUE encoder_optimize(int argc, VALUE *argv, VALUE self) {
751
- Encoder e = (Encoder)DATA_PTR(self);
762
+ Encoder e;
763
+ TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);
752
764
 
753
765
  optimize(argc, argv, &e->ropts, true);
754
766
 
@@ -804,7 +816,8 @@ rails_mimic_json(VALUE self) {
804
816
  * - *classes* [_Class_] a list of classes to deoptimize
805
817
  */
806
818
  static VALUE encoder_deoptimize(int argc, VALUE *argv, VALUE self) {
807
- Encoder e = (Encoder)DATA_PTR(self);
819
+ Encoder e;
820
+ TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);
808
821
 
809
822
  optimize(argc, argv, &e->ropts, false);
810
823
 
@@ -833,8 +846,11 @@ static VALUE rails_deoptimize(int argc, VALUE *argv, VALUE self) {
833
846
  * @return true if the class is being optimized for rails and false otherwise
834
847
  */
835
848
  static VALUE encoder_optimized(VALUE self, VALUE clas) {
836
- Encoder e = (Encoder)DATA_PTR(self);
837
- ROpt ro = oj_rails_get_opt(&e->ropts, clas);
849
+ Encoder e;
850
+ ROpt ro;
851
+
852
+ TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);
853
+ ro = oj_rails_get_opt(&e->ropts, clas);
838
854
 
839
855
  if (NULL == ro) {
840
856
  return Qfalse;
@@ -890,7 +906,6 @@ static VALUE encode(VALUE obj, ROptTable ropts, Options opts, int argc, VALUE *a
890
906
  oj_out_init(&out);
891
907
 
892
908
  out.omit_nil = copts.dump_opts.omit_nil;
893
- out.caller = 0;
894
909
  out.cur = out.buf;
895
910
  out.circ_cnt = 0;
896
911
  out.opts = &copts;
@@ -941,7 +956,8 @@ static VALUE encode(VALUE obj, ROptTable ropts, Options opts, int argc, VALUE *a
941
956
  * Returns encoded object as a JSON string.
942
957
  */
943
958
  static VALUE encoder_encode(VALUE self, VALUE obj) {
944
- Encoder e = (Encoder)DATA_PTR(self);
959
+ Encoder e;
960
+ TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);
945
961
 
946
962
  if (Qnil != e->arg) {
947
963
  VALUE argv[1] = {e->arg};
data/ext/oj/reader.c CHANGED
@@ -101,7 +101,7 @@ int oj_reader_read(Reader reader) {
101
101
  } else {
102
102
  shift = reader->pro - reader->head - 1; // leave one character so we can backup one
103
103
  }
104
- if (0 >= shift) { /* no space left so allocate more */
104
+ if (0 >= shift) { /* no space left so allocate more */
105
105
  const char *old = reader->head;
106
106
  size_t size = reader->end - reader->head + BUF_PAD;
107
107
 
@@ -164,7 +164,6 @@ static VALUE partial_io_cb(VALUE rbuf) {
164
164
  }
165
165
  str = StringValuePtr(rstr);
166
166
  cnt = RSTRING_LEN(rstr);
167
- // printf("*** partial read %lu bytes, str: '%s'\n", cnt, str);
168
167
  strcpy(reader->tail, str);
169
168
  reader->read_end = reader->tail + cnt;
170
169
 
@@ -185,7 +184,6 @@ static VALUE io_cb(VALUE rbuf) {
185
184
  }
186
185
  str = StringValuePtr(rstr);
187
186
  cnt = RSTRING_LEN(rstr);
188
- // printf("*** read %lu bytes, str: '%s'\n", cnt, str);
189
187
  strcpy(reader->tail, str);
190
188
  reader->read_end = reader->tail + cnt;
191
189
 
data/ext/oj/saj.c CHANGED
@@ -587,7 +587,7 @@ static void saj_parse(VALUE handler, char *json) {
587
587
  if (0 == getrlimit(RLIMIT_STACK, &lim) && RLIM_INFINITY != lim.rlim_cur) {
588
588
  pi.stack_min = (void *)((char *)&obj - (lim.rlim_cur / 4 * 3)); /* let 3/4ths of the stack be used only */
589
589
  } else {
590
- pi.stack_min = 0; /* indicates not to check stack limit */
590
+ pi.stack_min = 0; /* indicates not to check stack limit */
591
591
  }
592
592
  }
593
593
  #endif
@@ -21,6 +21,17 @@ static void stream_writer_free(void *ptr) {
21
21
  OJ_R_FREE(ptr);
22
22
  }
23
23
 
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
+
24
35
  static void stream_writer_reset_buf(StreamWriter sw) {
25
36
  sw->sw.out.cur = sw->sw.out.buf;
26
37
  *sw->sw.out.cur = '\0';
@@ -120,7 +131,7 @@ static VALUE stream_writer_new(int argc, VALUE *argv, VALUE self) {
120
131
  sw->type = type;
121
132
  sw->fd = fd;
122
133
 
123
- return Data_Wrap_Struct(oj_stream_writer_class, 0, stream_writer_free, sw);
134
+ return TypedData_Wrap_Struct(oj_stream_writer_class, &oj_stream_writer_type, sw);
124
135
  }
125
136
 
126
137
  /* Document-method: push_key
@@ -133,7 +144,8 @@ static VALUE stream_writer_new(int argc, VALUE *argv, VALUE self) {
133
144
  * - *key* [_String_] the key pending for the next push
134
145
  */
135
146
  static VALUE stream_writer_push_key(VALUE self, VALUE key) {
136
- StreamWriter sw = (StreamWriter)DATA_PTR(self);
147
+ StreamWriter sw;
148
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
137
149
 
138
150
  oj_str_writer_push_key(&sw->sw, StringValuePtr(key));
139
151
  if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
@@ -151,7 +163,8 @@ static VALUE stream_writer_push_key(VALUE self, VALUE key) {
151
163
  * - *key* [_String_] the key if adding to an object in the JSON document
152
164
  */
153
165
  static VALUE stream_writer_push_object(int argc, VALUE *argv, VALUE self) {
154
- StreamWriter sw = (StreamWriter)DATA_PTR(self);
166
+ StreamWriter sw;
167
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
155
168
 
156
169
  switch (argc) {
157
170
  case 0: oj_str_writer_push_object(&sw->sw, 0); break;
@@ -179,7 +192,8 @@ static VALUE stream_writer_push_object(int argc, VALUE *argv, VALUE self) {
179
192
  * - *key* [_String_] the key if adding to an object in the JSON document
180
193
  */
181
194
  static VALUE stream_writer_push_array(int argc, VALUE *argv, VALUE self) {
182
- StreamWriter sw = (StreamWriter)DATA_PTR(self);
195
+ StreamWriter sw;
196
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
183
197
 
184
198
  switch (argc) {
185
199
  case 0: oj_str_writer_push_array(&sw->sw, 0); break;
@@ -206,15 +220,16 @@ static VALUE stream_writer_push_array(int argc, VALUE *argv, VALUE self) {
206
220
  * - *key* [_String_] the key if adding to an object in the JSON document
207
221
  */
208
222
  static VALUE stream_writer_push_value(int argc, VALUE *argv, VALUE self) {
209
- StreamWriter sw = (StreamWriter)DATA_PTR(self);
223
+ StreamWriter sw;
224
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
210
225
 
211
226
  switch (argc) {
212
- case 1: oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, 0); break;
227
+ case 1: oj_str_writer_push_value((StrWriter)sw, *argv, 0); break;
213
228
  case 2:
214
229
  if (Qnil == argv[1]) {
215
- oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, 0);
230
+ oj_str_writer_push_value((StrWriter)sw, *argv, 0);
216
231
  } else {
217
- oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, StringValuePtr(argv[1]));
232
+ oj_str_writer_push_value((StrWriter)sw, *argv, StringValuePtr(argv[1]));
218
233
  }
219
234
  break;
220
235
  default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_value'."); break;
@@ -235,15 +250,16 @@ static VALUE stream_writer_push_value(int argc, VALUE *argv, VALUE self) {
235
250
  * - *key* [_String_] the key if adding to an object in the JSON document
236
251
  */
237
252
  static VALUE stream_writer_push_json(int argc, VALUE *argv, VALUE self) {
238
- StreamWriter sw = (StreamWriter)DATA_PTR(self);
253
+ StreamWriter sw;
254
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
239
255
 
240
256
  switch (argc) {
241
- case 1: oj_str_writer_push_json((StrWriter)DATA_PTR(self), StringValuePtr(*argv), 0); break;
257
+ case 1: oj_str_writer_push_json((StrWriter)sw, StringValuePtr(*argv), 0); break;
242
258
  case 2:
243
259
  if (Qnil == argv[1]) {
244
- oj_str_writer_push_json((StrWriter)DATA_PTR(self), StringValuePtr(*argv), 0);
260
+ oj_str_writer_push_json((StrWriter)sw, StringValuePtr(*argv), 0);
245
261
  } else {
246
- oj_str_writer_push_json((StrWriter)DATA_PTR(self), StringValuePtr(*argv), StringValuePtr(argv[1]));
262
+ oj_str_writer_push_json((StrWriter)sw, StringValuePtr(*argv), StringValuePtr(argv[1]));
247
263
  }
248
264
  break;
249
265
  default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_json'."); break;
@@ -261,7 +277,8 @@ static VALUE stream_writer_push_json(int argc, VALUE *argv, VALUE self) {
261
277
  * currently open.
262
278
  */
263
279
  static VALUE stream_writer_pop(VALUE self) {
264
- StreamWriter sw = (StreamWriter)DATA_PTR(self);
280
+ StreamWriter sw;
281
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
265
282
 
266
283
  oj_str_writer_pop(&sw->sw);
267
284
  if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
@@ -277,7 +294,8 @@ static VALUE stream_writer_pop(VALUE self) {
277
294
  * currently open.
278
295
  */
279
296
  static VALUE stream_writer_pop_all(VALUE self) {
280
- StreamWriter sw = (StreamWriter)DATA_PTR(self);
297
+ StreamWriter sw;
298
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
281
299
 
282
300
  oj_str_writer_pop_all(&sw->sw);
283
301
  stream_writer_write(sw);
@@ -291,7 +309,9 @@ static VALUE stream_writer_pop_all(VALUE self) {
291
309
  * Flush any remaining characters in the buffer.
292
310
  */
293
311
  static VALUE stream_writer_flush(VALUE self) {
294
- stream_writer_write((StreamWriter)DATA_PTR(self));
312
+ StreamWriter sw;
313
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
314
+ stream_writer_write(sw);
295
315
 
296
316
  return Qnil;
297
317
  }