Marshal/Unmarshal 整型
Marshal/Unmarshal int type
我使用 int
类型来表示枚举。当我将它编组为 JSON 时,我想将它转换为字符串,AFAIK,我应该实现 UnmarshalJSON
和 MarshalJSON
,但它抱怨:
marshal error: json: error calling MarshalJSON for type
main.trxStatus: invalid character 'b' looking for beginning of
valueunexpected end of JSON input
编组时。然后我将引号添加到编组字符串中:
func (s trxStatus) MarshalJSON() ([]byte, error) {
return []byte("\"" + s.String() + "\""), nil
}
Marshal
现在可以工作了,但是它不能 Unmarshal
从编组的字节流中正确地得到。
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)
}
在编写自定义封送拆收器和拆封拆收器实现时,请确保包含或trim json 字符串周围的双引号。
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
}
我使用 int
类型来表示枚举。当我将它编组为 JSON 时,我想将它转换为字符串,AFAIK,我应该实现 UnmarshalJSON
和 MarshalJSON
,但它抱怨:
marshal error: json: error calling MarshalJSON for type main.trxStatus: invalid character 'b' looking for beginning of valueunexpected end of JSON input
编组时。然后我将引号添加到编组字符串中:
func (s trxStatus) MarshalJSON() ([]byte, error) {
return []byte("\"" + s.String() + "\""), nil
}
Marshal
现在可以工作了,但是它不能 Unmarshal
从编组的字节流中正确地得到。
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)
}
在编写自定义封送拆收器和拆封拆收器实现时,请确保包含或trim json 字符串周围的双引号。
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
}