在Go中处理不同时间格式的反序列化的适当方法是什么?编码/ json包似乎只是在接受的RFC 3339中完全僵化.我可以反序列化为一个字符串,将其转换为RFC 3339然后解组它但我真的不想那样做.更好的解决方案?
One*_*One 49
您必须在自定义类型上实现json.Marshaler
/ json.Unmarshaler
interfaces并使用它,例如:
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())
}
Run Code Online (Sandbox Code Playgroud)
编辑:添加CustomTime.IsSet()
以检查它是否实际设置,以供将来参考.
Jim*_*imB 28
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)
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
24694 次 |
最近记录: |