-
Notifications
You must be signed in to change notification settings - Fork 18k
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
Comments
This is documented:
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. |
A very magical application scenario. I think Ordered dictionary, currently not provided by go, is very compatible with this funtion. |
Just like that type in Python. |
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. |
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. |
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.
The text was updated successfully, but these errors were encountered: