Skip to content

Commit

Permalink
types/json: fix an over-quoted bug in BinaryJSON.Unquote function (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sre-bot committed Sep 11, 2019
1 parent cdf4566 commit 820206e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
4 changes: 3 additions & 1 deletion expression/builtin_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ func (s *testEvaluatorSuite) TestJSONUnquote(c *C) {
{`{"a": "b"}`, `{"a": "b"}`},
{`"hello,\"quoted string\",world"`, `hello,"quoted string",world`},
{`"hello,\"宽字符\",world"`, `hello,"宽字符",world`},
{`Invalid Json string\tis OK`, `Invalid Json string is OK`},
{`Invalid Json string\tis OK`, `Invalid Json string\tis OK`},
{`"1\\u2232\\u22322"`, `1\u2232\u22322`},
{`"[{\"x\":\"{\\\"y\\\":12}\"}]"`, `[{"x":"{\"y\":12}"}]`},
{`[{\"x\":\"{\\\"y\\\":12}\"}]`, `[{\"x\":\"{\\\"y\\\":12}\"}]`},
}
dtbl := tblToDtbl(tbl)
for _, t := range dtbl {
Expand Down
22 changes: 10 additions & 12 deletions types/json/binary_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,17 @@ func (bj BinaryJSON) Unquote() (string, error) {
switch bj.TypeCode {
case TypeCodeString:
tmp := string(hack.String(bj.GetString()))
s, err := unquoteString(tmp)
if err != nil {
return "", errors.Trace(err)
}
// Remove prefix and suffix '"'.
slen := len(s)
if slen > 1 {
head, tail := s[0], s[slen-1]
if head == '"' && tail == '"' {
return s[1 : slen-1], nil
}
tlen := len(tmp)
if tlen < 2 {
return tmp, nil
}
head, tail := tmp[0], tmp[tlen-1]
if head == '"' && tail == '"' {
// Remove prefix and suffix '"' before unquoting
return unquoteString(tmp[1 : tlen-1])
}
return s, nil
// if value is not double quoted, do nothing
return tmp, nil
default:
return bj.String(), nil
}
Expand Down
1 change: 1 addition & 0 deletions types/json/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func (s *testJSONSuite) TestBinaryJSONUnquote(c *C) {
}{
{j: `3`, unquoted: "3"},
{j: `"3"`, unquoted: "3"},
{j: `"[{\"x\":\"{\\\"y\\\":12}\"}]"`, unquoted: `[{"x":"{\"y\":12}"}]`},
{j: `"hello, \"escaped quotes\" world"`, unquoted: "hello, \"escaped quotes\" world"},
{j: "\"\\u4f60\"", unquoted: "你"},
{j: `true`, unquoted: "true"},
Expand Down

0 comments on commit 820206e

Please sign in to comment.