Fix JSON parsing in SQL

Previously, JSON which contained bracket `[]` or brace `{}` characters inside strings would fail to parse as valid JSON.
This commit is contained in:
Tobie Morgan Hitchcock 2017-04-12 17:44:27 +01:00
parent b7f4984641
commit 17b1f9ff55

View file

@ -738,17 +738,18 @@ func (s *scanner) scanObject(chp ...rune) (tok Token, lit string, val interface{
beg := chp[0] beg := chp[0]
end := beg end := beg
sub := 0 sub := 0
qut := 0
if beg == '{' { if beg == '{' {
end = '}' end = '}'
tok = JSON
} }
if beg == '[' { if beg == '[' {
end = ']' end = ']'
tok = ARRAY
} }
tok = IDENT
// Create a buffer // Create a buffer
var buf bytes.Buffer var buf bytes.Buffer
@ -759,7 +760,7 @@ func (s *scanner) scanObject(chp ...rune) (tok Token, lit string, val interface{
// Read subsequent characters // Read subsequent characters
for { for {
if ch := s.next(); ch == end && sub == 0 { if ch := s.next(); ch == end && sub == 0 && qut == 0 {
buf.WriteRune(ch) buf.WriteRune(ch)
break break
} else if ch == beg { } else if ch == beg {
@ -770,6 +771,13 @@ func (s *scanner) scanObject(chp ...rune) (tok Token, lit string, val interface{
buf.WriteRune(ch) buf.WriteRune(ch)
} else if ch == eof { } else if ch == eof {
return ILLEGAL, buf.String(), val return ILLEGAL, buf.String(), val
} else if ch == '"' {
if qut == 0 {
qut++
} else {
qut--
}
buf.WriteRune(ch)
} else if ch == '\\' { } else if ch == '\\' {
switch chn := s.next(); chn { switch chn := s.next(); chn {
default: default:
@ -783,14 +791,7 @@ func (s *scanner) scanObject(chp ...rune) (tok Token, lit string, val interface{
} }
} }
if beg == '{' { return tok, buf.String(), val
return JSON, buf.String(), val
}
if beg == '[' {
return ARRAY, buf.String(), val
}
return ILLEGAL, buf.String(), val
} }