是否可以修改 json 序列化和反序列化,以便像这样构建:
type Foo struct {
A int64
B uint64
// and other stuff with int64 and uint64 and there's a lot of struct that are like this
}
x := Foo{A: 1234567890987654, B: 987654321012345678}
byt, err := json.Marshal(x)
fmt.Println(err)
fmt.Println(string(byt))
// 9223372036854775808 9223372036854775808
err = json.Unmarshal([]byte(`{"A":"12345678901234567", "B":"98765432101234567"}`), &x)
// ^ must be quoted since javascript can't represent those values properly (2^53)
// ^ json: cannot unmarshal string into Go struct field Foo.A of type int64
fmt.Println(err)
fmt.Printf("%#v\n", x)
// main.Foo{A:1234567890987654, B:0xdb4da5f44d20b4e}
https://play.golang.org/p/dHN-FcJ7p-N
因此,它可以接收 json 字符串,但解析为 int64/uint64,并且可以将 int64/uint64 反序列化为 json 字符串,而无需修改结构或结构标记
宝慕林4294392
相关分类