diff --git a/testing/roundtrip_test.go b/testing/roundtrip_test.go index 4bf92e9b1d6fff2f21706c71cd8c7a09c073b51b..fed37c644276b152187e02a213eecac103c8150b 100644 --- a/testing/roundtrip_test.go +++ b/testing/roundtrip_test.go @@ -2,6 +2,7 @@ package testing import ( "bytes" + "encoding/json" "math/rand" "reflect" "testing" @@ -146,4 +147,18 @@ func TestTimeIsh(t *testing.T) { t.Fatal("no") } + b, err := json.Marshal(val) + if err != nil { + t.Fatal(err) + } + + var out2 ThingWithSomeTime + if err := json.Unmarshal(b, &out2); err != nil { + t.Fatal(err) + } + + if out2.When != out.When { + t.Fatal(err) + } + } diff --git a/utils.go b/utils.go index 6be5c7728bd72b9d2397ea524b844e74edd398ec..0c97ce5c198470e6648b936b4c9438809db86dd7 100644 --- a/utils.go +++ b/utils.go @@ -739,3 +739,16 @@ func (ct *CborTime) UnmarshalCBOR(r io.Reader) error { func (ct CborTime) Time() time.Time { return (time.Time)(ct) } + +func (ct CborTime) MarshalJSON() ([]byte, error) { + return ct.Time().MarshalJSON() +} + +func (ct *CborTime) UnmarshalJSON(b []byte) error { + var t time.Time + if err := t.UnmarshalJSON(b); err != nil { + return err + } + *(*time.Time)(ct) = t + return nil +}