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: unmarshal failed once you implemtned UnmarshalText interface #15671

Closed
hsinhoyeh opened this issue May 13, 2016 · 3 comments

Comments

Projects
None yet
3 participants
@hsinhoyeh
Copy link

commented May 13, 2016

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?
    1.6.2
  2. What operating system and processor architecture are you using (go env)?
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"

we suffered an issue with json/unmarshal.
the following example works well as we expected

package main

import (
    "fmt"
    "encoding/json"
)

type Bar struct {
        BIntField    int    `json:"b_int_field"`
        BStringField string `json:"b_string_field"`
}

func main() {

    b := `{"b_int_field":101,"b_string_field":"bar"}`

    v := &Bar{}
    fmt.Println(json.Unmarshal([]byte(b), v)    )
    fmt.Println(v)
}

but everything mess up when we added an interface UnmarshalText interface to Bar structure

package main

import (
    "fmt"
    "encoding/json"
)

type Bar struct {
        BIntField    int    `json:"b_int_field"`
        BStringField string `json:"b_string_field"`
}

func (b *Bar) UnmarshalText(t []byte) error{
    return nil
}


func main() {

    b := `{"b_int_field":101,"b_string_field":"bar"}`

    v := &Bar{}
    fmt.Println(json.Unmarshal([]byte(b), v)    )
    fmt.Println(v)
}

I think it is a bug cuz I cannot find any documentation for this. thanks

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented May 13, 2016

The UnmarshalText method decodes a JSON string into the object. In your example you do not have a string, you have an object, so the JSON decoder does not know what to do.

Questions about how to use UnmarshalText would be better handled in a forum rather than the issue tracker. Please see https://golang.org/wiki/Questions . Thanks.

@hsinhoyeh

This comment has been minimized.

Copy link
Author

commented May 13, 2016

Hi @ianlancetaylor
thansk for your prompt reply and close the issue.
But I don't think you understand my problem properly.
what I was trying to say is "when using json to unmarshal a structure, i.e. Bar in this example, it will fail once Bar structure ever impled TextUnmarshaler interface". But you didn't explain any thing about that.

Seems that if a structure impled a TextMarshaler interface, it will get error here
https://golang.org/src/encoding/json/decode.go#L567

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented May 13, 2016

I don't see a bug report here. Please see https://golang.org/wiki/Questions .

@golang golang locked and limited conversation to collaborators May 13, 2017

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.