update json tree

This commit is contained in:
2024-09-23 15:59:09 +08:00
parent cd52bbb02b
commit b6a27fffd5

View File

@ -9,7 +9,7 @@ package tree
import ( import (
"fmt" "fmt"
"git.zhangdeman.cn/zhangdeman/util" "git.zhangdeman.cn/zhangdeman/wrapper"
"strings" "strings"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -88,7 +88,7 @@ func (g *Generate) init() error {
fmt.Println(jsonKey, valueType, jsonValue, startIndex) fmt.Println(jsonKey, valueType, jsonValue, startIndex)
// 创建节点, 并挂在到树上 // 创建节点, 并挂在到树上
var newNode *Node var newNode *Node
if util.Array.In(g.currentNode.ValueType, []string{ValueTypeArray, ValueTypeMap}) >= 0 { if wrapper.ArrayType([]string{ValueTypeArray, ValueTypeMap}).Has(g.currentNode.ValueType) >= 0 {
newNode = NewNode(jsonKey, jsonValue, valueType, g.currentNode) newNode = NewNode(jsonKey, jsonValue, valueType, g.currentNode)
g.currentParentNode = g.currentNode g.currentParentNode = g.currentNode
g.currentParentNode.SonNodeList = append(g.currentParentNode.SonNodeList, newNode) g.currentParentNode.SonNodeList = append(g.currentParentNode.SonNodeList, newNode)
@ -116,25 +116,26 @@ func (g *Generate) getKey(startIndex int) (string, int, error) {
continue continue
} }
if charStr == KeywordDoubleQuote && !hasStart { if charStr == KeywordDoubleQuote {
// 第一次遇见双引号 if !hasStart {
startIndex++ // 第一次遇见双引号
hasStart = true startIndex++
continue hasStart = true
} continue
if charStr == KeywordDoubleQuote && hasStart { } else {
// 第二次遇见双引号key探寻结束 // 第二次遇见双引号key探寻结束
startIndex++ startIndex++
break break
}
} }
if !hasStart { if !hasStart {
if util.Array.In(charStr, []string{ if wrapper.ArrayType([]string{
KeywordDoubleQuote, KeywordDoubleQuote,
KeywordObjectStart, KeywordObjectStart,
KeywordArrayStart, KeywordArrayStart,
KeywordComma, KeywordComma,
}) >= 0 { }).Has(charStr) >= 0 {
startIndex++ startIndex++
continue continue
} }
@ -181,21 +182,18 @@ func (g *Generate) getValueType(startIndex int) (string, int, error) {
hasFindColon := false hasFindColon := false
for startIndex < len(g.jsonDataByte) { for startIndex < len(g.jsonDataByte) {
charStr := string(g.jsonDataByte[startIndex]) charStr := string(g.jsonDataByte[startIndex])
if !hasFindColon { if charStr == KeywordSpace {
if charStr == KeywordSpace { // 跳过空格
// 跳过空格 startIndex++
startIndex++ continue
continue } else {
} // 非空格
if charStr != KeywordSpace { if charStr != KeywordColon {
// 非空格 return "", startIndex, errors.New("value is invalid")
if charStr != KeywordColon {
return "", startIndex, errors.New("value is invalid")
}
startIndex++
hasFindColon = true
break
} }
startIndex++
hasFindColon = true
break
} }
} }
@ -264,13 +262,13 @@ func (g *Generate) getValue(startIndex int) (string, int, error) {
continue continue
} }
if !isStart { if !isStart {
if util.Array.In(str, []string{KeywordArrayEnd, KeywordObjectEnd, KeywordComma}) >= 0 { if wrapper.ArrayType([]string{KeywordArrayEnd, KeywordObjectEnd, KeywordComma}).Has(str) >= 0 {
startIndex++ startIndex++
continue continue
} }
} }
if isStart { if isStart {
if util.Array.In(str, []string{KeywordDoubleQuote, KeywordArrayEnd, KeywordObjectEnd, KeywordComma}) >= 0 { if wrapper.ArrayType([]string{KeywordDoubleQuote, KeywordArrayEnd, KeywordObjectEnd, KeywordComma}).Has(str) >= 0 {
// 值的拼接已结束 // 值的拼接已结束
startIndex++ startIndex++
break break