本文整理汇总了Golang中Time.Time.MarshalJSON方法的典型用法代码示例。如果您正苦于以下问题:Golang Time.MarshalJSON方法的具体用法?Golang Time.MarshalJSON怎么用?Golang Time.MarshalJSON使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Time.Time
的用法示例。
在下文中一共展示了Time.MarshalJSON方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: MtimeSet
func (self *BundleFileMD) MtimeSet(value time.Time) error {
bytes, err := value.MarshalJSON()
if err != nil {
return err
}
return self.bundle.bm.BundleFileStringSet(self.bundle.user, self.bundle.id, self.id, "mtime", string(bytes))
}
示例2: EqTime
func (a *Assert) EqTime(expect, got time.Time, msg string, params ...interface{}) *Assert {
if !expect.Equal(got) {
_, file, line, _ := runtime.Caller(1)
file = filepath.Base(file)
sexpect, _ := expect.MarshalJSON()
sgot, _ := got.MarshalJSON()
a.t.Fatalf("Assert.EqTime failure <%s:%d>: (expect: %s, got:%s): %q", file, line, sexpect, sgot, fmt.Sprintf(msg, params...))
}
return a
}
示例3: rwTimeBytes
func rwTimeBytes(w jsWriter, msg []byte, scratch []byte) ([]byte, []byte, error) {
var t time.Time
var err error
t, msg, err = ReadTimeBytes(msg)
if err != nil {
return msg, scratch, err
}
bts, err := t.MarshalJSON()
if err != nil {
return msg, scratch, err
}
_, err = w.Write(bts)
return msg, scratch, err
}
示例4: rwExtensionBytes
func rwExtensionBytes(w jsWriter, msg []byte, scratch []byte) ([]byte, []byte, error) {
var err error
var et int8
et, err = peekExtension(msg)
if err != nil {
return msg, scratch, err
}
// if it's time.Time
if et == TimeExtension {
var tm time.Time
tm, msg, err = ReadTimeBytes(msg)
if err != nil {
return msg, scratch, err
}
bts, err := tm.MarshalJSON()
if err != nil {
return msg, scratch, err
}
_, err = w.Write(bts)
return msg, scratch, err
}
// if the extension is registered,
// use its canonical JSON form
if f, ok := extensionReg[et]; ok {
e := f()
msg, err = ReadExtensionBytes(msg, e)
if err != nil {
return msg, scratch, err
}
bts, err := json.Marshal(e)
if err != nil {
return msg, scratch, err
}
_, err = w.Write(bts)
return msg, scratch, err
}
// otherwise, write `{"type": <num>, "data": "<base64data>"}`
r := RawExtension{}
r.Type = et
msg, err = ReadExtensionBytes(msg, &r)
if err != nil {
return msg, scratch, err
}
scratch, err = writeExt(w, r, scratch)
return msg, scratch, err
}
示例5: Fuzz
func Fuzz(data []byte) int {
var t time.Time
if err := t.UnmarshalText(data); err != nil {
return 0
}
data1, err := t.MarshalText()
if err != nil {
panic(err)
}
var t1 time.Time
if err := t1.UnmarshalText(data1); err != nil {
panic(err)
}
if !fuzz.DeepEqual(t, t1) {
fmt.Printf("t0: %#v\n", t)
fmt.Printf("t1: %#v\n", t1)
panic("bad MarshalText")
}
data2, err := t.GobEncode()
if err != nil {
panic(err)
}
var t2 time.Time
if err := t2.GobDecode(data2); err != nil {
panic(err)
}
if !fuzz.DeepEqual(t, t2) {
fmt.Printf("t0: %#v\n", t)
fmt.Printf("t2: %#v\n", t2)
panic("bad GobEncode")
}
data3, err := t.MarshalBinary()
if err != nil {
panic(err)
}
var t3 time.Time
if err := t3.UnmarshalBinary(data3); err != nil {
panic(err)
}
if !fuzz.DeepEqual(t, t3) {
fmt.Printf("t0: %#v\n", t)
fmt.Printf("t3: %#v\n", t3)
panic("bad MarshalBinary")
}
data4, err := t.MarshalJSON()
if err != nil {
panic(err)
}
var t4 time.Time
if err := t4.UnmarshalJSON(data4); err != nil {
panic(err)
}
if !fuzz.DeepEqual(t, t4) {
fmt.Printf("t0: %#v\n", t)
fmt.Printf("t4: %#v\n", t4)
panic("bad MarshalJSON")
}
data5, err := t.MarshalText()
if err != nil {
panic(err)
}
var t5 time.Time
if err := t5.UnmarshalText(data5); err != nil {
panic(err)
}
if !fuzz.DeepEqual(t, t5) {
fmt.Printf("t0: %#v\n", t)
fmt.Printf("t5: %#v\n", t5)
panic("bad MarshalText")
}
data6 := t.Format(time.RFC3339Nano)
t6, err := time.Parse(time.RFC3339Nano, data6)
if err != nil {
panic(err)
}
if !fuzz.DeepEqual(t, t6) {
fmt.Printf("t0: %#v\n", t)
fmt.Printf("t6: %#v\n", t6)
panic("bad Format")
}
return 1
}