I am writing a RESTful JSON web service. When encoding maps I ran into a
behaviour, which I thought to be a bug and I submitted an
issue https://code.google.com/p/go/issues/detail?id=6244 .
Let me illustrate:
json.Marshal() sorts maps when it is encoding them, which does not seem
right
package main
import (
"fmt"
"encoding/json"
)
func main() {
fmt.Println("Go just broke my web form ...")
countries := map[string]string {
"USA" : "United States of America",
"Afg" : "Afghanistan",
}
jsonBytes, _ := json.MarshalIndent(countries, "", " ")
fmt.Println(countries)
fmt.Println(string(jsonBytes))
}
*Outputs*
Go just broke my web form ...
map[USA:United States of America Afg:Afghanistan]
{
"Afg": "Afghanistan",
"USA": "United States of America"
}
*Instead of*
...
{
"USA": "United States of America",
"Afg": "Afghanistan"
}
Which basically would mean that I would have change my data into
{
"countries" : {
"Afg": "Afghanistan",
"USA": "United States of America"
}
"order" : [
"USA",
"Afg"
]
}
http://play.golang.org/p/LL4euXjM7a
It got looked at right away by one of the the encoding/json authors and he
closed it as "WorkingAsIntended".
I am kind of lost here with my team mates, because we can not make any
sense of it and I wonder, if any of you guys could help us out / give us a
hint.
--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.