-
Notifications
You must be signed in to change notification settings - Fork 4
/
json.go
519 lines (464 loc) · 12.5 KB
/
json.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
package gjson
import (
"encoding/json"
"errors"
"fmt"
gcore "github.com/snail007/gmc/core"
gcast "github.com/snail007/gmc/util/cast"
gvalue "github.com/snail007/gmc/util/value"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
"io"
"strconv"
"strings"
)
var (
// AddModifier binds a custom modifier command to the GJSON syntax.
// This operation is not thread safe and should be executed prior to
// using all other gjson function.
AddModifier = gjson.AddModifier
// ModifierExists returns true when the specified modifier exists.
ModifierExists = gjson.ModifierExists
// Escape returns an escaped path component.
//
// json := `{
// "user":{
// "first.name": "Janet",
// "last.name": "Prichard"
// }
// }`
// user := gjson.Get(json, "user")
// println(user.Get(gjson.Escape("first.name"))
// println(user.Get(gjson.Escape("last.name"))
// // Output:
// // Janet
// // Prichard
Escape = gjson.Escape
// ForEachLine iterates through lines of JSON as specified by the JSON Lines
// format (http://jsonlines.org/).
// Each line is returned as a GJSON Result.
ForEachLine = gjson.ForEachLine
// Parse parses the json and returns a result.
//
// This function expects that the json is well-formed, and does not validate.
// Invalid json will not panic, but it may return back unexpected results.
// If you are consuming JSON from an unpredictable source then you may want to
// use the Valid function first.
Parse = func(json string) Result {
r := gjson.Parse(json)
return Result{
Result: r,
path: r.Path(json),
paths: r.Paths(json),
}
}
// ParseBytes parses the json and returns a result.
// If working with bytes, this method preferred over Parse(string(data))
ParseBytes = gjson.ParseBytes
// Valid returns true if the input is valid json.
//
// if !gjson.Valid(json) {
// return errors.New("invalid json")
// }
// value := gjson.Get(json, "name.last")
Valid = gjson.Valid
// ValidBytes returns true if the input is valid json.
//
// if !gjson.Valid(json) {
// return errors.New("invalid json")
// }
// value := gjson.Get(json, "name.last")
//
// If working with bytes, this method preferred over ValidBytes(string(data))
ValidBytes = gjson.ValidBytes
)
type Options = sjson.Options
type Result struct {
gjson.Result
path string
paths []string
}
func (s Result) Path() string {
return s.path
}
func (s Result) Paths() []string {
return s.paths
}
func (s Result) ToJSONObject() *JSONObject {
return NewJSONObject(s.Raw)
}
func (s Result) ToJSONArray() *JSONArray {
return NewJSONArray(s.Raw)
}
func (s Result) ForEach(f func(k, v Result) bool){
s.Result.ForEach(func(key, value gjson.Result) bool {
return f(Result{
Result:key,
},Result{
Result:value,
})
})
}
type Builder struct {
json string
}
func NewBuilderE(v interface{}) (*Builder, error) {
str, err := getJSONStr(v, "")
if err != nil {
return nil, err
}
return &Builder{json: str}, nil
}
func NewBuilder(v interface{}) *Builder {
obj, _ := NewBuilderE(v)
return obj
}
// Delete deletes a value from json for the specified path.
// path syntax: https://github.com/tidwall/sjson?tab=readme-ov-file#path-syntax
func (s *Builder) Delete(path string) error {
j, err := sjson.Delete(s.json, path)
if err == nil {
s.json = j
}
return err
}
// Set sets a json value for the specified path.
// A path is in dot syntax, such as "name.last" or "age".
// This function expects that the json is well-formed, and does not validate.
// Invalid json will not panic, but it may return back unexpected results.
// An error is returned if the path is not valid.
//
// A path is a series of keys separated by a dot.
//
// {
// "name": {"first": "Tom", "last": "Anderson"},
// "age":37,
// "children": ["Sara","Alex","Jack"],
// "friends": [
// {"first": "James", "last": "Murphy"},
// {"first": "Roger", "last": "Craig"}
// ]
// }
// "name.last" >> "Anderson"
// "age" >> 37
// "children.1" >> "Alex"
//
// path syntax: https://github.com/tidwall/sjson?tab=readme-ov-file#path-syntax
func (s *Builder) Set(path string, value interface{}) error {
j, err := sjson.Set(s.json, path, value)
if err == nil {
s.json = j
}
return err
}
// SetRaw sets a raw json value for the specified path.
// This function works the same as Set except that the value is set as a
// raw block of json. This allows for setting premarshalled json objects.
func (s *Builder) SetRaw(path, value string) error {
j, err := sjson.SetRaw(s.json, path, value)
if err == nil {
if !Valid(j) {
return errors.New("invalid json value: " + value)
}
s.json = j
}
return err
}
// Get searches json for the specified path.
// A path is in dot syntax, such as "name.last" or "age".
// When the value is found it's returned immediately.
//
// A path is a series of keys separated by a dot.
// A key may contain special wildcard characters '*' and '?'.
// To access an array value use the index as the key.
// To get the number of elements in an array or to access a child path, use
// the '#' character.
// The dot and wildcard character can be escaped with '\'.
//
// {
// "name": {"first": "Tom", "last": "Anderson"},
// "age":37,
// "children": ["Sara","Alex","Jack"],
// "friends": [
// {"first": "James", "last": "Murphy"},
// {"first": "Roger", "last": "Craig"}
// ]
// }
// "name.last" >> "Anderson"
// "age" >> 37
// "children" >> ["Sara","Alex","Jack"]
// "children.#" >> 3
// "children.1" >> "Alex"
// "child*.2" >> "Jack"
// "c?ildren.0" >> "Sara"
// "friends.#.first" >> ["James","Roger"]
//
// This function expects that the json is well-formed, and does not validate.
// Invalid json will not panic, but it may return back unexpected results.
// If you are consuming JSON from an unpredictable source then you may want to
// use the Valid function first.
// path syntax: https://github.com/tidwall/gjson/blob/master/SYNTAX.md
func (s *Builder) Get(path string) Result {
r := gjson.Get(s.json, path)
return Result{
paths: r.Paths(s.json),
path: r.Path(s.json),
Result: r,
}
}
// String convert the *Builder to JSON string,
func (s *Builder) String() string {
return s.json
}
// Interface convert the *Builder to Go DATA,
func (s *Builder) Interface() (v interface{}) {
json.Unmarshal([]byte(s.json), &v)
return
}
// JSONObject convert the *Builder to *JSONObject,
// if the *Builder is not a json object, nil returned.
func (s *Builder) JSONObject() *JSONObject {
if s.json != "" && !strings.HasPrefix(s.json, "{") {
return nil
}
return NewJSONObject(s.json)
}
// JSONArray convert the *Builder to *JSONArray,
// if the *Builder is not a json array, nil returned.
func (s *Builder) JSONArray() *JSONArray {
if s.json != "" && !strings.HasPrefix(s.json, "[") {
return nil
}
return NewJSONArray(s.json)
}
// GetMany searches json for the multiple paths.
// The return value is a Result array where the number of items
// will be equal to the number of input paths.
func (s *Builder) GetMany(path ...string) []Result {
rs1 := gjson.GetMany(s.json, path...)
var rs []Result
for _, r := range rs1 {
rs = append(rs,
Result{
Result: r,
path: r.Path(s.json),
paths: r.Paths(s.json),
})
}
return rs
}
type JSONObject struct {
*Builder
}
// NewJSONObjectE create a *JSONObject form v, returned error(if have)
// v can be json object content of []byte and string, or any data which json.Marshal can be processed.
func NewJSONObjectE(v interface{}) (*JSONObject, error) {
str, err := getJSONStr(v, "{}")
if err != nil {
return nil, err
}
if str != "" && !strings.HasPrefix(str, "{") {
return nil, errors.New("fail to convert v to json array")
}
return &JSONObject{
Builder: NewBuilder(str),
}, nil
}
// NewJSONObject create a *JSONObject form v, if error occurred nil returned.
// v can be json object content of []byte and string, or any data which json.Marshal can be processed.
func NewJSONObject(v interface{}) *JSONObject {
obj, _ := NewJSONObjectE(v)
return obj
}
type JSONArray struct {
*Builder
}
// NewJSONArrayE create a *JSONArray form v, returned error(if have)
// v can be json array content of []byte and string, or any data which json.Marshal can be processed.
func NewJSONArrayE(v interface{}) (*JSONArray, error) {
str, err := getJSONStr(v, "[]")
if err != nil {
return nil, err
}
if str != "" && !strings.HasPrefix(str, "[") {
return nil, errors.New("fail to convert v to json array")
}
return &JSONArray{
Builder: NewBuilder(str),
}, nil
}
// NewJSONArray create a *JSONArray form v, if error occurred nil returned.
// v can be json array content of []byte and string, or any data which json.Marshal can be processed.
func NewJSONArray(v interface{}) *JSONArray {
obj, _ := NewJSONArrayE(v)
return obj
}
// Merge *JSONArray, JSONArray or any valid slice to s
func (s *JSONArray) Merge(arr interface{}) (err error) {
var merge = func(a *JSONArray) {
a.Get("@this").ForEach(func(key, value Result) bool {
err = s.Append(value.Value())
return err == nil
})
}
switch a := arr.(type) {
case *JSONArray:
merge(a)
case JSONArray:
merge(&a)
default:
err = s.Append(gvalue.NewAny(arr).Slice()...)
}
return
}
func (s *JSONArray) Append(values ...interface{}) (err error) {
for _, value := range values {
switch v := value.(type) {
case *JSONObject:
err = s.SetRaw("-1", v.json)
case JSONObject:
err = s.SetRaw("-1", v.json)
case *JSONArray:
err = s.SetRaw("-1", v.json)
case JSONArray:
err = s.SetRaw("-1", v.json)
default:
err = s.Set("-1", value)
}
if err != nil {
return
}
}
return nil
}
func (s *JSONArray) Len() int64 {
return s.Get("#").Int()
}
func (s *JSONArray) Last() Result {
idx := s.Get("#").Int() - 1
if idx < 0 {
idx = 0
}
return s.Get(strconv.FormatInt(idx, 10))
}
func (s *JSONArray) First() Result {
return s.Get("0")
}
type JSONResult struct {
data map[string]interface{}
ctx gcore.Ctx
}
// NewResultCtx Optional args: code int, message string, data interface{}
func NewResultCtx(ctx gcore.Ctx, d ...interface{}) *JSONResult {
r := NewResult(d...)
r.ctx = ctx
return r
}
// NewResult Optional args: code int, message string, data interface{}
func NewResult(d ...interface{}) *JSONResult {
if len(d) == 1 {
var b []byte
switch d[0].(type) {
case string:
b = []byte(d[0].(string))
case []byte:
b = d[0].([]byte)
}
if len(b) > 0 {
s := &JSONResult{
data: map[string]interface{}{},
}
if e := json.Unmarshal(b, &s.data); e != nil {
return nil
}
return s
}
}
code := 0
message := ""
var data interface{}
if len(d) >= 1 {
code = gcast.ToInt(d[0])
}
if len(d) >= 2 {
message = gcast.ToString(d[1])
}
if len(d) >= 3 {
data = d[2]
}
return &JSONResult{
data: map[string]interface{}{
"code": code,
"message": message,
"data": data,
},
}
}
func (s *JSONResult) Set(key string, value interface{}) *JSONResult {
s.data[key] = value
return s
}
func (s *JSONResult) ToJSON() []byte {
j, _ := json.Marshal(s.data)
return j
}
func (s *JSONResult) SetCode(code int) *JSONResult {
return s.Set("code", code)
}
func (s *JSONResult) SetMessage(format string, msg ...interface{}) *JSONResult {
return s.Set("message", fmt.Sprintf(format, msg...))
}
func (s *JSONResult) SetData(d interface{}) *JSONResult {
return s.Set("data", d)
}
func (s *JSONResult) Code() int {
return gcast.ToInt(s.data["code"])
}
func (s *JSONResult) Message() string {
return gcast.ToString(s.data["message"])
}
func (s *JSONResult) Data() interface{} {
return s.data["data"]
}
func (s *JSONResult) DataMap() interface{} {
return s.data
}
func (s *JSONResult) WriteTo(dst io.Writer) (err error) {
_, err = dst.Write(s.ToJSON())
return
}
func (s *JSONResult) WriteToCtx(ctx gcore.Ctx) (err error) {
_, err = ctx.Response().Write(s.ToJSON())
return
}
// Success only worked with NewResultCtx()
func (s *JSONResult) Success(d ...interface{}) (err error) {
var data interface{}
if len(d) == 1 {
data = d[0]
}
return s.SetData(data).WriteToCtx(s.ctx)
}
// Fail only worked with NewResultCtx()
func (s *JSONResult) Fail(format string, v ...interface{}) (err error) {
return s.SetCode(1).SetMessage(format, v...).WriteToCtx(s.ctx)
}
func getJSONStr(v interface{}, nilValue string) (string, error) {
if gvalue.IsNil(v) {
return nilValue, nil
}
var str string
switch val := v.(type) {
case string:
str = val
case []byte:
str = string(val)
default:
b, _ := json.Marshal(v)
str = string(b)
}
if !Valid(str) {
return "", errors.New("fail to convert to invalid json")
}
return strings.Trim(str, " \r\n\t"), nil
}