/
gjson_api.go
214 lines (190 loc) · 5.74 KB
/
gjson_api.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gjson
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/util/gutil"
)
// Interface returns the json value.
func (j *Json) Interface() interface{} {
if j == nil {
return nil
}
j.mu.RLock()
defer j.mu.RUnlock()
return *(j.p)
}
// Var returns the json value as *gvar.Var.
func (j *Json) Var() *gvar.Var {
return gvar.New(j.Interface())
}
// IsNil checks whether the value pointed by `j` is nil.
func (j *Json) IsNil() bool {
if j == nil {
return true
}
j.mu.RLock()
defer j.mu.RUnlock()
return j.p == nil || *(j.p) == nil
}
// Get retrieves and returns value by specified `pattern`.
// It returns all values of current Json object if `pattern` is given empty or string ".".
// It returns nil if no value found by `pattern`.
//
// We can also access slice item by its index number in `pattern` like:
// "list.10", "array.0.name", "array.0.1.id".
//
// It returns a default value specified by `def` if value for `pattern` is not found.
func (j *Json) Get(pattern string, def ...interface{}) *gvar.Var {
if j == nil {
return nil
}
j.mu.RLock()
defer j.mu.RUnlock()
// It returns nil if pattern is empty.
if pattern == "" {
return nil
}
var result *interface{}
if j.vc {
result = j.getPointerByPattern(pattern)
} else {
result = j.getPointerByPatternWithoutViolenceCheck(pattern)
}
if result != nil {
return gvar.New(*result)
}
if len(def) > 0 {
return gvar.New(def[0])
}
return nil
}
// GetJson gets the value by specified `pattern`,
// and converts it to a un-concurrent-safe Json object.
func (j *Json) GetJson(pattern string, def ...interface{}) *Json {
return New(j.Get(pattern, def...).Val())
}
// GetJsons gets the value by specified `pattern`,
// and converts it to a slice of un-concurrent-safe Json object.
func (j *Json) GetJsons(pattern string, def ...interface{}) []*Json {
array := j.Get(pattern, def...).Array()
if len(array) > 0 {
jsonSlice := make([]*Json, len(array))
for i := 0; i < len(array); i++ {
jsonSlice[i] = New(array[i])
}
return jsonSlice
}
return nil
}
// GetJsonMap gets the value by specified `pattern`,
// and converts it to a map of un-concurrent-safe Json object.
func (j *Json) GetJsonMap(pattern string, def ...interface{}) map[string]*Json {
m := j.Get(pattern, def...).Map()
if len(m) > 0 {
jsonMap := make(map[string]*Json, len(m))
for k, v := range m {
jsonMap[k] = New(v)
}
return jsonMap
}
return nil
}
// Set sets value with specified `pattern`.
// It supports hierarchical data access by char separator, which is '.' in default.
func (j *Json) Set(pattern string, value interface{}) error {
return j.setValue(pattern, value, false)
}
// MustSet performs as Set, but it panics if any error occurs.
func (j *Json) MustSet(pattern string, value interface{}) {
if err := j.Set(pattern, value); err != nil {
panic(err)
}
}
// Remove deletes value with specified `pattern`.
// It supports hierarchical data access by char separator, which is '.' in default.
func (j *Json) Remove(pattern string) error {
return j.setValue(pattern, nil, true)
}
// MustRemove performs as Remove, but it panics if any error occurs.
func (j *Json) MustRemove(pattern string) {
if err := j.Remove(pattern); err != nil {
panic(err)
}
}
// Contains checks whether the value by specified `pattern` exist.
func (j *Json) Contains(pattern string) bool {
return j.Get(pattern) != nil
}
// Len returns the length/size of the value by specified `pattern`.
// The target value by `pattern` should be type of slice or map.
// It returns -1 if the target value is not found, or its type is invalid.
func (j *Json) Len(pattern string) int {
p := j.getPointerByPattern(pattern)
if p != nil {
switch (*p).(type) {
case map[string]interface{}:
return len((*p).(map[string]interface{}))
case []interface{}:
return len((*p).([]interface{}))
default:
return -1
}
}
return -1
}
// Append appends value to the value by specified `pattern`.
// The target value by `pattern` should be type of slice.
func (j *Json) Append(pattern string, value interface{}) error {
p := j.getPointerByPattern(pattern)
if p == nil || *p == nil {
if pattern == "." {
return j.Set("0", value)
}
return j.Set(fmt.Sprintf("%s.0", pattern), value)
}
switch (*p).(type) {
case []interface{}:
if pattern == "." {
return j.Set(fmt.Sprintf("%d", len((*p).([]interface{}))), value)
}
return j.Set(fmt.Sprintf("%s.%d", pattern, len((*p).([]interface{}))), value)
}
return gerror.NewCodef(gcode.CodeInvalidParameter, "invalid variable type of %s", pattern)
}
// MustAppend performs as Append, but it panics if any error occurs.
func (j *Json) MustAppend(pattern string, value interface{}) {
if err := j.Append(pattern, value); err != nil {
panic(err)
}
}
// Map converts current Json object to map[string]interface{}.
// It returns nil if fails.
func (j *Json) Map() map[string]interface{} {
return j.Var().Map()
}
// Array converts current Json object to []interface{}.
// It returns nil if fails.
func (j *Json) Array() []interface{} {
return j.Var().Array()
}
// Scan automatically calls Struct or Structs function according to the type of parameter
// `pointer` to implement the converting.
func (j *Json) Scan(pointer interface{}, mapping ...map[string]string) error {
return j.Var().Scan(pointer, mapping...)
}
// Dump prints current Json object with more manually readable.
func (j *Json) Dump() {
if j == nil {
return
}
j.mu.RLock()
defer j.mu.RUnlock()
gutil.Dump(*j.p)
}