增加关键字场景
This commit is contained in:
parent
8071532012
commit
5b099f34c3
11
lexical.go
11
lexical.go
@ -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
|
||||
}
|
||||
|
@ -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")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user