From 8065c5d93f09f65b4fd2f5822b30da022bdcddb5 Mon Sep 17 00:00:00 2001 From: kruskal <99559985+kruskall@users.noreply.github.com> Date: Mon, 12 Aug 2024 14:47:14 +0200 Subject: [PATCH 1/2] fix: marshal \b and \f properly writer wasn't encoding \b and \f causing some diff with the upstream json package. Update switch and add test to validate all utf8 runes are encoded correctly --- writer.go | 16 ++++++++++------ writer_test.go | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/writer.go b/writer.go index 85e7240..3f3329c 100644 --- a/writer.go +++ b/writer.go @@ -126,16 +126,20 @@ func (w *Writer) StringContents(s string) { // single-with character, need to escape w.RawString(s[p:i]) switch c { - case '\t': - w.RawString(`\t`) - case '\r': - w.RawString(`\r`) - case '\n': - w.RawString(`\n`) case '\\': w.RawString(`\\`) case '"': w.RawString(`\"`) + case '\b': + w.RawString(`\b`) + case '\f': + w.RawString(`\f`) + case '\n': + w.RawString(`\n`) + case '\r': + w.RawString(`\r`) + case '\t': + w.RawString(`\t`) default: w.RawString(`\u00`) w.RawByte(chars[c>>4]) diff --git a/writer_test.go b/writer_test.go index 68faeba..baea2c9 100644 --- a/writer_test.go +++ b/writer_test.go @@ -1,8 +1,11 @@ package fastjson import ( + "bytes" + "encoding/json" "testing" "time" + "unicode/utf8" ) func TestWriterReset(t *testing.T) { @@ -36,6 +39,25 @@ func TestWriterTime(t *testing.T) { assertEncoded(t, &w, `Thu, 01 Jan 1970 00:00:00 +0000`) } +func TestWriterString(t *testing.T) { + var i rune + for i = 0; i < utf8.MaxRune; i++ { + s := string(i) + var w Writer + w.String(s) + + expected, err := json.Marshal(s) + if err != nil { + t.Errorf("json.Marshal returned unexpected error: %v", err) + } + + got := w.Bytes() + if !bytes.Equal(expected, got) { + t.Errorf("rune %d: expected '%s', got '%s'", i, expected, got) + } + } +} + func TestWriterStringEscapes(t *testing.T) { var w Writer w.StringContents("\t\r\n\\\"\x00") From a08feb8deaf10c4a02d1ec77f581976d97b57d6e Mon Sep 17 00:00:00 2001 From: kruskal <99559985+kruskall@users.noreply.github.com> Date: Mon, 19 Aug 2024 11:10:29 +0200 Subject: [PATCH 2/2] feat: require a supported version of go --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index d932bd4..1d203a4 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module go.elastic.co/fastjson -go 1.19 +go 1.22 require golang.org/x/tools v0.24.0