Reputation: 3040
How do you unmarshal json into struct that contains a uint8? I am getting the error message json: cannot unmarshal object into Go struct field A.test of type uint8
In my struct I have
type A struct {
Test uint8 `json:"test omitempty" bson:"test"`
}
I inserted struct A into mongo and then I successfully do a mongo find and print out the collection struct A corresponds to. I can do a bson.MarshalExtJSON to convert the bson to json and then when I do a json.Unmarshal to convert json to struct A that is where I am failing.
Here is a sample golang playground that recreates the issue. I don't understand why this fails? How do I fix it?
https://play.golang.org/p/0HOAxsu166j
I see that unmarshal uses "float64, for JSON numbers" but I can't get float64 rather than uint8 to work either
Upvotes: 0
Views: 1595
Reputation: 3040
Thanks to @Brits comment, I found that when I called bson.MarshalExtJSON I got extendedJson. json.Unmarshal() cannot read extendedJson like {"$numberInt":"52"}
so that is why it was failing.
so to solve it I am using bson.UnmarshalExtJSON()
rather than json.Unmarshal()
to be able to unmarshal the extendedJSON into the struct
Upvotes: 0