github.com/anthropics/anthropic-sdk-go@v1.70.1/internal/encoding/json/opt.go (about) 1 // EDIT(begin): add custom options for JSON encoding 2 package json 3 4 import ( 5 stdjson "encoding/json" 6 "reflect" 7 8 "github.com/anthropics/anthropic-sdk-go/internal/encoding/json/shims" 9 ) 10 11 type Option func(*encOpts) 12 13 // Every time a sub-type of [json.Marshaler] is encountered, 14 // skip a redundant and costly compaction step, trust it to self-compact. 15 // 16 // This is a divergence from the standard library behavior, and is only guaranteed 17 // safe with SDK types. 18 func WithSkipCompaction(b bool) Option { 19 return func(eos *encOpts) { 20 eos.skipCompaction = true 21 } 22 } 23 24 func (eos encOpts) apply(opts ...Option) encOpts { 25 for _, opt := range opts { 26 opt(&eos) 27 } 28 return eos 29 } 30 31 var rawMessageType = shims.TypeFor[stdjson.RawMessage]() 32 33 // rawMessageEncoder is marshalerEncoder minus the [WithSkipCompaction] trust: 34 // a json.RawMessage returns caller-supplied bytes verbatim, so they always get 35 // the scanning pass, which compacts, HTML-escapes, and rejects invalid JSON. 36 func rawMessageEncoder(e *encodeState, v reflect.Value, opts encOpts) { 37 opts.skipCompaction = false 38 marshalerEncoder(e, v, opts) 39 } 40 41 // EDIT(end)