Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop Options argument from user-declared methods and functions #163

Merged
merged 1 commit into from
Feb 13, 2025

Conversation

dsnet
Copy link
Collaborator

@dsnet dsnet commented Feb 13, 2025

WARNING: This commit contains breaking changes.

This drops Options as an argument from
the MarshalerTo and UnmarshalerFrom interfaces and the MarshalToFunc and UnmarshalFromFunc functions.

Instead, the options is stored within the
jsontext.Encoder or jsontext.Decoder and
can be retrieved through the Options method.

This simplifies the API for custom marshalers and unmarshalers and makes it impossible to accidentally drop the options when recursively calling json.MarshalEncode or json.UnmarshalDecode from within a custom marshaler or unmarshaler implementation.

Fixes golang/go#71611

WARNING: This commit contains breaking changes.

This drops Options as an argument from
the MarshalerTo and UnmarshalerFrom interfaces and
the MarshalToFunc and UnmarshalFromFunc functions.

Instead, the options is stored within the
jsontext.Encoder or jsontext.Decoder and
can be retrieved through the Options method.

This simplifies the API for custom marshalers and unmarshalers and
makes it impossible to accidentally drop the options
when recursively calling json.MarshalEncode or json.UnmarshalDecode
from within a custom marshaler or unmarshaler implementation.

Fixes golang/go#71611
return nil
}

var structOptionsPool = &sync.Pool{New: func() any { return new(jsonopts.Struct) }}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay! We get to delete one of the pools.

@@ -544,6 +511,9 @@ func newAddressableValue(t reflect.Type) addressableValue {
return addressableValue{reflect.New(t).Elem(), true}
}

// TODO: Remove *jsonopts.Struct argument from [marshaler] and [unmarshaler].
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this will speed up the performance of marshaling and unmarshaling since each recursive call doesn't need to keep passing the same *Struct pointer on the stack.

Copy link
Collaborator

@johanbrandhorst johanbrandhorst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excited about this!

@dsnet dsnet merged commit 925ba3f into master Feb 13, 2025
8 checks passed
@dsnet dsnet deleted the drop-options branch February 13, 2025 06:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

proposal: encoding/json/v2: drop Options argument from user-declared methods and functions
2 participants