非 RFC 3339 格式的 json 解组时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25087960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
json unmarshal time that isn't in RFC 3339 format
提问by evanmcdonnal
What is the appropriate way to handle deserialization of different time formats in Go? The encoding/json package seems to be entirely rigid in only accepted RFC 3339. I can deserialize into a string, transform that into RFC 3339 and then unmarshal it but I don't really want to do that. Any better solutions?
在 Go 中处理不同时间格式的反序列化的合适方法是什么?encoding/json 包在仅接受的 RFC 3339 中似乎完全僵化。我可以反序列化为一个字符串,将其转换为 RFC 3339,然后对其进行解组,但我真的不想这样做。有什么更好的解决方案吗?
回答by OneOfOne
You will have to implement the json.Marshaler/ json.Unmarshalerinterfaces on a custom type and use that instead, an example:
您必须在自定义类型上实现json.Marshaler/json.Unmarshaler接口并使用它,例如:
type CustomTime struct {
time.Time
}
const ctLayout = "2006/01/02|15:04:05"
func (ct *CustomTime) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), "\"")
if s == "null" {
ct.Time = time.Time{}
return
}
ct.Time, err = time.Parse(ctLayout, s)
return
}
func (ct *CustomTime) MarshalJSON() ([]byte, error) {
if ct.Time.UnixNano() == nilTime {
return []byte("null"), nil
}
return []byte(fmt.Sprintf("\"%s\"", ct.Time.Format(ctLayout))), nil
}
var nilTime = (time.Time{}).UnixNano()
func (ct *CustomTime) IsSet() bool {
return ct.UnixNano() != nilTime
}
type Args struct {
Time CustomTime
}
var data = `
{"Time": "2014/08/01|11:27:18"}
`
func main() {
a := Args{}
fmt.Println(json.Unmarshal([]byte(data), &a))
fmt.Println(a.Time.String())
}
edit: added CustomTime.IsSet()to check it was actually set or not, for future reference.
编辑:添加CustomTime.IsSet()以检查它是否实际设置,以供将来参考。
回答by JimB
The encoding/decoding is done by time.Timeitself, in the MarshalJSONand UnamrshalJSONmethods. You could create your own time.Timetype and override those functions to work with the json however you want.
编码/解码是time.Time在MarshalJSON和UnamrshalJSON方法中自行完成的。您可以创建自己的time.Time类型并覆盖这些函数以根据需要使用 json。
type Time struct {
time.Time
}
// returns time.Now() no matter what!
func (t *Time) UnmarshalJSON(b []byte) error {
// you can now parse b as thoroughly as you want
*t = Time{time.Now()}
return nil
}
type Config struct {
T Time
}
func main() {
c := Config{}
json.Unmarshal([]byte(`{"T": "bad-time"}`), &c)
fmt.Printf("%+v\n", c)
}

