引入复杂数据类型 & 支持是slice/map

This commit is contained in:
白茶清欢 2021-04-13 22:11:52 +08:00
parent 89349705fd
commit 2fd996aa4c
3 changed files with 37 additions and 23 deletions

View File

@ -36,6 +36,7 @@ type JSONode struct {
IsSlice bool // 是否是list IsSlice bool // 是否是list
IsIndexNode bool // 是否是slice的索引节点 IsIndexNode bool // 是否是slice的索引节点
Sort int // 此属性用于 slice解析,保证最终排序是对的 Sort int // 此属性用于 slice解析,保证最终排序是对的
IsComplex bool // 是否为复杂数据类型
} }
// NewDynamicJSON 获取JSON实例 // NewDynamicJSON 获取JSON实例
@ -75,7 +76,7 @@ type DynamicJSON struct {
// Author : go_developer@163.com<张德满> // Author : go_developer@163.com<张德满>
// //
// Date : 10:45 下午 2021/3/10 // Date : 10:45 下午 2021/3/10
func (dj *DynamicJSON) SetValue(path string, value interface{}) { func (dj *DynamicJSON) SetValue(path string, value interface{}, isComplexType bool) {
pathList := strings.Split(path, PathSplit) pathList := strings.Split(path, PathSplit)
searchRoot := dj.root searchRoot := dj.root
parent := dj.root parent := dj.root
@ -89,7 +90,7 @@ func (dj *DynamicJSON) SetValue(path string, value interface{}) {
if keyIndex == len(pathList)-1 { if keyIndex == len(pathList)-1 {
val = value val = value
} }
_ = dj.createNode(parent, key, val) _ = dj.createNode(parent, key, val, isComplexType)
if len(parent.Child) > 0 { if len(parent.Child) > 0 {
searchRoot = parent.Child[len(parent.Child)-1] searchRoot = parent.Child[len(parent.Child)-1]
parent = parent.Child[len(parent.Child)-1] parent = parent.Child[len(parent.Child)-1]
@ -130,7 +131,7 @@ func (dj *DynamicJSON) buildTpl(root *JSONode, tplList *[]string, valList *[]int
*tplList = append(*tplList, valFormat) *tplList = append(*tplList, valFormat)
switch val := root.Value.(type) { switch val := root.Value.(type) {
case string: case string:
*valList = append(*valList, "\""+val+"\"") *valList = append(*valList, val)
default: default:
*valList = append(*valList, root.Value) *valList = append(*valList, root.Value)
} }
@ -182,10 +183,16 @@ func (dj *DynamicJSON) getValFormat(root *JSONode) string {
} }
switch root.Value.(type) { switch root.Value.(type) {
case string: case string:
if root.IsHasLastBrother { if !root.IsComplex {
return "\"%v\"," if root.IsHasLastBrother {
return "\"%v\","
}
return "\"%v\""
} }
return "\"%v\"" if root.IsHasLastBrother {
return "%v,"
}
return "%v"
default: default:
if root.IsHasLastBrother { if root.IsHasLastBrother {
return "%v," return "%v,"
@ -284,7 +291,7 @@ func (dj *DynamicJSON) search(root *JSONode, key string) *JSONode {
// Author : go_developer@163.com<张德满> // Author : go_developer@163.com<张德满>
// //
// Date : 10:57 下午 2021/3/10 // Date : 10:57 下午 2021/3/10
func (dj *DynamicJSON) createNode(parent *JSONode, key string, value interface{}) error { func (dj *DynamicJSON) createNode(parent *JSONode, key string, value interface{}, isComplexType bool) error {
if nil == parent { if nil == parent {
return errors.New("create node error : parent id nil") return errors.New("create node error : parent id nil")
} }
@ -303,6 +310,7 @@ func (dj *DynamicJSON) createNode(parent *JSONode, key string, value interface{}
Child: make([]*JSONode, 0), Child: make([]*JSONode, 0),
IsRoot: false, IsRoot: false,
IsHasLastBrother: false, IsHasLastBrother: false,
IsComplex: isComplexType,
} }
parent.IsSlice, newNode.Sort = dj.extraSliceIndex(key) parent.IsSlice, newNode.Sort = dj.extraSliceIndex(key)
if parent.IsSlice { if parent.IsSlice {

View File

@ -20,23 +20,23 @@ import (
func TestJSON(t *testing.T) { func TestJSON(t *testing.T) {
tree := NewDynamicJSON() tree := NewDynamicJSON()
fmt.Println(tree.extraSliceIndex("[200]")) fmt.Println(tree.extraSliceIndex("[200]"))
tree.SetValue("extra.height.value", 180) tree.SetValue("extra.height.value", 180, false)
tree.SetValue("extra.height.unit.use", "cm") tree.SetValue("extra.height.unit.use", "cm", false)
tree.SetValue("extra.height.unit.open", "1") tree.SetValue("extra.height.unit.open", "1", false)
tree.SetValue("name", "zhangdeman") tree.SetValue("name", "zhangdeman", false)
tree.SetValue("good.name", "good") tree.SetValue("good.name", "good", false)
tree.SetValue("work", "111") tree.SetValue("work", "111", false)
tree.SetValue("good.price", "180") tree.SetValue("good.price", "180", false)
tree.SetValue("good.unit", "$") tree.SetValue("good.unit", "$", false)
tree.SetValue("slice.[0].name", "zhang") tree.SetValue("slice.[0].name", "zhang", false)
tree.SetValue("slice.[1].name", "de") tree.SetValue("slice.[1].name", "de", false)
tree.SetValue("slice.[2].name", "man") tree.SetValue("slice.[2].name", "man", false)
tree.SetValue("slice.[3]", "zhangdeman") tree.SetValue("slice.[3]", "zhangdeman", false)
fmt.Println(tree.String()) fmt.Println(tree.String())
tree = NewDynamicJSON() tree = NewDynamicJSON()
tree.SetValue("[0]", "zhang") tree.SetValue("[0]", "zhang", false)
tree.SetValue("[1]", "de") tree.SetValue("[1]", "de", false)
tree.SetValue("[2]", "man") tree.SetValue("[2]", "man", false)
fmt.Println(tree.String()) fmt.Println(tree.String())
} }

View File

@ -9,6 +9,7 @@ package json
import ( import (
"encoding/json" "encoding/json"
"fmt"
"reflect" "reflect"
"strings" "strings"
@ -53,9 +54,14 @@ func (pjt *ParseJSONTree) Parse(pathList []string) (*DynamicJSON, error) {
// 为数组的处理 // 为数组的处理
pathArr := strings.Split(path, "[]") pathArr := strings.Split(path, "[]")
for idx, item := range pathArr { for idx, item := range pathArr {
val := gjson.Get(source, item)
isComplextType := val.IsArray()
if len(pathArr)-1 == idx { if len(pathArr)-1 == idx {
// 当前是最后一项,说明不是数组 // 当前是最后一项,说明不是数组
result.SetValue(item, gjson.Get(source, item).Raw) if isComplextType {
fmt.Println("这是一个数组")
}
result.SetValue(item, val.Raw, isComplextType)
continue continue
} }
} }