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: support int map keys in encoding/json #12529

Closed
allaud opened this issue Sep 7, 2015 · 11 comments

Comments

Projects
None yet
7 participants
@allaud
Copy link

commented Sep 7, 2015

Our team faced a strange issue with encoding map[int]string into json. Our current solution is to use patched json encoder, which can encode integer keys as strings.

Why not to implement this in the default module?

@minux minux changed the title Support int map keys in encoding/json encoding/json: support int map keys in encoding/json Sep 7, 2015

@minux

This comment has been minimized.

Copy link
Member

commented Sep 7, 2015

@kostya-sh

This comment has been minimized.

Copy link
Contributor

commented Sep 8, 2015

Related proposal: #12146

@allaud

This comment has been minimized.

Copy link
Author

commented Sep 12, 2015

@minux i experience some troubles moving from python to golang as i use integer keys a lot. The python behavior:

>>> d = {1: 1, 5: 4}
>>> json.dumps(d)
'{"1": 1, "5": 4}'
>>> d = {1.4: 1, 5.1: 4}
>>> d
{1.4: 1, 5.1: 4}
>>> json.dumps(d)
'{"1.4": 1, "5.1": 4}'

Maybe we can introduce a special method inside json module for marshalling such data?

@allaud

This comment has been minimized.

Copy link
Author

commented Sep 12, 2015

I'm speaking about data structures not json

12 сент. 2015 г., в 13:16, Artyom Pervukhin notifications@github.com написал(а):

Why not to implement this in the default module?

I believe one reason for this is that encoding/json implements RFC 4627, which defines JSON object as

An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.

(emphasis is mine)


Reply to this email directly or view it on GitHub.

@cfchris

This comment has been minimized.

Copy link

commented Oct 17, 2015

This week I added 100+ lines of code to one of our packages to support MarshalJSON and UnmarshalJSON map[int]OurType. I also added 130+ lines of unit tests. While in the standard lib code, I could see it would be far fewer lines based on all of the unexported types and utility functions.

I would love to add this to the standard lib. What is the process for getting an issue approved and assigned?

@rsc rsc added this to the Go1.6 milestone Oct 23, 2015

@rsc rsc added the Thinking label Oct 23, 2015

@rsc rsc modified the milestones: Go1.7, Go1.6 Nov 25, 2015

@rsc

This comment has been minimized.

Copy link
Contributor

commented Nov 25, 2015

Did not get to this for Go 1.6, sorry. We should think about this at the same time as #12146.

@cespare

This comment has been minimized.

Copy link
Contributor

commented Mar 20, 2016

@rsc, it seems like #12146 is likely to happen, and there's a CL out at https://golang.org/cl/20356.

Given that, you can make your integer keys work but you have to make your own int type with the obvious TextMarshaler/TextUnmarshaler methods.

What are your thoughts about also doing this change? I still think it's worth doing, and I'd like to send a 1.7 CL if you agree.

@cespare

This comment has been minimized.

Copy link
Contributor

commented Apr 13, 2016

Bump for @rsc or anyone else with opinions. Shall I send a CL for this?

The change from #12146 has now gone in.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Apr 13, 2016

If the change is small and you can make it happen soon, sure.

@cespare

This comment has been minimized.

Copy link
Contributor

commented Apr 13, 2016

I'll send a CL in the next couple of days.

@gopherbot

This comment has been minimized.

Copy link

commented Apr 16, 2016

CL https://golang.org/cl/22060 mentions this issue.

@gopherbot gopherbot closed this in f05c3aa May 10, 2016

@golang golang locked and limited conversation to collaborators May 10, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.