summaryrefslogtreecommitdiff
path: root/compat
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-02-14 11:44:36 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-02-16 21:19:46 -0700
commit851452243879498117cd7e71ac12856af44657cb (patch)
tree8542d06063bd773031c8550271f945cab624b68d /compat
parenta6cd78ec94f76feba180fa75e942bb5cdeae115f (diff)
reencode: Don't have the ReEncoderConfig member be public
Diffstat (limited to 'compat')
-rw-r--r--compat/json/compat.go43
1 files changed, 27 insertions, 16 deletions
diff --git a/compat/json/compat.go b/compat/json/compat.go
index 3678135..c96470d 100644
--- a/compat/json/compat.go
+++ b/compat/json/compat.go
@@ -72,8 +72,11 @@ func Marshal(v any) ([]byte, error) {
}
type Encoder struct {
- out io.Writer
- buf bytes.Buffer
+ out io.Writer
+ buf bytes.Buffer
+
+ cfg lowmemjson.ReEncoderConfig
+
encoder *lowmemjson.Encoder
formatter *lowmemjson.ReEncoder
}
@@ -81,17 +84,23 @@ type Encoder struct {
func NewEncoder(w io.Writer) *Encoder {
ret := &Encoder{
out: w,
- }
- ret.formatter = lowmemjson.NewReEncoder(&ret.buf, lowmemjson.ReEncoderConfig{
- AllowMultipleValues: true,
- Compact: true,
- ForceTrailingNewlines: true,
- })
- ret.encoder = lowmemjson.NewEncoder(ret.formatter)
+ cfg: lowmemjson.ReEncoderConfig{
+ AllowMultipleValues: true,
+
+ Compact: true,
+ ForceTrailingNewlines: true,
+ },
+ }
+ ret.refreshConfig()
return ret
}
+func (enc *Encoder) refreshConfig() {
+ enc.formatter = lowmemjson.NewReEncoder(&enc.buf, enc.cfg)
+ enc.encoder = lowmemjson.NewEncoder(enc.formatter)
+}
+
func (enc *Encoder) Encode(v any) error {
if err := convertEncodeError(enc.encoder.Encode(v)); err != nil {
enc.buf.Reset()
@@ -104,17 +113,19 @@ func (enc *Encoder) Encode(v any) error {
}
func (enc *Encoder) SetEscapeHTML(on bool) {
- var escaper lowmemjson.BackslashEscaper
- if !on {
- escaper = lowmemjson.EscapeDefaultNonHTMLSafe
+ if on {
+ enc.cfg.BackslashEscape = lowmemjson.EscapeDefault
+ } else {
+ enc.cfg.BackslashEscape = lowmemjson.EscapeDefaultNonHTMLSafe
}
- enc.formatter.BackslashEscape = escaper
+ enc.refreshConfig()
}
func (enc *Encoder) SetIndent(prefix, indent string) {
- enc.formatter.Compact = prefix == "" && indent == ""
- enc.formatter.Prefix = prefix
- enc.formatter.Indent = indent
+ enc.cfg.Compact = prefix == "" && indent == ""
+ enc.cfg.Prefix = prefix
+ enc.cfg.Indent = indent
+ enc.refreshConfig()
}
// ReEncode wrappers /////////////////////////////////////////////////