1. Map with string key. Everything works as expected.
http://play.golang.org/p/EuREjUUZfz
STAGE 1: Create Example 1
Example 1 is map[1:foo 2:goo 3:hoo]
Stage 2: Marshal Example 1 to Buffer
Buffer is {"1":"foo","2":"goo","3":"hoo"}
Stage 3: Unmarshal Buffer to Example 2
Example 1 is map[1:foo 2:goo 3:hoo]
Example 2 is map[1:foo 2:goo 3:hoo]
2. Map with int key. Doesn't work (as expected) because JSON doesn't like maps that aren't keyed on string.
http://play.golang.org/p/AdvjN8Yr6r
STAGE 1: Create Example 1
Example 1 is map[1:foo 2:goo 3:hoo]
Stage 2: Marshal Example 1 to Buffer
panic: json: unsupported type: main.IntMap
goroutine 1 [running]:
main.main()
/tmpfs/gosandbox-dabe65f3_542e93fe_8c04806b_21612e20_faec9118/prog.go:33 +0x3ee
[process exited with non-zero status]
3. Map with int-key and implementing json.Marshaler and json.Unmarshaler. Should work, right?
http://play.golang.org/p/Y3EKs7OPsG
STAGE 1: Create Example 1
Example 1 is map[1:foo 2:goo 3:hoo]
Stage 2: Marshal Example 1 to Buffer
Buffer is {"1":"foo","2":"goo","3":"hoo"}
Stage 3: Unmarshal Buffer to Example 2
Example 1 is map[1:foo 2:goo 3:hoo]
Example 2 is map[]
The problem, I think, is that receiver "im" comes in as nil and any attempt to assign it a map doesn't still doesn't return anything. I have tried to make the receiver a pointer to an IntMap, etc. But only get other errors. How can I correct my UnmarshalJSON method to correctly fill example 2 (ex2)? A solution would be to initialize im before UnmarshalJSON is called but this is really just an example. My real-life scenario has IntMap as a property of a more complex object. I'd prefer not to have to implement a custom (un)marshal for that struct just to initialize the IntMap property.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.