增减 "与:之间无效空格过滤
This commit is contained in:
parent
89f595c48a
commit
d933ff5f03
3
go.mod
3
go.mod
@ -3,15 +3,16 @@ module git.zhangdeman.cn/zhangdeman/filter
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20220704053716-ec9271c0f350
|
||||
github.com/Jeffail/gabs v1.4.0
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/tidwall/gjson v1.14.1
|
||||
)
|
||||
|
||||
require (
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20220704053716-ec9271c0f350 // indirect
|
||||
github.com/go-ini/ini v1.66.6 // indirect
|
||||
github.com/spaolacci/murmur3 v1.1.0 // indirect
|
||||
github.com/stretchr/testify v1.8.0 // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
|
12
go.sum
12
go.sum
@ -2,18 +2,30 @@ git.zhangdeman.cn/zhangdeman/util v0.0.0-20220704053716-ec9271c0f350 h1:y54/TJ7S
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20220704053716-ec9271c0f350/go.mod h1:aAIhnUdQewLipl4bddewAsAeSLST9SRvgTcPN5ITkAQ=
|
||||
github.com/Jeffail/gabs v1.4.0 h1://5fYRRTq1edjfIrQGvdkcd22pkYUrHZ5YC/H2GJVAo=
|
||||
github.com/Jeffail/gabs v1.4.0/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/bOXc=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-ini/ini v1.66.6 h1:h6k2Bb0HWS/BXXHCXj4QHjxPmlIU4NK+7MuLp9SD+4k=
|
||||
github.com/go-ini/ini v1.66.6/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
|
||||
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo=
|
||||
github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
|
||||
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
37
lexical.go
37
lexical.go
@ -15,12 +15,31 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// parseLexical 解析词法
|
||||
// NewLexical 获取实例
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 20:52 2022/7/4
|
||||
func NewLexical(jsonData string) *lexical {
|
||||
return &lexical{jsonData: jsonData}
|
||||
}
|
||||
|
||||
// lexical 词法解析
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 20:42 2022/7/4
|
||||
type lexical struct {
|
||||
jsonData string
|
||||
keyLeftRightTokenCnt int
|
||||
}
|
||||
|
||||
// Parse 解析词法
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:11 2022/7/4
|
||||
func parseLexical(jsonData string) ([]lexicalNode, error) {
|
||||
func (l *lexical) Parse(jsonData string) ([]lexicalNode, error) {
|
||||
jsonData = strings.ReplaceAll(strings.ReplaceAll(jsonData, "\n", ""), "\t", "")
|
||||
// mt.Println(jsonData)
|
||||
if len(jsonData) < 2 {
|
||||
@ -33,12 +52,20 @@ func parseLexical(jsonData string) ([]lexicalNode, error) {
|
||||
preChar := "-1"
|
||||
if len(lexicalList) > 0 {
|
||||
preChar = lexicalList[len(lexicalList)-1].Val
|
||||
if preChar == objectLeftToken && currentChar == " " {
|
||||
if len(tmpStr) == 0 && preChar == objectLeftToken && currentChar == " " {
|
||||
// 无意义的空格
|
||||
continue
|
||||
}
|
||||
if len(tmpStr) == 0 && currentChar == " " && preChar == keyLeftRightToken && l.keyLeftRightTokenCnt%2 == 0 {
|
||||
// " : 之间的空格无意义
|
||||
continue
|
||||
}
|
||||
}
|
||||
if inputCharIsToken(currentChar, preChar) {
|
||||
if l.inputCharIsToken(currentChar, preChar) {
|
||||
if currentChar == keyLeftRightToken {
|
||||
// 双引号计数
|
||||
l.keyLeftRightTokenCnt++
|
||||
}
|
||||
// 是关键词
|
||||
if len(tmpStr) > 0 {
|
||||
lexicalList = append(lexicalList, lexicalNode{
|
||||
@ -65,7 +92,7 @@ func parseLexical(jsonData string) ([]lexicalNode, error) {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:15 2022/7/4
|
||||
func inputCharIsToken(inputChar string, preChar string) bool {
|
||||
func (l *lexical) inputCharIsToken(inputChar string, preChar string) bool {
|
||||
if preChar == escapeCharacterToken {
|
||||
// 前一个是转义符, 当前不是关键字
|
||||
return false
|
||||
|
@ -13,7 +13,7 @@ import (
|
||||
|
||||
func Test_parseLexical(t *testing.T) {
|
||||
jsonData := `{
|
||||
"name":"zhangsan",
|
||||
"name" :"zhangsan",
|
||||
"age":"18",
|
||||
"extension":{
|
||||
"sex":"man",
|
||||
@ -31,5 +31,5 @@ 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"}]}`
|
||||
parseLexical(jsonData)
|
||||
NewLexical(jsonData).Parse(jsonData)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user