diff --git a/ast/buffer.go b/ast/buffer.go index 93f4ff47a..4eac215b4 100644 --- a/ast/buffer.go +++ b/ast/buffer.go @@ -43,6 +43,21 @@ func (self *linkedNodes) Len() int { return self.size } +func (self *linkedNodes) Get(i int) (*Node) { + if self == nil { + return nil + } + if i >= 0 && i= _DEFAULT_NODE_CAP && i 0 { - n := s.At(0) - if n.Exists() { - if err := n.encode(buf); err != nil { - return err - } - started = true - } - } - - for i := 1; i < nb; i++ { - n := s.At(i) + for i := 0; i < nb; i++ { + n := self.nodeAt(i) if !n.Exists() { continue } @@ -250,21 +239,10 @@ func (self *Node) encodeObject(buf *[]byte) error { *buf = append(*buf, '{') - var s = (*linkedPairs)(self.p) var started bool - if nb > 0 { - n := s.At(0) - if n.Value.Exists() { - if err := n.encode(buf); err != nil { - return err - } - started = true - } - } - - for i := 1; i < nb; i++ { - n := s.At(i) - if !n.Value.Exists() { + for i := 0; i < nb; i++ { + n := self.pairAt(i) + if n == nil || !n.Value.Exists() { continue } if started { diff --git a/ast/iterator.go b/ast/iterator.go index a102c67be..64e1e5a90 100644 --- a/ast/iterator.go +++ b/ast/iterator.go @@ -122,7 +122,7 @@ next_start: } else { n := self.p.pairAt(self.i) self.i++ - if !n.Value.Exists() { + if n == nil || !n.Value.Exists() { goto next_start } return n diff --git a/ast/node.go b/ast/node.go index 423b8ba81..7818ef4da 100644 --- a/ast/node.go +++ b/ast/node.go @@ -627,7 +627,7 @@ func (self *Node) UnsetByIndex(index int) (bool, error) { it := self.itype() if it == types.V_ARRAY { p = self.Index(index) - }else if it == types.V_OBJECT { + } else if it == types.V_OBJECT { if err := self.checkRaw(); err != nil { return false, err } @@ -1174,6 +1174,19 @@ func (self *Node) nodeAt(i int) *Node { p = &stack.v } else { p = (*linkedNodes)(self.p) + if l := p.Len(); l != self.len() { + // some nodes got unset, iterate to skip them + for j:=0; j