Skip to content

Commit

Permalink
fix encoding RawMessage that contains leading space (#136)
Browse files Browse the repository at this point in the history
* fix encoding RawMessage that contains leading space.

* add json/bugs/issue136
  • Loading branch information
extemporalgenome authored Nov 27, 2023
1 parent 3b49d71 commit 6dfc1b0
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions json/bugs/issue11/main.go

This file was deleted.

23 changes: 23 additions & 0 deletions json/bugs/issue136/main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
4 changes: 0 additions & 4 deletions json/bugs/issue18/main.go

This file was deleted.

3 changes: 0 additions & 3 deletions json/bugs/issue84/main.go

This file was deleted.

1 change: 1 addition & 0 deletions json/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion json/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6dfc1b0

Please sign in to comment.