oj 3.13.10 → 3.13.13

Sign up to get free protection for your applications and to get access to all the features.
data/ext/oj/dump_object.c CHANGED
@@ -24,12 +24,7 @@ static void dump_data(VALUE obj, int depth, Out out, bool as_ok) {
24
24
 
25
25
  if (rb_cTime == clas) {
26
26
  assure_size(out, 6);
27
- *out->cur++ = '{';
28
- *out->cur++ = '"';
29
- *out->cur++ = '^';
30
- *out->cur++ = 't';
31
- *out->cur++ = '"';
32
- *out->cur++ = ':';
27
+ APPEND_CHARS(out->cur, "{\"^t\":", 6);
33
28
  dump_time(obj, out);
34
29
  *out->cur++ = '}';
35
30
  *out->cur = '\0';
@@ -91,12 +86,7 @@ static void dump_class(VALUE obj, int depth, Out out, bool as_ok) {
91
86
  size_t len = strlen(s);
92
87
 
93
88
  assure_size(out, 6);
94
- *out->cur++ = '{';
95
- *out->cur++ = '"';
96
- *out->cur++ = '^';
97
- *out->cur++ = 'c';
98
- *out->cur++ = '"';
99
- *out->cur++ = ':';
89
+ APPEND_CHARS(out->cur, "{\"^c\":", 6);
100
90
  oj_dump_cstr(s, len, 0, 0, out);
101
91
  *out->cur++ = '}';
102
92
  *out->cur = '\0';
@@ -120,9 +110,7 @@ static void dump_array_class(VALUE a, VALUE clas, int depth, Out out) {
120
110
  if (0 < id) {
121
111
  assure_size(out, d2 * out->indent + 16);
122
112
  fill_indent(out, d2);
123
- *out->cur++ = '"';
124
- *out->cur++ = '^';
125
- *out->cur++ = 'i';
113
+ APPEND_CHARS(out->cur, "\"^i", 3);
126
114
  dump_ulong(id, out);
127
115
  *out->cur++ = '"';
128
116
  }
@@ -139,19 +127,17 @@ static void dump_array_class(VALUE a, VALUE clas, int depth, Out out) {
139
127
  } else {
140
128
  size = d2 * out->indent + 2;
141
129
  }
130
+ assure_size(out, size * cnt);
142
131
  cnt--;
143
132
  for (i = 0; i <= cnt; i++) {
144
- assure_size(out, size);
145
133
  if (out->opts->dump_opts.use) {
146
134
  if (0 < out->opts->dump_opts.array_size) {
147
- strcpy(out->cur, out->opts->dump_opts.array_nl);
148
- out->cur += out->opts->dump_opts.array_size;
135
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
149
136
  }
150
137
  if (0 < out->opts->dump_opts.indent_size) {
151
138
  int i;
152
139
  for (i = d2; 0 < i; i--) {
153
- strcpy(out->cur, out->opts->dump_opts.indent_str);
154
- out->cur += out->opts->dump_opts.indent_size;
140
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
155
141
  }
156
142
  }
157
143
  } else {
@@ -168,15 +154,13 @@ static void dump_array_class(VALUE a, VALUE clas, int depth, Out out) {
168
154
  // printf("*** d2: %u indent: %u '%s'\n", d2, out->opts->dump_opts->indent_size,
169
155
  // out->opts->dump_opts->indent);
170
156
  if (0 < out->opts->dump_opts.array_size) {
171
- strcpy(out->cur, out->opts->dump_opts.array_nl);
172
- out->cur += out->opts->dump_opts.array_size;
157
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
173
158
  }
174
159
  if (0 < out->opts->dump_opts.indent_size) {
175
160
  int i;
176
161
 
177
162
  for (i = depth; 0 < i; i--) {
178
- strcpy(out->cur, out->opts->dump_opts.indent_str);
179
- out->cur += out->opts->dump_opts.indent_size;
163
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
180
164
  }
181
165
  }
182
166
  } else {
@@ -248,9 +232,7 @@ static int hash_cb(VALUE key, VALUE value, VALUE ov) {
248
232
  uint8_t b;
249
233
 
250
234
  assure_size(out, s2 + 15);
251
- *out->cur++ = '"';
252
- *out->cur++ = '^';
253
- *out->cur++ = '#';
235
+ APPEND_CHARS(out->cur, "\"^#", 3);
254
236
  out->hash_cnt++;
255
237
  for (i = 28; 0 <= i; i -= 4) {
256
238
  b = (uint8_t)((out->hash_cnt >> i) & 0x0000000F);
@@ -261,9 +243,7 @@ static int hash_cb(VALUE key, VALUE value, VALUE ov) {
261
243
  *out->cur++ = hex_chars[b];
262
244
  }
263
245
  }
264
- *out->cur++ = '"';
265
- *out->cur++ = ':';
266
- *out->cur++ = '[';
246
+ APPEND_CHARS(out->cur, "\":[", 3);
267
247
  fill_indent(out, d2);
268
248
  oj_dump_obj_val(key, d2, out);
269
249
  assure_size(out, s2);
@@ -293,8 +273,7 @@ static void dump_hash_class(VALUE obj, VALUE clas, int depth, Out out) {
293
273
  size = depth * out->indent + 2;
294
274
  assure_size(out, 2);
295
275
  if (0 == cnt) {
296
- *out->cur++ = '{';
297
- *out->cur++ = '}';
276
+ APPEND_CHARS(out->cur, "{}", 2);
298
277
  } else {
299
278
  long id = oj_check_circular(obj, out);
300
279
 
@@ -305,11 +284,7 @@ static void dump_hash_class(VALUE obj, VALUE clas, int depth, Out out) {
305
284
  if (0 < id) {
306
285
  assure_size(out, size + 16);
307
286
  fill_indent(out, depth + 1);
308
- *out->cur++ = '"';
309
- *out->cur++ = '^';
310
- *out->cur++ = 'i';
311
- *out->cur++ = '"';
312
- *out->cur++ = ':';
287
+ APPEND_CHARS(out->cur, "\"^i\":", 5);
313
288
  dump_ulong(id, out);
314
289
  *out->cur++ = ',';
315
290
  }
@@ -325,15 +300,13 @@ static void dump_hash_class(VALUE obj, VALUE clas, int depth, Out out) {
325
300
  size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
326
301
  assure_size(out, size);
327
302
  if (0 < out->opts->dump_opts.hash_size) {
328
- strcpy(out->cur, out->opts->dump_opts.hash_nl);
329
- out->cur += out->opts->dump_opts.hash_size;
303
+ APPEND_CHARS(out->cur, out->opts->dump_opts.hash_nl, out->opts->dump_opts.hash_size);
330
304
  }
331
305
  if (0 < out->opts->dump_opts.indent_size) {
332
306
  int i;
333
307
 
334
308
  for (i = depth; 0 < i; i--) {
335
- strcpy(out->cur, out->opts->dump_opts.indent_str);
336
- out->cur += out->opts->dump_opts.indent_size;
309
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
337
310
  }
338
311
  }
339
312
  }
@@ -408,11 +381,7 @@ static void dump_odd(VALUE obj, Odd odd, VALUE clas, int depth, Out out) {
408
381
  size = d2 * out->indent + clen + 10;
409
382
  assure_size(out, size);
410
383
  fill_indent(out, d2);
411
- *out->cur++ = '"';
412
- *out->cur++ = '^';
413
- *out->cur++ = 'O';
414
- *out->cur++ = '"';
415
- *out->cur++ = ':';
384
+ APPEND_CHARS(out->cur, "\"^O\":", 5);
416
385
  oj_dump_cstr(class_name, clen, 0, 0, out);
417
386
  *out->cur++ = ',';
418
387
  }
@@ -430,12 +399,9 @@ static void dump_odd(VALUE obj, Odd odd, VALUE clas, int depth, Out out) {
430
399
  assure_size(out, size);
431
400
  fill_indent(out, d2);
432
401
  *out->cur++ = '"';
433
- memcpy(out->cur, name, nlen);
434
- out->cur += nlen;
435
- *out->cur++ = '"';
436
- *out->cur++ = ':';
437
- memcpy(out->cur, s, len);
438
- out->cur += len;
402
+ APPEND_CHARS(out->cur, name, nlen);
403
+ APPEND_CHARS(out->cur, "\":", 2);
404
+ APPEND_CHARS(out->cur, s, len);
439
405
  *out->cur = '\0';
440
406
  }
441
407
  } else {
@@ -446,7 +412,7 @@ static void dump_odd(VALUE obj, Odd odd, VALUE clas, int depth, Out out) {
446
412
  assure_size(out, size);
447
413
  name = rb_id2name(*idp);
448
414
  nlen = strlen(name);
449
- if (0 != *fp) {
415
+ if (NULL != *fp) {
450
416
  v = (*fp)(obj);
451
417
  } else if (0 == strchr(name, '.')) {
452
418
  v = rb_funcall(obj, *idp, 0);
@@ -509,22 +475,14 @@ static void dump_obj_attrs(VALUE obj, VALUE clas, slot_t id, int depth, Out out)
509
475
 
510
476
  assure_size(out, d2 * out->indent + clen + 10);
511
477
  fill_indent(out, d2);
512
- *out->cur++ = '"';
513
- *out->cur++ = '^';
514
- *out->cur++ = 'o';
515
- *out->cur++ = '"';
516
- *out->cur++ = ':';
478
+ APPEND_CHARS(out->cur, "\"^o\":", 5);
517
479
  oj_dump_cstr(class_name, clen, 0, 0, out);
518
480
  }
519
481
  if (0 < id) {
520
482
  assure_size(out, d2 * out->indent + 16);
521
483
  *out->cur++ = ',';
522
484
  fill_indent(out, d2);
523
- *out->cur++ = '"';
524
- *out->cur++ = '^';
525
- *out->cur++ = 'i';
526
- *out->cur++ = '"';
527
- *out->cur++ = ':';
485
+ APPEND_CHARS(out->cur, "\"^i\":", 5);
528
486
  dump_ulong(id, out);
529
487
  }
530
488
  switch (type) {
@@ -532,39 +490,21 @@ static void dump_obj_attrs(VALUE obj, VALUE clas, slot_t id, int depth, Out out)
532
490
  assure_size(out, d2 * out->indent + 14);
533
491
  *out->cur++ = ',';
534
492
  fill_indent(out, d2);
535
- *out->cur++ = '"';
536
- *out->cur++ = 's';
537
- *out->cur++ = 'e';
538
- *out->cur++ = 'l';
539
- *out->cur++ = 'f';
540
- *out->cur++ = '"';
541
- *out->cur++ = ':';
493
+ APPEND_CHARS(out->cur, "\"self\":", 7);
542
494
  oj_dump_cstr(RSTRING_PTR(obj), (int)RSTRING_LEN(obj), 0, 0, out);
543
495
  break;
544
496
  case T_ARRAY:
545
497
  assure_size(out, d2 * out->indent + 14);
546
498
  *out->cur++ = ',';
547
499
  fill_indent(out, d2);
548
- *out->cur++ = '"';
549
- *out->cur++ = 's';
550
- *out->cur++ = 'e';
551
- *out->cur++ = 'l';
552
- *out->cur++ = 'f';
553
- *out->cur++ = '"';
554
- *out->cur++ = ':';
500
+ APPEND_CHARS(out->cur, "\"self\":", 7);
555
501
  dump_array_class(obj, Qundef, depth + 1, out);
556
502
  break;
557
503
  case T_HASH:
558
504
  assure_size(out, d2 * out->indent + 14);
559
505
  *out->cur++ = ',';
560
506
  fill_indent(out, d2);
561
- *out->cur++ = '"';
562
- *out->cur++ = 's';
563
- *out->cur++ = 'e';
564
- *out->cur++ = 'l';
565
- *out->cur++ = 'f';
566
- *out->cur++ = '"';
567
- *out->cur++ = ':';
507
+ APPEND_CHARS(out->cur, "\"self\":", 7);
568
508
  dump_hash_class(obj, Qundef, depth + 1, out);
569
509
  break;
570
510
  default: break;
@@ -688,12 +628,7 @@ static void dump_struct(VALUE obj, int depth, Out out, bool as_ok) {
688
628
  assure_size(out, size);
689
629
  *out->cur++ = '{';
690
630
  fill_indent(out, d2);
691
- *out->cur++ = '"';
692
- *out->cur++ = '^';
693
- *out->cur++ = 'u';
694
- *out->cur++ = '"';
695
- *out->cur++ = ':';
696
- *out->cur++ = '[';
631
+ APPEND_CHARS(out->cur, "\"^u\":[", 6);
697
632
  if ('#' == *class_name) {
698
633
  VALUE ma = rb_struct_s_members(clas);
699
634
  const char *name;
@@ -711,16 +646,14 @@ static void dump_struct(VALUE obj, int depth, Out out, bool as_ok) {
711
646
  *out->cur++ = ',';
712
647
  }
713
648
  *out->cur++ = '"';
714
- memcpy(out->cur, name, len);
715
- out->cur += len;
649
+ APPEND_CHARS(out->cur, name, len);
716
650
  *out->cur++ = '"';
717
651
  }
718
652
  *out->cur++ = ']';
719
653
  } else {
720
654
  fill_indent(out, d3);
721
655
  *out->cur++ = '"';
722
- memcpy(out->cur, class_name, len);
723
- out->cur += len;
656
+ APPEND_CHARS(out->cur, class_name, len);
724
657
  *out->cur++ = '"';
725
658
  }
726
659
  *out->cur++ = ',';
@@ -764,8 +697,7 @@ static void dump_struct(VALUE obj, int depth, Out out, bool as_ok) {
764
697
  }
765
698
  #endif
766
699
  out->cur--;
767
- *out->cur++ = ']';
768
- *out->cur++ = '}';
700
+ APPEND_CHARS(out->cur, "]}", 2);
769
701
  *out->cur = '\0';
770
702
  }
771
703
 
data/ext/oj/dump_strict.c CHANGED
@@ -105,9 +105,7 @@ static void dump_float(VALUE obj, int depth, Out out, bool as_ok) {
105
105
  }
106
106
  }
107
107
  assure_size(out, cnt);
108
- for (b = buf; '\0' != *b; b++) {
109
- *out->cur++ = *b;
110
- }
108
+ APPEND_CHARS(out->cur, buf, cnt);
111
109
  *out->cur = '\0';
112
110
  }
113
111
 
@@ -134,19 +132,17 @@ static void dump_array(VALUE a, int depth, Out out, bool as_ok) {
134
132
  } else {
135
133
  size = d2 * out->indent + 2;
136
134
  }
135
+ assure_size(out, size * cnt);
137
136
  cnt--;
138
137
  for (i = 0; i <= cnt; i++) {
139
- assure_size(out, size);
140
138
  if (out->opts->dump_opts.use) {
141
139
  if (0 < out->opts->dump_opts.array_size) {
142
- strcpy(out->cur, out->opts->dump_opts.array_nl);
143
- out->cur += out->opts->dump_opts.array_size;
140
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
144
141
  }
145
142
  if (0 < out->opts->dump_opts.indent_size) {
146
143
  int i;
147
144
  for (i = d2; 0 < i; i--) {
148
- strcpy(out->cur, out->opts->dump_opts.indent_str);
149
- out->cur += out->opts->dump_opts.indent_size;
145
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
150
146
  }
151
147
  }
152
148
  } else {
@@ -167,15 +163,13 @@ static void dump_array(VALUE a, int depth, Out out, bool as_ok) {
167
163
  // printf("*** d2: %u indent: %u '%s'\n", d2, out->opts->dump_opts->indent_size,
168
164
  // out->opts->dump_opts->indent);
169
165
  if (0 < out->opts->dump_opts.array_size) {
170
- strcpy(out->cur, out->opts->dump_opts.array_nl);
171
- out->cur += out->opts->dump_opts.array_size;
166
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
172
167
  }
173
168
  if (0 < out->opts->dump_opts.indent_size) {
174
169
  int i;
175
170
 
176
171
  for (i = depth; 0 < i; i--) {
177
- strcpy(out->cur, out->opts->dump_opts.indent_str);
178
- out->cur += out->opts->dump_opts.indent_size;
172
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
179
173
  }
180
174
  }
181
175
  } else {
@@ -214,14 +208,12 @@ static int hash_cb(VALUE key, VALUE value, VALUE ov) {
214
208
  size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
215
209
  assure_size(out, size);
216
210
  if (0 < out->opts->dump_opts.hash_size) {
217
- strcpy(out->cur, out->opts->dump_opts.hash_nl);
218
- out->cur += out->opts->dump_opts.hash_size;
211
+ APPEND_CHARS(out->cur, out->opts->dump_opts.hash_nl, out->opts->dump_opts.hash_size);
219
212
  }
220
213
  if (0 < out->opts->dump_opts.indent_size) {
221
214
  int i;
222
215
  for (i = depth; 0 < i; i--) {
223
- strcpy(out->cur, out->opts->dump_opts.indent_str);
224
- out->cur += out->opts->dump_opts.indent_size;
216
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
225
217
  }
226
218
  }
227
219
  if (rtype == T_STRING) {
@@ -232,13 +224,11 @@ static int hash_cb(VALUE key, VALUE value, VALUE ov) {
232
224
  size = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
233
225
  assure_size(out, size);
234
226
  if (0 < out->opts->dump_opts.before_size) {
235
- strcpy(out->cur, out->opts->dump_opts.before_sep);
236
- out->cur += out->opts->dump_opts.before_size;
227
+ APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
237
228
  }
238
229
  *out->cur++ = ':';
239
230
  if (0 < out->opts->dump_opts.after_size) {
240
- strcpy(out->cur, out->opts->dump_opts.after_sep);
241
- out->cur += out->opts->dump_opts.after_size;
231
+ APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
242
232
  }
243
233
  }
244
234
  if (NullMode == out->opts->mode) {
@@ -281,15 +271,13 @@ static void dump_hash(VALUE obj, int depth, Out out, bool as_ok) {
281
271
  size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
282
272
  assure_size(out, size);
283
273
  if (0 < out->opts->dump_opts.hash_size) {
284
- strcpy(out->cur, out->opts->dump_opts.hash_nl);
285
- out->cur += out->opts->dump_opts.hash_size;
274
+ APPEND_CHARS(out->cur, out->opts->dump_opts.hash_nl, out->opts->dump_opts.hash_size);
286
275
  }
287
276
  if (0 < out->opts->dump_opts.indent_size) {
288
277
  int i;
289
278
 
290
279
  for (i = depth; 0 < i; i--) {
291
- strcpy(out->cur, out->opts->dump_opts.indent_str);
292
- out->cur += out->opts->dump_opts.indent_size;
280
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
293
281
  }
294
282
  }
295
283
  }
data/ext/oj/encoder.c ADDED
@@ -0,0 +1,43 @@
1
+ // Copyright (c) 2011, 2022 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
3
+
4
+ #include "oj.h"
5
+
6
+ typedef struct _encoder {
7
+ int indent; // indention for dump, default 2
8
+ char circular; // YesNo
9
+ char escape_mode; // Escape_Mode
10
+ char mode; // Mode
11
+ char time_format; // TimeFormat
12
+ char bigdec_as_num; // YesNo
13
+ char to_hash; // YesNo
14
+ char to_json; // YesNo
15
+ char as_json; // YesNo
16
+ char raw_json; // YesNo
17
+ char trace; // YesNo
18
+ char sec_prec_set; // boolean (0 or 1)
19
+ char ignore_under; // YesNo - ignore attrs starting with _ if true in object and custom modes
20
+ int64_t int_range_min; // dump numbers below as string
21
+ int64_t int_range_max; // dump numbers above as string
22
+ const char* create_id; // 0 or string
23
+ size_t create_id_len; // length of create_id
24
+ int sec_prec; // second precision when dumping time
25
+ char float_prec; // float precision, linked to float_fmt
26
+ char float_fmt[7]; // float format for dumping, if empty use Ruby
27
+ struct _dumpOpts dump_opts;
28
+ struct _rxClass str_rx;
29
+ VALUE* ignore; // Qnil terminated array of classes or NULL
30
+ } * Encoder;
31
+
32
+ /*
33
+ rb_define_module_function(Oj, "encode", encode, -1);
34
+ rb_define_module_function(Oj, "to_file", to_file, -1); // or maybe just write
35
+ rb_define_module_function(Oj, "to_stream", to_stream, -1);
36
+ */
37
+
38
+ // write(to, obj)
39
+ // if to is a string then open file
40
+ // else if stream then write to stream
41
+ // handle non-blocking
42
+
43
+ // should each mode have a different encoder or use delegates like the parser?
data/ext/oj/fast.c CHANGED
@@ -13,6 +13,7 @@
13
13
 
14
14
  #include "encode.h"
15
15
  #include "oj.h"
16
+ #include "dump.h"
16
17
 
17
18
  // maximum to allocate on the stack, arbitrary limit
18
19
  #define SMALL_JSON 65536
@@ -771,7 +772,7 @@ static VALUE parse_json(VALUE clas, char *json, bool given, bool allocated) {
771
772
  pi.doc = doc;
772
773
  #if IS_WINDOWS
773
774
  // assume a 1M stack and give half to ruby
774
- pi.stack_min = (void *)((char *)&pi - (512 * 1024));
775
+ pi.stack_min = (void *)((char *)&pi - (512L * 1024L));
775
776
  #else
776
777
  {
777
778
  struct rlimit lim;
@@ -1610,18 +1611,15 @@ static VALUE doc_dump(int argc, VALUE *argv, VALUE self) {
1610
1611
  volatile VALUE rjson;
1611
1612
 
1612
1613
  if (0 == filename) {
1613
- char buf[4096];
1614
1614
  struct _out out;
1615
1615
 
1616
- out.buf = buf;
1617
- out.end = buf + sizeof(buf) - 10;
1618
- out.allocated = false;
1616
+ oj_out_init(&out);
1617
+
1619
1618
  out.omit_nil = oj_default_options.dump_opts.omit_nil;
1620
1619
  oj_dump_leaf_to_json(leaf, &oj_default_options, &out);
1621
1620
  rjson = rb_str_new2(out.buf);
1622
- if (out.allocated) {
1623
- xfree(out.buf);
1624
- }
1621
+
1622
+ oj_out_free(&out);
1625
1623
  } else {
1626
1624
  oj_write_leaf_to_file(leaf, filename, &oj_default_options);
1627
1625
  rjson = Qnil;
@@ -1717,8 +1715,10 @@ static VALUE doc_not_implemented(VALUE self) {
1717
1715
  * # Now try again using a path to Oj::Doc.fetch() directly and not using a
1718
1716
  * block. doc = Oj::Doc.open(json) doc.fetch('/2/three') #=> 3 doc.close()
1719
1717
  */
1720
- void oj_init_doc() {
1718
+ void oj_init_doc(void) {
1721
1719
  oj_doc_class = rb_define_class_under(Oj, "Doc", rb_cObject);
1720
+ rb_gc_register_address(&oj_doc_class);
1721
+ rb_undef_alloc_func(oj_doc_class);
1722
1722
  rb_define_singleton_method(oj_doc_class, "open", doc_open, 1);
1723
1723
  rb_define_singleton_method(oj_doc_class, "open_file", doc_open_file, 1);
1724
1724
  rb_define_singleton_method(oj_doc_class, "parse", doc_open, 1);
data/ext/oj/intern.c CHANGED
@@ -86,8 +86,12 @@ static VALUE form_attr(const char *str, size_t len) {
86
86
  return (VALUE)rb_intern3(buf, len + 1, oj_utf8_encoding);
87
87
  }
88
88
 
89
- void oj_hash_init() {
89
+ void oj_hash_init(void) {
90
90
  VALUE cache_class = rb_define_class_under(Oj, "Cache", rb_cObject);
91
+ rb_undef_alloc_func(cache_class);
92
+
93
+ rb_gc_register_address(&cache_class);
94
+ rb_undef_alloc_func(cache_class);
91
95
 
92
96
  str_cache = cache_create(0, form_str, true, true);
93
97
  str_cache_obj = Data_Wrap_Struct(cache_class, cache_mark, cache_free, str_cache);
@@ -275,6 +279,7 @@ VALUE oj_class_intern(const char *key, size_t len, bool safe, ParseInfo pi, int
275
279
  bucket->len = len;
276
280
  bucket->val = resolve_classpath(pi, key, len, auto_define, error_class);
277
281
  }
282
+ rb_gc_register_mark_object(bucket->val);
278
283
  return bucket->val;
279
284
  }
280
285
 
@@ -287,8 +292,10 @@ char *oj_strndup(const char *s, size_t len) {
287
292
  return d;
288
293
  }
289
294
 
290
- void intern_cleanup() {
295
+ /*
296
+ void intern_cleanup(void) {
291
297
  cache_free(str_cache);
292
298
  cache_free(sym_cache);
293
299
  cache_free(attr_cache);
294
300
  }
301
+ */
data/ext/oj/intern.h CHANGED
@@ -9,7 +9,7 @@
9
9
 
10
10
  struct _parseInfo;
11
11
 
12
- extern void oj_hash_init();
12
+ extern void oj_hash_init(void);
13
13
 
14
14
  extern VALUE oj_str_intern(const char *key, size_t len);
15
15
  extern VALUE oj_sym_intern(const char *key, size_t len);
data/ext/oj/mimic_json.c CHANGED
@@ -198,7 +198,6 @@ static int mimic_limit_arg(VALUE a) {
198
198
  * Returns [_String_] a JSON string.
199
199
  */
200
200
  static VALUE mimic_dump(int argc, VALUE *argv, VALUE self) {
201
- char buf[4096];
202
201
  struct _out out;
203
202
  struct _options copts = oj_default_options;
204
203
  VALUE rstr;
@@ -206,9 +205,9 @@ static VALUE mimic_dump(int argc, VALUE *argv, VALUE self) {
206
205
 
207
206
  copts.str_rx.head = NULL;
208
207
  copts.str_rx.tail = NULL;
209
- out.buf = buf;
210
- out.end = buf + sizeof(buf) - 10;
211
- out.allocated = false;
208
+
209
+ oj_out_init(&out);
210
+
212
211
  out.caller = CALLER_DUMP;
213
212
  copts.escape_mode = JXEsc;
214
213
  copts.mode = CompatMode;
@@ -257,9 +256,9 @@ static VALUE mimic_dump(int argc, VALUE *argv, VALUE self) {
257
256
  rb_funcall2(io, oj_write_id, 1, args);
258
257
  rstr = io;
259
258
  }
260
- if (out.allocated) {
261
- xfree(out.buf);
262
- }
259
+
260
+ oj_out_free(&out);
261
+
263
262
  return rstr;
264
263
  }
265
264
 
@@ -358,15 +357,16 @@ static VALUE mimic_dump_load(int argc, VALUE *argv, VALUE self) {
358
357
  }
359
358
 
360
359
  static VALUE mimic_generate_core(int argc, VALUE *argv, Options copts) {
361
- char buf[4096];
362
360
  struct _out out;
363
361
  VALUE rstr;
364
362
 
365
- memset(buf, 0, sizeof(buf));
363
+ if (0 == argc) {
364
+ rb_raise(rb_eArgError, "wrong number of arguments (0))");
365
+ }
366
+ memset(out.stack_buffer, 0, sizeof(out.stack_buffer));
367
+
368
+ oj_out_init(&out);
366
369
 
367
- out.buf = buf;
368
- out.end = buf + sizeof(buf) - 10;
369
- out.allocated = false;
370
370
  out.omit_nil = copts->dump_opts.omit_nil;
371
371
  out.caller = CALLER_GENERATE;
372
372
  // For obj.to_json or generate nan is not allowed but if called from dump
@@ -398,9 +398,9 @@ static VALUE mimic_generate_core(int argc, VALUE *argv, Options copts) {
398
398
  }
399
399
  rstr = rb_str_new2(out.buf);
400
400
  rstr = oj_encode(rstr);
401
- if (out.allocated) {
402
- xfree(out.buf);
403
- }
401
+
402
+ oj_out_free(&out);
403
+
404
404
  return rstr;
405
405
  }
406
406
 
@@ -457,9 +457,12 @@ oj_mimic_pretty_generate(int argc, VALUE *argv, VALUE self) {
457
457
  // a Hash. I haven't dug deep enough to find out why but using a State
458
458
  // instance and not a Hash gives the desired behavior.
459
459
  *rargs = *argv;
460
+ if (0 == argc) {
461
+ rb_raise(rb_eArgError, "wrong number of arguments (0))");
462
+ }
460
463
  if (1 == argc) {
461
464
  h = rb_hash_new();
462
- } else {
465
+ } else {
463
466
  h = argv[1];
464
467
  }
465
468
  if (!oj_hash_has_key(h, oj_indent_sym)) {
@@ -740,16 +743,15 @@ static struct _options mimic_object_to_json_options = {0, // indent
740
743
  }};
741
744
 
742
745
  static VALUE mimic_object_to_json(int argc, VALUE *argv, VALUE self) {
743
- char buf[4096];
744
746
  struct _out out;
745
747
  VALUE rstr;
746
748
  struct _options copts = oj_default_options;
747
749
 
748
750
  copts.str_rx.head = NULL;
749
751
  copts.str_rx.tail = NULL;
750
- out.buf = buf;
751
- out.end = buf + sizeof(buf) - 10;
752
- out.allocated = false;
752
+
753
+ oj_out_init(&out);
754
+
753
755
  out.omit_nil = copts.dump_opts.omit_nil;
754
756
  copts.mode = CompatMode;
755
757
  copts.to_json = No;
@@ -765,9 +767,9 @@ static VALUE mimic_object_to_json(int argc, VALUE *argv, VALUE self) {
765
767
  }
766
768
  rstr = rb_str_new2(out.buf);
767
769
  rstr = oj_encode(rstr);
768
- if (out.allocated) {
769
- xfree(out.buf);
770
- }
770
+
771
+ oj_out_free(&out);
772
+
771
773
  return rstr;
772
774
  }
773
775