Skip to content

Commit f5636ed

Browse files
Merge pull request #468 from openai/dev/no-escape-html
feat(client): remove HTML escaping in JSON
2 parents 3fb5d8d + aea5ebc commit f5636ed

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

internal/encoding/json/encode.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import (
1919
"encoding"
2020
"encoding/base64"
2121
"fmt"
22-
"github.com/openai/openai-go/internal/encoding/json/sentinel"
23-
"github.com/openai/openai-go/internal/encoding/json/shims"
2422
"math"
2523
"reflect"
2624
"slices"
@@ -30,6 +28,9 @@ import (
3028
"unicode"
3129
"unicode/utf8"
3230
_ "unsafe" // for linkname
31+
32+
"github.com/openai/openai-go/internal/encoding/json/sentinel"
33+
"github.com/openai/openai-go/internal/encoding/json/shims"
3334
)
3435

3536
// Marshal returns the JSON encoding of v.
@@ -177,7 +178,7 @@ func Marshal(v any) ([]byte, error) {
177178
e := newEncodeState()
178179
defer encodeStatePool.Put(e)
179180

180-
err := e.marshal(v, encOpts{escapeHTML: true})
181+
err := e.marshal(v, encOpts{escapeHTML: false})
181182
if err != nil {
182183
return nil, err
183184
}

internal/requestconfig/requestconfig.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,13 @@ func NewRequestConfig(ctx context.Context, method string, u string, body any, ds
137137
// Fallback to json serialization if none of the serialization functions that we expect
138138
// to see is present.
139139
if body != nil && !hasSerializationFunc {
140-
content, err := json.Marshal(body)
141-
if err != nil {
140+
buf := new(bytes.Buffer)
141+
enc := json.NewEncoder(buf)
142+
enc.SetEscapeHTML(true)
143+
if err := enc.Encode(body); err != nil {
142144
return nil, err
143145
}
144-
reader = bytes.NewBuffer(content)
146+
reader = buf
145147
}
146148

147149
req, err := http.NewRequestWithContext(ctx, method, u, nil)

packages/param/option.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package param
33
import (
44
"encoding/json"
55
"fmt"
6-
shimjson "github.com/openai/openai-go/internal/encoding/json"
76
"time"
7+
8+
shimjson "github.com/openai/openai-go/internal/encoding/json"
89
)
910

1011
func NewOpt[T comparable](v T) Opt[T] {
@@ -62,7 +63,7 @@ func (o Opt[T]) MarshalJSON() ([]byte, error) {
6263
if !o.Valid() {
6364
return []byte("null"), nil
6465
}
65-
return json.Marshal(o.Value)
66+
return shimjson.Marshal(o.Value)
6667
}
6768

6869
func (o *Opt[T]) UnmarshalJSON(data []byte) error {
@@ -96,7 +97,7 @@ func (o Opt[T]) MarshalJSONWithTimeLayout(format string) []byte {
9697
return nil
9798
}
9899

99-
b, err := json.Marshal(t.Format(shimjson.TimeLayout(format)))
100+
b, err := shimjson.Marshal(t.Format(shimjson.TimeLayout(format)))
100101
if err != nil {
101102
return nil
102103
}

shared/constant/constants.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
package constant
44

55
import (
6-
"encoding/json"
6+
shimjson "github.com/openai/openai-go/internal/encoding/json"
77
)
88

99
type Constant[T any] interface {
@@ -727,5 +727,5 @@ func marshalString[T ~string, PT constant[T]](v T) ([]byte, error) {
727727
if v == zero {
728728
v = PT(&v).Default()
729729
}
730-
return json.Marshal(string(v))
730+
return shimjson.Marshal(string(v))
731731
}

0 commit comments

Comments
 (0)