From 8a779f6e4166f5b40e6a14d6f19553a3ebd61b65 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 21 Aug 2014 14:42:02 -0700 Subject: [PATCH] json: support \n --- decoder_test.go | 10 ++++++++++ json/lex.go | 12 ++++++++++-- test-fixtures/multiline.json | 3 +++ test-fixtures/multiline_bad.hcl | 2 ++ 4 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 test-fixtures/multiline.json create mode 100644 test-fixtures/multiline_bad.hcl diff --git a/decoder_test.go b/decoder_test.go index 75980ff..12276e2 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -34,6 +34,16 @@ func TestDecode_interface(t *testing.T) { }, }, }, + { + "multiline_bad.hcl", + false, + map[string]interface{}{"foo": "bar\nbaz"}, + }, + { + "multiline.json", + false, + map[string]interface{}{"foo": "bar\nbaz"}, + }, { "scientific.json", false, diff --git a/json/lex.go b/json/lex.go index 0601ec1..f9be208 100644 --- a/json/lex.go +++ b/json/lex.go @@ -126,8 +126,16 @@ func (x *jsonLex) lexString(yylval *jsonSymType) int { } // If we're escaping a quote, then escape the quote - if c == '\\' && x.peek() == '"' { - c = x.next() + if c == '\\' { + n := x.next() + switch n { + case '"': + c = n + case 'n': + c = '\n' + default: + x.backup() + } } if _, err := b.WriteRune(c); err != nil { diff --git a/test-fixtures/multiline.json b/test-fixtures/multiline.json new file mode 100644 index 0000000..93f7cc5 --- /dev/null +++ b/test-fixtures/multiline.json @@ -0,0 +1,3 @@ +{ + "foo": "bar\nbaz" +} diff --git a/test-fixtures/multiline_bad.hcl b/test-fixtures/multiline_bad.hcl new file mode 100644 index 0000000..7ef0cb0 --- /dev/null +++ b/test-fixtures/multiline_bad.hcl @@ -0,0 +1,2 @@ +foo = "bar +baz"