File: xjson.go

package info (click to toggle)
golang-github-likexian-gokit 0.24.7-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 720 kB
  • sloc: makefile: 5
file content (736 lines) | stat: -rw-r--r-- 16,219 bytes parent folder | download
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/*
 * Copyright 2012-2021 Li Kexian
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * A toolkit for Golang development
 * https://www.likexian.com/
 */

package xjson

import (
	"bytes"
	"encoding/json"
	"errors"
	"reflect"
	"strconv"
	"strings"
	"time"

	"github.com/likexian/gokit/xfile"
)

var (
	// ErrTooManyArguments is too many arguments error
	ErrTooManyArguments = errors.New("xjson: too many arguments")
	// ErrInvalidArgumentType is invalid argument type error
	ErrInvalidArgumentType = errors.New("xjson: argument type is invalid")
	// ErrInvalidValueType is invalid value type error
	ErrInvalidValueType = errors.New("xjson: valud type is invalid")
	// ErrAssertToMap is assert to map error
	ErrAssertToMap = errors.New("xjson: assert to map failed")
	// ErrAssertToArray is assert to array error
	ErrAssertToArray = errors.New("xjson: assert to array failed")
	// ErrAssertToBool is assert to bool error
	ErrAssertToBool = errors.New("xjson: assert to bool failed")
	// ErrAssertToString is assert to string error
	ErrAssertToString = errors.New("xjson: assert to string failed")
	// ErrAssertToStringArray is assert to string array error
	ErrAssertToStringArray = errors.New("xjson: assert to string array failed")
)

// Json storing json data
type Json struct {
	data       interface{}
	escapeHtml bool
}

// Version returns package version
func Version() string {
	return "0.14.0"
}

// Author returns package author
func Author() string {
	return "[Li Kexian](https://www.likexian.com/)"
}

// License returns package license
func License() string {
	return "Licensed under the Apache License 2.0"
}

// New returns a pointer to a new Json object
//   data_json := New()
//   data_json := New(type Data struct{data string}{"zzz"})
//   data_json := New(map[string]interface{}{"iam": "Li Kexian"})
func New(args ...interface{}) *Json {
	switch len(args) {
	case 1:
		return &Json{
			data: args[0],
		}
	default:
		return &Json{
			data: make(map[string]interface{}),
		}
	}
}

// Load loads data from file, returns a json object
func Load(path string) (*Json, error) {
	j := New()
	err := j.Load(path)
	return j, err
}

// Loads unmarshal json from string, returns json object
func Loads(text string) (*Json, error) {
	j := New()
	err := j.Loads(text)
	return j, err
}

// Dump dumps json object to a file
func Dump(path string, data interface{}) error {
	return New(data).Dump(path)
}

// Dumps marshal json object to string
func Dumps(data interface{}) (string, error) {
	return New(data).Dumps()
}

// PrettyDumps marshal json object to string, with identation
func PrettyDumps(data interface{}) (string, error) {
	return New(data).PrettyDumps()
}

// Load loads data from file, returns a json object
func (j *Json) Load(path string) error {
	text, err := xfile.ReadText(path)
	if err != nil {
		return err
	}

	return j.Loads(text)
}

// Loads unmarshal json from string, returns json object
func (j *Json) Loads(text string) error {
	dec := json.NewDecoder(bytes.NewBuffer([]byte(text)))
	dec.UseNumber()
	err := dec.Decode(&j.data)

	return err
}

// Dump dumps json object to a file
func (j *Json) Dump(path string) (err error) {
	result, err := j.PrettyDumps()
	if err != nil {
		return
	}

	return xfile.WriteText(path, result)
}

// Dumps marshal json object to string
func (j *Json) Dumps() (result string, err error) {
	return j.doDumps("")
}

// PrettyDumps marshal json object to string, with identation
func (j *Json) PrettyDumps() (result string, err error) {
	return j.doDumps(strings.Repeat(" ", 4))
}

// do marshal json to string
func (j *Json) doDumps(indent string) (result string, err error) {
	var buf bytes.Buffer

	enc := json.NewEncoder(&buf)
	enc.SetEscapeHTML(j.escapeHtml)
	enc.SetIndent("", indent)
	err = enc.Encode(j.data)
	if err != nil {
		return
	}

	result = buf.String()
	result = strings.TrimSpace(result)

	return
}

// SetHtmlEscape set html escape for escaping of <, >, and & in JSON strings
func (j *Json) SetHtmlEscape(escape bool) {
	j.escapeHtml = escape
}

// Set set key-value to json object, dot(.) separated key is supported
//   json.Set("status", 1)
//   json.Set("status.code", 1)
//   ! NOT SUPPORTED json.Set("result.intlist.3", 666)
func (j *Json) Set(key string, value interface{}) {
	key = strings.TrimSpace(key)
	if key == "" {
		j.data = value
		return
	}

	result, err := j.Map()
	if err != nil {
		return
	}

	keys := strings.Split(key, ".")
	for i := 0; i < len(keys)-1; i++ {
		v := strings.TrimSpace(keys[i])
		if v != "" {
			if _, ok := result[v]; !ok {
				result[v] = make(map[string]interface{})
			}
			result = result[v].(map[string]interface{})
		}
	}

	result[keys[len(keys)-1]] = value
}

// Del delete key-value from json object, dot(.) separated key is supported
//   json.Del("status")
//   json.Del("status.code")
//   ! NOT SUPPORTED json.Del("result.intlist.3")
func (j *Json) Del(key string) {
	result, err := j.Map()
	if err != nil {
		return
	}

	var ok bool
	keys := strings.Split(key, ".")
	for i := 0; i < len(keys)-1; i++ {
		v := strings.TrimSpace(keys[i])
		if v != "" {
			if _, ok = result[v]; !ok {
				return
			}
			result, ok = result[v].(map[string]interface{})
			if !ok {
				return
			}
		}
	}

	delete(result, keys[len(keys)-1])
}

// Has check json object has key, dot(.) separated key is supported
//   json.Has("status")
//   json.Has("status.code")
//   json.Has("result.intlist.3")
func (j *Json) Has(key string) bool {
	result := j

	keys := strings.Split(key, ".")
	for i := 0; i < len(keys); i++ {
		v := strings.TrimSpace(keys[i])
		if v != "" {
			tmp, err := result.Map()
			if err == nil {
				if _, ok := tmp[v]; !ok {
					return false
				}
				if i == len(keys)-1 {
					return true
				}
				result = result.Get(v)
			} else {
				tmp, err := result.Array()
				if err == nil {
					n, err := strconv.Atoi(v)
					if err != nil {
						return false
					}
					if n >= len(tmp) {
						return false
					}
					if i == len(keys)-1 {
						return true
					}
					result = result.Index(n)
				} else {
					return false
				}
			}
		}
	}

	return false
}

// Get returns the pointer to json object by key, dot(.) separated key is supported
//   json.Get("status").Int()
//   json.Get("status.code").Int()
//   json.Get("result.intlist.3").Int()
func (j *Json) Get(key string) *Json {
	result := j

	for _, v := range strings.Split(key, ".") {
		v = strings.TrimSpace(v)
		if v != "" {
			tmp, err := result.Map()
			if err == nil {
				if _, ok := tmp[v]; ok {
					result = &Json{tmp[v], j.escapeHtml}
				} else {
					return &Json{nil, j.escapeHtml}
				}
			} else {
				_, err := result.Array()
				if err == nil {
					i, err := strconv.Atoi(v)
					if err != nil {
						return &Json{nil, j.escapeHtml}
					}
					result = result.Index(i)
				} else {
					return &Json{nil, j.escapeHtml}
				}
			}
		}
	}

	return result
}

// Index returns a pointer to the index of json object
//   json.Get("int_list").Index(1).Int()
func (j *Json) Index(i int) *Json {
	data, err := j.Array()
	if err == nil {
		if len(data) > i {
			return &Json{data[i], j.escapeHtml}
		}
	}

	return &Json{nil, j.escapeHtml}
}

// Len returns len of json object, -1 if type invalid or error
func (j *Json) Len() int {
	if v, err := j.Map(); err == nil {
		return len(v)
	}

	if v, err := j.Array(); err == nil {
		return len(v)
	}

	if v, err := j.String(); err == nil {
		return len(v)
	}

	return -1
}

// IsMap returns json object is a map
func (j *Json) IsMap() bool {
	switch j.data.(type) {
	case map[string]interface{}:
		return true
	default:
		return false
	}
}

// IsArray returns json object is an array
func (j *Json) IsArray() bool {
	switch j.data.(type) {
	case []interface{}:
		return true
	default:
		return false
	}
}

// Map returns as map from json object
func (j *Json) Map() (result map[string]interface{}, err error) {
	result, ok := (j.data).(map[string]interface{})
	if !ok {
		err = ErrAssertToMap
	}
	return
}

// Array returns as array from json object
func (j *Json) Array() (result []interface{}, err error) {
	result, ok := (j.data).([]interface{})
	if !ok {
		err = ErrAssertToArray
	}
	return
}

// Bool returns as bool from json object
func (j *Json) Bool() (result bool, err error) {
	result, ok := (j.data).(bool)
	if !ok {
		err = ErrAssertToBool
	}
	return
}

// String returns as string from json object
func (j *Json) String() (result string, err error) {
	result, ok := (j.data).(string)
	if !ok {
		err = ErrAssertToString
	}
	return
}

// StringArray returns as string array from json object
func (j *Json) StringArray() (result []string, err error) {
	data, err := j.Array()
	if err != nil {
		return
	}

	for _, v := range data {
		if v == nil {
			result = append(result, "")
		} else {
			r, ok := v.(string)
			if !ok {
				err = ErrAssertToStringArray
				return
			}
			result = append(result, r)
		}
	}

	return
}

// Time returns as time.Time from json object
// optional args is to set the time string parsing format, time.RFC3339 by default
// if the time is of int, optional args must not set
//   json.Time()
//   json.Time("2006-01-02 15:04:05")
func (j *Json) Time(args ...string) (result time.Time, err error) {
	switch j.data.(type) {
	case string:
		if len(args) > 1 {
			return result, ErrTooManyArguments
		}
		format := time.RFC3339
		if len(args) == 1 && strings.TrimSpace(args[0]) != "" {
			format = strings.TrimSpace(args[0])
		}
		return time.ParseInLocation(format, j.data.(string), time.Local)
	default:
		if len(args) > 0 {
			return result, ErrTooManyArguments
		}
		r, e := j.Int64()
		if e != nil {
			return result, e
		}
		return time.Unix(r, 0), nil
	}
}

// Float64 returns as float64 from json object
func (j *Json) Float64() (result float64, err error) {
	switch j.data.(type) {
	case json.Number:
		return j.data.(json.Number).Float64()
	case float32, float64:
		return reflect.ValueOf(j.data).Float(), nil
	case int, int8, int16, int32, int64:
		return float64(reflect.ValueOf(j.data).Int()), nil
	case uint, uint8, uint16, uint32, uint64:
		return float64(reflect.ValueOf(j.data).Uint()), nil
	default:
		return 0, ErrInvalidValueType
	}
}

// Int returns as int from json object
func (j *Json) Int() (result int, err error) {
	switch j.data.(type) {
	case json.Number:
		r, err := j.data.(json.Number).Int64()
		return int(r), err
	case float32, float64:
		return int(reflect.ValueOf(j.data).Float()), nil
	case int, int8, int16, int32, int64:
		return int(reflect.ValueOf(j.data).Int()), nil
	case uint, uint8, uint16, uint32, uint64:
		return int(reflect.ValueOf(j.data).Uint()), nil
	default:
		return 0, ErrInvalidValueType
	}
}

// Int64 returns as int64 from json object
func (j *Json) Int64() (result int64, err error) {
	switch j.data.(type) {
	case json.Number:
		return j.data.(json.Number).Int64()
	case float32, float64:
		return int64(reflect.ValueOf(j.data).Float()), nil
	case int, int8, int16, int32, int64:
		return reflect.ValueOf(j.data).Int(), nil
	case uint, uint8, uint16, uint32, uint64:
		return int64(reflect.ValueOf(j.data).Uint()), nil
	default:
		return 0, ErrInvalidValueType
	}
}

// Uint64 returns as uint64 from json object
func (j *Json) Uint64() (result uint64, err error) {
	switch j.data.(type) {
	case json.Number:
		return strconv.ParseUint(j.data.(json.Number).String(), 10, 64)
	case float32, float64:
		return uint64(reflect.ValueOf(j.data).Float()), nil
	case int, int8, int16, int32, int64:
		return uint64(reflect.ValueOf(j.data).Int()), nil
	case uint, uint8, uint16, uint32, uint64:
		return reflect.ValueOf(j.data).Uint(), nil
	default:
		return 0, ErrInvalidValueType
	}
}

// MustMap returns as map from json object with optional default value
// if error return default(if set) or panic
func (j *Json) MustMap(args ...map[string]interface{}) map[string]interface{} {
	if len(args) > 1 {
		panic(ErrTooManyArguments)
	}

	r, err := j.Map()
	if err == nil {
		return r
	}

	if len(args) == 1 {
		return args[0]
	}

	panic(err)
}

// MustArray returns as array from json object with optional default value
// if error return default(if set) or panic
func (j *Json) MustArray(args ...[]interface{}) []interface{} {
	if len(args) > 1 {
		panic(ErrTooManyArguments)
	}

	r, err := j.Array()
	if err == nil {
		return r
	}

	if len(args) == 1 {
		return args[0]
	}

	panic(err)
}

// MustBool returns as bool from json object with optional default value
// if error return default(if set) or panic
func (j *Json) MustBool(args ...bool) bool {
	if len(args) > 1 {
		panic(ErrTooManyArguments)
	}

	r, err := j.Bool()
	if err == nil {
		return r
	}

	if len(args) == 1 {
		return args[0]
	}

	panic(err)
}

// MustString returns as string from json object with optional default value
// if error return default(if set) or panic
func (j *Json) MustString(args ...string) string {
	if len(args) > 1 {
		panic(ErrTooManyArguments)
	}

	r, err := j.String()
	if err == nil {
		return r
	}

	if len(args) == 1 {
		return args[0]
	}

	panic(err)
}

// MustStringArray returns as string from json object with optional default value
// if error return default(if set) or panic
func (j *Json) MustStringArray(args ...[]string) []string {
	if len(args) > 1 {
		panic(ErrTooManyArguments)
	}

	r, err := j.StringArray()
	if err == nil {
		return r
	}

	if len(args) == 1 {
		return args[0]
	}

	panic(err)
}

// MustTime returns as time.Time from json object
// if error return default(if set) or panic
//   json.Time()                                                 // No format,  No default
//   json.Time("2006-01-02 15:04:05")                            // Has format, No default
//   json.Time(time.Unix(1548907870, 0))                         // No format,  Has default
//   json.Time("2006-01-02 15:04:05", time.Unix(1548907870, 0))  // Has format, Has default
func (j *Json) MustTime(args ...interface{}) time.Time {
	if len(args) > 2 {
		panic(ErrTooManyArguments)
	}

	format := ""
	defset := false
	var defbak time.Time

	for i := 0; i < len(args); i++ {
		switch args[i].(type) {
		case string:
			format = args[i].(string)
		case time.Time:
			defbak = args[i].(time.Time)
			defset = true
		default:
			panic(ErrInvalidArgumentType)
		}
	}

	var r time.Time
	var err error
	if format != "" {
		r, err = j.Time(format)
	} else {
		r, err = j.Time()
	}

	if err == nil {
		return r
	}

	if defset {
		return defbak
	}

	panic(err)
}

// MustFloat64 returns as float64 from json object with optional default value
// if error return default(if set) or panic
func (j *Json) MustFloat64(args ...float64) float64 {
	if len(args) > 1 {
		panic(ErrTooManyArguments)
	}

	r, err := j.Float64()
	if err == nil {
		return r
	}

	if len(args) == 1 {
		return args[0]
	}

	panic(err)
}

// MustInt returns as int from json object with optional default value
// if error return default(if set) or panic
func (j *Json) MustInt(args ...int) int {
	if len(args) > 1 {
		panic(ErrTooManyArguments)
	}

	r, err := j.Int()
	if err == nil {
		return r
	}

	if len(args) == 1 {
		return args[0]
	}

	panic(err)
}

// MustInt64 returns as int64 from json object with optional default value
// if error return default(if set) or panic
func (j *Json) MustInt64(args ...int64) int64 {
	if len(args) > 1 {
		panic(ErrTooManyArguments)
	}

	r, err := j.Int64()
	if err == nil {
		return r
	}

	if len(args) == 1 {
		return args[0]
	}

	panic(err)
}

// MustUint64 returns as uint64 from json object with optional default value
// if error return default(if set) or panic
func (j *Json) MustUint64(args ...uint64) uint64 {
	if len(args) > 1 {
		panic(ErrTooManyArguments)
	}

	r, err := j.Uint64()
	if err == nil {
		return r
	}

	if len(args) == 1 {
		return args[0]
	}

	panic(err)
}