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