/
object.c
831 lines (766 loc) · 26.3 KB
/
object.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
// Copyright (c) 2012 Peter Ohler. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license details.
#include <stdint.h>
#include <stdio.h>
#include <time.h>
#include "encode.h"
#include "err.h"
#include "hash.h"
#include "odd.h"
#include "oj.h"
#include "parse.h"
#include "resolve.h"
#include "trace.h"
#include "util.h"
inline static long read_long(const char *str, size_t len) {
long n = 0;
for (; 0 < len; str++, len--) {
if ('0' <= *str && *str <= '9') {
n = n * 10 + (*str - '0');
} else {
return -1;
}
}
return n;
}
static VALUE calc_hash_key(ParseInfo pi, Val kval, char k1) {
volatile VALUE rkey;
#if 0
VALUE *slot;
if (':' == k1) {
if (Qnil == (rkey = oj_sym_hash_get(kval->key + 1, kval->klen - 1, &slot))) {
rkey = rb_str_new(kval->key + 1, kval->klen - 1);
rkey = oj_encode(rkey);
rkey = rb_str_intern(rkey);
*slot = rkey;
rb_gc_register_address(slot);
}
} else if (Yes == pi->options.sym_key) {
if (Qnil == (rkey = oj_sym_hash_get(kval->key, kval->klen, &slot))) {
rkey = rb_str_new(kval->key, kval->klen);
rkey = oj_encode(rkey);
rkey = rb_str_intern(rkey);
*slot = rkey;
rb_gc_register_address(slot);
}
} else {
if (Qnil == (rkey = oj_str_hash_get(kval->key, kval->klen, &slot))) {
rkey = rb_str_new(kval->key, kval->klen);
rkey = oj_encode(rkey);
*slot = rkey;
rb_gc_register_address(slot);
}
}
#else
if (':' == k1) {
rkey = rb_str_new(kval->key + 1, kval->klen - 1);
rkey = oj_encode(rkey);
rkey = rb_str_intern(rkey);
} else {
rkey = rb_str_new(kval->key, kval->klen);
rkey = oj_encode(rkey);
if (Yes == pi->options.sym_key) {
rkey = rb_str_intern(rkey);
}
}
#endif
return rkey;
}
static VALUE str_to_value(ParseInfo pi, const char *str, size_t len, const char *orig) {
volatile VALUE rstr = Qnil;
if (':' == *orig && 0 < len) {
rstr = rb_str_new(str + 1, len - 1);
rstr = oj_encode(rstr);
rstr = rb_funcall(rstr, oj_to_sym_id, 0);
} else if (pi->circ_array && 3 <= len && '^' == *orig && 'r' == orig[1]) {
long i = read_long(str + 2, len - 2);
if (0 > i) {
oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a valid ID number");
return Qnil;
}
rstr = oj_circ_array_get(pi->circ_array, i);
} else {
rstr = rb_str_new(str, len);
rstr = oj_encode(rstr);
}
return rstr;
}
#if (RUBY_VERSION_MAJOR == 1 && RUBY_VERSION_MINOR == 8)
static VALUE oj_parse_xml_time(const char *str, int len) {
return rb_funcall(rb_cTime, oj_parse_id, 1, rb_str_new(str, len));
}
#else
// The much faster approach (4x faster)
static int parse_num(const char *str, const char *end, int cnt) {
int n = 0;
char c;
int i;
for (i = cnt; 0 < i; i--, str++) {
c = *str;
if (end <= str || c < '0' || '9' < c) {
return -1;
}
n = n * 10 + (c - '0');
}
return n;
}
VALUE
oj_parse_xml_time(const char *str, int len) {
VALUE args[8];
const char *end = str + len;
int n;
// year
if (0 > (n = parse_num(str, end, 4))) {
return Qnil;
}
str += 4;
args[0] = LONG2NUM(n);
if ('-' != *str++) {
return Qnil;
}
// month
if (0 > (n = parse_num(str, end, 2))) {
return Qnil;
}
str += 2;
args[1] = LONG2NUM(n);
if ('-' != *str++) {
return Qnil;
}
// day
if (0 > (n = parse_num(str, end, 2))) {
return Qnil;
}
str += 2;
args[2] = LONG2NUM(n);
if ('T' != *str++) {
return Qnil;
}
// hour
if (0 > (n = parse_num(str, end, 2))) {
return Qnil;
}
str += 2;
args[3] = LONG2NUM(n);
if (':' != *str++) {
return Qnil;
}
// minute
if (0 > (n = parse_num(str, end, 2))) {
return Qnil;
}
str += 2;
args[4] = LONG2NUM(n);
if (':' != *str++) {
return Qnil;
}
// second
if (0 > (n = parse_num(str, end, 2))) {
return Qnil;
}
str += 2;
if (str == end) {
args[5] = LONG2NUM(n);
args[6] = LONG2NUM(0);
} else {
char c = *str++;
if ('.' == c) {
long long nsec = 0;
for (; str < end; str++) {
c = *str;
if (c < '0' || '9' < c) {
str++;
break;
}
nsec = nsec * 10 + (c - '0');
}
args[5] = rb_float_new((double)n + ((double)nsec + 0.5) / 1000000000.0);
} else {
args[5] = rb_ll2inum(n);
}
if (end < str) {
args[6] = LONG2NUM(0);
} else {
if ('Z' == c) {
return rb_funcall2(rb_cTime, oj_utc_id, 6, args);
} else if ('+' == c) {
int hr = parse_num(str, end, 2);
int min;
str += 2;
if (0 > hr || ':' != *str++) {
return Qnil;
}
min = parse_num(str, end, 2);
if (0 > min) {
return Qnil;
}
args[6] = LONG2NUM(hr * 3600 + min * 60);
} else if ('-' == c) {
int hr = parse_num(str, end, 2);
int min;
str += 2;
if (0 > hr || ':' != *str++) {
return Qnil;
}
min = parse_num(str, end, 2);
if (0 > min) {
return Qnil;
}
args[6] = LONG2NUM(-(hr * 3600 + min * 60));
} else {
args[6] = LONG2NUM(0);
}
}
}
return rb_funcall2(rb_cTime, oj_new_id, 7, args);
}
#endif
static int hat_cstr(ParseInfo pi, Val parent, Val kval, const char *str, size_t len) {
const char *key = kval->key;
int klen = kval->klen;
if (2 == klen) {
switch (key[1]) {
case 'o': // object
{ // name2class sets an error if the class is not found or created
VALUE clas = oj_name2class(pi, str, len, Yes == pi->options.auto_define, rb_eArgError);
if (Qundef != clas) {
parent->val = rb_obj_alloc(clas);
}
} break;
case 'O': // odd object
{
Odd odd = oj_get_oddc(str, len);
if (0 == odd) {
return 0;
}
parent->val = odd->clas;
parent->odd_args = oj_odd_alloc_args(odd);
} break;
case 'm':
parent->val = rb_str_new(str + 1, len - 1);
parent->val = oj_encode(parent->val);
parent->val = rb_funcall(parent->val, oj_to_sym_id, 0);
break;
case 's':
parent->val = rb_str_new(str, len);
parent->val = oj_encode(parent->val);
break;
case 'c': // class
{
VALUE clas = oj_name2class(pi, str, len, Yes == pi->options.auto_define, rb_eArgError);
if (Qundef == clas) {
return 0;
} else {
parent->val = clas;
}
} break;
case 't': // time
parent->val = oj_parse_xml_time(str, (int)len);
break;
default: return 0; break;
}
return 1; // handled
}
return 0;
}
static int hat_num(ParseInfo pi, Val parent, Val kval, NumInfo ni) {
if (2 == kval->klen) {
switch (kval->key[1]) {
case 't': // time as a float
if (0 == ni->div || 9 < ni->di) {
rb_raise(rb_eArgError, "Invalid time decimal representation.");
// parent->val = rb_time_nano_new(0, 0);
} else {
int64_t nsec = ni->num * 1000000000LL / ni->div;
if (ni->neg) {
ni->i = -ni->i;
if (0 < nsec) {
ni->i--;
nsec = 1000000000LL - nsec;
}
}
if (86400 == ni->exp) { // UTC time
parent->val = rb_time_nano_new(ni->i, (long)nsec);
// Since the ruby C routines alway create local time, the
// offset and then a conversion to UTC keeps makes the time
// match the expected value.
parent->val = rb_funcall2(parent->val, oj_utc_id, 0, 0);
} else if (ni->has_exp) {
int64_t t = (int64_t)(ni->i + ni->exp);
struct _timeInfo ti;
VALUE args[8];
sec_as_time(t, &ti);
args[0] = LONG2NUM((long)(ti.year));
args[1] = LONG2NUM(ti.mon);
args[2] = LONG2NUM(ti.day);
args[3] = LONG2NUM(ti.hour);
args[4] = LONG2NUM(ti.min);
args[5] = rb_float_new((double)ti.sec + ((double)nsec + 0.5) / 1000000000.0);
args[6] = LONG2NUM(ni->exp);
parent->val = rb_funcall2(rb_cTime, oj_new_id, 7, args);
} else {
parent->val = rb_time_nano_new(ni->i, (long)nsec);
}
}
break;
case 'i': // circular index
if (!ni->infinity && !ni->neg && 1 == ni->div && 0 == ni->exp &&
0 != pi->circ_array) { // fixnum
if (Qnil == parent->val) {
parent->val = rb_hash_new();
}
oj_circ_array_set(pi->circ_array, parent->val, ni->i);
} else {
return 0;
}
break;
default: return 0; break;
}
return 1; // handled
}
return 0;
}
static int hat_value(ParseInfo pi, Val parent, const char *key, size_t klen, volatile VALUE value) {
if (T_ARRAY == rb_type(value)) {
int len = (int)RARRAY_LEN(value);
if (2 == klen && 'u' == key[1]) {
volatile VALUE sc;
volatile VALUE e1;
int slen;
if (0 == len) {
oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "Invalid struct data");
return 1;
}
e1 = *RARRAY_PTR(value);
// check for anonymous Struct
if (T_ARRAY == rb_type(e1)) {
VALUE args[1024];
volatile VALUE rstr;
int i, cnt = (int)RARRAY_LEN(e1);
for (i = 0; i < cnt; i++) {
rstr = rb_ary_entry(e1, i);
args[i] = rb_funcall(rstr, oj_to_sym_id, 0);
}
sc = rb_funcall2(rb_cStruct, oj_new_id, cnt, args);
} else {
// If struct is not defined then we let this fail and raise an exception.
sc = oj_name2struct(pi, *RARRAY_PTR(value), rb_eArgError);
}
// Create a properly initialized struct instance without calling the initialize method.
parent->val = rb_obj_alloc(sc);
// If the JSON array has more entries than the struct class allows, we record an error.
#ifdef RSTRUCT_LEN
#if RSTRUCT_LEN_RETURNS_INTEGER_OBJECT
slen = (int)NUM2LONG(RSTRUCT_LEN(parent->val));
#else // RSTRUCT_LEN_RETURNS_INTEGER_OBJECT
slen = (int)RSTRUCT_LEN(parent->val);
#endif // RSTRUCT_LEN_RETURNS_INTEGER_OBJECT
#else
slen = FIX2INT(rb_funcall2(parent->val, oj_length_id, 0, 0));
#endif
// MRI >= 1.9
if (len - 1 > slen) {
oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "Invalid struct data");
} else {
int i;
for (i = 0; i < len - 1; i++) {
rb_struct_aset(parent->val, INT2FIX(i), RARRAY_PTR(value)[i + 1]);
}
}
return 1;
} else if (3 <= klen && '#' == key[1]) {
volatile VALUE *a;
if (2 != len) {
oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "invalid hash pair");
return 1;
}
parent->val = rb_hash_new();
a = RARRAY_PTR(value);
rb_hash_aset(parent->val, *a, a[1]);
return 1;
}
}
return 0;
}
void oj_set_obj_ivar(Val parent, Val kval, VALUE value) {
const char *key = kval->key;
int klen = kval->klen;
ID var_id;
ID * slot;
#ifdef HAVE_PTHREAD_MUTEX_INIT
pthread_mutex_lock(&oj_cache_mutex);
#else
rb_mutex_lock(oj_cache_mutex);
#endif
if (0 == (var_id = oj_attr_hash_get(key, klen, &slot))) {
char attr[256];
if ((int)sizeof(attr) <= klen + 2) {
char *buf = ALLOC_N(char, klen + 2);
if ('~' == *key) {
strncpy(buf, key + 1, klen - 1);
buf[klen - 1] = '\0';
} else {
*buf = '@';
strncpy(buf + 1, key, klen);
buf[klen + 1] = '\0';
}
var_id = rb_intern(buf);
xfree(buf);
} else {
if ('~' == *key) {
strncpy(attr, key + 1, klen - 1);
attr[klen - 1] = '\0';
} else {
*attr = '@';
strncpy(attr + 1, key, klen);
attr[klen + 1] = '\0';
}
var_id = rb_intern(attr);
}
*slot = var_id;
}
#ifdef HAVE_PTHREAD_MUTEX_INIT
pthread_mutex_unlock(&oj_cache_mutex);
#else
rb_mutex_unlock(oj_cache_mutex);
#endif
rb_ivar_set(parent->val, var_id, value);
}
static void hash_set_cstr(ParseInfo pi, Val kval, const char *str, size_t len, const char *orig) {
const char * key = kval->key;
int klen = kval->klen;
Val parent = stack_peek(&pi->stack);
volatile VALUE rval = Qnil;
WHICH_TYPE:
switch (rb_type(parent->val)) {
case T_NIL:
parent->odd_args = NULL; // make sure it is NULL in case not odd
if ('^' != *key || !hat_cstr(pi, parent, kval, str, len)) {
parent->val = rb_hash_new();
goto WHICH_TYPE;
}
break;
case T_HASH:
rb_hash_aset(parent->val,
calc_hash_key(pi, kval, parent->k1),
str_to_value(pi, str, len, orig));
break;
case T_STRING:
rval = str_to_value(pi, str, len, orig);
if (4 == klen && 's' == *key && 'e' == key[1] && 'l' == key[2] && 'f' == key[3]) {
rb_funcall(parent->val, oj_replace_id, 1, rval);
} else {
oj_set_obj_ivar(parent, kval, rval);
}
break;
case T_OBJECT:
rval = str_to_value(pi, str, len, orig);
oj_set_obj_ivar(parent, kval, rval);
break;
case T_CLASS:
if (NULL == parent->odd_args) {
oj_set_error_at(pi,
oj_parse_error_class,
__FILE__,
__LINE__,
"%s is not an odd class",
rb_class2name(rb_obj_class(parent->val)));
return;
} else {
rval = str_to_value(pi, str, len, orig);
if (0 != oj_odd_set_arg(parent->odd_args, kval->key, kval->klen, rval)) {
char buf[256];
if ((int)sizeof(buf) - 1 <= klen) {
klen = sizeof(buf) - 2;
}
memcpy(buf, key, klen);
buf[klen] = '\0';
oj_set_error_at(pi,
oj_parse_error_class,
__FILE__,
__LINE__,
"%s is not an attribute of %s",
buf,
rb_class2name(rb_obj_class(parent->val)));
}
}
break;
default:
oj_set_error_at(pi,
oj_parse_error_class,
__FILE__,
__LINE__,
"can not add attributes to a %s",
rb_class2name(rb_obj_class(parent->val)));
return;
}
if (Yes == pi->options.trace) {
oj_trace_parse_call("set_string", pi, __FILE__, __LINE__, rval);
}
}
static void hash_set_num(ParseInfo pi, Val kval, NumInfo ni) {
const char * key = kval->key;
int klen = kval->klen;
Val parent = stack_peek(&pi->stack);
volatile VALUE rval = Qnil;
WHICH_TYPE:
switch (rb_type(parent->val)) {
case T_NIL:
parent->odd_args = NULL; // make sure it is NULL in case not odd
if ('^' != *key || !hat_num(pi, parent, kval, ni)) {
parent->val = rb_hash_new();
goto WHICH_TYPE;
}
break;
case T_HASH:
rval = oj_num_as_value(ni);
rb_hash_aset(parent->val, calc_hash_key(pi, kval, parent->k1), rval);
break;
case T_OBJECT:
if (2 == klen && '^' == *key && 'i' == key[1] && !ni->infinity && !ni->neg &&
1 == ni->div && 0 == ni->exp && 0 != pi->circ_array) { // fixnum
oj_circ_array_set(pi->circ_array, parent->val, ni->i);
} else {
rval = oj_num_as_value(ni);
oj_set_obj_ivar(parent, kval, rval);
}
break;
case T_CLASS:
if (NULL == parent->odd_args) {
oj_set_error_at(pi,
oj_parse_error_class,
__FILE__,
__LINE__,
"%s is not an odd class",
rb_class2name(rb_obj_class(parent->val)));
return;
} else {
rval = oj_num_as_value(ni);
if (0 != oj_odd_set_arg(parent->odd_args, key, klen, rval)) {
char buf[256];
if ((int)sizeof(buf) - 1 <= klen) {
klen = sizeof(buf) - 2;
}
memcpy(buf, key, klen);
buf[klen] = '\0';
oj_set_error_at(pi,
oj_parse_error_class,
__FILE__,
__LINE__,
"%s is not an attribute of %s",
buf,
rb_class2name(rb_obj_class(parent->val)));
}
}
break;
default:
oj_set_error_at(pi,
oj_parse_error_class,
__FILE__,
__LINE__,
"can not add attributes to a %s",
rb_class2name(rb_obj_class(parent->val)));
return;
}
if (Yes == pi->options.trace) {
oj_trace_parse_call("add_number", pi, __FILE__, __LINE__, rval);
}
}
static void hash_set_value(ParseInfo pi, Val kval, VALUE value) {
const char *key = kval->key;
int klen = kval->klen;
Val parent = stack_peek(&pi->stack);
WHICH_TYPE:
switch (rb_type(parent->val)) {
case T_NIL:
parent->odd_args = NULL; // make sure it is NULL in case not odd
if ('^' != *key || !hat_value(pi, parent, key, klen, value)) {
parent->val = rb_hash_new();
goto WHICH_TYPE;
}
break;
case T_HASH:
if (rb_cHash != rb_obj_class(parent->val)) {
if (4 == klen && 's' == *key && 'e' == key[1] && 'l' == key[2] && 'f' == key[3]) {
rb_funcall(parent->val, oj_replace_id, 1, value);
} else {
oj_set_obj_ivar(parent, kval, value);
}
} else {
if (3 <= klen && '^' == *key && '#' == key[1] && T_ARRAY == rb_type(value)) {
long len = RARRAY_LEN(value);
volatile VALUE *a = RARRAY_PTR(value);
if (2 != len) {
oj_set_error_at(pi,
oj_parse_error_class,
__FILE__,
__LINE__,
"invalid hash pair");
return;
}
rb_hash_aset(parent->val, *a, a[1]);
} else {
rb_hash_aset(parent->val, calc_hash_key(pi, kval, parent->k1), value);
}
}
break;
case T_ARRAY:
if (4 == klen && 's' == *key && 'e' == key[1] && 'l' == key[2] && 'f' == key[3]) {
rb_funcall(parent->val, oj_replace_id, 1, value);
} else {
oj_set_obj_ivar(parent, kval, value);
}
break;
case T_STRING: // for subclassed strings
case T_OBJECT: oj_set_obj_ivar(parent, kval, value); break;
case T_MODULE:
case T_CLASS:
if (NULL == parent->odd_args) {
oj_set_error_at(pi,
oj_parse_error_class,
__FILE__,
__LINE__,
"%s is not an odd class",
rb_class2name(rb_obj_class(parent->val)));
return;
} else if (0 != oj_odd_set_arg(parent->odd_args, key, klen, value)) {
char buf[256];
if ((int)sizeof(buf) - 1 <= klen) {
klen = sizeof(buf) - 2;
}
memcpy(buf, key, klen);
buf[klen] = '\0';
oj_set_error_at(pi,
oj_parse_error_class,
__FILE__,
__LINE__,
"%s is not an attribute of %s",
buf,
rb_class2name(rb_obj_class(parent->val)));
}
break;
default:
oj_set_error_at(pi,
oj_parse_error_class,
__FILE__,
__LINE__,
"can not add attributes to a %s",
rb_class2name(rb_obj_class(parent->val)));
return;
}
if (Yes == pi->options.trace) {
oj_trace_parse_call("add_value", pi, __FILE__, __LINE__, value);
}
}
static VALUE start_hash(ParseInfo pi) {
if (Yes == pi->options.trace) {
oj_trace_parse_in("start_hash", pi, __FILE__, __LINE__);
}
return Qnil;
}
static void end_hash(ParseInfo pi) {
Val parent = stack_peek(&pi->stack);
if (Qnil == parent->val) {
parent->val = rb_hash_new();
} else if (NULL != parent->odd_args) {
OddArgs oa = parent->odd_args;
parent->val = rb_funcall2(oa->odd->create_obj,
oa->odd->create_op,
oa->odd->attr_cnt,
oa->args);
oj_odd_free(oa);
parent->odd_args = NULL;
}
if (Yes == pi->options.trace) {
oj_trace_parse_hash_end(pi, __FILE__, __LINE__);
}
}
static void array_append_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
volatile VALUE rval = Qnil;
// orig lets us know whether the string was ^r1 or \u005er1
if (3 <= len && 0 != pi->circ_array && '^' == orig[0] &&
0 == rb_array_len(stack_peek(&pi->stack)->val)) {
if ('i' == str[1]) {
long i = read_long(str + 2, len - 2);
if (0 < i) {
oj_circ_array_set(pi->circ_array, stack_peek(&pi->stack)->val, i);
return;
}
} else if ('r' == str[1]) {
long i = read_long(str + 2, len - 2);
if (0 < i) {
rb_ary_push(stack_peek(&pi->stack)->val, oj_circ_array_get(pi->circ_array, i));
return;
}
}
}
rval = str_to_value(pi, str, len, orig);
rb_ary_push(stack_peek(&pi->stack)->val, rval);
if (Yes == pi->options.trace) {
oj_trace_parse_call("append_string", pi, __FILE__, __LINE__, rval);
}
}
static void array_append_num(ParseInfo pi, NumInfo ni) {
volatile VALUE rval = oj_num_as_value(ni);
rb_ary_push(stack_peek(&pi->stack)->val, rval);
if (Yes == pi->options.trace) {
oj_trace_parse_call("append_number", pi, __FILE__, __LINE__, rval);
}
}
static void add_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
pi->stack.head->val = str_to_value(pi, str, len, orig);
if (Yes == pi->options.trace) {
oj_trace_parse_call("add_string", pi, __FILE__, __LINE__, pi->stack.head->val);
}
}
static void add_num(ParseInfo pi, NumInfo ni) {
pi->stack.head->val = oj_num_as_value(ni);
if (Yes == pi->options.trace) {
oj_trace_parse_call("add_num", pi, __FILE__, __LINE__, pi->stack.head->val);
}
}
void oj_set_object_callbacks(ParseInfo pi) {
oj_set_strict_callbacks(pi);
pi->end_hash = end_hash;
pi->start_hash = start_hash;
pi->hash_set_cstr = hash_set_cstr;
pi->hash_set_num = hash_set_num;
pi->hash_set_value = hash_set_value;
pi->add_cstr = add_cstr;
pi->add_num = add_num;
pi->array_append_cstr = array_append_cstr;
pi->array_append_num = array_append_num;
}
VALUE
oj_object_parse(int argc, VALUE *argv, VALUE self) {
struct _parseInfo pi;
parse_info_init(&pi);
pi.options = oj_default_options;
pi.handler = Qnil;
pi.err_class = Qnil;
oj_set_object_callbacks(&pi);
if (T_STRING == rb_type(*argv)) {
return oj_pi_parse(argc, argv, &pi, 0, 0, 1);
} else {
return oj_pi_sparse(argc, argv, &pi, 0);
}
}
VALUE
oj_object_parse_cstr(int argc, VALUE *argv, char *json, size_t len) {
struct _parseInfo pi;
parse_info_init(&pi);
pi.options = oj_default_options;
pi.handler = Qnil;
pi.err_class = Qnil;
oj_set_strict_callbacks(&pi);
pi.end_hash = end_hash;
pi.start_hash = start_hash;
pi.hash_set_cstr = hash_set_cstr;
pi.hash_set_num = hash_set_num;
pi.hash_set_value = hash_set_value;
pi.add_cstr = add_cstr;
pi.add_num = add_num;
pi.array_append_cstr = array_append_cstr;
pi.array_append_num = array_append_num;
return oj_pi_parse(argc, argv, &pi, json, len, 1);
}