1
16
17 package ast
18
19 import (
20 `encoding/json`
21 `fmt`
22 `strconv`
23 `unsafe`
24
25 `github.com/bytedance/sonic/internal/native/types`
26 `github.com/bytedance/sonic/internal/rt`
27 )
28
29 const (
30 _V_NONE types.ValueType = 0
31 _V_NODE_BASE types.ValueType = 1 << 5
32 _V_LAZY types.ValueType = 1 << 7
33 _V_RAW types.ValueType = 1 << 8
34 _V_NUMBER = _V_NODE_BASE + 1
35 _V_ANY = _V_NODE_BASE + 2
36 _V_ARRAY_LAZY = _V_LAZY | types.V_ARRAY
37 _V_OBJECT_LAZY = _V_LAZY | types.V_OBJECT
38 _MASK_LAZY = _V_LAZY - 1
39 _MASK_RAW = _V_RAW - 1
40 )
41
42 const (
43 V_NONE = 0
44 V_ERROR = 1
45 V_NULL = int(types.V_NULL)
46 V_TRUE = int(types.V_TRUE)
47 V_FALSE = int(types.V_FALSE)
48 V_ARRAY = int(types.V_ARRAY)
49 V_OBJECT = int(types.V_OBJECT)
50 V_STRING = int(types.V_STRING)
51 V_NUMBER = int(_V_NUMBER)
52 V_ANY = int(_V_ANY)
53 )
54
55 type Node struct {
56 t types.ValueType
57 l uint
58 p unsafe.Pointer
59 }
60
61
62
63 func (self *Node) UnmarshalJSON(data []byte) (err error) {
64 *self = NewRaw(string(data))
65 return self.Check()
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 func (self Node) Type() int {
83 return int(self.t & _MASK_LAZY & _MASK_RAW)
84 }
85
86 func (self Node) itype() types.ValueType {
87 return self.t & _MASK_LAZY & _MASK_RAW
88 }
89
90
91 func (self *Node) Exists() bool {
92 return self.Valid() && self.t != _V_NONE
93 }
94
95
96 func (self *Node) Valid() bool {
97 if self == nil {
98 return false
99 }
100 return self.t != V_ERROR
101 }
102
103
104
105
106 func (self *Node) Check() error {
107 if self == nil {
108 return ErrNotExist
109 } else if self.t != V_ERROR {
110 return nil
111 } else {
112 return self
113 }
114 }
115
116
117 func (self Node) IsRaw() bool {
118 return self.t&_V_RAW != 0
119 }
120
121 func (self *Node) isLazy() bool {
122 return self != nil && self.t&_V_LAZY != 0
123 }
124
125 func (self *Node) isAny() bool {
126 return self != nil && self.t == _V_ANY
127 }
128
129
130
131
132 func (self *Node) Raw() (string, error) {
133 if self == nil {
134 return "", ErrNotExist
135 }
136 if !self.IsRaw() {
137 buf, err := self.MarshalJSON()
138 return rt.Mem2Str(buf), err
139 }
140 return self.toString(), nil
141 }
142
143 func (self *Node) checkRaw() error {
144 if err := self.Check(); err != nil {
145 return err
146 }
147 if self.IsRaw() {
148 self.parseRaw(false)
149 }
150 return self.Check()
151 }
152
153
154
155
156 func (self *Node) Bool() (bool, error) {
157 if err := self.checkRaw(); err != nil {
158 return false, err
159 }
160 switch self.t {
161 case types.V_TRUE : return true , nil
162 case types.V_FALSE : return false, nil
163 case types.V_NULL : return false, nil
164 case _V_NUMBER :
165 if i, err := self.toInt64(); err == nil {
166 return i != 0, nil
167 } else if f, err := self.toFloat64(); err == nil {
168 return f != 0, nil
169 } else {
170 return false, err
171 }
172 case types.V_STRING: return strconv.ParseBool(self.toString())
173 case _V_ANY :
174 any := self.packAny()
175 switch v := any.(type) {
176 case bool : return v, nil
177 case int : return v != 0, nil
178 case int8 : return v != 0, nil
179 case int16 : return v != 0, nil
180 case int32 : return v != 0, nil
181 case int64 : return v != 0, nil
182 case uint : return v != 0, nil
183 case uint8 : return v != 0, nil
184 case uint16 : return v != 0, nil
185 case uint32 : return v != 0, nil
186 case uint64 : return v != 0, nil
187 case float32: return v != 0, nil
188 case float64: return v != 0, nil
189 case string : return strconv.ParseBool(v)
190 case json.Number:
191 if i, err := v.Int64(); err == nil {
192 return i != 0, nil
193 } else if f, err := v.Float64(); err == nil {
194 return f != 0, nil
195 } else {
196 return false, err
197 }
198 default: return false, ErrUnsupportType
199 }
200 default : return false, ErrUnsupportType
201 }
202 }
203
204
205
206
207 func (self *Node) Int64() (int64, error) {
208 if err := self.checkRaw(); err != nil {
209 return 0, err
210 }
211 switch self.t {
212 case _V_NUMBER, types.V_STRING :
213 if i, err := self.toInt64(); err == nil {
214 return i, nil
215 } else if f, err := self.toFloat64(); err == nil {
216 return int64(f), nil
217 } else {
218 return 0, err
219 }
220 case types.V_TRUE : return 1, nil
221 case types.V_FALSE : return 0, nil
222 case types.V_NULL : return 0, nil
223 case _V_ANY :
224 any := self.packAny()
225 switch v := any.(type) {
226 case bool : if v { return 1, nil } else { return 0, nil }
227 case int : return int64(v), nil
228 case int8 : return int64(v), nil
229 case int16 : return int64(v), nil
230 case int32 : return int64(v), nil
231 case int64 : return int64(v), nil
232 case uint : return int64(v), nil
233 case uint8 : return int64(v), nil
234 case uint16 : return int64(v), nil
235 case uint32 : return int64(v), nil
236 case uint64 : return int64(v), nil
237 case float32: return int64(v), nil
238 case float64: return int64(v), nil
239 case string :
240 if i, err := strconv.ParseInt(v, 10, 64); err == nil {
241 return i, nil
242 } else if f, err := strconv.ParseFloat(v, 64); err == nil {
243 return int64(f), nil
244 } else {
245 return 0, err
246 }
247 case json.Number:
248 if i, err := v.Int64(); err == nil {
249 return i, nil
250 } else if f, err := v.Float64(); err == nil {
251 return int64(f), nil
252 } else {
253 return 0, err
254 }
255 default: return 0, ErrUnsupportType
256 }
257 default : return 0, ErrUnsupportType
258 }
259 }
260
261
262 func (self *Node) StrictInt64() (int64, error) {
263 if err := self.checkRaw(); err != nil {
264 return 0, err
265 }
266 switch self.t {
267 case _V_NUMBER : return self.toInt64()
268 case _V_ANY :
269 any := self.packAny()
270 switch v := any.(type) {
271 case int : return int64(v), nil
272 case int8 : return int64(v), nil
273 case int16 : return int64(v), nil
274 case int32 : return int64(v), nil
275 case int64 : return int64(v), nil
276 case uint : return int64(v), nil
277 case uint8 : return int64(v), nil
278 case uint16: return int64(v), nil
279 case uint32: return int64(v), nil
280 case uint64: return int64(v), nil
281 case json.Number:
282 if i, err := v.Int64(); err == nil {
283 return i, nil
284 } else {
285 return 0, err
286 }
287 default: return 0, ErrUnsupportType
288 }
289 default : return 0, ErrUnsupportType
290 }
291 }
292
293 func castNumber(v bool) json.Number {
294 if v {
295 return json.Number("1")
296 } else {
297 return json.Number("0")
298 }
299 }
300
301
302
303
304 func (self *Node) Number() (json.Number, error) {
305 if err := self.checkRaw(); err != nil {
306 return json.Number(""), err
307 }
308 switch self.t {
309 case _V_NUMBER : return self.toNumber(), nil
310 case types.V_STRING :
311 if _, err := self.toInt64(); err == nil {
312 return self.toNumber(), nil
313 } else if _, err := self.toFloat64(); err == nil {
314 return self.toNumber(), nil
315 } else {
316 return json.Number(""), err
317 }
318 case types.V_TRUE : return json.Number("1"), nil
319 case types.V_FALSE : return json.Number("0"), nil
320 case types.V_NULL : return json.Number("0"), nil
321 case _V_ANY :
322 any := self.packAny()
323 switch v := any.(type) {
324 case bool : return castNumber(v), nil
325 case int : return castNumber(v != 0), nil
326 case int8 : return castNumber(v != 0), nil
327 case int16 : return castNumber(v != 0), nil
328 case int32 : return castNumber(v != 0), nil
329 case int64 : return castNumber(v != 0), nil
330 case uint : return castNumber(v != 0), nil
331 case uint8 : return castNumber(v != 0), nil
332 case uint16 : return castNumber(v != 0), nil
333 case uint32 : return castNumber(v != 0), nil
334 case uint64 : return castNumber(v != 0), nil
335 case float32: return castNumber(v != 0), nil
336 case float64: return castNumber(v != 0), nil
337 case string :
338 if _, err := strconv.ParseFloat(v, 64); err == nil {
339 return json.Number(v), nil
340 } else {
341 return json.Number(""), err
342 }
343 case json.Number: return v, nil
344 default: return json.Number(""), ErrUnsupportType
345 }
346 default : return json.Number(""), ErrUnsupportType
347 }
348 }
349
350
351 func (self *Node) StrictNumber() (json.Number, error) {
352 if err := self.checkRaw(); err != nil {
353 return json.Number(""), err
354 }
355 switch self.t {
356 case _V_NUMBER : return self.toNumber() , nil
357 case _V_ANY :
358 if v, ok := self.packAny().(json.Number); ok {
359 return v, nil
360 } else {
361 return json.Number(""), ErrUnsupportType
362 }
363 default : return json.Number(""), ErrUnsupportType
364 }
365 }
366
367
368
369
370 func (self *Node) String() (string, error) {
371 if err := self.checkRaw(); err != nil {
372 return "", err
373 }
374 switch self.t {
375 case types.V_NULL : return "" , nil
376 case types.V_TRUE : return "true" , nil
377 case types.V_FALSE : return "false", nil
378 case types.V_STRING, _V_NUMBER : return self.toString(), nil
379 case _V_ANY :
380 any := self.packAny()
381 switch v := any.(type) {
382 case bool : return strconv.FormatBool(v), nil
383 case int : return strconv.Itoa(v), nil
384 case int8 : return strconv.Itoa(int(v)), nil
385 case int16 : return strconv.Itoa(int(v)), nil
386 case int32 : return strconv.Itoa(int(v)), nil
387 case int64 : return strconv.Itoa(int(v)), nil
388 case uint : return strconv.Itoa(int(v)), nil
389 case uint8 : return strconv.Itoa(int(v)), nil
390 case uint16 : return strconv.Itoa(int(v)), nil
391 case uint32 : return strconv.Itoa(int(v)), nil
392 case uint64 : return strconv.Itoa(int(v)), nil
393 case float32: return strconv.FormatFloat(float64(v), 'g', -1, 64), nil
394 case float64: return strconv.FormatFloat(float64(v), 'g', -1, 64), nil
395 case string : return v, nil
396 case json.Number: return v.String(), nil
397 default: return "", ErrUnsupportType
398 }
399 default : return "" , ErrUnsupportType
400 }
401 }
402
403
404
405 func (self *Node) StrictString() (string, error) {
406 if err := self.checkRaw(); err != nil {
407 return "", err
408 }
409 switch self.t {
410 case types.V_STRING : return self.toString(), nil
411 case _V_ANY :
412 if v, ok := self.packAny().(string); ok {
413 return v, nil
414 } else {
415 return "", ErrUnsupportType
416 }
417 default : return "", ErrUnsupportType
418 }
419 }
420
421
422
423
424 func (self *Node) Float64() (float64, error) {
425 if err := self.checkRaw(); err != nil {
426 return 0.0, err
427 }
428 switch self.t {
429 case _V_NUMBER, types.V_STRING : return self.toFloat64()
430 case types.V_TRUE : return 1.0, nil
431 case types.V_FALSE : return 0.0, nil
432 case types.V_NULL : return 0.0, nil
433 case _V_ANY :
434 any := self.packAny()
435 switch v := any.(type) {
436 case bool :
437 if v {
438 return 1.0, nil
439 } else {
440 return 0.0, nil
441 }
442 case int : return float64(v), nil
443 case int8 : return float64(v), nil
444 case int16 : return float64(v), nil
445 case int32 : return float64(v), nil
446 case int64 : return float64(v), nil
447 case uint : return float64(v), nil
448 case uint8 : return float64(v), nil
449 case uint16 : return float64(v), nil
450 case uint32 : return float64(v), nil
451 case uint64 : return float64(v), nil
452 case float32: return float64(v), nil
453 case float64: return float64(v), nil
454 case string :
455 if f, err := strconv.ParseFloat(v, 64); err == nil {
456 return float64(f), nil
457 } else {
458 return 0, err
459 }
460 case json.Number:
461 if f, err := v.Float64(); err == nil {
462 return float64(f), nil
463 } else {
464 return 0, err
465 }
466 default : return 0, ErrUnsupportType
467 }
468 default : return 0.0, ErrUnsupportType
469 }
470 }
471
472
473 func (self *Node) StrictFloat64() (float64, error) {
474 if err := self.checkRaw(); err != nil {
475 return 0.0, err
476 }
477 switch self.t {
478 case _V_NUMBER : return self.toFloat64()
479 case _V_ANY :
480 any := self.packAny()
481 switch v := any.(type) {
482 case float32 : return float64(v), nil
483 case float64 : return float64(v), nil
484 default : return 0, ErrUnsupportType
485 }
486 default : return 0.0, ErrUnsupportType
487 }
488 }
489
490
491
492
493
494 func (self *Node) Len() (int, error) {
495 if err := self.checkRaw(); err != nil {
496 return 0, err
497 }
498 if self.t == types.V_ARRAY || self.t == types.V_OBJECT || self.t == _V_ARRAY_LAZY || self.t == _V_OBJECT_LAZY || self.t == types.V_STRING {
499 return int(self.l), nil
500 } else if self.t == _V_NONE || self.t == types.V_NULL {
501 return 0, nil
502 } else {
503 return 0, ErrUnsupportType
504 }
505 }
506
507 func (self Node) len() int {
508 return int(self.l)
509 }
510
511
512 func (self *Node) Cap() (int, error) {
513 if err := self.checkRaw(); err != nil {
514 return 0, err
515 }
516 switch self.t {
517 case types.V_ARRAY: return (*linkedNodes)(self.p).Cap(), nil
518 case types.V_OBJECT: return (*linkedPairs)(self.p).Cap(), nil
519 case _V_ARRAY_LAZY: return (*parseArrayStack)(self.p).v.Cap(), nil
520 case _V_OBJECT_LAZY: return (*parseObjectStack)(self.p).v.Cap(), nil
521 case _V_NONE, types.V_NULL: return 0, nil
522 default: return 0, ErrUnsupportType
523 }
524 }
525
526
527
528
529 func (self *Node) Set(key string, node Node) (bool, error) {
530 if err := self.Check(); err != nil {
531 return false, err
532 }
533 if err := node.Check(); err != nil {
534 return false, err
535 }
536
537 if self.t == _V_NONE || self.t == types.V_NULL {
538 *self = NewObject([]Pair{{key, node}})
539 return false, nil
540 } else if self.itype() != types.V_OBJECT {
541 return false, ErrUnsupportType
542 }
543
544 p := self.Get(key)
545
546 if !p.Exists() {
547
548 if self.len() == 0 {
549 *self = newObject(new(linkedPairs))
550 }
551 s := (*linkedPairs)(self.p)
552 s.Push(Pair{key, node})
553 self.l++
554 return false, nil
555
556 } else if err := p.Check(); err != nil {
557 return false, err
558 }
559
560 *p = node
561 return true, nil
562 }
563
564
565 func (self *Node) SetAny(key string, val interface{}) (bool, error) {
566 return self.Set(key, NewAny(val))
567 }
568
569
570 func (self *Node) Unset(key string) (bool, error) {
571 if err := self.should(types.V_OBJECT, "an object"); err != nil {
572 return false, err
573 }
574
575 if err := self.skipAllKey(); err != nil {
576 return false, err
577 }
578 p, i := self.skipKey(key)
579 if !p.Exists() {
580 return false, nil
581 } else if err := p.Check(); err != nil {
582 return false, err
583 }
584 self.removePair(i)
585 return true, nil
586 }
587
588
589
590
591 func (self *Node) SetByIndex(index int, node Node) (bool, error) {
592 if err := self.Check(); err != nil {
593 return false, err
594 }
595 if err := node.Check(); err != nil {
596 return false, err
597 }
598
599 if index == 0 && (self.t == _V_NONE || self.t == types.V_NULL) {
600 *self = NewArray([]Node{node})
601 return false, nil
602 }
603
604 p := self.Index(index)
605 if !p.Exists() {
606 return false, ErrNotExist
607 } else if err := p.Check(); err != nil {
608 return false, err
609 }
610
611 *p = node
612 return true, nil
613 }
614
615
616 func (self *Node) SetAnyByIndex(index int, val interface{}) (bool, error) {
617 return self.SetByIndex(index, NewAny(val))
618 }
619
620
621
622
623
624 func (self *Node) UnsetByIndex(index int) (bool, error) {
625 if err := self.checkRaw(); err != nil {
626 return false, err
627 }
628
629 var p *Node
630 it := self.itype()
631
632 if it == types.V_ARRAY {
633 if err := self.skipAllIndex(); err != nil {
634 return false, err
635 }
636 p = self.nodeAt(index)
637 } else if it == types.V_OBJECT {
638 if err := self.skipAllKey(); err != nil {
639 return false, err
640 }
641 pr := self.pairAt(index)
642 if pr == nil {
643 return false, ErrNotExist
644 }
645 p = &pr.Value
646 } else {
647 return false, ErrUnsupportType
648 }
649
650 if !p.Exists() {
651 return false, ErrNotExist
652 }
653
654
655 if index == self.len() - 1 {
656 return true, self.Pop()
657 }
658
659
660 if it == types.V_ARRAY {
661 self.removeNode(index)
662 }else if it == types.V_OBJECT {
663 self.removePair(index)
664 }
665 return true, nil
666 }
667
668
669
670
671 func (self *Node) Add(node Node) error {
672 if err := self.Check(); err != nil {
673 return err
674 }
675
676 if self != nil && (self.t == _V_NONE || self.t == types.V_NULL) {
677 *self = NewArray([]Node{node})
678 return nil
679 }
680 if err := self.should(types.V_ARRAY, "an array"); err != nil {
681 return err
682 }
683
684 s, err := self.unsafeArray()
685 if err != nil {
686 return err
687 }
688
689
690 s.Push(node)
691 self.l++
692 return nil
693 }
694
695
696 func (self *Node) Pop() error {
697 if err := self.checkRaw(); err != nil {
698 return err
699 }
700
701 if it := self.itype(); it == types.V_ARRAY {
702 s, err := self.unsafeArray()
703 if err != nil {
704 return err
705 }
706
707 for i := s.Len()-1; i >= 0; i-- {
708 if s.At(i).Exists() {
709 s.Pop()
710 self.l--
711 break
712 }
713 s.Pop()
714 }
715
716 } else if it == types.V_OBJECT {
717 s, err := self.unsafeMap()
718 if err != nil {
719 return err
720 }
721
722 for i := s.Len()-1; i >= 0; i-- {
723 if p := s.At(i); p != nil && p.Value.Exists() {
724 s.Pop()
725 self.l--
726 break
727 }
728 s.Pop()
729 }
730
731 } else {
732 return ErrUnsupportType
733 }
734
735 return nil
736 }
737
738
739
740
741
742 func (self *Node) Move(dst, src int) error {
743 if err := self.should(types.V_ARRAY, "an array"); err != nil {
744 return err
745 }
746
747 s, err := self.unsafeArray()
748 if err != nil {
749 return err
750 }
751
752
753 if l := s.Len(); self.len() != l {
754 di, si := dst, src
755
756 for i := 0; i < l; i++ {
757 if s.At(i).Exists() {
758 di--
759 si--
760 }
761 if di == -1 {
762 dst = i
763 di--
764 }
765 if si == -1 {
766 src = i
767 si--
768 }
769 if di == -2 && si == -2 {
770 break
771 }
772 }
773 }
774
775 s.MoveOne(src, dst)
776 return nil
777 }
778
779
780 func (self *Node) AddAny(val interface{}) error {
781 return self.Add(NewAny(val))
782 }
783
784
785
786
787
788
789 func (self *Node) GetByPath(path ...interface{}) *Node {
790 if !self.Valid() {
791 return self
792 }
793 var s = self
794 for _, p := range path {
795 switch p := p.(type) {
796 case int:
797 s = s.Index(p)
798 if !s.Valid() {
799 return s
800 }
801 case string:
802 s = s.Get(p)
803 if !s.Valid() {
804 return s
805 }
806 default:
807 panic("path must be either int or string")
808 }
809 }
810 return s
811 }
812
813
814 func (self *Node) Get(key string) *Node {
815 if err := self.should(types.V_OBJECT, "an object"); err != nil {
816 return unwrapError(err)
817 }
818 n, _ := self.skipKey(key)
819 return n
820 }
821
822
823
824 func (self *Node) Index(idx int) *Node {
825 if err := self.checkRaw(); err != nil {
826 return unwrapError(err)
827 }
828
829 it := self.itype()
830 if it == types.V_ARRAY {
831 return self.skipIndex(idx)
832
833 }else if it == types.V_OBJECT {
834 pr := self.skipIndexPair(idx)
835 if pr == nil {
836 return newError(_ERR_NOT_FOUND, "value not exists")
837 }
838 return &pr.Value
839
840 } else {
841 return newError(_ERR_UNSUPPORT_TYPE, fmt.Sprintf("unsupported type: %v", self.itype()))
842 }
843 }
844
845
846
847 func (self *Node) IndexPair(idx int) *Pair {
848 if err := self.should(types.V_OBJECT, "an object"); err != nil {
849 return nil
850 }
851 return self.skipIndexPair(idx)
852 }
853
854
855
856 func (self *Node) IndexOrGet(idx int, key string) *Node {
857 if err := self.should(types.V_OBJECT, "an object"); err != nil {
858 return unwrapError(err)
859 }
860
861 pr := self.skipIndexPair(idx)
862 if pr != nil && pr.Key == key {
863 return &pr.Value
864 }
865 n, _ := self.skipKey(key)
866 return n
867 }
868
869
870
871
872 func (self *Node) Map() (map[string]interface{}, error) {
873 if self.isAny() {
874 any := self.packAny()
875 if v, ok := any.(map[string]interface{}); ok {
876 return v, nil
877 } else {
878 return nil, ErrUnsupportType
879 }
880 }
881 if err := self.should(types.V_OBJECT, "an object"); err != nil {
882 return nil, err
883 }
884 if err := self.loadAllKey(); err != nil {
885 return nil, err
886 }
887 return self.toGenericObject()
888 }
889
890
891 func (self *Node) MapUseNumber() (map[string]interface{}, error) {
892 if self.isAny() {
893 any := self.packAny()
894 if v, ok := any.(map[string]interface{}); ok {
895 return v, nil
896 } else {
897 return nil, ErrUnsupportType
898 }
899 }
900 if err := self.should(types.V_OBJECT, "an object"); err != nil {
901 return nil, err
902 }
903 if err := self.loadAllKey(); err != nil {
904 return nil, err
905 }
906 return self.toGenericObjectUseNumber()
907 }
908
909
910
911 func (self *Node) MapUseNode() (map[string]Node, error) {
912 if self.isAny() {
913 any := self.packAny()
914 if v, ok := any.(map[string]Node); ok {
915 return v, nil
916 } else {
917 return nil, ErrUnsupportType
918 }
919 }
920 if err := self.should(types.V_OBJECT, "an object"); err != nil {
921 return nil, err
922 }
923 if err := self.skipAllKey(); err != nil {
924 return nil, err
925 }
926 return self.toGenericObjectUseNode()
927 }
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944 func (self *Node) unsafeMap() (*linkedPairs, error) {
945 if err := self.skipAllKey(); err != nil {
946 return nil, err
947 }
948 if self.p == nil {
949 *self = newObject(new(linkedPairs))
950 }
951 return (*linkedPairs)(self.p), nil
952 }
953
954
955
956 func (self *Node) SortKeys(recurse bool) error {
957
958 if err := self.checkRaw(); err != nil {
959 return err
960 }
961 if self.itype() == types.V_OBJECT {
962 return self.sortKeys(recurse)
963 } else {
964 var err error
965 err2 := self.ForEach(func(path Sequence, node *Node) bool {
966 it := node.itype()
967 if it == types.V_ARRAY || it == types.V_OBJECT {
968 err = node.SortKeys(recurse)
969 if err != nil {
970 return false
971 }
972 }
973 return true
974 })
975 if err != nil {
976 return err
977 }
978 return err2
979 }
980 }
981
982 func (self *Node) sortKeys(recurse bool) (err error) {
983 ps, err := self.unsafeMap()
984 if err != nil {
985 return err
986 }
987 ps.Sort()
988 if recurse {
989 var sc Scanner
990 sc = func(path Sequence, node *Node) bool {
991 if node.itype() == types.V_OBJECT {
992 if err := node.sortKeys(recurse); err != nil {
993 return false
994 }
995 }
996 if node.itype() == types.V_ARRAY {
997 if err := node.ForEach(sc); err != nil {
998 return false
999 }
1000 }
1001 return true
1002 }
1003 if err := self.ForEach(sc); err != nil {
1004 return err
1005 }
1006 }
1007 return nil
1008 }
1009
1010
1011 func (self *Node) Array() ([]interface{}, error) {
1012 if self.isAny() {
1013 any := self.packAny()
1014 if v, ok := any.([]interface{}); ok {
1015 return v, nil
1016 } else {
1017 return nil, ErrUnsupportType
1018 }
1019 }
1020 if err := self.should(types.V_ARRAY, "an array"); err != nil {
1021 return nil, err
1022 }
1023 if err := self.loadAllIndex(); err != nil {
1024 return nil, err
1025 }
1026 return self.toGenericArray()
1027 }
1028
1029
1030 func (self *Node) ArrayUseNumber() ([]interface{}, error) {
1031 if self.isAny() {
1032 any := self.packAny()
1033 if v, ok := any.([]interface{}); ok {
1034 return v, nil
1035 } else {
1036 return nil, ErrUnsupportType
1037 }
1038 }
1039 if err := self.should(types.V_ARRAY, "an array"); err != nil {
1040 return nil, err
1041 }
1042 if err := self.loadAllIndex(); err != nil {
1043 return nil, err
1044 }
1045 return self.toGenericArrayUseNumber()
1046 }
1047
1048
1049
1050 func (self *Node) ArrayUseNode() ([]Node, error) {
1051 if self.isAny() {
1052 any := self.packAny()
1053 if v, ok := any.([]Node); ok {
1054 return v, nil
1055 } else {
1056 return nil, ErrUnsupportType
1057 }
1058 }
1059 if err := self.should(types.V_ARRAY, "an array"); err != nil {
1060 return nil, err
1061 }
1062 if err := self.skipAllIndex(); err != nil {
1063 return nil, err
1064 }
1065 return self.toGenericArrayUseNode()
1066 }
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083 func (self *Node) unsafeArray() (*linkedNodes, error) {
1084 if err := self.skipAllIndex(); err != nil {
1085 return nil, err
1086 }
1087 if self.p == nil {
1088 *self = newArray(new(linkedNodes))
1089 }
1090 return (*linkedNodes)(self.p), nil
1091 }
1092
1093
1094
1095
1096 func (self *Node) Interface() (interface{}, error) {
1097 if err := self.checkRaw(); err != nil {
1098 return nil, err
1099 }
1100 switch self.t {
1101 case V_ERROR : return nil, self.Check()
1102 case types.V_NULL : return nil, nil
1103 case types.V_TRUE : return true, nil
1104 case types.V_FALSE : return false, nil
1105 case types.V_ARRAY : return self.toGenericArray()
1106 case types.V_OBJECT : return self.toGenericObject()
1107 case types.V_STRING : return self.toString(), nil
1108 case _V_NUMBER :
1109 v, err := self.toFloat64()
1110 if err != nil {
1111 return nil, err
1112 }
1113 return v, nil
1114 case _V_ARRAY_LAZY :
1115 if err := self.loadAllIndex(); err != nil {
1116 return nil, err
1117 }
1118 return self.toGenericArray()
1119 case _V_OBJECT_LAZY :
1120 if err := self.loadAllKey(); err != nil {
1121 return nil, err
1122 }
1123 return self.toGenericObject()
1124 case _V_ANY:
1125 switch v := self.packAny().(type) {
1126 case Node : return v.Interface()
1127 case *Node: return v.Interface()
1128 default : return v, nil
1129 }
1130 default : return nil, ErrUnsupportType
1131 }
1132 }
1133
1134 func (self *Node) packAny() interface{} {
1135 return *(*interface{})(self.p)
1136 }
1137
1138
1139
1140 func (self *Node) InterfaceUseNumber() (interface{}, error) {
1141 if err := self.checkRaw(); err != nil {
1142 return nil, err
1143 }
1144 switch self.t {
1145 case V_ERROR : return nil, self.Check()
1146 case types.V_NULL : return nil, nil
1147 case types.V_TRUE : return true, nil
1148 case types.V_FALSE : return false, nil
1149 case types.V_ARRAY : return self.toGenericArrayUseNumber()
1150 case types.V_OBJECT : return self.toGenericObjectUseNumber()
1151 case types.V_STRING : return self.toString(), nil
1152 case _V_NUMBER : return self.toNumber(), nil
1153 case _V_ARRAY_LAZY :
1154 if err := self.loadAllIndex(); err != nil {
1155 return nil, err
1156 }
1157 return self.toGenericArrayUseNumber()
1158 case _V_OBJECT_LAZY :
1159 if err := self.loadAllKey(); err != nil {
1160 return nil, err
1161 }
1162 return self.toGenericObjectUseNumber()
1163 case _V_ANY : return self.packAny(), nil
1164 default : return nil, ErrUnsupportType
1165 }
1166 }
1167
1168
1169
1170 func (self *Node) InterfaceUseNode() (interface{}, error) {
1171 if err := self.checkRaw(); err != nil {
1172 return nil, err
1173 }
1174 switch self.t {
1175 case types.V_ARRAY : return self.toGenericArrayUseNode()
1176 case types.V_OBJECT : return self.toGenericObjectUseNode()
1177 case _V_ARRAY_LAZY :
1178 if err := self.skipAllIndex(); err != nil {
1179 return nil, err
1180 }
1181 return self.toGenericArrayUseNode()
1182 case _V_OBJECT_LAZY :
1183 if err := self.skipAllKey(); err != nil {
1184 return nil, err
1185 }
1186 return self.toGenericObjectUseNode()
1187 default : return *self, self.Check()
1188 }
1189 }
1190
1191
1192
1193 func (self *Node) LoadAll() error {
1194 if self.IsRaw() {
1195 self.parseRaw(true)
1196 return self.Check()
1197 }
1198
1199 switch self.itype() {
1200 case types.V_ARRAY:
1201 e := self.len()
1202 if err := self.loadAllIndex(); err != nil {
1203 return err
1204 }
1205 for i := 0; i < e; i++ {
1206 n := self.nodeAt(i)
1207 if n.IsRaw() {
1208 n.parseRaw(true)
1209 }
1210 if err := n.Check(); err != nil {
1211 return err
1212 }
1213 }
1214 return nil
1215 case types.V_OBJECT:
1216 e := self.len()
1217 if err := self.loadAllKey(); err != nil {
1218 return err
1219 }
1220 for i := 0; i < e; i++ {
1221 n := self.pairAt(i)
1222 if n.Value.IsRaw() {
1223 n.Value.parseRaw(true)
1224 }
1225 if err := n.Value.Check(); err != nil {
1226 return err
1227 }
1228 }
1229 return nil
1230 default:
1231 return self.Check()
1232 }
1233 }
1234
1235
1236
1237 func (self *Node) Load() error {
1238 if err := self.checkRaw(); err != nil {
1239 return err
1240 }
1241
1242 switch self.t {
1243 case _V_ARRAY_LAZY:
1244 return self.skipAllIndex()
1245 case _V_OBJECT_LAZY:
1246 return self.skipAllKey()
1247 default:
1248 return self.Check()
1249 }
1250 }
1251
1252
1253
1254 func (self *Node) should(t types.ValueType, s string) error {
1255 if err := self.checkRaw(); err != nil {
1256 return err
1257 }
1258 if self.itype() != t {
1259 return ErrUnsupportType
1260 }
1261 return nil
1262 }
1263
1264 func (self *Node) nodeAt(i int) *Node {
1265 var p *linkedNodes
1266 if self.isLazy() {
1267 _, stack := self.getParserAndArrayStack()
1268 p = &stack.v
1269 } else {
1270 p = (*linkedNodes)(self.p)
1271 if l := p.Len(); l != self.len() {
1272
1273 for j:=0; j<l; j++ {
1274 v := p.At(j)
1275 if v.Exists() {
1276 i--
1277 }
1278 if i < 0 {
1279 return v
1280 }
1281 }
1282 return nil
1283 }
1284 }
1285 return p.At(i)
1286 }
1287
1288 func (self *Node) pairAt(i int) *Pair {
1289 var p *linkedPairs
1290 if self.isLazy() {
1291 _, stack := self.getParserAndObjectStack()
1292 p = &stack.v
1293 } else {
1294 p = (*linkedPairs)(self.p)
1295 if l := p.Len(); l != self.len() {
1296
1297 for j:=0; j<l; j++ {
1298 v := p.At(j)
1299 if v != nil && v.Value.Exists() {
1300 i--
1301 }
1302 if i < 0 {
1303 return v
1304 }
1305 }
1306 return nil
1307 }
1308 }
1309 return p.At(i)
1310 }
1311
1312 func (self *Node) skipAllIndex() error {
1313 if !self.isLazy() {
1314 return nil
1315 }
1316 var err types.ParsingError
1317 parser, stack := self.getParserAndArrayStack()
1318 parser.skipValue = true
1319 parser.noLazy = true
1320 *self, err = parser.decodeArray(&stack.v)
1321 if err != 0 {
1322 return parser.ExportError(err)
1323 }
1324 return nil
1325 }
1326
1327 func (self *Node) skipAllKey() error {
1328 if !self.isLazy() {
1329 return nil
1330 }
1331 var err types.ParsingError
1332 parser, stack := self.getParserAndObjectStack()
1333 parser.skipValue = true
1334 parser.noLazy = true
1335 *self, err = parser.decodeObject(&stack.v)
1336 if err != 0 {
1337 return parser.ExportError(err)
1338 }
1339 return nil
1340 }
1341
1342 func (self *Node) skipKey(key string) (*Node, int) {
1343 nb := self.len()
1344 lazy := self.isLazy()
1345
1346 if nb > 0 {
1347
1348 var p *Pair
1349 var i int
1350 if lazy {
1351 s := (*parseObjectStack)(self.p)
1352 p, i = s.v.Get(key)
1353 } else {
1354 p, i = (*linkedPairs)(self.p).Get(key)
1355 }
1356
1357 if p != nil {
1358 return &p.Value, i
1359 }
1360 }
1361
1362
1363 if !lazy {
1364 return nil, -1
1365 }
1366
1367
1368 for last, i := self.skipNextPair(), nb; last != nil; last, i = self.skipNextPair(), i+1 {
1369 if last.Value.Check() != nil {
1370 return &last.Value, -1
1371 }
1372 if last.Key == key {
1373 return &last.Value, i
1374 }
1375 }
1376
1377 return nil, -1
1378 }
1379
1380 func (self *Node) skipIndex(index int) *Node {
1381 nb := self.len()
1382 if nb > index {
1383 v := self.nodeAt(index)
1384 return v
1385 }
1386 if !self.isLazy() {
1387 return nil
1388 }
1389
1390
1391 for last := self.skipNextNode(); last != nil; last = self.skipNextNode(){
1392 if last.Check() != nil {
1393 return last
1394 }
1395 if self.len() > index {
1396 return last
1397 }
1398 }
1399
1400 return nil
1401 }
1402
1403 func (self *Node) skipIndexPair(index int) *Pair {
1404 nb := self.len()
1405 if nb > index {
1406 return self.pairAt(index)
1407 }
1408 if !self.isLazy() {
1409 return nil
1410 }
1411
1412
1413 for last := self.skipNextPair(); last != nil; last = self.skipNextPair(){
1414 if last.Value.Check() != nil {
1415 return last
1416 }
1417 if self.len() > index {
1418 return last
1419 }
1420 }
1421
1422 return nil
1423 }
1424
1425 func (self *Node) loadAllIndex() error {
1426 if !self.isLazy() {
1427 return nil
1428 }
1429 var err types.ParsingError
1430 parser, stack := self.getParserAndArrayStack()
1431 parser.noLazy = true
1432 *self, err = parser.decodeArray(&stack.v)
1433 if err != 0 {
1434 return parser.ExportError(err)
1435 }
1436 return nil
1437 }
1438
1439 func (self *Node) loadAllKey() error {
1440 if !self.isLazy() {
1441 return nil
1442 }
1443 var err types.ParsingError
1444 parser, stack := self.getParserAndObjectStack()
1445 parser.noLazy = true
1446 *self, err = parser.decodeObject(&stack.v)
1447 if err != 0 {
1448 return parser.ExportError(err)
1449 }
1450 return nil
1451 }
1452
1453 func (self *Node) removeNode(i int) {
1454 node := self.nodeAt(i)
1455 if node == nil {
1456 return
1457 }
1458 *node = Node{}
1459
1460 self.l--
1461 }
1462
1463 func (self *Node) removePair(i int) {
1464 last := self.pairAt(i)
1465 if last == nil {
1466 return
1467 }
1468 *last = Pair{}
1469
1470 self.l--
1471 }
1472
1473 func (self *Node) toGenericArray() ([]interface{}, error) {
1474 nb := self.len()
1475 if nb == 0 {
1476 return []interface{}{}, nil
1477 }
1478 ret := make([]interface{}, 0, nb)
1479
1480
1481 it := self.values()
1482 for v := it.next(); v != nil; v = it.next() {
1483 vv, err := v.Interface()
1484 if err != nil {
1485 return nil, err
1486 }
1487 ret = append(ret, vv)
1488 }
1489
1490
1491 return ret, nil
1492 }
1493
1494 func (self *Node) toGenericArrayUseNumber() ([]interface{}, error) {
1495 nb := self.len()
1496 if nb == 0 {
1497 return []interface{}{}, nil
1498 }
1499 ret := make([]interface{}, 0, nb)
1500
1501
1502 it := self.values()
1503 for v := it.next(); v != nil; v = it.next() {
1504 vv, err := v.InterfaceUseNumber()
1505 if err != nil {
1506 return nil, err
1507 }
1508 ret = append(ret, vv)
1509 }
1510
1511
1512 return ret, nil
1513 }
1514
1515 func (self *Node) toGenericArrayUseNode() ([]Node, error) {
1516 var nb = self.len()
1517 if nb == 0 {
1518 return []Node{}, nil
1519 }
1520
1521 var s = (*linkedNodes)(self.p)
1522 var out = make([]Node, nb)
1523 s.ToSlice(out)
1524
1525 return out, nil
1526 }
1527
1528 func (self *Node) toGenericObject() (map[string]interface{}, error) {
1529 nb := self.len()
1530 if nb == 0 {
1531 return map[string]interface{}{}, nil
1532 }
1533 ret := make(map[string]interface{}, nb)
1534
1535
1536 it := self.properties()
1537 for v := it.next(); v != nil; v = it.next() {
1538 vv, err := v.Value.Interface()
1539 if err != nil {
1540 return nil, err
1541 }
1542 ret[v.Key] = vv
1543 }
1544
1545
1546 return ret, nil
1547 }
1548
1549
1550 func (self *Node) toGenericObjectUseNumber() (map[string]interface{}, error) {
1551 nb := self.len()
1552 if nb == 0 {
1553 return map[string]interface{}{}, nil
1554 }
1555 ret := make(map[string]interface{}, nb)
1556
1557
1558 it := self.properties()
1559 for v := it.next(); v != nil; v = it.next() {
1560 vv, err := v.Value.InterfaceUseNumber()
1561 if err != nil {
1562 return nil, err
1563 }
1564 ret[v.Key] = vv
1565 }
1566
1567
1568 return ret, nil
1569 }
1570
1571 func (self *Node) toGenericObjectUseNode() (map[string]Node, error) {
1572 var nb = self.len()
1573 if nb == 0 {
1574 return map[string]Node{}, nil
1575 }
1576
1577 var s = (*linkedPairs)(self.p)
1578 var out = make(map[string]Node, nb)
1579 s.ToMap(out)
1580
1581
1582 return out, nil
1583 }
1584
1585
1586
1587 var (
1588 nullNode = Node{t: types.V_NULL}
1589 trueNode = Node{t: types.V_TRUE}
1590 falseNode = Node{t: types.V_FALSE}
1591 )
1592
1593
1594
1595 func NewRaw(json string) Node {
1596 parser := NewParserObj(json)
1597 start, err := parser.skip()
1598 if err != 0 {
1599 return *newError(err, err.Message())
1600 }
1601 it := switchRawType(parser.s[start])
1602 if it == _V_NONE {
1603 return Node{}
1604 }
1605 return newRawNode(parser.s[start:parser.p], it)
1606 }
1607
1608
1609
1610 func NewAny(any interface{}) Node {
1611 switch n := any.(type) {
1612 case Node:
1613 return n
1614 case *Node:
1615 return *n
1616 default:
1617 return Node{
1618 t: _V_ANY,
1619 p: unsafe.Pointer(&any),
1620 }
1621 }
1622 }
1623
1624
1625 func NewBytes(src []byte) Node {
1626 if len(src) == 0 {
1627 panic("empty src bytes")
1628 }
1629 out := encodeBase64(src)
1630 return NewString(out)
1631 }
1632
1633
1634 func NewNull() Node {
1635 return Node{
1636 p: nil,
1637 t: types.V_NULL,
1638 }
1639 }
1640
1641
1642
1643
1644 func NewBool(v bool) Node {
1645 var t = types.V_FALSE
1646 if v {
1647 t = types.V_TRUE
1648 }
1649 return Node{
1650 p: nil,
1651 t: t,
1652 }
1653 }
1654
1655
1656
1657 func NewNumber(v string) Node {
1658 return Node{
1659 l: uint(len(v)),
1660 p: rt.StrPtr(v),
1661 t: _V_NUMBER,
1662 }
1663 }
1664
1665 func (node Node) toNumber() json.Number {
1666 return json.Number(rt.StrFrom(node.p, int64(node.l)))
1667 }
1668
1669 func (self Node) toString() string {
1670 return rt.StrFrom(self.p, int64(self.l))
1671 }
1672
1673 func (node Node) toFloat64() (float64, error) {
1674 ret, err := node.toNumber().Float64()
1675 if err != nil {
1676 return 0, err
1677 }
1678 return ret, nil
1679 }
1680
1681 func (node Node) toInt64() (int64, error) {
1682 ret,err := node.toNumber().Int64()
1683 if err != nil {
1684 return 0, err
1685 }
1686 return ret, nil
1687 }
1688
1689 func newBytes(v []byte) Node {
1690 return Node{
1691 t: types.V_STRING,
1692 p: mem2ptr(v),
1693 l: uint(len(v)),
1694 }
1695 }
1696
1697
1698
1699
1700
1701 func NewString(v string) Node {
1702 return Node{
1703 t: types.V_STRING,
1704 p: rt.StrPtr(v),
1705 l: uint(len(v)),
1706 }
1707 }
1708
1709
1710
1711 func NewArray(v []Node) Node {
1712 s := new(linkedNodes)
1713 s.FromSlice(v)
1714 return newArray(s)
1715 }
1716
1717 func newArray(v *linkedNodes) Node {
1718 return Node{
1719 t: types.V_ARRAY,
1720 l: uint(v.Len()),
1721 p: unsafe.Pointer(v),
1722 }
1723 }
1724
1725 func (self *Node) setArray(v *linkedNodes) {
1726 self.t = types.V_ARRAY
1727 self.l = uint(v.Len())
1728 self.p = unsafe.Pointer(v)
1729 }
1730
1731
1732
1733 func NewObject(v []Pair) Node {
1734 s := new(linkedPairs)
1735 s.FromSlice(v)
1736 return newObject(s)
1737 }
1738
1739 func newObject(v *linkedPairs) Node {
1740 return Node{
1741 t: types.V_OBJECT,
1742 l: uint(v.Len()),
1743 p: unsafe.Pointer(v),
1744 }
1745 }
1746
1747 func (self *Node) setObject(v *linkedPairs) {
1748 self.t = types.V_OBJECT
1749 self.l = uint(v.Len())
1750 self.p = unsafe.Pointer(v)
1751 }
1752
1753 func newRawNode(str string, typ types.ValueType) Node {
1754 return Node{
1755 t: _V_RAW | typ,
1756 p: rt.StrPtr(str),
1757 l: uint(len(str)),
1758 }
1759 }
1760
1761 func (self *Node) parseRaw(full bool) {
1762 raw := self.toString()
1763 parser := NewParserObj(raw)
1764 if full {
1765 parser.noLazy = true
1766 parser.skipValue = false
1767 }
1768 var e types.ParsingError
1769 *self, e = parser.Parse()
1770 if e != 0 {
1771 *self = *newSyntaxError(parser.syntaxError(e))
1772 }
1773 }
1774
1775 var typeJumpTable = [256]types.ValueType{
1776 '"' : types.V_STRING,
1777 '-' : _V_NUMBER,
1778 '0' : _V_NUMBER,
1779 '1' : _V_NUMBER,
1780 '2' : _V_NUMBER,
1781 '3' : _V_NUMBER,
1782 '4' : _V_NUMBER,
1783 '5' : _V_NUMBER,
1784 '6' : _V_NUMBER,
1785 '7' : _V_NUMBER,
1786 '8' : _V_NUMBER,
1787 '9' : _V_NUMBER,
1788 '[' : types.V_ARRAY,
1789 'f' : types.V_FALSE,
1790 'n' : types.V_NULL,
1791 't' : types.V_TRUE,
1792 '{' : types.V_OBJECT,
1793 }
1794
1795 func switchRawType(c byte) types.ValueType {
1796 return typeJumpTable[c]
1797 }
1798
View as plain text