summaryrefslogtreecommitdiff
path: root/encode.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@datawire.io>2022-08-21 21:39:59 -0600
committerLuke Shumaker <lukeshu@datawire.io>2022-08-21 21:39:59 -0600
commit325838f35ce90080aa6c892a998d960c06c1c144 (patch)
tree60e87ee9a622a0261b93ef1e47dbca632c588276 /encode.go
parentd25456172946e5921747cd57fb04eb5b6da72fb6 (diff)
Add tests for the actual usability of the Decodable and Encodable interfaces
Diffstat (limited to 'encode.go')
-rw-r--r--encode.go11
1 files changed, 8 insertions, 3 deletions
diff --git a/encode.go b/encode.go
index 44fd985..f93cba4 100644
--- a/encode.go
+++ b/encode.go
@@ -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.