2017-12-29 16:03:30 +08:00
|
|
|
|
// Copyright 2017 gf Author(https://gitee.com/johng/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://gitee.com/johng/gf.
|
|
|
|
|
|
2018-01-03 10:53:45 +08:00
|
|
|
|
// JSON解析/封装
|
2017-11-23 10:21:28 +08:00
|
|
|
|
package gjson
|
|
|
|
|
|
|
|
|
|
import (
|
2018-01-23 15:02:42 +08:00
|
|
|
|
"sync"
|
2017-11-23 10:21:28 +08:00
|
|
|
|
"strings"
|
|
|
|
|
"strconv"
|
2017-12-14 17:32:51 +08:00
|
|
|
|
"io/ioutil"
|
2017-12-25 17:04:54 +08:00
|
|
|
|
"encoding/json"
|
2018-01-19 15:26:28 +08:00
|
|
|
|
"gitee.com/johng/gf/g/os/gfile"
|
2018-01-19 16:19:48 +08:00
|
|
|
|
"gitee.com/johng/gf/g/util/gconv"
|
2018-01-19 15:26:28 +08:00
|
|
|
|
"gitee.com/johng/gf/g/encoding/gxml"
|
2018-01-19 16:19:48 +08:00
|
|
|
|
"gitee.com/johng/gf/g/encoding/gyaml"
|
2018-01-20 11:09:27 +08:00
|
|
|
|
"gitee.com/johng/gf/g/encoding/gtoml"
|
2017-11-23 10:21:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// json解析结果存放数组
|
|
|
|
|
type Json struct {
|
2018-01-23 15:02:42 +08:00
|
|
|
|
mu sync.RWMutex
|
|
|
|
|
p *interface{} // 注意这是一个指针
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 编码go变量为json字符串,并返回json字符串指针
|
2017-12-25 17:04:54 +08:00
|
|
|
|
func Encode (v interface{}) ([]byte, error) {
|
|
|
|
|
return json.Marshal(v)
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 解码字符串为interface{}变量
|
2017-12-25 17:04:54 +08:00
|
|
|
|
func Decode (b []byte) (interface{}, error) {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
var v interface{}
|
2018-01-11 18:02:56 +08:00
|
|
|
|
if err := DecodeTo(b, &v); err != nil {
|
2017-12-13 17:35:43 +08:00
|
|
|
|
return nil, err
|
|
|
|
|
} else {
|
|
|
|
|
return v, nil
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-19 15:26:28 +08:00
|
|
|
|
// 解析json字符串为go变量,注意第二个参数为指针(任意结构的变量)
|
2017-12-25 17:04:54 +08:00
|
|
|
|
func DecodeTo (b []byte, v interface{}) error {
|
|
|
|
|
return json.Unmarshal(b, v)
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 解析json字符串为gjson.Json对象,并返回操作对象指针
|
2017-12-25 17:04:54 +08:00
|
|
|
|
func DecodeToJson (b []byte) (*Json, error) {
|
|
|
|
|
if v, err := Decode(b); err != nil {
|
2017-12-13 17:35:43 +08:00
|
|
|
|
return nil, err
|
2018-01-11 18:02:56 +08:00
|
|
|
|
} else {
|
2018-01-23 16:40:48 +08:00
|
|
|
|
return NewJson(v), nil
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-19 15:26:28 +08:00
|
|
|
|
// 支持多种配置文件类型转换为json格式内容并解析为gjson.Json对象
|
2017-12-14 17:32:51 +08:00
|
|
|
|
func Load (path string) (*Json, error) {
|
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2018-01-19 16:19:48 +08:00
|
|
|
|
return LoadContent(data, gfile.Ext(path))
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-20 11:09:27 +08:00
|
|
|
|
// 支持的配置文件格式:xml, json, yaml/yml, toml
|
2018-01-19 16:19:48 +08:00
|
|
|
|
func LoadContent (data []byte, t string) (*Json, error) {
|
|
|
|
|
var err error
|
|
|
|
|
var result interface{}
|
|
|
|
|
switch t {
|
2018-01-20 11:09:27 +08:00
|
|
|
|
case "xml": fallthrough
|
2018-01-19 15:26:28 +08:00
|
|
|
|
case ".xml":
|
|
|
|
|
data, err = gxml.ToJson(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2018-01-20 11:09:27 +08:00
|
|
|
|
case "yml": fallthrough
|
|
|
|
|
case "yaml": fallthrough
|
|
|
|
|
case ".yml": fallthrough
|
2018-01-19 16:19:48 +08:00
|
|
|
|
case ".yaml":
|
|
|
|
|
data, err = gyaml.ToJson(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2018-01-20 11:09:27 +08:00
|
|
|
|
|
|
|
|
|
case "toml": fallthrough
|
|
|
|
|
case ".toml":
|
|
|
|
|
data, err = gtoml.ToJson(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2018-01-19 15:26:28 +08:00
|
|
|
|
}
|
2017-12-14 17:32:51 +08:00
|
|
|
|
if err := json.Unmarshal(data, &result); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2018-01-23 16:40:48 +08:00
|
|
|
|
return NewJson(result), nil
|
2017-12-14 17:32:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-23 10:21:28 +08:00
|
|
|
|
// 将变量转换为Json对象进行处理,该变量至少应当是一个map或者array,否者转换没有意义
|
2018-01-25 10:09:52 +08:00
|
|
|
|
func NewJson(value interface{}) *Json {
|
|
|
|
|
switch value.(type) {
|
|
|
|
|
case map[string]interface{}:
|
|
|
|
|
return &Json{ p: &value }
|
|
|
|
|
case []interface{}:
|
|
|
|
|
return &Json{ p: &value }
|
|
|
|
|
default:
|
|
|
|
|
// 这里效率会比较低
|
|
|
|
|
b, _ := Encode(value)
|
|
|
|
|
v, _ := Decode(b)
|
|
|
|
|
return &Json{ p: &v }
|
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将指定的json内容转换为指定结构返回,查找失败或者转换失败,目标对象转换为nil
|
|
|
|
|
// 注意第二个参数需要给的是变量地址
|
2018-01-22 17:55:12 +08:00
|
|
|
|
func (j *Json) GetToVar(pattern string, v interface{}) error {
|
|
|
|
|
r := j.Get(pattern)
|
2017-11-23 10:21:28 +08:00
|
|
|
|
if r != nil {
|
2017-12-13 17:35:43 +08:00
|
|
|
|
if t, err := Encode(r); err == nil {
|
|
|
|
|
return DecodeTo(t, v)
|
|
|
|
|
} else {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
} else {
|
|
|
|
|
v = nil
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获得一个键值对关联数组/哈希表,方便操作,不需要自己做类型转换
|
|
|
|
|
// 注意,如果获取的值不存在,或者类型与json类型不匹配,那么将会返回nil
|
2018-01-22 17:55:12 +08:00
|
|
|
|
func (j *Json) GetMap(pattern string) map[string]interface{} {
|
|
|
|
|
result := j.Get(pattern)
|
2017-11-23 10:21:28 +08:00
|
|
|
|
if result != nil {
|
|
|
|
|
if r, ok := result.(map[string]interface{}); ok {
|
|
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-18 11:07:44 +08:00
|
|
|
|
// 将检索值转换为Json对象指针返回
|
2018-01-22 17:55:12 +08:00
|
|
|
|
func (j *Json) GetJson(pattern string) *Json {
|
|
|
|
|
result := j.Get(pattern)
|
2017-12-18 11:07:44 +08:00
|
|
|
|
if result != nil {
|
2018-01-23 16:40:48 +08:00
|
|
|
|
return NewJson(result)
|
2017-12-18 11:07:44 +08:00
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-23 10:21:28 +08:00
|
|
|
|
// 获得一个数组[]interface{},方便操作,不需要自己做类型转换
|
|
|
|
|
// 注意,如果获取的值不存在,或者类型与json类型不匹配,那么将会返回nil
|
2018-01-22 17:55:12 +08:00
|
|
|
|
func (j *Json) GetArray(pattern string) []interface{} {
|
|
|
|
|
result := j.Get(pattern)
|
2017-11-23 10:21:28 +08:00
|
|
|
|
if result != nil {
|
|
|
|
|
if r, ok := result.([]interface{}); ok {
|
|
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回指定json中的string
|
2018-01-22 17:55:12 +08:00
|
|
|
|
func (j *Json) GetString(pattern string) string {
|
|
|
|
|
return gconv.String(j.Get(pattern))
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-31 11:09:16 +08:00
|
|
|
|
// 返回指定json中的bool(false:"", 0, false, off)
|
2018-01-22 17:55:12 +08:00
|
|
|
|
func (j *Json) GetBool(pattern string) bool {
|
|
|
|
|
return gconv.Bool(j.Get(pattern))
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-22 17:55:12 +08:00
|
|
|
|
func (j *Json) GetInt(pattern string) int {
|
|
|
|
|
return gconv.Int(j.Get(pattern))
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-22 17:55:12 +08:00
|
|
|
|
func (j *Json) GetUint(pattern string) uint {
|
|
|
|
|
return gconv.Uint(j.Get(pattern))
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-22 17:55:12 +08:00
|
|
|
|
func (j *Json) GetFloat32(pattern string) float32 {
|
|
|
|
|
return gconv.Float32(j.Get(pattern))
|
2017-12-31 11:09:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-22 17:55:12 +08:00
|
|
|
|
func (j *Json) GetFloat64(pattern string) float64 {
|
|
|
|
|
return gconv.Float64(j.Get(pattern))
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-26 17:42:14 +08:00
|
|
|
|
// 动态设置层级变量
|
|
|
|
|
func (j *Json) Set(pattern string, value interface{}) error {
|
|
|
|
|
return j.setValue(pattern, value, false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 动态删除层级变量
|
|
|
|
|
func (j *Json) Remove(pattern string) error {
|
|
|
|
|
return j.setValue(pattern, nil, true)
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-26 16:36:07 +08:00
|
|
|
|
// 根据pattern查找并设置数据
|
2018-01-23 16:40:48 +08:00
|
|
|
|
// 注意:
|
2018-01-25 17:43:07 +08:00
|
|
|
|
// 1、写入的时候"."符号只能表示层级,不能使用带"."符号的键名;
|
2018-01-26 17:42:14 +08:00
|
|
|
|
// 2、写入的value为nil且removed为true时,表示删除;
|
2018-01-26 16:36:07 +08:00
|
|
|
|
// 3、里面的层级处理比较复杂,逻辑较复杂的地方在于层级检索及节点创建,叶子赋值;
|
2018-01-26 17:42:14 +08:00
|
|
|
|
func (j *Json) setValue(pattern string, value interface{}, removed bool) error {
|
2018-01-23 18:23:05 +08:00
|
|
|
|
// 初始化判断
|
|
|
|
|
if *j.p == nil {
|
|
|
|
|
if isNumeric(pattern) {
|
|
|
|
|
*j.p = make([]interface{}, 0)
|
|
|
|
|
} else {
|
|
|
|
|
*j.p = make(map[string]interface{})
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-26 16:36:07 +08:00
|
|
|
|
var pparent *interface{}
|
|
|
|
|
var pointer *interface{}
|
|
|
|
|
|
|
|
|
|
pointer = j.p
|
|
|
|
|
pparent = nil
|
|
|
|
|
value = j.convertValue(value)
|
|
|
|
|
array := strings.Split(pattern, ".")
|
|
|
|
|
length := len(array)
|
|
|
|
|
|
2018-01-23 15:02:42 +08:00
|
|
|
|
j.mu.Lock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
for i:= 0; i < length; i++ {
|
|
|
|
|
switch (*pointer).(type) {
|
|
|
|
|
case map[string]interface{}:
|
2018-01-22 17:55:12 +08:00
|
|
|
|
if i == length - 1 {
|
2018-01-26 16:36:07 +08:00
|
|
|
|
if removed && value == nil {
|
2018-01-25 17:43:07 +08:00
|
|
|
|
// 删除map元素
|
2018-01-23 16:40:48 +08:00
|
|
|
|
delete((*pointer).(map[string]interface{}), array[i])
|
|
|
|
|
} else {
|
|
|
|
|
(*pointer).(map[string]interface{})[array[i]] = value
|
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
} else {
|
2018-01-25 17:43:07 +08:00
|
|
|
|
// 当键名不存在的情况这里会进行处理
|
2018-01-22 17:55:12 +08:00
|
|
|
|
v, ok := (*pointer).(map[string]interface{})[array[i]]
|
|
|
|
|
if !ok {
|
2018-01-26 16:36:07 +08:00
|
|
|
|
if removed && value == nil {
|
2018-01-23 18:23:05 +08:00
|
|
|
|
goto done
|
|
|
|
|
}
|
2018-01-22 17:55:12 +08:00
|
|
|
|
}
|
2018-01-25 17:43:07 +08:00
|
|
|
|
pparent = pointer
|
2018-01-22 17:55:12 +08:00
|
|
|
|
pointer = &v
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
2018-01-25 17:43:07 +08:00
|
|
|
|
|
2017-11-23 10:21:28 +08:00
|
|
|
|
case []interface{}:
|
|
|
|
|
if isNumeric(array[i]) {
|
2018-01-22 17:55:12 +08:00
|
|
|
|
if n, err := strconv.Atoi(array[i]); err == nil {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
if i == length - 1 {
|
2018-01-25 17:43:07 +08:00
|
|
|
|
if len((*pointer).([]interface{})) > n {
|
2018-01-26 16:36:07 +08:00
|
|
|
|
if removed && value == nil {
|
2018-01-25 17:43:07 +08:00
|
|
|
|
// 删除数据元素
|
2018-01-26 16:36:07 +08:00
|
|
|
|
j.setPointerWithValue(pparent, array[i - 1], append((*pointer).([]interface{})[ : n], (*pointer).([]interface{})[n + 1 : ]...))
|
2018-01-23 16:40:48 +08:00
|
|
|
|
} else {
|
|
|
|
|
(*pointer).([]interface{})[n] = value
|
|
|
|
|
}
|
2018-01-22 17:55:12 +08:00
|
|
|
|
} else {
|
2018-01-26 16:36:07 +08:00
|
|
|
|
if removed && value == nil {
|
2018-01-25 17:43:07 +08:00
|
|
|
|
goto done
|
|
|
|
|
}
|
2018-01-26 16:36:07 +08:00
|
|
|
|
// 叶子节点:需要对父级重新赋值
|
2018-01-25 17:43:07 +08:00
|
|
|
|
s := make([]interface{}, n + 1)
|
|
|
|
|
copy(s, (*pointer).([]interface{}))
|
|
|
|
|
s[n] = value
|
2018-01-26 16:36:07 +08:00
|
|
|
|
if pparent != nil {
|
|
|
|
|
pparent = j.setPointerWithValue(pparent, array[i - 1], s)
|
2018-01-25 17:43:07 +08:00
|
|
|
|
} else {
|
2018-01-26 16:36:07 +08:00
|
|
|
|
*pointer = s
|
|
|
|
|
pparent = pointer
|
2018-01-23 16:40:48 +08:00
|
|
|
|
}
|
2018-01-22 17:55:12 +08:00
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
} else {
|
2018-01-25 17:43:07 +08:00
|
|
|
|
// 不存在则创建节点
|
|
|
|
|
if len((*pointer).([]interface{})) > n {
|
|
|
|
|
pparent = pointer
|
|
|
|
|
pointer = &(*pointer).([]interface{})[n]
|
|
|
|
|
} else {
|
2018-01-26 16:36:07 +08:00
|
|
|
|
if removed && value == nil {
|
2018-01-25 17:43:07 +08:00
|
|
|
|
goto done
|
|
|
|
|
}
|
2018-01-26 16:36:07 +08:00
|
|
|
|
// 1.0
|
|
|
|
|
s := make([]interface{}, n + 1)
|
2018-01-25 17:43:07 +08:00
|
|
|
|
copy(s, (*pointer).([]interface{}))
|
2018-01-26 16:36:07 +08:00
|
|
|
|
if pparent != nil {
|
|
|
|
|
pparent = j.setPointerWithValue(pparent, array[i - 1], s)
|
|
|
|
|
} else {
|
|
|
|
|
*pointer = s
|
|
|
|
|
pparent = pointer
|
|
|
|
|
}
|
|
|
|
|
pointer = &s[n]
|
2018-01-25 17:43:07 +08:00
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
2018-01-22 17:55:12 +08:00
|
|
|
|
} else {
|
2018-01-23 15:02:42 +08:00
|
|
|
|
j.mu.Unlock()
|
2018-01-22 17:55:12 +08:00
|
|
|
|
return err
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
2018-01-23 15:02:42 +08:00
|
|
|
|
} else {
|
2018-01-26 16:36:07 +08:00
|
|
|
|
var v interface{}
|
|
|
|
|
if i == length - 1 {
|
|
|
|
|
v = map[string]interface{}{
|
|
|
|
|
array[i] : value,
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
v = map[string]interface{}{}
|
|
|
|
|
}
|
|
|
|
|
if pparent != nil {
|
|
|
|
|
pparent = j.setPointerWithValue(pparent, array[i], v)
|
|
|
|
|
} else {
|
|
|
|
|
*pointer = v
|
|
|
|
|
pparent = pointer
|
|
|
|
|
}
|
|
|
|
|
pointer = &v
|
2018-01-22 17:55:12 +08:00
|
|
|
|
}
|
2018-01-25 17:43:07 +08:00
|
|
|
|
|
|
|
|
|
default:
|
2018-01-26 16:36:07 +08:00
|
|
|
|
if removed && value == nil {
|
2018-01-25 17:43:07 +08:00
|
|
|
|
goto done
|
|
|
|
|
}
|
2018-01-25 18:57:21 +08:00
|
|
|
|
// 判断当前节点应当为map或者数组
|
2018-01-26 16:36:07 +08:00
|
|
|
|
if isNumeric(array[i]) {
|
|
|
|
|
if n, err := strconv.Atoi(array[i]); err == nil {
|
|
|
|
|
s := make([]interface{}, n + 1)
|
|
|
|
|
if i == length - 1 {
|
|
|
|
|
s[n] = value
|
|
|
|
|
}
|
|
|
|
|
if pparent != nil {
|
|
|
|
|
pparent = j.setPointerWithValue(pparent, array[i - 1], s)
|
2018-01-25 18:57:21 +08:00
|
|
|
|
} else {
|
2018-01-26 16:36:07 +08:00
|
|
|
|
*pointer = s
|
|
|
|
|
pparent = pointer
|
2018-01-25 18:57:21 +08:00
|
|
|
|
}
|
2018-01-26 16:36:07 +08:00
|
|
|
|
pointer = &s[n]
|
|
|
|
|
} else {
|
|
|
|
|
return err
|
2018-01-25 17:43:07 +08:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2018-01-26 16:36:07 +08:00
|
|
|
|
var v interface{}
|
2018-01-25 17:43:07 +08:00
|
|
|
|
if i == length - 1 {
|
|
|
|
|
v = map[string]interface{}{
|
|
|
|
|
array[i] : value,
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2018-01-26 16:36:07 +08:00
|
|
|
|
v = map[string]interface{}{}
|
2018-01-25 17:43:07 +08:00
|
|
|
|
}
|
2018-01-26 16:36:07 +08:00
|
|
|
|
if pparent != nil {
|
|
|
|
|
pparent = j.setPointerWithValue(pparent, array[i - 1], v)
|
|
|
|
|
} else {
|
|
|
|
|
*pointer = v
|
|
|
|
|
pparent = pointer
|
|
|
|
|
}
|
|
|
|
|
pointer = &v
|
2018-01-25 17:43:07 +08:00
|
|
|
|
}
|
2018-01-22 17:55:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-23 18:23:05 +08:00
|
|
|
|
done:
|
2018-01-23 15:02:42 +08:00
|
|
|
|
j.mu.Unlock()
|
2018-01-22 17:55:12 +08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-23 15:02:42 +08:00
|
|
|
|
// 数据结构转换,map参数必须转换为map[string]interface{},数组参数必须转换为[]interface{}
|
|
|
|
|
func (j *Json) convertValue(value interface{}) interface{} {
|
|
|
|
|
switch value.(type) {
|
|
|
|
|
case map[string]interface{}:
|
|
|
|
|
return value
|
|
|
|
|
case []interface{}:
|
|
|
|
|
return value
|
|
|
|
|
default:
|
2018-01-25 10:09:52 +08:00
|
|
|
|
// 这里效率会比较低,当然比直接用反射也不会差到哪儿去
|
|
|
|
|
// 为了操作的灵活性,牺牲了一定的效率
|
2018-01-23 15:02:42 +08:00
|
|
|
|
b, _ := Encode(value)
|
|
|
|
|
v, _ := Decode(b)
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-26 16:36:07 +08:00
|
|
|
|
// 用于Set方法中,对指针指向的内存地址进行赋值
|
|
|
|
|
// 返回修改后的父级指针
|
|
|
|
|
func (j *Json) setPointerWithValue(pointer *interface{}, key string, value interface{}) *interface{} {
|
|
|
|
|
switch (*pointer).(type) {
|
2018-01-22 17:55:12 +08:00
|
|
|
|
case map[string]interface{}:
|
2018-01-26 16:36:07 +08:00
|
|
|
|
(*pointer).(map[string]interface{})[key] = value
|
|
|
|
|
return &value
|
2018-01-22 17:55:12 +08:00
|
|
|
|
case []interface{}:
|
2018-01-26 16:36:07 +08:00
|
|
|
|
n, _ := strconv.Atoi(key)
|
|
|
|
|
if len((*pointer).([]interface{})) > n {
|
|
|
|
|
(*pointer).([]interface{})[n] = value
|
|
|
|
|
return &(*pointer).([]interface{})[n]
|
|
|
|
|
} else {
|
|
|
|
|
s := make([]interface{}, n + 1)
|
|
|
|
|
copy(s, (*pointer).([]interface{}))
|
|
|
|
|
s[n] = value
|
|
|
|
|
*pointer = s
|
|
|
|
|
return &s[n]
|
2018-01-22 17:55:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-26 16:36:07 +08:00
|
|
|
|
return pointer
|
2018-01-22 17:55:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据约定字符串方式访问json解析数据,参数形如: "items.name.first", "list.0"
|
|
|
|
|
// 返回的结果类型的interface{},因此需要自己做类型转换
|
|
|
|
|
// 如果找不到对应节点的数据,返回nil
|
|
|
|
|
func (j *Json) Get(pattern string) interface{} {
|
2018-01-23 15:02:42 +08:00
|
|
|
|
j.mu.RLock()
|
|
|
|
|
defer j.mu.RUnlock()
|
2018-01-22 17:55:12 +08:00
|
|
|
|
if r := j.getPointerByPattern(pattern); r != nil {
|
|
|
|
|
return *r
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据pattern层级查找变量指针
|
|
|
|
|
func (j *Json) getPointerByPattern(pattern string) *interface{} {
|
|
|
|
|
index := len(pattern)
|
2018-04-10 15:36:35 +08:00
|
|
|
|
start := 0
|
2018-01-22 17:55:12 +08:00
|
|
|
|
length := 0
|
|
|
|
|
pointer := j.p
|
2018-04-10 15:36:35 +08:00
|
|
|
|
if index == 0 {
|
|
|
|
|
return pointer
|
|
|
|
|
}
|
2018-01-22 17:55:12 +08:00
|
|
|
|
for {
|
|
|
|
|
if r := j.checkPatternByPointer(pattern[start:index], pointer); r != nil {
|
|
|
|
|
length += index - start
|
|
|
|
|
if start > 0 {
|
|
|
|
|
length += 1
|
|
|
|
|
}
|
|
|
|
|
start = index + 1
|
|
|
|
|
index = len(pattern)
|
|
|
|
|
if length == len(pattern) {
|
|
|
|
|
return r
|
|
|
|
|
} else {
|
|
|
|
|
pointer = r
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
index = strings.LastIndex(pattern[start:index], ".")
|
2018-01-23 15:43:16 +08:00
|
|
|
|
if index != -1 && length > 0 {
|
|
|
|
|
index += length + 1
|
|
|
|
|
}
|
2018-01-22 17:55:12 +08:00
|
|
|
|
}
|
|
|
|
|
if start >= index {
|
|
|
|
|
break
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-22 17:55:12 +08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 判断给定的pattern在当前的pointer下是否有值,并返回对应的pointer
|
|
|
|
|
// 注意这里返回的指针都是临时变量的内存地址
|
|
|
|
|
func (j *Json) checkPatternByPointer(pattern string, pointer *interface{}) *interface{} {
|
|
|
|
|
switch (*pointer).(type) {
|
|
|
|
|
case map[string]interface{}:
|
|
|
|
|
if v, ok := (*pointer).(map[string]interface{})[pattern]; ok {
|
|
|
|
|
return &v
|
|
|
|
|
}
|
|
|
|
|
case []interface{}:
|
|
|
|
|
if isNumeric(pattern) {
|
|
|
|
|
n, err := strconv.Atoi(pattern)
|
|
|
|
|
if err == nil && len((*pointer).([]interface{})) > n {
|
|
|
|
|
return &(*pointer).([]interface{})[n]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 转换为map[string]interface{}类型,如果转换失败,返回nil
|
2018-01-22 17:55:12 +08:00
|
|
|
|
func (j *Json) ToMap() map[string]interface{} {
|
2018-01-23 15:02:42 +08:00
|
|
|
|
j.mu.RLock()
|
|
|
|
|
defer j.mu.RUnlock()
|
2018-01-22 17:55:12 +08:00
|
|
|
|
switch (*(j.p)).(type) {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
case map[string]interface{}:
|
2018-01-22 17:55:12 +08:00
|
|
|
|
return (*(j.p)).(map[string]interface{})
|
2017-11-23 10:21:28 +08:00
|
|
|
|
default:
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 转换为[]interface{}类型,如果转换失败,返回nil
|
2018-01-22 17:55:12 +08:00
|
|
|
|
func (j *Json) ToArray() []interface{} {
|
2018-01-23 15:02:42 +08:00
|
|
|
|
j.mu.RLock()
|
|
|
|
|
defer j.mu.RUnlock()
|
2018-01-22 17:55:12 +08:00
|
|
|
|
switch (*(j.p)).(type) {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
case []interface{}:
|
2018-01-22 17:55:12 +08:00
|
|
|
|
return (*(j.p)).([]interface{})
|
2017-11-23 10:21:28 +08:00
|
|
|
|
default:
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-22 17:55:12 +08:00
|
|
|
|
func (j *Json) ToXml(rootTag...string) ([]byte, error) {
|
|
|
|
|
return gxml.Encode(j.ToMap(), rootTag...)
|
2018-01-19 16:19:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-22 17:55:12 +08:00
|
|
|
|
func (j *Json) ToXmlIndent(rootTag...string) ([]byte, error) {
|
|
|
|
|
return gxml.EncodeWithIndent(j.ToMap(), rootTag...)
|
2018-01-19 16:19:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-22 17:55:12 +08:00
|
|
|
|
func (j *Json) ToJson() ([]byte, error) {
|
2018-01-23 15:02:42 +08:00
|
|
|
|
j.mu.RLock()
|
|
|
|
|
defer j.mu.RUnlock()
|
2018-01-22 17:55:12 +08:00
|
|
|
|
return Encode(*(j.p))
|
2018-01-19 16:19:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-22 17:55:12 +08:00
|
|
|
|
func (j *Json) ToJsonIndent() ([]byte, error) {
|
2018-01-23 15:02:42 +08:00
|
|
|
|
j.mu.RLock()
|
|
|
|
|
defer j.mu.RUnlock()
|
2018-01-22 17:55:12 +08:00
|
|
|
|
return json.MarshalIndent(*(j.p), "", "\t")
|
2018-01-19 16:19:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-22 17:55:12 +08:00
|
|
|
|
func (j *Json) ToYaml() ([]byte, error) {
|
2018-01-23 15:02:42 +08:00
|
|
|
|
j.mu.RLock()
|
|
|
|
|
defer j.mu.RUnlock()
|
2018-01-22 17:55:12 +08:00
|
|
|
|
return gyaml.Encode(*(j.p))
|
2018-01-19 16:19:48 +08:00
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
|
2018-01-22 17:55:12 +08:00
|
|
|
|
func (j *Json) ToToml() ([]byte, error) {
|
2018-01-23 15:02:42 +08:00
|
|
|
|
j.mu.RLock()
|
|
|
|
|
defer j.mu.RUnlock()
|
2018-01-22 17:55:12 +08:00
|
|
|
|
return gtoml.Encode(*(j.p))
|
2018-01-20 11:09:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-23 10:21:28 +08:00
|
|
|
|
// 判断所给字符串是否为数字
|
|
|
|
|
func isNumeric(s string) bool {
|
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
|
if s[i] < byte('0') || s[i] > byte('9') {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|