generic: provide configured encoder

This encoder emits data identical to upstream API response body.

Signed-off-by: Yonah <contrib@gensokyo.uk>
This commit is contained in:
Yonah 2025-09-18 04:53:05 +09:00
parent bb1164d081
commit 343a8c5f45
Signed by: yonah
SSH Key Fingerprint: SHA256:vnQvK8+XXH9Tbni2AV1a/8qdVK/zPcXw52GM0ruQvwA
2 changed files with 8 additions and 4 deletions

View File

@ -75,3 +75,10 @@ func (i *StringInt) UnmarshalJSON(data []byte) (err error) {
} }
return return
} }
// NewEncoder returns a new encoder that writes to w, configured to match upstream API behaviour.
func NewEncoder(w io.Writer) *json.Encoder {
e := json.NewEncoder(w)
e.SetEscapeHTML(false)
return e
}

View File

@ -84,10 +84,7 @@ func checkJSONRoundTrip[T any](t *testing.T, v T, data []byte) {
t.Run("marshal", func(t *testing.T) { t.Run("marshal", func(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
buf.Grow(len(data)) buf.Grow(len(data))
e := json.NewEncoder(buf) err := NewEncoder(buf).Encode(&v)
// to match upstream behaviour
e.SetEscapeHTML(false)
err := e.Encode(&v)
if err != nil { if err != nil {
t.Fatalf("Marshal: error = %v", err) t.Fatalf("Marshal: error = %v", err)
} }