diff --git a/tools/tools.go b/tools/tools.go index c319a6d4..b8ea767d 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -358,7 +358,23 @@ func (p *Parser) done() bool { } var count int + var inString, escaped bool for _, c := range p.buffer { + if escaped { + escaped = false + continue + } + if c == '\\' { + escaped = true + continue + } + if c == '"' { + inString = !inString + continue + } + if inString { + continue + } if c == byte(open) { count++ } else if c == byte(close) { diff --git a/tools/tools_test.go b/tools/tools_test.go index 2b8b04f8..689d6517 100644 --- a/tools/tools_test.go +++ b/tools/tools_test.go @@ -842,6 +842,24 @@ func TestDone(t *testing.T) { buffer: []byte("[]"), want: true, }, + { + name: "json close brace inside string", + tag: "{", + buffer: []byte("{\"note\": \"a}b\""), + want: false, + }, + { + name: "json complete with brace inside string", + tag: "{", + buffer: []byte("{\"note\": \"a}b\"}"), + want: true, + }, + { + name: "list close bracket inside string", + tag: "[", + buffer: []byte("[{\"note\": \"x]y\"}"), + want: false, + }, } for _, tt := range tests {