You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you call json.Marshal with a map, it sorts the keys. If you do a search, you will find a Stack Overflow page explaining this[1]. However, this behavior isn't mentioned in the godoc.
It's useful to assume that the emitted JSON will be deterministic, since then you can use string equality or compare hashes to look for duplicates and compare the emitted JSON to a golden copy in tests. But since this property isn't mentioned, it's unclear whether users can rely on the emitted JSON remaining the same in new versions of Go.
Many people will do it anyway, so perhaps we should make it official?
The text was updated successfully, but these errors were encountered:
bradfitz
changed the title
document order of keys when encoding a map with json.Marshal
encoding/json: document that object keys are sorted
Apr 23, 2016
If you call json.Marshal with a map, it sorts the keys. If you do a search, you will find a Stack Overflow page explaining this[1]. However, this behavior isn't mentioned in the godoc.
It's useful to assume that the emitted JSON will be deterministic, since then you can use string equality or compare hashes to look for duplicates and compare the emitted JSON to a golden copy in tests. But since this property isn't mentioned, it's unclear whether users can rely on the emitted JSON remaining the same in new versions of Go.
Many people will do it anyway, so perhaps we should make it official?
[1] http://stackoverflow.com/questions/18668652/how-to-produce-json-with-sorted-keys-in-go
The text was updated successfully, but these errors were encountered: