From 98ff746add95917fe81e6648976944e531fb6076 Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Mon, 24 Dec 2018 21:08:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E4=BA=86gjson=E5=8C=B9=E9=85=8D?= =?UTF-8?q?=E8=A7=84=E5=88=99.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gjson.go | 78 +++++++++++++-------------------------------------- gjson_test.go | 6 ++-- 2 files changed, 23 insertions(+), 61 deletions(-) diff --git a/gjson.go b/gjson.go index c95d49b..138b52f 100644 --- a/gjson.go +++ b/gjson.go @@ -5,7 +5,6 @@ import ( "encoding/base64" "encoding/json" "errors" - "log" "reflect" "regexp" "strconv" @@ -781,68 +780,32 @@ func parseArrayPath(path string) (r arrayPathResult) { break } } - s = i - if r.query.op == "~" { - s = i + 1 - pair := path[i] - i++ - GETREGEXPSTR: - for ; i < len(path); i++ { - if path[i] == pair { - for iend := i + 1; iend < len(path); iend++ { - - if path[iend] == ' ' { - continue - } - - if path[iend] == ']' { - if iend+1 < len(path) && path[iend+1] == '#' { - r.query.all = true - } - break GETREGEXPSTR - } else { - break - } + s = i + 1 + pair := path[i] + i++ + GETREGEXPSTR: + for ; i < len(path); i++ { + if path[i] == pair { + for iend := i + 1; iend < len(path); iend++ { + if path[iend] == ' ' { + continue } + + if path[iend] == ']' { + if iend+1 < len(path) && path[iend+1] == '#' { + r.query.all = true + } + break GETREGEXPSTR + } else { + break + } + } } - } else { - - for ; i < len(path); i++ { - if path[i] == '"' { - i++ - s2 := i - for ; i < len(path); i++ { - if path[i] > '\\' { - continue - } - if path[i] == '"' { - // look for an escaped slash - if path[i-1] == '\\' { - n := 0 - for j := i - 2; j > s2-1; j-- { - if path[j] != '\\' { - break - } - n++ - } - if n%2 == 0 { - continue - } - } - break - } - } - } else if path[i] == ']' { - if i+1 < len(path) && path[i+1] == '#' { - r.query.all = true - } - break - } - } // } + if i > len(path) { i = len(path) } @@ -1136,7 +1099,6 @@ func queryMatches(rp *arrayPathResult, value Result) bool { case String: switch rp.query.op { case "=": - log.Println(rpv, value.Str) return value.Str == rpv case "!=": return value.Str != rpv diff --git a/gjson_test.go b/gjson_test.go index f3b4a6a..45785a5 100644 --- a/gjson_test.go +++ b/gjson_test.go @@ -1443,8 +1443,8 @@ func TestRegexp(t *testing.T) { } func TestChinese(t *testing.T) { - js := `{"data": [{"f":"广告"}, {"f": "广告"}]}` - mtok := get(js, `data.#[f~"广告"]#`) - log.Println(mtok.String()) + js := `{"data": [{"f":"\"广告"}, {"f": "广告"}]}` + mtok := get(js, `data.#[f=""广告"]#`) + log.Println(mtok.Array()[0].String()) t.Error("") }