From b6a27fffd5008abad852a4eb54357636e7f13de8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 23 Sep 2024 15:59:09 +0800 Subject: [PATCH] update json tree --- tree/generate.go | 58 +++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/tree/generate.go b/tree/generate.go index 0aa7d02..9f84f37 100644 --- a/tree/generate.go +++ b/tree/generate.go @@ -9,7 +9,7 @@ package tree import ( "fmt" - "git.zhangdeman.cn/zhangdeman/util" + "git.zhangdeman.cn/zhangdeman/wrapper" "strings" "github.com/pkg/errors" @@ -88,7 +88,7 @@ func (g *Generate) init() error { fmt.Println(jsonKey, valueType, jsonValue, startIndex) // 创建节点, 并挂在到树上 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) g.currentParentNode = g.currentNode g.currentParentNode.SonNodeList = append(g.currentParentNode.SonNodeList, newNode) @@ -116,25 +116,26 @@ func (g *Generate) getKey(startIndex int) (string, int, error) { continue } - if charStr == KeywordDoubleQuote && !hasStart { - // 第一次遇见双引号 - startIndex++ - hasStart = true - continue - } - if charStr == KeywordDoubleQuote && hasStart { - // 第二次遇见双引号,key探寻结束 - startIndex++ - break + if charStr == KeywordDoubleQuote { + if !hasStart { + // 第一次遇见双引号 + startIndex++ + hasStart = true + continue + } else { + // 第二次遇见双引号,key探寻结束 + startIndex++ + break + } } if !hasStart { - if util.Array.In(charStr, []string{ + if wrapper.ArrayType([]string{ KeywordDoubleQuote, KeywordObjectStart, KeywordArrayStart, KeywordComma, - }) >= 0 { + }).Has(charStr) >= 0 { startIndex++ continue } @@ -181,21 +182,18 @@ func (g *Generate) getValueType(startIndex int) (string, int, error) { hasFindColon := false for startIndex < len(g.jsonDataByte) { charStr := string(g.jsonDataByte[startIndex]) - if !hasFindColon { - if charStr == KeywordSpace { - // 跳过空格 - startIndex++ - continue - } - if charStr != KeywordSpace { - // 非空格 - if charStr != KeywordColon { - return "", startIndex, errors.New("value is invalid") - } - startIndex++ - hasFindColon = true - break + if charStr == KeywordSpace { + // 跳过空格 + startIndex++ + continue + } else { + // 非空格 + if charStr != KeywordColon { + return "", startIndex, errors.New("value is invalid") } + startIndex++ + hasFindColon = true + break } } @@ -264,13 +262,13 @@ func (g *Generate) getValue(startIndex int) (string, int, error) { continue } if !isStart { - if util.Array.In(str, []string{KeywordArrayEnd, KeywordObjectEnd, KeywordComma}) >= 0 { + if wrapper.ArrayType([]string{KeywordArrayEnd, KeywordObjectEnd, KeywordComma}).Has(str) >= 0 { startIndex++ continue } } 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++ break