diff --git a/internal/encoding/json/encode.go b/internal/encoding/json/encode.go index d5471325..63b76786 100644 --- a/internal/encoding/json/encode.go +++ b/internal/encoding/json/encode.go @@ -19,8 +19,6 @@ import ( "encoding" "encoding/base64" "fmt" - "github.com/openai/openai-go/internal/encoding/json/sentinel" - "github.com/openai/openai-go/internal/encoding/json/shims" "math" "reflect" "slices" @@ -30,6 +28,9 @@ import ( "unicode" "unicode/utf8" _ "unsafe" // for linkname + + "github.com/openai/openai-go/internal/encoding/json/sentinel" + "github.com/openai/openai-go/internal/encoding/json/shims" ) // Marshal returns the JSON encoding of v. @@ -177,7 +178,7 @@ func Marshal(v any) ([]byte, error) { e := newEncodeState() defer encodeStatePool.Put(e) - err := e.marshal(v, encOpts{escapeHTML: true}) + err := e.marshal(v, encOpts{escapeHTML: false}) if err != nil { return nil, err } diff --git a/internal/requestconfig/requestconfig.go b/internal/requestconfig/requestconfig.go index def7c58e..ac1c85ea 100644 --- a/internal/requestconfig/requestconfig.go +++ b/internal/requestconfig/requestconfig.go @@ -137,11 +137,13 @@ func NewRequestConfig(ctx context.Context, method string, u string, body any, ds // Fallback to json serialization if none of the serialization functions that we expect // to see is present. if body != nil && !hasSerializationFunc { - content, err := json.Marshal(body) - if err != nil { + buf := new(bytes.Buffer) + enc := json.NewEncoder(buf) + enc.SetEscapeHTML(true) + if err := enc.Encode(body); err != nil { return nil, err } - reader = bytes.NewBuffer(content) + reader = buf } req, err := http.NewRequestWithContext(ctx, method, u, nil) diff --git a/packages/param/option.go b/packages/param/option.go index 107b58a5..d722c63e 100644 --- a/packages/param/option.go +++ b/packages/param/option.go @@ -3,8 +3,9 @@ package param import ( "encoding/json" "fmt" - shimjson "github.com/openai/openai-go/internal/encoding/json" "time" + + shimjson "github.com/openai/openai-go/internal/encoding/json" ) func NewOpt[T comparable](v T) Opt[T] { @@ -62,7 +63,7 @@ func (o Opt[T]) MarshalJSON() ([]byte, error) { if !o.Valid() { return []byte("null"), nil } - return json.Marshal(o.Value) + return shimjson.Marshal(o.Value) } func (o *Opt[T]) UnmarshalJSON(data []byte) error { @@ -96,7 +97,7 @@ func (o Opt[T]) MarshalJSONWithTimeLayout(format string) []byte { return nil } - b, err := json.Marshal(t.Format(shimjson.TimeLayout(format))) + b, err := shimjson.Marshal(t.Format(shimjson.TimeLayout(format))) if err != nil { return nil } diff --git a/shared/constant/constants.go b/shared/constant/constants.go index 3847a3b0..9df79f42 100644 --- a/shared/constant/constants.go +++ b/shared/constant/constants.go @@ -3,7 +3,7 @@ package constant import ( - "encoding/json" + shimjson "github.com/openai/openai-go/internal/encoding/json" ) type Constant[T any] interface { @@ -727,5 +727,5 @@ func marshalString[T ~string, PT constant[T]](v T) ([]byte, error) { if v == zero { v = PT(&v).Default() } - return json.Marshal(string(v)) + return shimjson.Marshal(string(v)) }