From 047935c85c30732643432d73f4e07fd375a65338 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 22:06:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dkey=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tree/generate.go | 77 ++++++++++++++++++++++++++++++++++++++----- tree/generate_test.go | 23 +++++++++++++ 2 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 tree/generate_test.go diff --git a/tree/generate.go b/tree/generate.go index 4840708..afd8ef3 100644 --- a/tree/generate.go +++ b/tree/generate.go @@ -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 } diff --git a/tree/generate_test.go b/tree/generate_test.go new file mode 100644 index 0000000..a124fa3 --- /dev/null +++ b/tree/generate_test.go @@ -0,0 +1,23 @@ +// Package tree ... +// +// Description : tree ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-03-29 21:32 +package tree + +import ( + "fmt" + "testing" +) + +func TestNew(t *testing.T) { + jsonData := `{"name": "zhang", "age":17}` + g, err := New(jsonData) + if nil != err { + fmt.Println(err.Error()) + return + } + fmt.Println(g) +}