Skip to content

json.Marshal will sort the keys of map. #27050

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

Closed
ghost opened this issue Aug 17, 2018 · 5 comments
Closed

json.Marshal will sort the keys of map. #27050

ghost opened this issue Aug 17, 2018 · 5 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@ghost
Copy link

ghost commented Aug 17, 2018

json.Marshal will sort the keys of map.
Although I can change this behavior by rewriting the interface of MarshalJSON, it would be better if there was another function that does not perform the operation of sort.

@mvdan
Copy link
Member

mvdan commented Aug 17, 2018

This is documented:

The map keys are sorted and used as JSON object keys by applying the following rules, [...]

I presume the reason is so that the output is deterministic. Otherwise, lots of applications that use JSON would not behave consistently and would break in unexpected ways.

Why exactly do you need the order of the keys to be randomized? I can't think of a reason why that would be useful.

@mvdan mvdan added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Aug 17, 2018
@ghost
Copy link
Author

ghost commented Aug 17, 2018

A very magical application scenario.

I think Ordered dictionary, currently not provided by go, is very compatible with this funtion.
also fmt.Println.

@ghost
Copy link
Author

ghost commented Aug 17, 2018

Just like that type in Python.
OrderedDict

@agnivade
Copy link
Contributor

Well, we would need to understand what the requirements are before adding new API to the standard library.

Any new API needs to go through the proposal process. I would request you to create a proposal issue outlining the reasoning behind the need to add additional API in the standard library. Simply saying "magical" won't let us understand why this is necessary and how might it help other users of the standard library.

Also note that there are other scenarios too like https://golang.org/pkg/io/ioutil/#ReadDir where we return entries sorted by filename. In this way, all standard library APIs are coherent in how they return random output.

@mvdan
Copy link
Member

mvdan commented Aug 17, 2018

This issue is a bit all over the place. You seem to be asking for ordered maps, which has very little to do with JSON. This issue has been raised in the past - see #6244.

In short, JSON object keys are unordered, so it would likely not make sense even if Go had ordered maps. I'm closing this for now.

@mvdan mvdan closed this as completed Aug 17, 2018
@golang golang locked and limited conversation to collaborators Aug 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

3 participants