-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: sync/atomic: add JSON and Text Marshal and Unmarshal functions to scalar types #54582
Comments
We don't need the |
The problem with using type N int
func (n N) MarshalText() ([]byte, error) {
return []byte("1234"), nil
}
type S struct {
A N
}
func main() {
b, _ := json.Marshal(S{})
fmt.Println(string(b))
}
|
This proposal has been added to the active column of the proposals project |
There is some movement on taking a holistic look at all encoding/json issues. I'm a bit concerned about adding |
I proposed #56235, which would affect what happens here. If that proposal is accepted, we would add something like: func (*Bool) MarshalScalar() (bool, error)
func (*Bool) UnmarshalScalar(bool) error
func (*Int32) MarshalScalar() (int64, error)
func (*Int32) UnmarshalScalar(int64) error
func (*Int64) MarshalScalar() (int64, error)
func (*Int64) UnmarshalScalar(int64) error
func (*Uint32) MarshalScalar() (uint64, error)
func (*Uint32) UnmarshalScalar(uint64) error
func (*Uint32) MarshalScalar() (uint64, error)
func (*Uint32) UnmarshalScalar(uint64) error |
Let's hold this proposal for #56235. |
Placed on hold. |
https://go.uber.org/atomic support this |
Simplified use case:
Adding these methods would allow changing
Metrics.Requests
to anatomic.Int64
to make it simpler and safer.The previous time this was suggested it was reject because
sync/atomic
can't importencoding/json
. But this is not required to implement the interfaces. Onlystrconv
would be required.Sample implementation for
atomic.Int64
:atomic.Bool
shouldn't usestrconv.ParseBool
as it isn't JSON compatible. Instead if can just compare against"true"
and"false"
.atomic.Pointer
,atomic.Uintptr
andatomic.Value
are omitted from this proposal as they would require importing ofencoding/json
which is not allowed.The text was updated successfully, but these errors were encountered: