From f30bf03b589ede0f4e0ac6087e0565ffb1926de2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 29 Mar 2023 14:40:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=A7=A3=E6=9E=90json=20key?= =?UTF-8?q?=E5=92=8C=E6=95=B0=E6=8D=AE=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tree/consts.go | 22 ++++++++ tree/generate.go | 141 ++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 142 insertions(+), 21 deletions(-) diff --git a/tree/consts.go b/tree/consts.go index 203b24c..9a34962 100644 --- a/tree/consts.go +++ b/tree/consts.go @@ -18,10 +18,30 @@ const ( KeywordArrayEnd = "]" // KeywordColon 冒号 KeywordColon = ":" + // KeywordDot . + KeywordDot = "." // KeywordDoubleQuote 双引号 KeywordDoubleQuote = `"` // KeywordEscapeSymbol 转义符号 KeywordEscapeSymbol = `\` + // KeywordSpace 空格 + KeywordSpace = " " + // KeywordMinus 负号 + KeywordMinus = "-" + // KeywordTrueStart true的起始值 + KeywordTrueStart = "t" + // KeywordFalseStart false的起始值 + KeywordFalseStart = "f" + KeywordZero = "0" + KeywordOne = "1" + KeywordTwo = "2" + KeywordThree = "3" + KeywordFour = "4" + KeywordFive = "5" + KeywordSix = "6" + KeywordSeven = "7" + KeywordEight = "8" + KeywordNine = "9" ) const ( @@ -33,6 +53,8 @@ const ( ValueTypeInteger = "int64" // ValueTypeFloat float类型 ValueTypeFloat = "float64" + // ValueTypeNumber 数字 + ValueTypeNumber = "number" // ValueTypeMap map数据 ValueTypeMap = "map" // ValueTypeArray 数组 diff --git a/tree/generate.go b/tree/generate.go index 09a7160..4840708 100644 --- a/tree/generate.go +++ b/tree/generate.go @@ -7,7 +7,10 @@ // Date : 2023-03-28 18:42 package tree -import "strings" +import ( + "github.com/pkg/errors" + "strings" +) // New 生成一棵JSON树 // @@ -17,25 +20,11 @@ import "strings" func New(jsonData string) *Generate { jsonData = strings.TrimSpace(jsonData) g := &Generate{ - jsonData: jsonData, - } - if g.isArray() { - g.root = &Node{ - IsVirtual: true, - Value: nil, - ValueType: "", - Key: "", - Show: false, - ShowKey: "", - AllowEdit: false, - AllowChangeType: false, - DefaultValue: nil, - ParentNode: nil, - PreBrotherNode: nil, - LastBrotherNode: nil, - SonNodeList: nil, - } + jsonData: jsonData, + jsonDataByte: []byte(jsonData), } + g.root = NewVirtualNode() + g.currentNode = g.root return g } @@ -45,8 +34,118 @@ func New(jsonData string) *Generate { // // Date : 22:46 2023/3/28 type Generate struct { - root *Node - jsonData string + root *Node // 根节点 + currentNode *Node // 当前节点 + jsonData string + jsonDataByte []byte +} + +// getKey 获取jsonKey TODO : 转义符识别 +// +// Author : zhangdeman001@ke.com<张德满> +// +// Date : 11:36 2023/3/29 +func (g *Generate) getKey(startIndex int) (string, int, error) { + keyCharList := []string{} + hasStart := false + for startIndex < len(g.jsonDataByte) { + charStr := string(g.jsonDataByte[startIndex]) + if charStr == KeywordSpace { + // 跳过空格 + continue + } + if charStr == KeywordDoubleQuote && !hasStart { + // 第一次遇见双引号 + hasStart = true + } + if charStr == KeywordDoubleQuote && hasStart { + // 第二次遇见双引号,key探寻结束 + break + } + keyCharList = append(keyCharList, charStr) + startIndex++ + } + jsonKey := strings.Join(keyCharList, "") + + return jsonKey, startIndex, nil +} + +// getValueType 获取数据类型 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:37 2023/3/29 +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 !hasFindColon { + return "", startIndex, errors.New("value is invalid") + } + + keyValType := "" + // 冒号后面探寻值的类型 + for startIndex < len(g.jsonDataByte) { + charStr := string(g.jsonDataByte[startIndex]) + if charStr == KeywordSpace { + // 跳过空格 + startIndex++ + continue + } + + // 非空 + switch charStr { + case KeywordMinus: + fallthrough + case KeywordOne: + fallthrough + case KeywordTwo: + fallthrough + case KeywordThree: + fallthrough + case KeywordFour: + fallthrough + case KeywordFive: + fallthrough + case KeywordSix: + fallthrough + case KeywordSeven: + fallthrough + case KeywordEight: + fallthrough + case KeywordNine: + keyValType = ValueTypeNumber + case KeywordDoubleQuote: + keyValType = ValueTypeString + case KeywordArrayStart: + keyValType = ValueTypeArray + case KeywordObjectStart: + keyValType = ValueTypeMap + default: + return "", startIndex, errors.New("value type is invalid") + } + break + } + return keyValType, startIndex, nil } // isObject 整体是否为对象