summaryrefslogtreecommitdiff
path: root/encode.go
diff options
context:
space:
mode:
Diffstat (limited to 'encode.go')
-rw-r--r--encode.go34
1 files changed, 17 insertions, 17 deletions
diff --git a/encode.go b/encode.go
index 00d3dad..684cc75 100644
--- a/encode.go
+++ b/encode.go
@@ -87,7 +87,7 @@ func (enc *Encoder) Encode(obj any) (err error) {
if escaper == nil {
escaper = EscapeDefault
}
- if err := encode(enc.w, reflect.ValueOf(obj), escaper, false, 0, map[any]struct{}{}); err != nil {
+ if err := encode(enc.w, reflect.ValueOf(obj), escaper, enc.w.utf, false, 0, map[any]struct{}{}); err != nil {
if rwe, ok := err.(*ReEncodeWriteError); ok {
err = &EncodeWriteError{
Err: rwe.Err,
@@ -108,7 +108,7 @@ func discardInt(_ int, err error) error {
const startDetectingCyclesAfter = 1000
-func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, quote bool, cycleDepth uint, cycleSeen map[any]struct{}) error {
+func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, utf InvalidUTF8Mode, quote bool, cycleDepth uint, cycleSeen map[any]struct{}) error {
if !val.IsValid() {
return discardInt(w.WriteString("null"))
}
@@ -197,7 +197,7 @@ func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, quote boo
Err: err,
}
}
- if err := jsonstring.EncodeStringFromBytes(w, escaper, text); err != nil {
+ if err := jsonstring.EncodeStringFromBytes(w, escaper, utf, val, text); err != nil {
return err
}
default:
@@ -309,14 +309,14 @@ func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, quote boo
} else {
if quote {
var buf bytes.Buffer
- if err := jsonstring.EncodeStringFromString(&buf, escaper, val.String()); err != nil {
+ if err := jsonstring.EncodeStringFromString(&buf, escaper, utf, val, val.String()); err != nil {
return err
}
- if err := jsonstring.EncodeStringFromBytes(w, escaper, buf.Bytes()); err != nil {
+ if err := jsonstring.EncodeStringFromBytes(w, escaper, utf, val, buf.Bytes()); err != nil {
return err
}
} else {
- if err := jsonstring.EncodeStringFromString(w, escaper, val.String()); err != nil {
+ if err := jsonstring.EncodeStringFromString(w, escaper, utf, val, val.String()); err != nil {
return err
}
}
@@ -327,7 +327,7 @@ func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, quote boo
return err
}
} else {
- if err := encode(w, val.Elem(), escaper, quote, cycleDepth, cycleSeen); err != nil {
+ if err := encode(w, val.Elem(), escaper, utf, quote, cycleDepth, cycleSeen); err != nil {
return err
}
}
@@ -350,13 +350,13 @@ func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, quote boo
}
}
empty = false
- if err := jsonstring.EncodeStringFromString(w, escaper, field.Name); err != nil {
+ if err := jsonstring.EncodeStringFromString(w, escaper, utf, val, field.Name); err != nil {
return err
}
if err := w.WriteByte(':'); err != nil {
return err
}
- if err := encode(w, fVal, escaper, field.Quote, cycleDepth, cycleSeen); err != nil {
+ if err := encode(w, fVal, escaper, utf, field.Quote, cycleDepth, cycleSeen); err != nil {
return err
}
}
@@ -394,7 +394,7 @@ func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, quote boo
for i := 0; iter.Next(); i++ {
// TODO: Avoid buffering the map key
var k strings.Builder
- if err := encode(NewReEncoder(&k, ReEncoderConfig{BackslashEscape: escaper}), iter.Key(), escaper, false, cycleDepth, cycleSeen); err != nil {
+ if err := encode(NewReEncoder(&k, ReEncoderConfig{BackslashEscape: escaper, InvalidUTF8: utf}), iter.Key(), escaper, utf, false, cycleDepth, cycleSeen); err != nil {
return err
}
kStr := k.String()
@@ -403,7 +403,7 @@ func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, quote boo
}
if !strings.HasPrefix(kStr, `"`) {
k.Reset()
- if err := jsonstring.EncodeStringFromString(&k, escaper, kStr); err != nil {
+ if err := jsonstring.EncodeStringFromString(&k, escaper, utf, iter.Key(), kStr); err != nil {
return err
}
kStr = k.String()
@@ -427,7 +427,7 @@ func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, quote boo
if err := w.WriteByte(':'); err != nil {
return err
}
- if err := encode(w, kv.V, escaper, false, cycleDepth, cycleSeen); err != nil {
+ if err := encode(w, kv.V, escaper, utf, false, cycleDepth, cycleSeen); err != nil {
return err
}
}
@@ -491,12 +491,12 @@ func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, quote boo
cycleSeen[ptr] = struct{}{}
defer delete(cycleSeen, ptr)
}
- if err := encodeArray(w, val, escaper, cycleDepth, cycleSeen); err != nil {
+ if err := encodeArray(w, val, escaper, utf, cycleDepth, cycleSeen); err != nil {
return err
}
}
case reflect.Array:
- if err := encodeArray(w, val, escaper, cycleDepth, cycleSeen); err != nil {
+ if err := encodeArray(w, val, escaper, utf, cycleDepth, cycleSeen); err != nil {
return err
}
case reflect.Pointer:
@@ -516,7 +516,7 @@ func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, quote boo
cycleSeen[ptr] = struct{}{}
defer delete(cycleSeen, ptr)
}
- if err := encode(w, val.Elem(), escaper, quote, cycleDepth, cycleSeen); err != nil {
+ if err := encode(w, val.Elem(), escaper, utf, quote, cycleDepth, cycleSeen); err != nil {
return err
}
}
@@ -529,7 +529,7 @@ func encode(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, quote boo
return nil
}
-func encodeArray(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, cycleDepth uint, cycleSeen map[any]struct{}) error {
+func encodeArray(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, utf InvalidUTF8Mode, cycleDepth uint, cycleSeen map[any]struct{}) error {
if err := w.WriteByte('['); err != nil {
return err
}
@@ -540,7 +540,7 @@ func encodeArray(w *ReEncoder, val reflect.Value, escaper BackslashEscaper, cycl
return err
}
}
- if err := encode(w, val.Index(i), escaper, false, cycleDepth, cycleSeen); err != nil {
+ if err := encode(w, val.Index(i), escaper, utf, false, cycleDepth, cycleSeen); err != nil {
return err
}
}