Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(266)

Issue 6338067: code review 6338067: encoding/json: Marshal a map with numeric keys. (Closed)

Can't Edit
Can't Publish+Mail
Start Review
Created:
13 years, 6 months ago by mikespook
Modified:
11 years, 5 months ago
Reviewers:
CC:
golang-china_googlegroups.com, golang-dev
Visibility:
Public.

Description

encoding/json: Marshal a map with numeric keys. Json package only accept a string keys map to encode currently. There will be an error when encoding a map with numeric keys. Eg. [http://play.golang.org/p/11oo0H0NtE] got "json: unsupported type: map[int]string" In developing, it is common that using a map with numeric keys. Then we must copy & convert numbers into strings befor encoding into json. It is not convenient. This patch was created for encoding the map with numeric keys into json. Sorry for my English. Hope I have explained it clearly.

Patch Set 1 #

Patch Set 2 : diff -r 7abccc974354 https://code.google.com/p/go #

Patch Set 3 : diff -r 7abccc974354 https://code.google.com/p/go #

Unified diffs Side-by-side diffs Delta from patch set Stats (+32 lines, -3 lines) Patch
M src/pkg/encoding/json/encode.go View 1 3 chunks +19 lines, -3 lines 0 comments Download
M src/pkg/encoding/json/encode_test.go View 1 1 chunk +13 lines, -0 lines 0 comments Download

Messages

Total messages: 2
mikespook
Hello golang-dev@googlegroups.com (cc: golang-china@googlegroups.com, golang-dev@googlegroups.com), I'd like you to review this change to https://code.google.com/p/go
13 years, 6 months ago (2012-06-26 14:52:42 UTC) #1
rsc
13 years, 4 months ago (2012-07-30 01:57:21 UTC) #2
Hi,

I understand what you're trying to do, but we have to draw a line somewhere in
the generic JSON representation. I don't actually understand why
string-number-keyed maps are so common in your app, but it seems like using the
strings should not be that big a hardship, especially if you write a simple
helper function.

Each JSON construct has exactly one Go type that is used for the default
unmarshal (when there's no other type information) and recognized when
marshaling. For arrays it is []interface{}. For objects it is
map[string]interface{}, because the object keys are required to be JSON strings.

I would suggest trying to use data structures instead of string-number-keyed
maps anyway. You'll be much happier with package json converting between data
structures and JSON instead of using these generic forms.

The video at http://research.swtch.com/gotour shows an example of what I mean.
Sign in to reply to this message.

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b