增加关键字场景

This commit is contained in:
白茶清欢 2022-07-04 23:28:12 +08:00
parent 8071532012
commit 5b099f34c3
2 changed files with 13 additions and 5 deletions

View File

@ -43,7 +43,7 @@ type lexical struct {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:11 2022/7/4
func (l *lexical) Parse(jsonData string) ([]lexicalNode, error) {
func (l *lexical) Parse(jsonData string) ([]*lexicalNode, error) {
// mt.Println(jsonData)
if len(jsonData) < 2 {
return nil, errors.New("input data is not json")
@ -101,8 +101,8 @@ func (l *lexical) Parse(jsonData string) ([]lexicalNode, error) {
format = append(format, val)
}
l.lexicalResult = format
util.JSON.ConsoleOutput(l.lexicalResult)
return nil, nil
// util.JSON.ConsoleOutput(l.lexicalResult)
return l.lexicalResult, nil
}
// inputCharIsToken 输入字符是否为关键字
@ -185,6 +185,7 @@ func (l *lexical) inputCharIsToken(inputChar string, tmpStr string) bool {
// {"person": {"name":"zhangsan", "age": 18}}
// {"person": {"work":true}}
// {"person": {"like":1}}
// {"person_list": [{"like":1}]}
if inputChar == objectRightToken && len(tmpStr) == 0 && (
// 对应 {}, [{}]
(nil != preNode && preNode.Val == objectLeftToken) ||
@ -193,7 +194,9 @@ func (l *lexical) inputCharIsToken(inputChar string, tmpStr string) bool {
// 对应 {"person": {"name":"zhangsan"}}
(nil != preNode && preNode.Val == objectRightToken)) ||
// 对应 {"person": {"work":true}} / {"person": {"like":1}}
(nil != preNode && preNode.Val == colonToken && (tmpStr == "number" || tmpStr == "bool")) {
(nil != preNode && preNode.Val == colonToken && (tmpStr == "number" || tmpStr == "bool")) ||
// 对应 {"person_list": [{"like":1}]}
(nil != preNode && preNode.Val == listRightToken) {
// } 是关键字
return true
}

View File

@ -8,6 +8,7 @@
package filter
import (
"fmt"
"testing"
)
@ -31,5 +32,9 @@ func Test_parseLexical(t *testing.T) {
]
}`
//jsonData = `{"name":"zhangsan","age":"18","extension":{"sex":"man","height":"180"},"teacher_list":[{"name":"t1","age":"11"},{"name":"t2","age":"12"}]}`
NewLexical(jsonData).Parse(jsonData)
r, _ := NewLexical(jsonData).Parse(jsonData)
for _, val := range r {
fmt.Print(val.Val)
}
fmt.Print("\n")
}