gf/encoding/gjson/gjson_api.go

210 lines
5.6 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2019-05-08 22:04:36 +08:00
//
// 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"
2021-11-13 23:23:55 +08:00
"github.com/gogf/gf/v2/container/gvar"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/util/gutil"
2019-05-08 22:04:36 +08:00
)
2021-07-08 22:44:16 +08:00
// Interface returns the json value.
func (j *Json) Interface() interface{} {
if j == nil {
return nil
}
2019-05-08 22:04:36 +08:00
j.mu.RLock()
defer j.mu.RUnlock()
return *(j.p)
}
2021-01-19 14:26:17 +08:00
// Var returns the json value as *gvar.Var.
func (j *Json) Var() *gvar.Var {
return gvar.New(j.Interface())
2021-01-19 14:26:17 +08:00
}
// IsNil checks whether the value pointed by `j` is nil.
2019-07-18 18:59:49 +08:00
func (j *Json) IsNil() bool {
if j == nil {
return true
}
2019-07-18 18:59:49 +08:00
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`.
2019-05-08 22:04:36 +08:00
//
// 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
}
2019-05-08 22:04:36 +08:00
j.mu.RLock()
defer j.mu.RUnlock()
// It returns nil if pattern is empty.
if pattern == "" {
return nil
}
2022-02-23 00:46:13 +08:00
result := j.getPointerByPattern(pattern)
2019-05-08 22:04:36 +08:00
if result != nil {
return gvar.New(*result)
2019-05-08 22:04:36 +08:00
}
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.
2019-06-19 09:06:52 +08:00
func (j *Json) GetJson(pattern string, def ...interface{}) *Json {
return New(j.Get(pattern, def...).Val())
2019-05-08 22:04:36 +08:00
}
// GetJsons gets the value by specified `pattern`,
// and converts it to a slice of un-concurrent-safe Json object.
2019-06-19 09:06:52 +08:00
func (j *Json) GetJsons(pattern string, def ...interface{}) []*Json {
array := j.Get(pattern, def...).Array()
2019-06-19 09:06:52 +08:00
if len(array) > 0 {
jsonSlice := make([]*Json, len(array))
for i := 0; i < len(array); i++ {
jsonSlice[i] = New(array[i])
2019-06-19 09:06:52 +08:00
}
return jsonSlice
}
return nil
2019-05-08 22:04:36 +08:00
}
// GetJsonMap gets the value by specified `pattern`,
// and converts it to a map of un-concurrent-safe Json object.
2019-06-19 09:06:52 +08:00
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`.
2019-05-08 22:04:36 +08:00
// It supports hierarchical data access by char separator, which is '.' in default.
func (j *Json) Set(pattern string, value interface{}) error {
2019-06-19 09:06:52 +08:00
return j.setValue(pattern, value, false)
2019-05-08 22:04:36 +08:00
}
2021-12-20 00:32:23 +08:00
// 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`.
2019-05-08 22:04:36 +08:00
// It supports hierarchical data access by char separator, which is '.' in default.
func (j *Json) Remove(pattern string) error {
2019-06-19 09:06:52 +08:00
return j.setValue(pattern, nil, true)
2019-05-08 22:04:36 +08:00
}
2021-12-20 00:32:23 +08:00
// 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.
2019-05-08 22:04:36 +08:00
func (j *Json) Contains(pattern string) bool {
2019-06-19 09:06:52 +08:00
return j.Get(pattern) != nil
2019-05-08 22:04:36 +08:00
}
// Len returns the length/size of the value by specified `pattern`.
// The target value by `pattern` should be type of slice or map.
2019-05-08 22:04:36 +08:00
// It returns -1 if the target value is not found, or its type is invalid.
func (j *Json) Len(pattern string) int {
2019-06-19 09:06:52 +08:00
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
2019-05-08 22:04:36 +08:00
}
// Append appends value to the value by specified `pattern`.
// The target value by `pattern` should be type of slice.
2019-05-08 22:04:36 +08:00
func (j *Json) Append(pattern string, value interface{}) error {
2019-06-19 09:06:52 +08:00
p := j.getPointerByPattern(pattern)
if p == nil || *p == nil {
if pattern == "." {
return j.Set("0", value)
}
2019-06-19 09:06:52 +08:00
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)
}
2019-06-19 09:06:52 +08:00
return j.Set(fmt.Sprintf("%s.%d", pattern, len((*p).([]interface{}))), value)
}
return gerror.NewCodef(gcode.CodeInvalidParameter, "invalid variable type of %s", pattern)
2019-05-08 22:04:36 +08:00
}
2021-12-20 00:32:23 +08:00
// 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)
}
}
2020-12-14 18:54:14 +08:00
// Map converts current Json object to map[string]interface{}.
2019-05-08 22:04:36 +08:00
// It returns nil if fails.
2020-12-14 18:54:14 +08:00
func (j *Json) Map() map[string]interface{} {
return j.Var().Map()
2019-05-08 22:04:36 +08:00
}
2020-12-14 18:54:14 +08:00
// Array converts current Json object to []interface{}.
2019-05-08 22:04:36 +08:00
// It returns nil if fails.
2020-12-14 18:54:14 +08:00
func (j *Json) Array() []interface{} {
return j.Var().Array()
}
2020-12-14 18:54:14 +08:00
// Scan automatically calls Struct or Structs function according to the type of parameter
// `pointer` to implement the converting.
2020-12-14 18:54:14 +08:00
func (j *Json) Scan(pointer interface{}, mapping ...map[string]string) error {
return j.Var().Scan(pointer, mapping...)
}
2019-05-08 22:04:36 +08:00
// Dump prints current Json object with more manually readable.
2019-07-18 18:59:49 +08:00
func (j *Json) Dump() {
if j == nil {
return
}
2019-06-19 09:06:52 +08:00
j.mu.RLock()
defer j.mu.RUnlock()
2019-07-18 18:59:49 +08:00
gutil.Dump(*j.p)
}