---------- 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?
The text was updated successfully, but these errors were encountered:
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.
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.
The text was updated successfully, but these errors were encountered: