Skip to content

Commit b371b61

Browse files
feat(client): support optional json html escaping
1 parent 1e22194 commit b371b61

File tree

5 files changed

+16
-8
lines changed

5 files changed

+16
-8
lines changed

internal/encoding/json/encode.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,11 @@ func Marshal(v any) ([]byte, error) {
177177
e := newEncodeState()
178178
defer encodeStatePool.Put(e)
179179

180-
err := e.marshal(v, encOpts{escapeHTML: true})
180+
// SHIM(begin): don't escape HTML by default
181+
err := e.marshal(v, encOpts{escapeHTML: shims.EscapeHTMLByDefault})
182+
// ORIGINAL:
183+
// err := e.marshal(v, encOpts{escapeHTML: true})
184+
// SHIM(end)
181185
if err != nil {
182186
return nil, err
183187
}

internal/encoding/json/shims/shims.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"slices"
1212
)
1313

14+
const EscapeHTMLByDefault = true
15+
1416
type OverflowableType struct{ reflect.Type }
1517

1618
func (t OverflowableType) OverflowInt(x int64) bool {

internal/requestconfig/requestconfig.go

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

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

packages/param/option.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (o Opt[T]) MarshalJSON() ([]byte, error) {
6262
if !o.Valid() {
6363
return []byte("null"), nil
6464
}
65-
return json.Marshal(o.Value)
65+
return shimjson.Marshal(o.Value)
6666
}
6767

6868
func (o *Opt[T]) UnmarshalJSON(data []byte) error {
@@ -96,7 +96,7 @@ func (o Opt[T]) MarshalJSONWithTimeLayout(format string) []byte {
9696
return nil
9797
}
9898

99-
b, err := json.Marshal(t.Format(shimjson.TimeLayout(format)))
99+
b, err := shimjson.Marshal(t.Format(shimjson.TimeLayout(format)))
100100
if err != nil {
101101
return nil
102102
}

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/ScrapeGraphAI/scrapegraph-sdk/internal/encoding/json"
77
)
88

99
type Constant[T any] interface {
@@ -28,5 +28,5 @@ func marshalString[T ~string, PT constant[T]](v T) ([]byte, error) {
2828
if v == zero {
2929
v = PT(&v).Default()
3030
}
31-
return json.Marshal(string(v))
31+
return shimjson.Marshal(string(v))
3232
}

0 commit comments

Comments
 (0)