diff options
author | Luke Shumaker <lukeshu@datawire.io> | 2022-08-30 01:36:46 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@datawire.io> | 2022-08-30 01:36:46 -0600 |
commit | 8cf459090af5a4e5db0f8c9a4ac21667029f8639 (patch) | |
tree | 5e4e38582eaa2a4e4df72bb37548eb6d1764d47d | |
parent | 325838f35ce90080aa6c892a998d960c06c1c144 (diff) |
Allow calling lowmemjson.Encode at the root of EncodeJSON
-rw-r--r-- | encode.go | 4 | ||||
-rw-r--r-- | methods_test.go | 9 |
2 files changed, 11 insertions, 2 deletions
@@ -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 { |