MCPcopy Create free account
hub / github.com/chain/txvm / UnmarshalJSON

Method UnmarshalJSON

encoding/json/duration.go:24–55  ·  view source on GitHub ↗

UnmarshalJSON fulfills the encoding/json.Unmarshaler interface. It attempts to parse text as a time.Duration string. The Go documentation defines this as a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time unit

(b []byte)

Source from the content-addressed store, hash-verified

22// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h").
23// If there is no time unit, UnmarshalJSON defaults to ms.
24func (d *Duration) UnmarshalJSON(b []byte) error {
25 if bytes.Equal(b, []byte("null")) {
26 return nil
27 }
28
29 dMS, err := strconv.ParseInt(string(b), 10, 64)
30 if err != nil {
31 // Assume this is a string instead, in which case we need to unmarshal it as a string
32 // before we try to parse it as a time.Duration.
33 var str string
34 err = json.Unmarshal(b, &str)
35 if err != nil {
36 return errors.New("invalid json.Duration")
37 }
38
39 d0, err := time.ParseDuration(str)
40 if err != nil {
41 return errors.New("invalid json.Duration")
42 }
43 if d0 < 0 {
44 return errors.New("invalid json.Duration: Duration cannot be less than 0")
45 }
46 d.Duration = d0
47 } else {
48 if dMS < 0 {
49 return errors.New("invalid json.Duration: Duration cannot be less than 0")
50 }
51 d.Duration = time.Duration(dMS) * time.Millisecond
52 }
53
54 return nil
55}
56
57// MarshalJSON implements json.Marshaler.
58func (d Duration) MarshalJSON() ([]byte, error) {

Callers

nothing calls this directly

Calls 1

EqualMethod · 0.80

Tested by

no test coverage detected