Marshal/Unmarshal int type

末鹿安然 提交于 2020-01-06 06:01:30

问题


I use an int type to represent an enum. I want to convert it to string when I marshal it to JSON, AFAIK, I should implement UnmarshalJSON and MarshalJSON, but it complains:

marshal error: json: error calling MarshalJSON for type main.trxStatus: invalid character 'b' looking for beginning of valueunexpected end of JSON input

when marshalling. Then I've add the quotes to the marshalled string:

func (s trxStatus) MarshalJSON() ([]byte, error) {
    return []byte("\"" + s.String() + "\""), nil
}

the Marshal works now but it can't Unmarshal from the marshalled byte stream correctly.

package main

import (
    "encoding/json"
    "fmt"
)

type trxStatus int

type test struct {
    S trxStatus
    A string
}

const (
    buySubmitted trxStatus = iota
    buyFilled
    sellSubmiited
    sellFilled
    finished
)

var ss = [...]string{"buySubmitted", "buyFilled", "sellSubmiited", "sellFilled", "Finished"}

func (s *trxStatus) UnmarshalJSON(bytes []byte) error {
    status := string(bytes)
    // unknown
    for i, v := range ss {
        if v == status {
            tttt := trxStatus(i)
            *s = tttt
            break
        }
    }
    return nil
}

func (s trxStatus) MarshalJSON() ([]byte, error) {
    return []byte(s.String()), nil
}

func (s trxStatus) String() string {

    if s < buySubmitted || s > finished {
        return "Unknown"
    }

    return ss[s]
}

func main() {
    s := test{S: buyFilled, A: "hello"}
    j, err := json.Marshal(s)
    if err != nil {
        fmt.Printf("marshal error: %v", err)
    }
    var tt test
    fmt.Println(json.Unmarshal(j, &tt))
    fmt.Println(tt)
}

回答1:


When writing your custom Marshaler and Unmarshaler implementations make sure to include or trim the surrounding double quotes of json strings.

func (s *trxStatus) UnmarshalJSON(bytes []byte) error {
    status := string(bytes)
    if n := len(status); n > 1 && status[0] == '"' && status[n-1] == '"' {
        status = status[1:n-1] // trim surrounding quotes
    }
    // unknown
    for i, v := range ss {
        if v == status {
            tttt := trxStatus(i)
            *s = tttt
            break
        }
    }
    return nil
}

func (s trxStatus) MarshalJSON() ([]byte, error) {
    return []byte(`"` + s.String() + `"`), nil
}


来源:https://stackoverflow.com/questions/50187325/marshal-unmarshal-int-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!