2016-08-30 15:36


  • serialization
  • json

I have a problem deserializing my object. I use an interface to this object to call the Serialization, and from reading the output the serialization works perfectly. Here is the underlying struct of my object:

type pimp struct {
    Price       int
    ExpDate     int64
    BidItem     Item
    CurrentBid  int
    PrevBidders []string

And here's the interface it implements:

type Pimp interface {
    GetStartingPrice() int
    GetTimeLeft() int64
    GetItem() Item
    GetCurrentBid() int
    GetPrevBidders() []string
    AddBidder(string) error
    Serialize() ([]byte, error)

The Serialize() method:

func (p *pimp) Serialize() ([]byte, error) {
    return json.Marshal(*p)

As you may have noticed, pimp has a variable by the name of Item. This item is also, an interface:

type item struct {
    Name string

type Item interface {
    GetName() string

Now serializing a sample of such an object returns the following JSON:


Here is my deserialization code:

func PimpFromJSON(content []byte) (Pimp, error) {
    p := new(pimp)
    err := json.Unmarshal(content, p)
    return p, err

Running it, however, gives me the following error:

json: cannot unmarshal object into Go value of type Auction.Item

Any help is appreciated.

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享