FAQ
If I define a type that's not a struct, like "type foo int", and embed this
type into a struct, the JSON encoder panics on it. This might be a bug. If
not, please educate me about what I'm doing wrong!

Here's a minimal example:

---------------------------
package main

import (
"encoding/json"
"fmt"
)

type IntType int

type MyStruct struct {
IntType
Name string
}

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

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

Running this, I get

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
main.main()
/Users/thomas.kappler/jsonEmbedded.go:19 +0x7f


Looking at encoding/json/encode.go line 604, function typeFields, it calls
NumField() without checking whether f.typ is actually a struct.


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

$ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread -fno-common"
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="EDITED/go"
GOROOT="EDITED/go"
GOTOOLDIR="EDITED/go/pkg/tool/darwin_amd64"
CGO_ENABLED="1"

--

Search Discussions

  • Minux at Nov 30, 2012 at 9:26 am
    please file an issue.
    On Friday, November 30, 2012, Thomas Kappler wrote:

    If I define a type that's not a struct, like "type foo int", and embed
    this type into a struct, the JSON encoder panics on it. This might be a
    bug. If not, please educate me about what I'm doing wrong!

    Here's a minimal example:

    ---------------------------
    package main

    import (
    "encoding/json"
    "fmt"
    )

    type IntType int

    type MyStruct struct {
    IntType
    Name string
    }

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

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

    Running this, I get

    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
    main.main()
    /Users/thomas.kappler/jsonEmbedded.go:19 +0x7f


    Looking at encoding/json/encode.go line 604, function typeFields, it calls
    NumField() without checking whether f.typ is actually a struct.


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

    $ go env
    GOARCH="amd64"
    GOBIN=""
    GOCHAR="6"
    GOEXE=""
    GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread -fno-common"
    GOHOSTARCH="amd64"
    GOHOSTOS="darwin"
    GOOS="darwin"
    GOPATH="EDITED/go"
    GOROOT="EDITED/go"
    GOTOOLDIR="EDITED/go/pkg/tool/darwin_amd64"
    CGO_ENABLED="1"

    --

    --
  • Thomas Kappler at Nov 30, 2012 at 9:37 am
    Thanks, filed http://code.google.com/p/go/issues/detail?id=4474.
    On Friday, November 30, 2012 10:26:16 AM UTC+1, minux wrote:

    please file an issue.
    On Friday, November 30, 2012, Thomas Kappler wrote:

    If I define a type that's not a struct, like "type foo int", and embed
    this type into a struct, the JSON encoder panics on it. This might be a
    bug. If not, please educate me about what I'm doing wrong!

    Here's a minimal example:

    ---------------------------
    package main

    import (
    "encoding/json"
    "fmt"
    )

    type IntType int

    type MyStruct struct {
    IntType
    Name string
    }

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

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

    Running this, I get

    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
    main.main()
    /Users/thomas.kappler/jsonEmbedded.go:19 +0x7f


    Looking at encoding/json/encode.go line 604, function typeFields, it
    calls NumField() without checking whether f.typ is actually a struct.


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

    $ go env
    GOARCH="amd64"
    GOBIN=""
    GOCHAR="6"
    GOEXE=""
    GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread -fno-common"
    GOHOSTARCH="amd64"
    GOHOSTOS="darwin"
    GOOS="darwin"
    GOPATH="EDITED/go"
    GOROOT="EDITED/go"
    GOTOOLDIR="EDITED/go/pkg/tool/darwin_amd64"
    CGO_ENABLED="1"

    --

    --
  • Frederic Donckels at Feb 22, 2013 at 12:38 pm
    I have a similar problem, though without panic.
    http://play.golang.org/p/ihWKpTZ8mq

    Will the fix also address this or should I open a new issue?


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Rémy Oudompheng at Feb 22, 2013 at 12:37 pm

    On 2013/2/22 wrote:
    I have a similar problem, though without panic.
    http://play.golang.org/p/ihWKpTZ8mq

    Will the fix also address this or should I open a new issue?
    The problme is not similar, your example is about embedded structs
    (issues 3069, 4632).
    It's fixed for Go 1.1 and your program will print:

    {"Source":54,"Date":"2013-02-22T13:35:12.000679436+01:00"}

    for example.

    Rémy.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Frédéric Donckels at Feb 22, 2013 at 1:18 pm

    The problme is not similar, your example is about embedded structs
    (issues 3069, 4632).
    It's fixed for Go 1.1 and your program will print:

    {"Source":54,"Date":"2013-02-22T13:35:12.000679436+01:00"}
    Perfect! I can't wait for Go 1.1

    Frédéric

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 30, '12 at 9:19a
activeFeb 22, '13 at 1:18p
posts6
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase