-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
gjson_api.go
441 lines (376 loc) · 15 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright 2017 gf Author(https://github.com/gogf/gf). 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"
"time"
"github.com/gogf/gf/util/gutil"
"github.com/gogf/gf/container/gvar"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/util/gconv"
)
// Value returns the json value.
func (j *Json) Value() interface{} {
j.mu.RLock()
defer j.mu.RUnlock()
return *(j.p)
}
// IsNil checks whether the value pointed by <j> is nil.
func (j *Json) IsNil() bool {
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>,
// eg: "items.name.first", "list.10".
//
// It returns a default value specified by <def> if value for <pattern> is not found.
func (j *Json) Get(pattern string, def ...interface{}) interface{} {
j.mu.RLock()
defer j.mu.RUnlock()
// It returns nil if pattern is empty.
if pattern == "" {
return nil
}
// It returns all if pattern is ".".
if pattern == "." {
return *j.p
}
var result *interface{}
if j.vc {
result = j.getPointerByPattern(pattern)
} else {
result = j.getPointerByPatternWithoutViolenceCheck(pattern)
}
if result != nil {
return *result
}
if len(def) > 0 {
return def[0]
}
return nil
}
// GetVar returns a *gvar.Var with value by given <pattern>.
func (j *Json) GetVar(pattern string, def ...interface{}) *gvar.Var {
return gvar.New(j.Get(pattern, def...))
}
// GetVars returns []*gvar.Var with value by given <pattern>.
func (j *Json) GetVars(pattern string, def ...interface{}) []*gvar.Var {
return gvar.New(j.Get(pattern, def...)).Vars()
}
// GetMap retrieves the value by specified <pattern>,
// and converts it to map[string]interface{}.
func (j *Json) GetMap(pattern string, def ...interface{}) map[string]interface{} {
result := j.Get(pattern, def...)
if result != nil {
return gconv.Map(result)
}
return nil
}
// GetMapStrStr retrieves the value by specified <pattern>,
// and converts it to map[string]string.
func (j *Json) GetMapStrStr(pattern string, def ...interface{}) map[string]string {
result := j.Get(pattern, def...)
if result != nil {
return gconv.MapStrStr(result)
}
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...))
}
// 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.GetArray(pattern, def...)
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.GetMap(pattern, def...)
if len(m) > 0 {
jsonMap := make(map[string]*Json, len(m))
for k, v := range m {
jsonMap[k] = New(v)
}
return jsonMap
}
return nil
}
// GetArray retrieves the value by specified <pattern>,
// and converts it to a slice of []interface{}.
func (j *Json) GetArray(pattern string, def ...interface{}) []interface{} {
return gconv.Interfaces(j.Get(pattern, def...))
}
// GetString retrieves the value by specified <pattern> and converts it to string.
func (j *Json) GetString(pattern string, def ...interface{}) string {
return gconv.String(j.Get(pattern, def...))
}
// GetBytes retrieves the value by specified <pattern> and converts it to byte.
func (j *Json) GetBytes(pattern string, def ...interface{}) []byte {
return gconv.Bytes(j.Get(pattern, def...))
}
// GetBool gets the value by specified <pattern>,
// and converts it to bool.
// It returns false when value is: "", 0, false, off, nil;
// or returns true instead.
func (j *Json) GetBool(pattern string, def ...interface{}) bool {
return gconv.Bool(j.Get(pattern, def...))
}
// GetInt retrieves the value by specified <pattern> and converts it to int.
func (j *Json) GetInt(pattern string, def ...interface{}) int {
return gconv.Int(j.Get(pattern, def...))
}
// GetInt8 retrieves the value by specified <pattern> and converts it to int8.
func (j *Json) GetInt8(pattern string, def ...interface{}) int8 {
return gconv.Int8(j.Get(pattern, def...))
}
// GetInt16 retrieves the value by specified <pattern> and converts it to int16.
func (j *Json) GetInt16(pattern string, def ...interface{}) int16 {
return gconv.Int16(j.Get(pattern, def...))
}
// GetInt32 retrieves the value by specified <pattern> and converts it to int32.
func (j *Json) GetInt32(pattern string, def ...interface{}) int32 {
return gconv.Int32(j.Get(pattern, def...))
}
// GetInt64 retrieves the value by specified <pattern> and converts it to int64.
func (j *Json) GetInt64(pattern string, def ...interface{}) int64 {
return gconv.Int64(j.Get(pattern, def...))
}
// GetUint retrieves the value by specified <pattern> and converts it to uint.
func (j *Json) GetUint(pattern string, def ...interface{}) uint {
return gconv.Uint(j.Get(pattern, def...))
}
// GetUint8 retrieves the value by specified <pattern> and converts it to uint8.
func (j *Json) GetUint8(pattern string, def ...interface{}) uint8 {
return gconv.Uint8(j.Get(pattern, def...))
}
// GetUint16 retrieves the value by specified <pattern> and converts it to uint16.
func (j *Json) GetUint16(pattern string, def ...interface{}) uint16 {
return gconv.Uint16(j.Get(pattern, def...))
}
// GetUint32 retrieves the value by specified <pattern> and converts it to uint32.
func (j *Json) GetUint32(pattern string, def ...interface{}) uint32 {
return gconv.Uint32(j.Get(pattern, def...))
}
// GetUint64 retrieves the value by specified <pattern> and converts it to uint64.
func (j *Json) GetUint64(pattern string, def ...interface{}) uint64 {
return gconv.Uint64(j.Get(pattern, def...))
}
// GetFloat32 retrieves the value by specified <pattern> and converts it to float32.
func (j *Json) GetFloat32(pattern string, def ...interface{}) float32 {
return gconv.Float32(j.Get(pattern, def...))
}
// GetFloat64 retrieves the value by specified <pattern> and converts it to float64.
func (j *Json) GetFloat64(pattern string, def ...interface{}) float64 {
return gconv.Float64(j.Get(pattern, def...))
}
// GetFloats retrieves the value by specified <pattern> and converts it to []float64.
func (j *Json) GetFloats(pattern string, def ...interface{}) []float64 {
return gconv.Floats(j.Get(pattern, def...))
}
// GetInts retrieves the value by specified <pattern> and converts it to []int.
func (j *Json) GetInts(pattern string, def ...interface{}) []int {
return gconv.Ints(j.Get(pattern, def...))
}
// GetStrings retrieves the value by specified <pattern> and converts it to []string.
func (j *Json) GetStrings(pattern string, def ...interface{}) []string {
return gconv.Strings(j.Get(pattern, def...))
}
// GetInterfaces is alias of GetArray.
// See GetArray.
func (j *Json) GetInterfaces(pattern string, def ...interface{}) []interface{} {
return gconv.Interfaces(j.Get(pattern, def...))
}
// GetTime retrieves the value by specified <pattern> and converts it to time.Time.
func (j *Json) GetTime(pattern string, format ...string) time.Time {
return gconv.Time(j.Get(pattern), format...)
}
// GetDuration retrieves the value by specified <pattern> and converts it to time.Duration.
func (j *Json) GetDuration(pattern string, def ...interface{}) time.Duration {
return gconv.Duration(j.Get(pattern, def...))
}
// GetGTime retrieves the value by specified <pattern> and converts it to *gtime.Time.
func (j *Json) GetGTime(pattern string, format ...string) *gtime.Time {
return gconv.GTime(j.Get(pattern), format...)
}
// 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)
}
// 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)
}
// 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 {
return j.Set(fmt.Sprintf("%s.0", pattern), value)
}
switch (*p).(type) {
case []interface{}:
return j.Set(fmt.Sprintf("%s.%d", pattern, len((*p).([]interface{}))), value)
}
return fmt.Errorf("invalid variable type of %s", pattern)
}
// GetStruct retrieves the value by specified <pattern> and converts it to specified object
// <pointer>. The <pointer> should be the pointer to an object.
func (j *Json) GetStruct(pattern string, pointer interface{}, mapping ...map[string]string) error {
return gconv.Struct(j.Get(pattern), pointer, mapping...)
}
// GetStructDeep does GetStruct recursively.
func (j *Json) GetStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
return gconv.StructDeep(j.Get(pattern), pointer, mapping...)
}
// GetStructs converts any slice to given struct slice.
func (j *Json) GetStructs(pattern string, pointer interface{}, mapping ...map[string]string) error {
return gconv.Structs(j.Get(pattern), pointer, mapping...)
}
// GetStructsDeep converts any slice to given struct slice recursively.
func (j *Json) GetStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
return gconv.StructsDeep(j.Get(pattern), pointer, mapping...)
}
// GetMapToMap retrieves the value by specified <pattern> and converts it specified map variable.
// The parameter of <pointer> should be type of *map.
func (j *Json) GetMapToMap(pattern string, pointer interface{}, mapping ...map[string]string) error {
return gconv.MapToMap(j.Get(pattern), pointer, mapping...)
}
// GetMapToMapDeep retrieves the value by specified <pattern> and converts it specified map
// variable recursively. The parameter of <pointer> should be type of *map.
func (j *Json) GetMapToMapDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
return gconv.MapToMapDeep(j.Get(pattern), pointer, mapping...)
}
// GetMapToMaps retrieves the value by specified <pattern> and converts it specified map slice
// variable. The parameter of <pointer> should be type of []map/*map.
func (j *Json) GetMapToMaps(pattern string, pointer interface{}, mapping ...map[string]string) error {
return gconv.MapToMaps(j.Get(pattern), pointer, mapping...)
}
// GetMapToMapsDeep retrieves the value by specified <pattern> and converts it specified map slice
// variable recursively. The parameter of <pointer> should be type of []map/*map.
func (j *Json) GetMapToMapsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
return gconv.MapToMapsDeep(j.Get(pattern), pointer, mapping...)
}
// ToMap converts current Json object to map[string]interface{}.
// It returns nil if fails.
func (j *Json) ToMap() map[string]interface{} {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.Map(*(j.p))
}
// ToArray converts current Json object to []interface{}.
// It returns nil if fails.
func (j *Json) ToArray() []interface{} {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.Interfaces(*(j.p))
}
// ToStruct converts current Json object to specified object.
// The <pointer> should be a pointer type of *struct.
func (j *Json) ToStruct(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.Struct(*(j.p), pointer, mapping...)
}
// ToStructDeep converts current Json object to specified object recursively.
// The <pointer> should be a pointer type of *struct.
func (j *Json) ToStructDeep(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.StructDeep(*(j.p), pointer, mapping...)
}
// ToStructs converts current Json object to specified object slice.
// The <pointer> should be a pointer type of []struct/*struct.
func (j *Json) ToStructs(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.Structs(*(j.p), pointer, mapping...)
}
// ToStructsDeep converts current Json object to specified object slice recursively.
// The <pointer> should be a pointer type of []struct/*struct.
func (j *Json) ToStructsDeep(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.StructsDeep(*(j.p), pointer, mapping...)
}
// ToMapToMap converts current Json object to specified map variable.
// The parameter of <pointer> should be type of *map.
func (j *Json) ToMapToMap(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.MapToMap(*(j.p), pointer, mapping...)
}
// ToMapToMapDeep converts current Json object to specified map variable recursively.
// The parameter of <pointer> should be type of *map.
func (j *Json) ToMapToMapDeep(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.MapToMapDeep(*(j.p), pointer, mapping...)
}
// ToMapToMaps converts current Json object to specified map variable slice.
// The parameter of <pointer> should be type of []map/*map.
func (j *Json) ToMapToMaps(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.MapToMaps(*(j.p), pointer, mapping...)
}
// ToMapToMapsDeep converts current Json object to specified map variable slice recursively.
// The parameter of <pointer> should be type of []map/*map.
func (j *Json) ToMapToMapsDeep(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.MapToMapsDeep(*(j.p), pointer, mapping...)
}
// Dump prints current Json object with more manually readable.
func (j *Json) Dump() {
j.mu.RLock()
defer j.mu.RUnlock()
gutil.Dump(*j.p)
}
// Export returns <j> as a string with more manually readable.
func (j *Json) Export() string {
j.mu.RLock()
defer j.mu.RUnlock()
return gutil.Export(*j.p)
}