a***@gmail.com
2015-05-05 16:15:45 UTC
Currently, json.Marshal will fail to marshal maps with non-string keys,
e.g.:
// http://play.golang.org/p/2m9wLZATqw
type Coord struct { X,Y int }
occupied := map[Coord]bool{}
occupied[Coord{1,2}] = true
data, err := json.Marshal(occupied)
fmt.Printf("Data: %s\nErr: %v", data, err)
I propose to enhance the encoding/json package such that:
(1) for json.Marshal: If the map key is a string kind it is used
directly. Otherwise if the map key satisfies the encoding.TextMarshaler
interface then that is used to generate the map key. Otherwise it fails as
it does today.
(2) for json.Unmarshal: If the map key is a string kind it is written
directly. Otherwise if the map key satisfies the encoding.TextUnmarshaler
interface then that is used to decode the map key. Otherwise it fails as
it does today.
This would, for example, allow http://play.golang.org/p/350eQmK6KL to work.
This would be for the 1.6 release, this discussion is for planning purposes
only.
Thoughts?
- Augusto
e.g.:
// http://play.golang.org/p/2m9wLZATqw
type Coord struct { X,Y int }
occupied := map[Coord]bool{}
occupied[Coord{1,2}] = true
data, err := json.Marshal(occupied)
fmt.Printf("Data: %s\nErr: %v", data, err)
I propose to enhance the encoding/json package such that:
(1) for json.Marshal: If the map key is a string kind it is used
directly. Otherwise if the map key satisfies the encoding.TextMarshaler
interface then that is used to generate the map key. Otherwise it fails as
it does today.
(2) for json.Unmarshal: If the map key is a string kind it is written
directly. Otherwise if the map key satisfies the encoding.TextUnmarshaler
interface then that is used to decode the map key. Otherwise it fails as
it does today.
This would, for example, allow http://play.golang.org/p/350eQmK6KL to work.
This would be for the 1.6 release, this discussion is for planning purposes
only.
Thoughts?
- Augusto
--
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.