问题
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