diff options
author | Luke Shumaker <lukeshu@datawire.io> | 2022-08-21 21:39:59 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@datawire.io> | 2022-08-21 21:39:59 -0600 |
commit | 325838f35ce90080aa6c892a998d960c06c1c144 (patch) | |
tree | 60e87ee9a622a0261b93ef1e47dbca632c588276 /encode.go | |
parent | d25456172946e5921747cd57fb04eb5b6da72fb6 (diff) |
Add tests for the actual usability of the Decodable and Encodable interfaces
Diffstat (limited to 'encode.go')
-rw-r--r-- | encode.go | 11 |
1 files changed, 8 insertions, 3 deletions
@@ -40,7 +40,8 @@ func encodeWriteString(w io.Writer, str string) { } type Encoder struct { - w *ReEncoder + w *ReEncoder + closeAfterEncode bool } // NewEncoder returns a new encoder. @@ -57,7 +58,8 @@ func NewEncoder(w io.Writer) *Encoder { } } return &Encoder{ - w: re, + w: re, + closeAfterEncode: len(re.par.stack) == 0 || (len(re.par.stack) == 1 && re.par.stack[0] == RuneTypeError), } } @@ -72,7 +74,10 @@ func (enc *Encoder) Encode(obj any) (err error) { } }() encode(enc.w, reflect.ValueOf(obj), enc.w.BackslashEscape, false, 0, map[any]struct{}{}) - return enc.w.Close() + if enc.closeAfterEncode { + return enc.w.Close() + } + return nil } // Encode encodes a value to w. |