@@ -266,8 +266,8 @@ func JSONToYAML(bytes []byte) ([]byte, error) {
266266var (
267267 globalCustomMarshalerMu sync.Mutex
268268 globalCustomUnmarshalerMu sync.Mutex
269- globalCustomMarshalerMap = map [reflect.Type ]func (interface {}) ([]byte , error ){}
270- globalCustomUnmarshalerMap = map [reflect.Type ]func (interface {}, []byte ) error {}
269+ globalCustomMarshalerMap = map [reflect.Type ]func (context. Context , interface {}) ([]byte , error ){}
270+ globalCustomUnmarshalerMap = map [reflect.Type ]func (context. Context , interface {}, []byte ) error {}
271271)
272272
273273// RegisterCustomMarshaler overrides any encoding process for the type specified in generics.
@@ -281,11 +281,23 @@ func RegisterCustomMarshaler[T any](marshaler func(T) ([]byte, error)) {
281281 defer globalCustomMarshalerMu .Unlock ()
282282
283283 var typ T
284- globalCustomMarshalerMap [reflect .TypeOf (typ )] = func (v interface {}) ([]byte , error ) {
284+ globalCustomMarshalerMap [reflect .TypeOf (typ )] = func (ctx context. Context , v interface {}) ([]byte , error ) {
285285 return marshaler (v .(T ))
286286 }
287287}
288288
289+ // RegisterCustomMarshalerContext overrides any encoding process for the type specified in generics.
290+ // Similar to RegisterCustomMarshalerContext, but allows passing a context to the unmarshaler function.
291+ func RegisterCustomMarshalerContext [T any ](marshaler func (context.Context , T ) ([]byte , error )) {
292+ globalCustomMarshalerMu .Lock ()
293+ defer globalCustomMarshalerMu .Unlock ()
294+
295+ var typ T
296+ globalCustomMarshalerMap [reflect .TypeOf (typ )] = func (ctx context.Context , v interface {}) ([]byte , error ) {
297+ return marshaler (ctx , v .(T ))
298+ }
299+ }
300+
289301// RegisterCustomUnmarshaler overrides any decoding process for the type specified in generics.
290302// If you want to switch the behavior for each decoder, use `CustomUnmarshaler` defined as DecodeOption.
291303//
@@ -296,7 +308,19 @@ func RegisterCustomUnmarshaler[T any](unmarshaler func(*T, []byte) error) {
296308 defer globalCustomUnmarshalerMu .Unlock ()
297309
298310 var typ * T
299- globalCustomUnmarshalerMap [reflect .TypeOf (typ )] = func (v interface {}, b []byte ) error {
311+ globalCustomUnmarshalerMap [reflect .TypeOf (typ )] = func (ctx context. Context , v interface {}, b []byte ) error {
300312 return unmarshaler (v .(* T ), b )
301313 }
302314}
315+
316+ // RegisterCustomUnmarshalerContext overrides any decoding process for the type specified in generics.
317+ // Similar to RegisterCustomUnmarshalerContext, but allows passing a context to the unmarshaler function.
318+ func RegisterCustomUnmarshalerContext [T any ](unmarshaler func (context.Context , * T , []byte ) error ) {
319+ globalCustomUnmarshalerMu .Lock ()
320+ defer globalCustomUnmarshalerMu .Unlock ()
321+
322+ var typ * T
323+ globalCustomUnmarshalerMap [reflect .TypeOf (typ )] = func (ctx context.Context , v interface {}, b []byte ) error {
324+ return unmarshaler (ctx , v .(* T ), b )
325+ }
326+ }
0 commit comments