From 8cf459090af5a4e5db0f8c9a4ac21667029f8639 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Tue, 30 Aug 2022 01:36:46 -0600 Subject: Allow calling lowmemjson.Encode at the root of EncodeJSON --- encode.go | 4 +++- methods_test.go | 9 ++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/encode.go b/encode.go index f93cba4..acef214 100644 --- a/encode.go +++ b/encode.go @@ -9,8 +9,10 @@ import ( "encoding" "encoding/base64" "encoding/json" + "errors" "fmt" "io" + iofs "io/fs" "reflect" "sort" "strconv" @@ -130,7 +132,7 @@ func encode(w io.Writer, val reflect.Value, escaper BackslashEscaper, quote bool SourceFunc: "EncodeJSON", }}) } - if err := validator.Close(); err != nil { + if err := validator.Close(); err != nil && !errors.Is(err, iofs.ErrClosed) { panic(encodeError{err}) } diff --git a/methods_test.go b/methods_test.go index 8280a94..a74532c 100644 --- a/methods_test.go +++ b/methods_test.go @@ -15,10 +15,17 @@ import ( "git.lukeshu.com/go/lowmemjson" ) +type ShortSum string + +func (s ShortSum) EncodeJSON(w io.Writer) error { + // Test that it's OK to call lowmemjson.Encode for the top-level value in a method. + return lowmemjson.Encode(w, string(s)) +} + type SumRun struct { ChecksumSize int `json:",omitempty"` Addr int64 `json:",omitempty"` - Sums string + Sums ShortSum } type SumRunWithGaps struct { -- cgit v1.2.3-2-g168b