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: json encoder fails for embedded non-struct fields #4474

gopherbot opened this issue Nov 30, 2012 · 6 comments

encoding/json: json encoder fails for embedded non-struct fields #4474

gopherbot opened this issue Nov 30, 2012 · 6 comments


Copy link

@gopherbot gopherbot commented Nov 30, 2012

by tkappler:

What steps will reproduce the problem?

1. Run the following program using Go tip. Go 1, and therefore, will not
work since their JSON encoder ignores embedded fields in a struct.

package main

import (

type IntType int

type MyStruct struct {
    Name string

func main() {
    var i IntType = 11
    a := MyStruct{i, "test"}

    jsonStr, err := json.Marshal(a)
    fmt.Println(string(jsonStr), err)

What is the expected output?

A JSON serialization of a, something like {"IntType": 11, "Name":

What do you see instead?

panic: reflect: NumField of non-struct type [recovered]
    panic: interface conversion: string is not error: missing method Error

goroutine 1 [running]:
----- stack segment boundary -----
encoding/json.func·002(0x101d0e90, 0x101d0100)
    /Users/thomas.kappler/software/go/src/pkg/encoding/json/encode.go:220 +0x92
----- stack segment boundary -----
reflect.(*rtype).NumField(0x86878, 0xc, 0xc, 0x86878)
    /Users/thomas.kappler/software/go/src/pkg/reflect/type.go:623 +0x7e
encoding/json.typeFields(0xc200042000, 0xb7868, 0xc200042000, 0xb7868, 0x0, ...)
    /Users/thomas.kappler/software/go/src/pkg/encoding/json/encode.go:607 +0x3a9
encoding/json.cachedTypeFields(0xc200042000, 0xb7868, 0x192, 0xc200042000, 0xb7868, ...)
    /Users/thomas.kappler/software/go/src/pkg/encoding/json/encode.go:694 +0xcb
encoding/json.(*encodeState).reflectValueQuoted(0xc200068000, 0xb7868, 0xc200043400,
0x192, 0x0, ...)
    /Users/thomas.kappler/software/go/src/pkg/encoding/json/encode.go:346 +0x19b5
encoding/json.(*encodeState).reflectValue(0xc200068000, 0xb7868, 0xc200043400, 0x192)
    /Users/thomas.kappler/software/go/src/pkg/encoding/json/encode.go:252 +0x48
encoding/json.(*encodeState).marshal(0xc200068000, 0xb7868, 0xc200043400, 0x0, 0x0, ...)
    /Users/thomas.kappler/software/go/src/pkg/encoding/json/encode.go:223 +0x89
encoding/json.Marshal(0xb7868, 0xc200043400, 0xcd8e8, 0x4, 0xb7868, ...)
    /Users/thomas.kappler/software/go/src/pkg/encoding/json/encode.go:117 +0x60
    /Users/thomas.kappler/jsonEmbedded.go:19 +0x7f

Which compiler are you using (5g, 6g, 8g, gccgo)?

$ go env
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread -fno-common"

Which operating system are you using?

Mac OS X 10.6.8

Which version are you using?  (run 'go version')

go version devel +697f36fec52c Fri Nov 30 20:02:30 2012 +1100

Please provide any additional information below.
Copy link

@gopherbot gopherbot commented Nov 30, 2012

Comment 1 by tkappler:

Sorry, part of my stack trace is off by three lines because I inserted some println()s
in encode.go. It should be:
----- stack segment boundary -----
reflect.(*rtype).NumField(0x86778, 0xc200043480, 0xc200042000, 0x86778)
    /Users/thomas.kappler/software/go/src/pkg/reflect/type.go:623 +0x7e
encoding/json.typeFields(0xc200042000, 0xb7768, 0xc200042000, 0xb7768, 0x0, ...)
    /Users/thomas.kappler/software/go/src/pkg/encoding/json/encode.go:604 +0x335
encoding/json.cachedTypeFields(0xc200042000, 0xb7768, 0x192, 0xc200042000, 0xb7768, ...)
    /Users/thomas.kappler/software/go/src/pkg/encoding/json/encode.go:691 +0xcb
Copy link

@rsc rsc commented Dec 10, 2012

Comment 3:

Labels changed: added size-m.

Copy link

@rsc rsc commented Dec 10, 2012

Comment 4:

Labels changed: added suggested.

Copy link

@davecheney davecheney commented Dec 13, 2012

Comment 5: fixes the double panic due to the incorrect type
assertion. It does not fix the overall issue.
Copy link

@rsc rsc commented Dec 30, 2012

Comment 6:

Labels changed: added priority-later, removed priority-triage.

Status changed to Accepted.

Copy link

@rsc rsc commented Jan 2, 2013

Comment 7:

This issue was closed by revision cdec085.

Status changed to Fixed.

@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
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.