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

encoding/json: document that object keys are sorted #15424

Closed
skybrian opened this issue Apr 23, 2016 · 0 comments
Closed

encoding/json: document that object keys are sorted #15424

skybrian opened this issue Apr 23, 2016 · 0 comments

Comments

@skybrian
Copy link
Contributor

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

@bradfitz 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
@bradfitz bradfitz added this to the Go1.7 milestone Apr 23, 2016
@golang golang locked and limited conversation to collaborators May 16, 2017
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

3 participants