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: inconsistent handling of keys with spaces #3887

Closed
rsc opened this issue Jul 29, 2012 · 6 comments
Closed

encoding/json: inconsistent handling of keys with spaces #3887

rsc opened this issue Jul 29, 2012 · 6 comments
Labels
FrozenDueToAge Suggested Issues that may be good for new contributors looking for work to do.
Milestone

Comments

@rsc
Copy link
Contributor

rsc commented Jul 29, 2012

---------- Forwarded message ----------
From:  <billkatz@gmail.com>
Date: Mon, Jul 16, 2012 at 2:19 PM
Subject: [golang-dev] JSON key names with spaces get decoded properly but encoded
incorrectly
To: golang-dev@googlegroups.com


I've run into a snag using JSON keys with spaces in the encoding/json package.  

An example:

type Message struct {
    Name string `json:"myName"`
    Number int `json:"my number"`
}

Decodes work properly, correctly parsing a JSON string for something like:

{"myName": "Ed", "my number": 42}

But when encoding, json.Marshall will output:

{"myName": "Ed", "Number": 42}

Seems like any spaces in key name will cause encoding to drop back to the struct's field
name instead of the tag value.  I assume this is not by design and a patch is desired?
@gopherbot
Copy link

Comment 1 by billkatz:

It looks like JSON tag values don't compose naturally, which makes it unintuitive.
For example:
type SharedFields struct {
   Name string `json:"myName"`
   Number int `json:"my number"`
}
type Foo struct {
   SharedFields
   fooName string `json:"foo name"`
}
type Moo struct {
   SharedFields
   mooName string `json:"moo name"`
}
Here, both Moo and Foo would not be able to properly decode fields that were obtained
through composition.

@rsc
Copy link
Contributor Author

rsc commented Aug 3, 2012

Comment 2:

Re comment #1, I think that's a separate issue (anonymous fields, issue #3069).
Note, by the way, that fooName and mooName are unexported so ignored by the package no
matter what.

@rsc
Copy link
Contributor Author

rsc commented Sep 12, 2012

Comment 4:

Labels changed: added go1.1.

@rsc
Copy link
Contributor Author

rsc commented Dec 10, 2012

Comment 5:

Labels changed: added size-m.

@rsc
Copy link
Contributor Author

rsc commented Dec 10, 2012

Comment 6:

Labels changed: added suggested.

@rsc
Copy link
Contributor Author

rsc commented Dec 22, 2012

Comment 7:

This issue was closed by revision 475f3df.

Status changed to Fixed.

@rsc rsc added fixed Suggested Issues that may be good for new contributors looking for work to do. labels Dec 22, 2012
@rsc rsc self-assigned this Dec 22, 2012
@rsc rsc added this to the Go1.1 milestone Apr 14, 2015
@rsc rsc removed the go1.1 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc removed their assignment Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge Suggested Issues that may be good for new contributors looking for work to do.
Projects
None yet
Development

No branches or pull requests

2 participants