From 6a3548766dc27904e4e88a06f61501aa413a6e12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 5 Jul 2022 14:25:02 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=8D=E6=B3=95=E5=88=86=E6=9E=90=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E7=9C=9F=E5=AE=9E=E5=80=BC=20&&=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=20,=20=E5=85=B3=E9=94=AE=E8=AF=8D=E8=A7=A3=E6=9E=90=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lexical.go | 18 ++++++++++-------- lexical_test.go | 1 + 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lexical.go b/lexical.go index 593379c..aa77818 100644 --- a/lexical.go +++ b/lexical.go @@ -49,7 +49,7 @@ func (l *lexical) Parse(jsonData string) ([]*lexicalNode, error) { tmpStr := "" for _, itemChar := range jsonData { currentChar := string(itemChar) - tmpStrType := l.getTmpStrType(tmpStr) + tmpRealVal, tmpStrType := l.getTmpStrType(tmpStr) if l.inputCharIsToken(currentChar, tmpStr, tmpStrType) { if currentChar == keyLeftRightToken { // 双引号计数 @@ -58,13 +58,15 @@ func (l *lexical) Parse(jsonData string) ([]*lexicalNode, error) { // 是关键词 if len(tmpStr) > 0 { l.lexicalResult = append(l.lexicalResult, &lexicalNode{ - Val: tmpStr, + Val: tmpRealVal, IsToken: false, + Type: tmpStrType, }) } l.lexicalResult = append(l.lexicalResult, &lexicalNode{ Val: currentChar, IsToken: true, + Type: "string", }) tmpStr = "" } else { @@ -161,7 +163,7 @@ func (l *lexical) inputCharIsToken(inputChar string, tmpStr string, tmpStrType s // [[],[]] // [1,2,3] // [true,false,true] - if inputChar == commaToken && len(tmpStr) == 0 && ( + if inputChar == commaToken && ( // 对应 {"name":"zhangsan", "age":"18"} (nil != preNode && preNode.ValStr() == keyLeftRightToken) || // 对应[{"name":"zhangsan", "age":"18"}, {"name":"zhangsan", "age":"18"}] @@ -169,7 +171,7 @@ func (l *lexical) inputCharIsToken(inputChar string, tmpStr string, tmpStrType s // 对应[[],[]] (nil != preNode && preNode.ValStr() == listRightToken) || // 对应 [true,false,true] / [1,2,3] / [1,true,2,false] - (nil != preNode && (preNode.ValStr() == listLeftToken || preNode.ValStr() == commaToken) && (tmpStrType == "number" || tmpStr == "bool"))) { // 对应 + (nil != preNode && (preNode.ValStr() == colonToken || preNode.ValStr() == listLeftToken || preNode.ValStr() == commaToken) && (tmpStrType == "number" || tmpStrType == "bool"))) { // 对应 return true } @@ -244,7 +246,7 @@ func (l *lexical) inputCharIsToken(inputChar string, tmpStr string, tmpStrType s return false } -func (l *lexical) getTmpStrType(tmpStr string) string { +func (l *lexical) getTmpStrType(tmpStr string) (interface{}, string) { var preNode *lexicalNode if len(l.lexicalResult) > 0 { preNode = l.lexicalResult[len(l.lexicalResult)-1] @@ -255,7 +257,7 @@ func (l *lexical) getTmpStrType(tmpStr string) string { // 判断是否可转数字 var floatVal float64 if err := util.ConvertAssign(&floatVal, tmpStr); nil == err { - return "number" + return floatVal, "number" } } @@ -265,8 +267,8 @@ func (l *lexical) getTmpStrType(tmpStr string) string { // 判断是否可转数字 var boolVal bool if err := util.ConvertAssign(&boolVal, tmpStr); nil == err { - return "bool" + return boolVal, "bool" } } - return "string" + return tmpStr, "string" } diff --git a/lexical_test.go b/lexical_test.go index 13bfce5..98a6dd2 100644 --- a/lexical_test.go +++ b/lexical_test.go @@ -14,6 +14,7 @@ import ( func Test_parseLexical(t *testing.T) { jsonData := `{ + "start" : 123456, "name" : "zhangsan", "age":"18", "extension":{