|
|
|
@@ -8,8 +8,10 @@
|
|
|
|
|
package tree
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// New 生成一棵JSON树
|
|
|
|
@@ -17,7 +19,7 @@ import (
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 18:42 2023/3/28
|
|
|
|
|
func New(jsonData string) *Generate {
|
|
|
|
|
func New(jsonData string) (*Generate, error) {
|
|
|
|
|
jsonData = strings.TrimSpace(jsonData)
|
|
|
|
|
g := &Generate{
|
|
|
|
|
jsonData: jsonData,
|
|
|
|
@@ -25,7 +27,8 @@ func New(jsonData string) *Generate {
|
|
|
|
|
}
|
|
|
|
|
g.root = NewVirtualNode()
|
|
|
|
|
g.currentNode = g.root
|
|
|
|
|
return g
|
|
|
|
|
err := g.init()
|
|
|
|
|
return g, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate ...
|
|
|
|
@@ -34,10 +37,49 @@ func New(jsonData string) *Generate {
|
|
|
|
|
//
|
|
|
|
|
// Date : 22:46 2023/3/28
|
|
|
|
|
type Generate struct {
|
|
|
|
|
root *Node // 根节点
|
|
|
|
|
currentNode *Node // 当前节点
|
|
|
|
|
jsonData string
|
|
|
|
|
jsonDataByte []byte
|
|
|
|
|
root *Node // 根节点
|
|
|
|
|
currentNode *Node // 当前节点
|
|
|
|
|
currentParentNode *Node // 当前节点的父节点
|
|
|
|
|
jsonData string
|
|
|
|
|
jsonDataByte []byte
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// init 初始化
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 21:23 2023/3/29
|
|
|
|
|
func (g *Generate) init() error {
|
|
|
|
|
startIndex := 0
|
|
|
|
|
var (
|
|
|
|
|
jsonKey string
|
|
|
|
|
err error
|
|
|
|
|
valueType string
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 获取类型
|
|
|
|
|
if valueType, startIndex, err = g.getValueType(startIndex); nil != err {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g.root.ValueType = valueType
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
if startIndex >= len(g.jsonDataByte) {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取字段
|
|
|
|
|
if jsonKey, startIndex, err = g.getKey(startIndex); nil != err {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
// 获取类型
|
|
|
|
|
if valueType, startIndex, err = g.getValueType(startIndex); nil != err {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
fmt.Println(jsonKey, valueType, startIndex)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// getKey 获取jsonKey TODO : 转义符识别
|
|
|
|
@@ -52,14 +94,18 @@ func (g *Generate) getKey(startIndex int) (string, int, error) {
|
|
|
|
|
charStr := string(g.jsonDataByte[startIndex])
|
|
|
|
|
if charStr == KeywordSpace {
|
|
|
|
|
// 跳过空格
|
|
|
|
|
startIndex++
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if charStr == KeywordDoubleQuote && !hasStart {
|
|
|
|
|
// 第一次遇见双引号
|
|
|
|
|
startIndex++
|
|
|
|
|
hasStart = true
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if charStr == KeywordDoubleQuote && hasStart {
|
|
|
|
|
// 第二次遇见双引号,key探寻结束
|
|
|
|
|
startIndex++
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
keyCharList = append(keyCharList, charStr)
|
|
|
|
@@ -76,6 +122,19 @@ func (g *Generate) getKey(startIndex int) (string, int, error) {
|
|
|
|
|
//
|
|
|
|
|
// Date : 14:37 2023/3/29
|
|
|
|
|
func (g *Generate) getValueType(startIndex int) (string, int, error) {
|
|
|
|
|
if startIndex == 0 {
|
|
|
|
|
// 初始对象特殊处理
|
|
|
|
|
if string(g.jsonDataByte[0]) == KeywordArrayStart {
|
|
|
|
|
return ValueTypeArray, 1, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始对象特殊处理
|
|
|
|
|
if string(g.jsonDataByte[0]) == KeywordObjectStart {
|
|
|
|
|
return ValueTypeMap, 1, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "", 0, errors.New("root data parse : value is invalid")
|
|
|
|
|
}
|
|
|
|
|
// 指针向后, 探寻到冒号之后第一个非空格字符串
|
|
|
|
|
hasFindColon := false
|
|
|
|
|
for startIndex < len(g.jsonDataByte) {
|
|
|
|
@@ -99,7 +158,7 @@ func (g *Generate) getValueType(startIndex int) (string, int, error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !hasFindColon {
|
|
|
|
|
return "", startIndex, errors.New("value is invalid")
|
|
|
|
|
return "", startIndex, errors.New("check json value type : value is invalid")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
keyValType := ""
|
|
|
|
@@ -141,7 +200,7 @@ func (g *Generate) getValueType(startIndex int) (string, int, error) {
|
|
|
|
|
case KeywordObjectStart:
|
|
|
|
|
keyValType = ValueTypeMap
|
|
|
|
|
default:
|
|
|
|
|
return "", startIndex, errors.New("value type is invalid")
|
|
|
|
|
return "", startIndex, errors.New("validate json value type : value type is invalid")
|
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|