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: Sequential Key Casing #23726

Closed
andreidorin13 opened this issue Feb 7, 2018 · 2 comments

Comments

Projects
None yet
4 participants
@andreidorin13
Copy link

commented Feb 7, 2018

What version of Go are you using (go version)?

go version go1.9.3 linux/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build924240465=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

package main

import (
	"fmt"
	"encoding/json"
)

type Test struct {
	Event string `json:"v"`
	Bytes json.RawMessage `json:"B"`
}

func main() {
	string := []byte(`{"v": "Test", "V": 1234, "B": {"key": "value"}}`)
	var test Test
	json.Unmarshal(string, &test)
	fmt.Printf("%+v\n", test)
}

Link: https://play.golang.org/p/FNjS_tks7Ps

What did you expect to see?

{Event:Test Struct:[123 34 107 101 121 34 58 32 34 118 97 108 117 101 34 125]}

What did you see instead?

{Event:Test Struct:[]}

Why does the lower case "v", followed by an upper case "V", cause the byte array to be empty?

@andreidorin13 andreidorin13 changed the title encoding/json: Key encoding/json: Sequential Key Casing Feb 7, 2018

@bradfitz bradfitz added this to the Go1.11 milestone Feb 7, 2018

@artyom

This comment has been minimized.

Copy link
Contributor

commented Feb 7, 2018

@andreidorin13 don't ignore error from json.Unmarshal — it's non-nil here: json: cannot unmarshal number into Go struct field Test.v of type string.

As I see it the real caveat here is presence of v and V attributes in json object, and the following documented behavior of json.Unmarshal:

To unmarshal JSON into a struct, Unmarshal matches incoming object keys to the keys used by Marshal (either the struct field name or its tag), preferring an exact match but also accepting a case-insensitive match.

Please see the illustration here: https://play.golang.org/p/A9OXJRX4LlL

I believe that in presence of two or more attributes whose names differ only by case the preferring an exact match clause of json.Unmarshal doesn't hold — it seem to always use the value from the last attribute it encounters.

@bradfitz

This comment has been minimized.

Copy link
Member

commented Feb 8, 2018

Duplicate of #14750.

Closing this one.

@bradfitz bradfitz closed this Feb 8, 2018

@golang golang locked and limited conversation to collaborators Feb 8, 2019

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.