使用 "valid" 零值解组 JSON
Unmarshal JSON with "valid" zero values
我有一些 JSON 正在解组成各种结构,以便我可以处理数据,看来这正在变成项目中最难的部分!!!
此 JSON 的格式是,如果该字段丢失,则它基本上为零。这是继 之后的内容,但认为它应该在 SO 上提出自己的问题。
因此零是一个有效值,我需要能够在我的 Go 代码中识别它。有没有办法让 Go 完全用指针解组到这个结构中?
在示例中 playground 你可以明白我的意思,它 "appears" 可以工作但是当我打印出其中一个指针值时它总是打印指针地址而不是实际的值。
package main
import "fmt"
import "log"
import "encoding/json"
const inputMissing = `
["AAAAAA", {"testCode" : "Sss"}, 123456]
`
const inputZero = `
["AAAAAA", {"testCode" : "Sss", "missingInt" : 0, "defaultInt" : 0,"missingString" : "", "defaultString" : ""}, 123456]
`
type RawMessage struct {
AlwaysString string
ClientData ClientData
ReceptionTime int
}
type ClientData struct {
TestCode string
MissingInt *int
DefaultInt int
MissingString *string
DefaultString string
}
func main() {
var n RawMessage
if err := json.Unmarshal([]byte(inputMissing), &n); err != nil {
log.Fatal(err)
}
fmt.Printf("%#v\n", n)
var o RawMessage
if err := json.Unmarshal([]byte(inputZero), &o); err != nil {
log.Fatal(err)
}
fmt.Printf("%#v\n", o)
fmt.Printf("Should print the value of the int, not pointer... %i", o.ClientData.MissingInt)
}
func (n *RawMessage) UnmarshalJSON(buf []byte) error {
tmp := []interface{}{&n.AlwaysString, &n.ClientData, &n.ReceptionTime}
wantLen := len(tmp)
if err := json.Unmarshal(buf, &tmp); err != nil {
return err
}
if g, e := len(tmp), wantLen; g != e {
return fmt.Errorf("wrong number of fields in RawMessage: %d != %d", g, e)
}
return nil
}
您的代码是正确的。针对 nil 测试指针并在需要值时取消引用指针:
fmt.Printf("Should print the value of the int, not pointer... %d", *o.ClientData.MissingInt)
您的困惑来自 fmt
包的默认格式,它在指针字段的情况下打印十六进制值,而不是指针值。
如果您使用 %#v
动词进行打印,您可以在结构上实现 fmt.GoStringer
接口来覆盖此 "behavior":
func (c ClientData) GoString() string {
mi := "<missing>"
if c.MissingInt != nil {
mi = strconv.Itoa(*c.MissingInt)
}
ms := "<missing>"
if c.MissingString != nil {
ms = *c.MissingString
}
return fmt.Sprintf("{TestCode: %s, MissingInt: %s, DefaultInt: %d, MissingString: %s, DefaultString: %s}",
c.TestCode, mi, c.DefaultInt, ms, c.DefaultString)
}
并将最后打印行更改为:
fmt.Printf("Should print the value of the int, not pointer... %d",
*o.ClientData.MissingInt)
您的输出变得更具可读性(在 Go Playground 上尝试):
main.RawMessage{AlwaysString:"AAAAAA", ClientData:{TestCode: Sss, MissingInt: <missing>, DefaultInt: 0, MissingString: <missing>, DefaultString: }, ReceptionTime:123456}
main.RawMessage{AlwaysString:"AAAAAA", ClientData:{TestCode: Sss, MissingInt: 0, DefaultInt: 0, MissingString: , DefaultString: }, ReceptionTime:123456}
Should print the value of the int, not pointer... 0
正如您从上面的输出中看到的,MissingXX
字段显示了静态值 <missing>
。
注:如果要使用%v
动词,则必须实现fmt.Stringer
接口,基本相同,只是方法名称不是 GoString()
而只是 String()
.
我有一些 JSON 正在解组成各种结构,以便我可以处理数据,看来这正在变成项目中最难的部分!!!
此 JSON 的格式是,如果该字段丢失,则它基本上为零。这是继
因此零是一个有效值,我需要能够在我的 Go 代码中识别它。有没有办法让 Go 完全用指针解组到这个结构中?
在示例中 playground 你可以明白我的意思,它 "appears" 可以工作但是当我打印出其中一个指针值时它总是打印指针地址而不是实际的值。
package main
import "fmt"
import "log"
import "encoding/json"
const inputMissing = `
["AAAAAA", {"testCode" : "Sss"}, 123456]
`
const inputZero = `
["AAAAAA", {"testCode" : "Sss", "missingInt" : 0, "defaultInt" : 0,"missingString" : "", "defaultString" : ""}, 123456]
`
type RawMessage struct {
AlwaysString string
ClientData ClientData
ReceptionTime int
}
type ClientData struct {
TestCode string
MissingInt *int
DefaultInt int
MissingString *string
DefaultString string
}
func main() {
var n RawMessage
if err := json.Unmarshal([]byte(inputMissing), &n); err != nil {
log.Fatal(err)
}
fmt.Printf("%#v\n", n)
var o RawMessage
if err := json.Unmarshal([]byte(inputZero), &o); err != nil {
log.Fatal(err)
}
fmt.Printf("%#v\n", o)
fmt.Printf("Should print the value of the int, not pointer... %i", o.ClientData.MissingInt)
}
func (n *RawMessage) UnmarshalJSON(buf []byte) error {
tmp := []interface{}{&n.AlwaysString, &n.ClientData, &n.ReceptionTime}
wantLen := len(tmp)
if err := json.Unmarshal(buf, &tmp); err != nil {
return err
}
if g, e := len(tmp), wantLen; g != e {
return fmt.Errorf("wrong number of fields in RawMessage: %d != %d", g, e)
}
return nil
}
您的代码是正确的。针对 nil 测试指针并在需要值时取消引用指针:
fmt.Printf("Should print the value of the int, not pointer... %d", *o.ClientData.MissingInt)
您的困惑来自 fmt
包的默认格式,它在指针字段的情况下打印十六进制值,而不是指针值。
如果您使用 %#v
动词进行打印,您可以在结构上实现 fmt.GoStringer
接口来覆盖此 "behavior":
func (c ClientData) GoString() string {
mi := "<missing>"
if c.MissingInt != nil {
mi = strconv.Itoa(*c.MissingInt)
}
ms := "<missing>"
if c.MissingString != nil {
ms = *c.MissingString
}
return fmt.Sprintf("{TestCode: %s, MissingInt: %s, DefaultInt: %d, MissingString: %s, DefaultString: %s}",
c.TestCode, mi, c.DefaultInt, ms, c.DefaultString)
}
并将最后打印行更改为:
fmt.Printf("Should print the value of the int, not pointer... %d",
*o.ClientData.MissingInt)
您的输出变得更具可读性(在 Go Playground 上尝试):
main.RawMessage{AlwaysString:"AAAAAA", ClientData:{TestCode: Sss, MissingInt: <missing>, DefaultInt: 0, MissingString: <missing>, DefaultString: }, ReceptionTime:123456}
main.RawMessage{AlwaysString:"AAAAAA", ClientData:{TestCode: Sss, MissingInt: 0, DefaultInt: 0, MissingString: , DefaultString: }, ReceptionTime:123456}
Should print the value of the int, not pointer... 0
正如您从上面的输出中看到的,MissingXX
字段显示了静态值 <missing>
。
注:如果要使用%v
动词,则必须实现fmt.Stringer
接口,基本相同,只是方法名称不是 GoString()
而只是 String()
.