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

proposal: sync: define marshal/unmarshal methods for Map #31136

Closed
PumpkinSeed opened this issue Mar 29, 2019 · 5 comments
Closed

proposal: sync: define marshal/unmarshal methods for Map #31136

PumpkinSeed opened this issue Mar 29, 2019 · 5 comments

Comments

@PumpkinSeed
Copy link

What version of Go are you using (go version)?

$ go version
1.12.1

How about to having sync.Map JSON Marshal methods and it doesn't have to prepare manually like the following conversation.

@gopherbot gopherbot added this to the Proposal milestone Mar 29, 2019
@ianlancetaylor ianlancetaylor changed the title proposal: sync.Map json marshal proposal: sync: define marshal/unmarshal methods for Map Mar 29, 2019
@ianlancetaylor
Copy link
Contributor

CC @bcmills

@bcmills
Copy link
Contributor

bcmills commented Mar 29, 2019

I don't see how we would implement UnmarshalJSON: sync.Map stores its values as type interface{}, so in general the Unmarshal method has no way to know what type of values to put into the map.

That would mean that UnmarshalJSON and MarshalJSON are not inverses: marshaling and then unmarshaling a map would potentially result in different key and value types from the original, even though the underlying map has the same concrete type.

I guess that MarshalJSON on its own would be ok, but it seems awkward to have one without the other.

@zigazeljko
Copy link

I don't see how we would implement UnmarshalJSON: sync.Map stores its values as type interface{}, so in general the Unmarshal method has no way to know what type of values to put into the map.

The same way json.Unmarshal handles interface values. Unmarshaling into sync.Map would in that case be identical to unmarshaling into map[interface{}]interface{}.

That would mean that UnmarshalJSON and MarshalJSON are not inverses: marshaling and then unmarshaling a map would potentially result in different key and value types from the original, even though the underlying map has the same concrete type.

That is already the case for map[interface{}]interface{}.

@bcmills
Copy link
Contributor

bcmills commented Mar 29, 2019

The key difference vs. map[interface{}]interface{} is that it is very easy to choose some other (more-specific) built-in map type instead. In contrast, there is no typed alternative to sync.Map.

@rsc
Copy link
Contributor

rsc commented Apr 10, 2019

Note that if you want a marshal'able sync.Map you can always write your own type that embeds a sync.Map and adds whatever specific serialized format you need. Adding that specifically to package sync is a bit of a layer inversion. Sync is about synchronizing the map, not serialization formats.

@rsc rsc closed this as completed Apr 10, 2019
@golang golang locked and limited conversation to collaborators Apr 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants