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
Closed

encoding/json: support int map keys in encoding/json #12529

allaud opened this issue Sep 7, 2015 · 11 comments

Comments

@allaud
Copy link

allaud 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
Copy link
Member

minux commented Sep 7, 2015 via email

@kostya-sh
Copy link
Contributor

Related proposal: #12146

@allaud
Copy link
Author

allaud 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
Copy link
Author

allaud 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
Copy link

cfchris 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
Copy link
Contributor

rsc 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
Copy link
Contributor

cespare 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
Copy link
Contributor

cespare 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
Copy link
Contributor

rsc commented Apr 13, 2016

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

@cespare
Copy link
Contributor

cespare commented Apr 13, 2016

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

@gopherbot
Copy link

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

@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.
Projects
None yet
Development

No branches or pull requests

7 participants