From d34819bcd6aac89d008354a8758c46be2368a4d8 Mon Sep 17 00:00:00 2001 From: Kevin Gillette Date: Sat, 25 Nov 2023 13:12:14 -0700 Subject: [PATCH 1/2] fix encoding RawMessage that contains leading space. --- json/encode.go | 1 + json/parse.go | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/json/encode.go b/json/encode.go index acb3b67..4173ddd 100644 --- a/json/encode.go +++ b/json/encode.go @@ -858,6 +858,7 @@ func (e encoder) encodeRawMessage(b []byte, p unsafe.Pointer) ([]byte, error) { s = v } else { var err error + v = skipSpaces(v) // don't assume that a RawMessage starts with a token. d := decoder{} s, _, _, err = d.parseValue(v) if err != nil { diff --git a/json/parse.go b/json/parse.go index 3e65621..49f63aa 100644 --- a/json/parse.go +++ b/json/parse.go @@ -709,7 +709,6 @@ func (d decoder) parseValue(b []byte) ([]byte, []byte, Kind, error) { case '{': v, b, k, err = d.parseObject(b) case '[': - k = Array v, b, k, err = d.parseArray(b) case '"': v, b, k, err = d.parseString(b) From 5453c72181d04e165a4d40f98fe83217cbda3657 Mon Sep 17 00:00:00 2001 From: Kevin Gillette Date: Sat, 25 Nov 2023 13:24:18 -0700 Subject: [PATCH 2/2] add json/bugs/issue136 --- Makefile | 2 +- json/bugs/issue11/main.go | 3 --- json/bugs/issue136/main_test.go | 23 +++++++++++++++++++++++ json/bugs/issue18/main.go | 4 ---- json/bugs/issue84/main.go | 3 --- 5 files changed, 24 insertions(+), 11 deletions(-) delete mode 100644 json/bugs/issue11/main.go create mode 100644 json/bugs/issue136/main_test.go delete mode 100644 json/bugs/issue18/main.go delete mode 100644 json/bugs/issue84/main.go diff --git a/Makefile b/Makefile index 9e540d7..ffb1396 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ test-json: go test -cover -race ./json test-json-bugs: - go test -cover -race ./json/bugs/... + go test -race ./json/bugs/... test-json-1.17: go test -cover -race -tags go1.17 ./json diff --git a/json/bugs/issue11/main.go b/json/bugs/issue11/main.go deleted file mode 100644 index 38dd16d..0000000 --- a/json/bugs/issue11/main.go +++ /dev/null @@ -1,3 +0,0 @@ -package main - -func main() {} diff --git a/json/bugs/issue136/main_test.go b/json/bugs/issue136/main_test.go new file mode 100644 index 0000000..044af10 --- /dev/null +++ b/json/bugs/issue136/main_test.go @@ -0,0 +1,23 @@ +package main + +import ( + "bytes" + "testing" + + "github.com/segmentio/encoding/json" +) + +func TestIssue136(t *testing.T) { + input := json.RawMessage(` null`) + + got, err := json.Marshal(input) + if err != nil { + t.Fatal(err) + } + + want := bytes.TrimSpace(input) + + if !bytes.Equal(got, want) { + t.Fatalf("Marshal(%q) = %q, want %q", input, got, want) + } +} diff --git a/json/bugs/issue18/main.go b/json/bugs/issue18/main.go deleted file mode 100644 index da29a2c..0000000 --- a/json/bugs/issue18/main.go +++ /dev/null @@ -1,4 +0,0 @@ -package main - -func main() { -} diff --git a/json/bugs/issue84/main.go b/json/bugs/issue84/main.go deleted file mode 100644 index 38dd16d..0000000 --- a/json/bugs/issue84/main.go +++ /dev/null @@ -1,3 +0,0 @@ -package main - -func main() {}