-
Notifications
You must be signed in to change notification settings - Fork 5
/
node.go
128 lines (102 loc) · 2.75 KB
/
node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package json
import (
"io"
"sort"
"github.com/go-restit/lzjson"
)
// Node is an interface for all JSON nodes.
type Node struct {
node lzjson.Node
}
// Bytes returns the raw JSON string in []byte.
func (n *Node) Bytes() []byte {
return n.node.Raw()
}
// Error returns the JSON parse error, if any.
func (n *Node) Error() error {
return n.node.ParseError()
}
// Int unmarshal the JSON into int.
func (n *Node) Int() int {
return n.node.Int()
}
// Keys gets an array object's keys, or nil if not an object.
func (n *Node) Keys() []string {
return n.node.GetKeys()
}
// SortedKeys gets an array object's keys in alphabetical order
func (n *Node) SortedKeys() []string {
ss := n.Keys()
sort.Strings(ss)
return ss
}
// Len gets the length of the value.
// Only works with Array and String value type.
func (n *Node) Len() int {
return n.node.Len()
}
// String unmarshal the JSON into string.
func (n *Node) String() string {
return n.node.String()
}
// Type returns the lzjson.Type of the containing JSON value.
func (n *Node) Type() lzjson.Type {
return n.node.Type()
}
// IsObject checks whether the node is valid JSON object.
func (n *Node) IsObject() bool {
return n.Type() == lzjson.TypeObject
}
// IsArray checks whether the node is valid JSON array.
func (n *Node) IsArray() bool {
return n.Type() == lzjson.TypeArray
}
// IsString checks whether the node is valid JSON string.
func (n *Node) IsString() bool {
return n.Type() == lzjson.TypeString
}
// IsNumber checks whether the node is valid JSON number.
func (n *Node) IsNumber() bool {
return n.Type() == lzjson.TypeNumber
}
// IsBool checks whether the node is valid JSON boolean.
func (n *Node) IsBool() bool {
return n.Type() == lzjson.TypeBool
}
// IsNull checks whether the node is valid JSON null.
func (n *Node) IsNull() bool {
return n.Type() == lzjson.TypeNull
}
// IsValid checks whether the node is valid JSON value.
func (n *Node) IsValid() bool {
return n.Type() != lzjson.TypeError
}
// IsEmpty checks whether the node is having empty value.
func (n *Node) IsEmpty() bool {
if n.IsObject() {
return len(n.Keys()) == 0
}
return n.Len() == 0
}
// Get gets object's inner value.
// Only works with Object value type.
func (n *Node) Get(key string) *Node {
return beNode(n.node.Get(key))
}
// GetN gets array's inner value.
// Only works with Array value type.
// 0 for the first item.
func (n *Node) GetN(i int) *Node {
return beNode(n.node.GetN(i))
}
// Unmarshal parses the JSON node data into variable v.
func (n *Node) Unmarshal(v interface{}) error {
return n.node.Unmarshal(v)
}
// NewNode reads and decodes a JSON from io.Reader then returns a Node of it.
func NewNode(r io.Reader) *Node {
return beNode(lzjson.Decode(r))
}
func beNode(n lzjson.Node) *Node {
return &Node{node: n}
}