github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/ast/node.go (about) 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 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 // UnmarshalJSON is just an adapter to json.Unmarshaler. 62 // If you want better performance, use Searcher.GetByPath() directly 63 func (self *Node) UnmarshalJSON(data []byte) (err error) { 64 *self = NewRaw(string(data)) 65 return self.Check() 66 } 67 68 /** Node Type Accessor **/ 69 70 // Type returns json type represented by the node 71 // It will be one of belows: 72 // V_NONE = 0 (empty node, key not exists) 73 // V_ERROR = 1 (error node) 74 // V_NULL = 2 (json value `null`, key exists) 75 // V_TRUE = 3 (json value `true`) 76 // V_FALSE = 4 (json value `false`) 77 // V_ARRAY = 5 (json value array) 78 // V_OBJECT = 6 (json value object) 79 // V_STRING = 7 (json value string) 80 // V_NUMBER = 33 (json value number ) 81 // V_ANY = 34 (golang interface{}) 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 // Exists returns false only if the self is nil or empty node V_NONE 91 func (self *Node) Exists() bool { 92 return self.Valid() && self.t != _V_NONE 93 } 94 95 // Valid reports if self is NOT V_ERROR or nil 96 func (self *Node) Valid() bool { 97 if self == nil { 98 return false 99 } 100 return self.t != V_ERROR 101 } 102 103 // Check checks if the node itself is valid, and return: 104 // - ErrNotExist If the node is nil 105 // - Its underlying error If the node is V_ERROR 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 // IsRaw returns true if node's underlying value is raw json 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 /** Simple Value Methods **/ 130 131 // Raw returns json representation of the node, 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 // Bool returns bool value represented by this node, 154 // including types.V_TRUE|V_FALSE|V_NUMBER|V_STRING|V_ANY|V_NULL, 155 // V_NONE will return error 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 // Int64 casts the node to int64 value, 205 // including V_NUMBER|V_TRUE|V_FALSE|V_ANY|V_STRING 206 // V_NONE it will return error 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 // StrictInt64 exports underlying int64 value, including V_NUMBER, V_ANY 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 // Number casts node to float64, 302 // including V_NUMBER|V_TRUE|V_FALSE|V_ANY|V_STRING|V_NULL, 303 // V_NONE it will return error 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 // Number exports underlying float64 value, including V_NUMBER, V_ANY of json.Number 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 // String cast node to string, 368 // including V_NUMBER|V_TRUE|V_FALSE|V_ANY|V_STRING|V_NULL, 369 // V_NONE it will return error 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 // StrictString returns string value (unescaped), includeing V_STRING, V_ANY of string. 404 // In other cases, it will return empty string. 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 // Float64 cast node to float64, 422 // including V_NUMBER|V_TRUE|V_FALSE|V_ANY|V_STRING|V_NULL, 423 // V_NONE it will return error 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 // Float64 exports underlying float64 value, includeing V_NUMBER, V_ANY 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 /** Sequencial Value Methods **/ 491 492 // Len returns children count of a array|object|string node 493 // WARN: For partially loaded node, it also works but only counts the parsed children 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 // Cap returns malloc capacity of a array|object node for children 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 // Set sets the node of given key under self, and reports if the key has existed. 527 // 528 // If self is V_NONE or V_NULL, it becomes V_OBJECT and sets the node at the key. 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 // self must be fully-loaded here 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 // SetAny wraps val with V_ANY node, and Set() the node. 565 func (self *Node) SetAny(key string, val interface{}) (bool, error) { 566 return self.Set(key, NewAny(val)) 567 } 568 569 // Unset REMOVE (soft) the node of given key under object parent, and reports if the key has existed. 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 // NOTICE: must get acurate length before deduct 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.removePairAt(i) 585 return true, nil 586 } 587 588 // SetByIndex sets the node of given index, and reports if the key has existed. 589 // 590 // The index must be within self's children. 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 // SetAny wraps val with V_ANY node, and SetByIndex() the node. 616 func (self *Node) SetAnyByIndex(index int, val interface{}) (bool, error) { 617 return self.SetByIndex(index, NewAny(val)) 618 } 619 620 // UnsetByIndex REOMVE (softly) the node of given index. 621 // 622 // WARN: this will change address of elements, which is a dangerous action. 623 // Use Unset() for object or Pop() for array instead. 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 // last elem 655 if index == self.len() - 1 { 656 return true, self.Pop() 657 } 658 659 // not last elem, self.len() change but linked-chunk not change 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 // Add appends the given node under self. 669 // 670 // If self is V_NONE or V_NULL, it becomes V_ARRAY and sets the node at index 0. 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 // Notice: array won't have unset node in tail 690 s.Push(node) 691 self.l++ 692 return nil 693 } 694 695 // Pop remove the last child of the V_Array or V_Object node. 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 // remove tail unset nodes 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 // remove tail unset nodes 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 // Move moves the child at src index to dst index, 739 // meanwhile slides sliblings from src+1 to dst. 740 // 741 // WARN: this will change address of elements, which is a dangerous action. 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 // check if any unset node exists 753 if l := s.Len(); self.len() != l { 754 di, si := dst, src 755 // find real pos of src and dst 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 // SetAny wraps val with V_ANY node, and Add() the node. 780 func (self *Node) AddAny(val interface{}) error { 781 return self.Add(NewAny(val)) 782 } 783 784 // GetByPath load given path on demands, 785 // which only ensure nodes before this path got parsed. 786 // 787 // Note, the api expects the json is well-formed at least, 788 // otherwise it may return unexpected result. 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 // Get loads given key of an object node on demands 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 // Index indexies node at given idx, 823 // node type CAN be either V_OBJECT or V_ARRAY 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 // IndexPair indexies pair at given idx, 846 // node type MUST be either V_OBJECT 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 func (self *Node) indexOrGet(idx int, key string) (*Node, int) { 855 if err := self.should(types.V_OBJECT, "an object"); err != nil { 856 return unwrapError(err), idx 857 } 858 859 pr := self.skipIndexPair(idx) 860 if pr != nil && pr.Key == key { 861 return &pr.Value, idx 862 } 863 864 return self.skipKey(key) 865 } 866 867 // IndexOrGet firstly use idx to index a value and check if its key matches 868 // If not, then use the key to search value 869 func (self *Node) IndexOrGet(idx int, key string) *Node { 870 node, _ := self.indexOrGet(idx, key) 871 return node 872 } 873 874 // IndexOrGetWithIdx attempts to retrieve a node by index and key, returning the node and its correct index. 875 // If the key does not match at the given index, it searches by key and returns the node with its updated index. 876 func (self *Node) IndexOrGetWithIdx(idx int, key string) (*Node, int) { 877 return self.indexOrGet(idx, key) 878 } 879 880 /** Generic Value Converters **/ 881 882 // Map loads all keys of an object node 883 func (self *Node) Map() (map[string]interface{}, error) { 884 if self.isAny() { 885 any := self.packAny() 886 if v, ok := any.(map[string]interface{}); ok { 887 return v, nil 888 } else { 889 return nil, ErrUnsupportType 890 } 891 } 892 if err := self.should(types.V_OBJECT, "an object"); err != nil { 893 return nil, err 894 } 895 if err := self.loadAllKey(); err != nil { 896 return nil, err 897 } 898 return self.toGenericObject() 899 } 900 901 // MapUseNumber loads all keys of an object node, with numeric nodes casted to json.Number 902 func (self *Node) MapUseNumber() (map[string]interface{}, error) { 903 if self.isAny() { 904 any := self.packAny() 905 if v, ok := any.(map[string]interface{}); ok { 906 return v, nil 907 } else { 908 return nil, ErrUnsupportType 909 } 910 } 911 if err := self.should(types.V_OBJECT, "an object"); err != nil { 912 return nil, err 913 } 914 if err := self.loadAllKey(); err != nil { 915 return nil, err 916 } 917 return self.toGenericObjectUseNumber() 918 } 919 920 // MapUseNode scans both parsed and non-parsed chidren nodes, 921 // and map them by their keys 922 func (self *Node) MapUseNode() (map[string]Node, error) { 923 if self.isAny() { 924 any := self.packAny() 925 if v, ok := any.(map[string]Node); ok { 926 return v, nil 927 } else { 928 return nil, ErrUnsupportType 929 } 930 } 931 if err := self.should(types.V_OBJECT, "an object"); err != nil { 932 return nil, err 933 } 934 if err := self.skipAllKey(); err != nil { 935 return nil, err 936 } 937 return self.toGenericObjectUseNode() 938 } 939 940 // MapUnsafe exports the underlying pointer to its children map 941 // WARN: don't use it unless you know what you are doing 942 // 943 // Deprecated: this API now returns copied nodes instead of directly reference, 944 // func (self *Node) UnsafeMap() ([]Pair, error) { 945 // if err := self.should(types.V_OBJECT, "an object"); err != nil { 946 // return nil, err 947 // } 948 // if err := self.skipAllKey(); err != nil { 949 // return nil, err 950 // } 951 // return self.toGenericObjectUsePair() 952 // } 953 954 //go:nocheckptr 955 func (self *Node) unsafeMap() (*linkedPairs, error) { 956 if err := self.skipAllKey(); err != nil { 957 return nil, err 958 } 959 if self.p == nil { 960 *self = newObject(new(linkedPairs)) 961 } 962 return (*linkedPairs)(self.p), nil 963 } 964 965 // SortKeys sorts children of a V_OBJECT node in ascending key-order. 966 // If recurse is true, it recursively sorts children's children as long as a V_OBJECT node is found. 967 func (self *Node) SortKeys(recurse bool) error { 968 // check raw node first 969 if err := self.checkRaw(); err != nil { 970 return err 971 } 972 if self.itype() == types.V_OBJECT { 973 return self.sortKeys(recurse) 974 } else if self.itype() == types.V_ARRAY { 975 var err error 976 err2 := self.ForEach(func(path Sequence, node *Node) bool { 977 it := node.itype() 978 if it == types.V_ARRAY || it == types.V_OBJECT { 979 err = node.SortKeys(recurse) 980 if err != nil { 981 return false 982 } 983 } 984 return true 985 }) 986 if err != nil { 987 return err 988 } 989 return err2 990 } else { 991 return nil 992 } 993 } 994 995 func (self *Node) sortKeys(recurse bool) (err error) { 996 // check raw node first 997 if err := self.checkRaw(); err != nil { 998 return err 999 } 1000 ps, err := self.unsafeMap() 1001 if err != nil { 1002 return err 1003 } 1004 ps.Sort() 1005 if recurse { 1006 var sc Scanner 1007 sc = func(path Sequence, node *Node) bool { 1008 if node.itype() == types.V_OBJECT { 1009 if err := node.sortKeys(recurse); err != nil { 1010 return false 1011 } 1012 } 1013 if node.itype() == types.V_ARRAY { 1014 if err := node.ForEach(sc); err != nil { 1015 return false 1016 } 1017 } 1018 return true 1019 } 1020 if err := self.ForEach(sc); err != nil { 1021 return err 1022 } 1023 } 1024 return nil 1025 } 1026 1027 // Array loads all indexes of an array node 1028 func (self *Node) Array() ([]interface{}, error) { 1029 if self.isAny() { 1030 any := self.packAny() 1031 if v, ok := any.([]interface{}); ok { 1032 return v, nil 1033 } else { 1034 return nil, ErrUnsupportType 1035 } 1036 } 1037 if err := self.should(types.V_ARRAY, "an array"); err != nil { 1038 return nil, err 1039 } 1040 if err := self.loadAllIndex(); err != nil { 1041 return nil, err 1042 } 1043 return self.toGenericArray() 1044 } 1045 1046 // ArrayUseNumber loads all indexes of an array node, with numeric nodes casted to json.Number 1047 func (self *Node) ArrayUseNumber() ([]interface{}, error) { 1048 if self.isAny() { 1049 any := self.packAny() 1050 if v, ok := any.([]interface{}); ok { 1051 return v, nil 1052 } else { 1053 return nil, ErrUnsupportType 1054 } 1055 } 1056 if err := self.should(types.V_ARRAY, "an array"); err != nil { 1057 return nil, err 1058 } 1059 if err := self.loadAllIndex(); err != nil { 1060 return nil, err 1061 } 1062 return self.toGenericArrayUseNumber() 1063 } 1064 1065 // ArrayUseNode copys both parsed and non-parsed chidren nodes, 1066 // and indexes them by original order 1067 func (self *Node) ArrayUseNode() ([]Node, error) { 1068 if self.isAny() { 1069 any := self.packAny() 1070 if v, ok := any.([]Node); ok { 1071 return v, nil 1072 } else { 1073 return nil, ErrUnsupportType 1074 } 1075 } 1076 if err := self.should(types.V_ARRAY, "an array"); err != nil { 1077 return nil, err 1078 } 1079 if err := self.skipAllIndex(); err != nil { 1080 return nil, err 1081 } 1082 return self.toGenericArrayUseNode() 1083 } 1084 1085 // ArrayUnsafe exports the underlying pointer to its children array 1086 // WARN: don't use it unless you know what you are doing 1087 // 1088 // Deprecated: this API now returns copied nodes instead of directly reference, 1089 // which has no difference with ArrayUseNode 1090 // func (self *Node) UnsafeArray() ([]Node, error) { 1091 // if err := self.should(types.V_ARRAY, "an array"); err != nil { 1092 // return nil, err 1093 // } 1094 // if err := self.skipAllIndex(); err != nil { 1095 // return nil, err 1096 // } 1097 // return self.toGenericArrayUseNode() 1098 // } 1099 1100 func (self *Node) unsafeArray() (*linkedNodes, error) { 1101 if err := self.skipAllIndex(); err != nil { 1102 return nil, err 1103 } 1104 if self.p == nil { 1105 *self = newArray(new(linkedNodes)) 1106 } 1107 return (*linkedNodes)(self.p), nil 1108 } 1109 1110 // Interface loads all children under all pathes from this node, 1111 // and converts itself as generic type. 1112 // WARN: all numberic nodes are casted to float64 1113 func (self *Node) Interface() (interface{}, error) { 1114 if err := self.checkRaw(); err != nil { 1115 return nil, err 1116 } 1117 switch self.t { 1118 case V_ERROR : return nil, self.Check() 1119 case types.V_NULL : return nil, nil 1120 case types.V_TRUE : return true, nil 1121 case types.V_FALSE : return false, nil 1122 case types.V_ARRAY : return self.toGenericArray() 1123 case types.V_OBJECT : return self.toGenericObject() 1124 case types.V_STRING : return self.toString(), nil 1125 case _V_NUMBER : 1126 v, err := self.toFloat64() 1127 if err != nil { 1128 return nil, err 1129 } 1130 return v, nil 1131 case _V_ARRAY_LAZY : 1132 if err := self.loadAllIndex(); err != nil { 1133 return nil, err 1134 } 1135 return self.toGenericArray() 1136 case _V_OBJECT_LAZY : 1137 if err := self.loadAllKey(); err != nil { 1138 return nil, err 1139 } 1140 return self.toGenericObject() 1141 case _V_ANY: 1142 switch v := self.packAny().(type) { 1143 case Node : return v.Interface() 1144 case *Node: return v.Interface() 1145 default : return v, nil 1146 } 1147 default : return nil, ErrUnsupportType 1148 } 1149 } 1150 1151 func (self *Node) packAny() interface{} { 1152 return *(*interface{})(self.p) 1153 } 1154 1155 // InterfaceUseNumber works same with Interface() 1156 // except numberic nodes are casted to json.Number 1157 func (self *Node) InterfaceUseNumber() (interface{}, error) { 1158 if err := self.checkRaw(); err != nil { 1159 return nil, err 1160 } 1161 switch self.t { 1162 case V_ERROR : return nil, self.Check() 1163 case types.V_NULL : return nil, nil 1164 case types.V_TRUE : return true, nil 1165 case types.V_FALSE : return false, nil 1166 case types.V_ARRAY : return self.toGenericArrayUseNumber() 1167 case types.V_OBJECT : return self.toGenericObjectUseNumber() 1168 case types.V_STRING : return self.toString(), nil 1169 case _V_NUMBER : return self.toNumber(), nil 1170 case _V_ARRAY_LAZY : 1171 if err := self.loadAllIndex(); err != nil { 1172 return nil, err 1173 } 1174 return self.toGenericArrayUseNumber() 1175 case _V_OBJECT_LAZY : 1176 if err := self.loadAllKey(); err != nil { 1177 return nil, err 1178 } 1179 return self.toGenericObjectUseNumber() 1180 case _V_ANY : return self.packAny(), nil 1181 default : return nil, ErrUnsupportType 1182 } 1183 } 1184 1185 // InterfaceUseNode clone itself as a new node, 1186 // or its children as map[string]Node (or []Node) 1187 func (self *Node) InterfaceUseNode() (interface{}, error) { 1188 if err := self.checkRaw(); err != nil { 1189 return nil, err 1190 } 1191 switch self.t { 1192 case types.V_ARRAY : return self.toGenericArrayUseNode() 1193 case types.V_OBJECT : return self.toGenericObjectUseNode() 1194 case _V_ARRAY_LAZY : 1195 if err := self.skipAllIndex(); err != nil { 1196 return nil, err 1197 } 1198 return self.toGenericArrayUseNode() 1199 case _V_OBJECT_LAZY : 1200 if err := self.skipAllKey(); err != nil { 1201 return nil, err 1202 } 1203 return self.toGenericObjectUseNode() 1204 default : return *self, self.Check() 1205 } 1206 } 1207 1208 // LoadAll loads all the node's children and children's children as parsed. 1209 // After calling it, the node can be safely used on concurrency 1210 func (self *Node) LoadAll() error { 1211 if self.IsRaw() { 1212 self.parseRaw(true) 1213 return self.Check() 1214 } 1215 1216 switch self.itype() { 1217 case types.V_ARRAY: 1218 e := self.len() 1219 if err := self.loadAllIndex(); err != nil { 1220 return err 1221 } 1222 for i := 0; i < e; i++ { 1223 n := self.nodeAt(i) 1224 if n.IsRaw() { 1225 n.parseRaw(true) 1226 } 1227 if err := n.Check(); err != nil { 1228 return err 1229 } 1230 } 1231 return nil 1232 case types.V_OBJECT: 1233 e := self.len() 1234 if err := self.loadAllKey(); err != nil { 1235 return err 1236 } 1237 for i := 0; i < e; i++ { 1238 n := self.pairAt(i) 1239 if n.Value.IsRaw() { 1240 n.Value.parseRaw(true) 1241 } 1242 if err := n.Value.Check(); err != nil { 1243 return err 1244 } 1245 } 1246 return nil 1247 default: 1248 return self.Check() 1249 } 1250 } 1251 1252 // Load loads the node's children as parsed. 1253 // After calling it, only the node itself can be used on concurrency (not include its children) 1254 func (self *Node) Load() error { 1255 if err := self.checkRaw(); err != nil { 1256 return err 1257 } 1258 1259 switch self.t { 1260 case _V_ARRAY_LAZY: 1261 return self.skipAllIndex() 1262 case _V_OBJECT_LAZY: 1263 return self.skipAllKey() 1264 default: 1265 return self.Check() 1266 } 1267 } 1268 1269 /**---------------------------------- Internal Helper Methods ----------------------------------**/ 1270 1271 func (self *Node) should(t types.ValueType, s string) error { 1272 if err := self.checkRaw(); err != nil { 1273 return err 1274 } 1275 if self.itype() != t { 1276 return ErrUnsupportType 1277 } 1278 return nil 1279 } 1280 1281 func (self *Node) nodeAt(i int) *Node { 1282 var p *linkedNodes 1283 if self.isLazy() { 1284 _, stack := self.getParserAndArrayStack() 1285 p = &stack.v 1286 } else { 1287 p = (*linkedNodes)(self.p) 1288 if l := p.Len(); l != self.len() { 1289 // some nodes got unset, iterate to skip them 1290 for j:=0; j<l; j++ { 1291 v := p.At(j) 1292 if v.Exists() { 1293 i-- 1294 } 1295 if i < 0 { 1296 return v 1297 } 1298 } 1299 return nil 1300 } 1301 } 1302 return p.At(i) 1303 } 1304 1305 func (self *Node) pairAt(i int) *Pair { 1306 var p *linkedPairs 1307 if self.isLazy() { 1308 _, stack := self.getParserAndObjectStack() 1309 p = &stack.v 1310 } else { 1311 p = (*linkedPairs)(self.p) 1312 if l := p.Len(); l != self.len() { 1313 // some nodes got unset, iterate to skip them 1314 for j:=0; j<l; j++ { 1315 v := p.At(j) 1316 if v != nil && v.Value.Exists() { 1317 i-- 1318 } 1319 if i < 0 { 1320 return v 1321 } 1322 } 1323 return nil 1324 } 1325 } 1326 return p.At(i) 1327 } 1328 1329 func (self *Node) skipAllIndex() error { 1330 if !self.isLazy() { 1331 return nil 1332 } 1333 var err types.ParsingError 1334 parser, stack := self.getParserAndArrayStack() 1335 parser.skipValue = true 1336 parser.noLazy = true 1337 *self, err = parser.decodeArray(&stack.v) 1338 if err != 0 { 1339 return parser.ExportError(err) 1340 } 1341 return nil 1342 } 1343 1344 func (self *Node) skipAllKey() error { 1345 if !self.isLazy() { 1346 return nil 1347 } 1348 var err types.ParsingError 1349 parser, stack := self.getParserAndObjectStack() 1350 parser.skipValue = true 1351 parser.noLazy = true 1352 *self, err = parser.decodeObject(&stack.v) 1353 if err != 0 { 1354 return parser.ExportError(err) 1355 } 1356 return nil 1357 } 1358 1359 func (self *Node) skipKey(key string) (*Node, int) { 1360 nb := self.len() 1361 lazy := self.isLazy() 1362 1363 if nb > 0 { 1364 /* linear search */ 1365 var p *Pair 1366 var i int 1367 if lazy { 1368 s := (*parseObjectStack)(self.p) 1369 p, i = s.v.Get(key) 1370 } else { 1371 p, i = (*linkedPairs)(self.p).Get(key) 1372 } 1373 1374 if p != nil { 1375 return &p.Value, i 1376 } 1377 } 1378 1379 /* not found */ 1380 if !lazy { 1381 return nil, -1 1382 } 1383 1384 // lazy load 1385 for last, i := self.skipNextPair(), nb; last != nil; last, i = self.skipNextPair(), i+1 { 1386 if last.Value.Check() != nil { 1387 return &last.Value, -1 1388 } 1389 if last.Key == key { 1390 return &last.Value, i 1391 } 1392 } 1393 1394 return nil, -1 1395 } 1396 1397 func (self *Node) skipIndex(index int) *Node { 1398 nb := self.len() 1399 if nb > index { 1400 v := self.nodeAt(index) 1401 return v 1402 } 1403 if !self.isLazy() { 1404 return nil 1405 } 1406 1407 // lazy load 1408 for last := self.skipNextNode(); last != nil; last = self.skipNextNode(){ 1409 if last.Check() != nil { 1410 return last 1411 } 1412 if self.len() > index { 1413 return last 1414 } 1415 } 1416 1417 return nil 1418 } 1419 1420 func (self *Node) skipIndexPair(index int) *Pair { 1421 nb := self.len() 1422 if nb > index { 1423 return self.pairAt(index) 1424 } 1425 if !self.isLazy() { 1426 return nil 1427 } 1428 1429 // lazy load 1430 for last := self.skipNextPair(); last != nil; last = self.skipNextPair(){ 1431 if last.Value.Check() != nil { 1432 return last 1433 } 1434 if self.len() > index { 1435 return last 1436 } 1437 } 1438 1439 return nil 1440 } 1441 1442 func (self *Node) loadAllIndex() error { 1443 if !self.isLazy() { 1444 return nil 1445 } 1446 var err types.ParsingError 1447 parser, stack := self.getParserAndArrayStack() 1448 parser.noLazy = true 1449 *self, err = parser.decodeArray(&stack.v) 1450 if err != 0 { 1451 return parser.ExportError(err) 1452 } 1453 return nil 1454 } 1455 1456 func (self *Node) loadAllKey() error { 1457 if !self.isLazy() { 1458 return nil 1459 } 1460 var err types.ParsingError 1461 parser, stack := self.getParserAndObjectStack() 1462 parser.noLazy = true 1463 *self, err = parser.decodeObject(&stack.v) 1464 if err != 0 { 1465 return parser.ExportError(err) 1466 } 1467 return nil 1468 } 1469 1470 func (self *Node) removeNode(i int) { 1471 node := self.nodeAt(i) 1472 if node == nil { 1473 return 1474 } 1475 *node = Node{} 1476 // NOTICE: not be consistent with linkedNode.Len() 1477 self.l-- 1478 } 1479 1480 func (self *Node) removePair(i int) { 1481 last := self.pairAt(i) 1482 if last == nil { 1483 return 1484 } 1485 *last = Pair{} 1486 // NOTICE: should be consistent with linkedPair.Len() 1487 self.l-- 1488 } 1489 1490 func (self *Node) removePairAt(i int) { 1491 p := (*linkedPairs)(self.p).At(i) 1492 if p == nil { 1493 return 1494 } 1495 *p = Pair{} 1496 // NOTICE: should be consistent with linkedPair.Len() 1497 self.l-- 1498 } 1499 1500 func (self *Node) toGenericArray() ([]interface{}, error) { 1501 nb := self.len() 1502 if nb == 0 { 1503 return []interface{}{}, nil 1504 } 1505 ret := make([]interface{}, 0, nb) 1506 1507 /* convert each item */ 1508 it := self.values() 1509 for v := it.next(); v != nil; v = it.next() { 1510 vv, err := v.Interface() 1511 if err != nil { 1512 return nil, err 1513 } 1514 ret = append(ret, vv) 1515 } 1516 1517 /* all done */ 1518 return ret, nil 1519 } 1520 1521 func (self *Node) toGenericArrayUseNumber() ([]interface{}, error) { 1522 nb := self.len() 1523 if nb == 0 { 1524 return []interface{}{}, nil 1525 } 1526 ret := make([]interface{}, 0, nb) 1527 1528 /* convert each item */ 1529 it := self.values() 1530 for v := it.next(); v != nil; v = it.next() { 1531 vv, err := v.InterfaceUseNumber() 1532 if err != nil { 1533 return nil, err 1534 } 1535 ret = append(ret, vv) 1536 } 1537 1538 /* all done */ 1539 return ret, nil 1540 } 1541 1542 func (self *Node) toGenericArrayUseNode() ([]Node, error) { 1543 var nb = self.len() 1544 if nb == 0 { 1545 return []Node{}, nil 1546 } 1547 1548 var s = (*linkedNodes)(self.p) 1549 var out = make([]Node, nb) 1550 s.ToSlice(out) 1551 1552 return out, nil 1553 } 1554 1555 func (self *Node) toGenericObject() (map[string]interface{}, error) { 1556 nb := self.len() 1557 if nb == 0 { 1558 return map[string]interface{}{}, nil 1559 } 1560 ret := make(map[string]interface{}, nb) 1561 1562 /* convert each item */ 1563 it := self.properties() 1564 for v := it.next(); v != nil; v = it.next() { 1565 vv, err := v.Value.Interface() 1566 if err != nil { 1567 return nil, err 1568 } 1569 ret[v.Key] = vv 1570 } 1571 1572 /* all done */ 1573 return ret, nil 1574 } 1575 1576 1577 func (self *Node) toGenericObjectUseNumber() (map[string]interface{}, error) { 1578 nb := self.len() 1579 if nb == 0 { 1580 return map[string]interface{}{}, nil 1581 } 1582 ret := make(map[string]interface{}, nb) 1583 1584 /* convert each item */ 1585 it := self.properties() 1586 for v := it.next(); v != nil; v = it.next() { 1587 vv, err := v.Value.InterfaceUseNumber() 1588 if err != nil { 1589 return nil, err 1590 } 1591 ret[v.Key] = vv 1592 } 1593 1594 /* all done */ 1595 return ret, nil 1596 } 1597 1598 func (self *Node) toGenericObjectUseNode() (map[string]Node, error) { 1599 var nb = self.len() 1600 if nb == 0 { 1601 return map[string]Node{}, nil 1602 } 1603 1604 var s = (*linkedPairs)(self.p) 1605 var out = make(map[string]Node, nb) 1606 s.ToMap(out) 1607 1608 /* all done */ 1609 return out, nil 1610 } 1611 1612 /**------------------------------------ Factory Methods ------------------------------------**/ 1613 1614 var ( 1615 nullNode = Node{t: types.V_NULL} 1616 trueNode = Node{t: types.V_TRUE} 1617 falseNode = Node{t: types.V_FALSE} 1618 ) 1619 1620 // NewRaw creates a node of raw json. 1621 // If the input json is invalid, NewRaw returns a error Node. 1622 func NewRaw(json string) Node { 1623 parser := NewParserObj(json) 1624 start, err := parser.skip() 1625 if err != 0 { 1626 return *newError(err, err.Message()) 1627 } 1628 it := switchRawType(parser.s[start]) 1629 if it == _V_NONE { 1630 return Node{} 1631 } 1632 return newRawNode(parser.s[start:parser.p], it) 1633 } 1634 1635 // NewAny creates a node of type V_ANY if any's type isn't Node or *Node, 1636 // which stores interface{} and can be only used for `.Interface()`\`.MarshalJSON()`. 1637 func NewAny(any interface{}) Node { 1638 switch n := any.(type) { 1639 case Node: 1640 return n 1641 case *Node: 1642 return *n 1643 default: 1644 return Node{ 1645 t: _V_ANY, 1646 p: unsafe.Pointer(&any), 1647 } 1648 } 1649 } 1650 1651 // NewBytes encodes given src with Base64 (RFC 4648), and creates a node of type V_STRING. 1652 func NewBytes(src []byte) Node { 1653 if len(src) == 0 { 1654 panic("empty src bytes") 1655 } 1656 out := encodeBase64(src) 1657 return NewString(out) 1658 } 1659 1660 // NewNull creates a node of type V_NULL 1661 func NewNull() Node { 1662 return Node{ 1663 p: nil, 1664 t: types.V_NULL, 1665 } 1666 } 1667 1668 // NewBool creates a node of type bool: 1669 // If v is true, returns V_TRUE node 1670 // If v is false, returns V_FALSE node 1671 func NewBool(v bool) Node { 1672 var t = types.V_FALSE 1673 if v { 1674 t = types.V_TRUE 1675 } 1676 return Node{ 1677 p: nil, 1678 t: t, 1679 } 1680 } 1681 1682 // NewNumber creates a json.Number node 1683 // v must be a decimal string complying with RFC8259 1684 func NewNumber(v string) Node { 1685 return Node{ 1686 l: uint(len(v)), 1687 p: rt.StrPtr(v), 1688 t: _V_NUMBER, 1689 } 1690 } 1691 1692 func (node Node) toNumber() json.Number { 1693 return json.Number(rt.StrFrom(node.p, int64(node.l))) 1694 } 1695 1696 func (self Node) toString() string { 1697 return rt.StrFrom(self.p, int64(self.l)) 1698 } 1699 1700 func (node Node) toFloat64() (float64, error) { 1701 ret, err := node.toNumber().Float64() 1702 if err != nil { 1703 return 0, err 1704 } 1705 return ret, nil 1706 } 1707 1708 func (node Node) toInt64() (int64, error) { 1709 ret,err := node.toNumber().Int64() 1710 if err != nil { 1711 return 0, err 1712 } 1713 return ret, nil 1714 } 1715 1716 func newBytes(v []byte) Node { 1717 return Node{ 1718 t: types.V_STRING, 1719 p: mem2ptr(v), 1720 l: uint(len(v)), 1721 } 1722 } 1723 1724 // NewString creates a node of type V_STRING. 1725 // v is considered to be a valid UTF-8 string, 1726 // which means it won't be validated and unescaped. 1727 // when the node is encoded to json, v will be escaped. 1728 func NewString(v string) Node { 1729 return Node{ 1730 t: types.V_STRING, 1731 p: rt.StrPtr(v), 1732 l: uint(len(v)), 1733 } 1734 } 1735 1736 // NewArray creates a node of type V_ARRAY, 1737 // using v as its underlying children 1738 func NewArray(v []Node) Node { 1739 s := new(linkedNodes) 1740 s.FromSlice(v) 1741 return newArray(s) 1742 } 1743 1744 func newArray(v *linkedNodes) Node { 1745 return Node{ 1746 t: types.V_ARRAY, 1747 l: uint(v.Len()), 1748 p: unsafe.Pointer(v), 1749 } 1750 } 1751 1752 func (self *Node) setArray(v *linkedNodes) { 1753 self.t = types.V_ARRAY 1754 self.l = uint(v.Len()) 1755 self.p = unsafe.Pointer(v) 1756 } 1757 1758 // NewObject creates a node of type V_OBJECT, 1759 // using v as its underlying children 1760 func NewObject(v []Pair) Node { 1761 s := new(linkedPairs) 1762 s.FromSlice(v) 1763 return newObject(s) 1764 } 1765 1766 func newObject(v *linkedPairs) Node { 1767 return Node{ 1768 t: types.V_OBJECT, 1769 l: uint(v.Len()), 1770 p: unsafe.Pointer(v), 1771 } 1772 } 1773 1774 func (self *Node) setObject(v *linkedPairs) { 1775 self.t = types.V_OBJECT 1776 self.l = uint(v.Len()) 1777 self.p = unsafe.Pointer(v) 1778 } 1779 1780 func newRawNode(str string, typ types.ValueType) Node { 1781 return Node{ 1782 t: _V_RAW | typ, 1783 p: rt.StrPtr(str), 1784 l: uint(len(str)), 1785 } 1786 } 1787 1788 func (self *Node) parseRaw(full bool) { 1789 raw := self.toString() 1790 parser := NewParserObj(raw) 1791 if full { 1792 parser.noLazy = true 1793 parser.skipValue = false 1794 } 1795 var e types.ParsingError 1796 *self, e = parser.Parse() 1797 if e != 0 { 1798 *self = *newSyntaxError(parser.syntaxError(e)) 1799 } 1800 } 1801 1802 var typeJumpTable = [256]types.ValueType{ 1803 '"' : types.V_STRING, 1804 '-' : _V_NUMBER, 1805 '0' : _V_NUMBER, 1806 '1' : _V_NUMBER, 1807 '2' : _V_NUMBER, 1808 '3' : _V_NUMBER, 1809 '4' : _V_NUMBER, 1810 '5' : _V_NUMBER, 1811 '6' : _V_NUMBER, 1812 '7' : _V_NUMBER, 1813 '8' : _V_NUMBER, 1814 '9' : _V_NUMBER, 1815 '[' : types.V_ARRAY, 1816 'f' : types.V_FALSE, 1817 'n' : types.V_NULL, 1818 't' : types.V_TRUE, 1819 '{' : types.V_OBJECT, 1820 } 1821 1822 func switchRawType(c byte) types.ValueType { 1823 return typeJumpTable[c] 1824 }