-
Notifications
You must be signed in to change notification settings - Fork 6
/
json.go
71 lines (60 loc) · 1.06 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package transport
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
)
func JsonUnmarshal[V any](jsonBlob []byte) V {
var v V
err := json.Unmarshal(jsonBlob, &v)
if err != nil {
panic(fmt.Sprint("json unmarshal: ", err))
}
return v
}
type Float64 float64
func (o *Float64) UnmarshalJSON(b []byte) error {
s := string(b)
s = strings.Trim(s, `"`)
if s == "null" {
s = ""
}
if s == "" {
*o = 0
return nil
}
f, err := strconv.ParseFloat(s, 64)
*o = Float64(f)
return err
}
func (o Float64) Value() float64 {
return float64(o)
}
func (o Float64) Ptr() *float64 {
v := o.Value()
return &v
}
func (o Float64) IsZero() bool {
return o.Value() == 0
}
func (o Float64) IsNotZero() bool {
return o.Value() != 0
}
type TimeMs time.Time
func (o *TimeMs) UnmarshalJSON(b []byte) error {
s := string(b)
s = strings.Trim(s, `"`)
if s == "" {
*o = TimeMs{}
return nil
}
i, err := strconv.ParseInt(s, 10, 64)
t := time.Unix(0, i*int64(time.Millisecond))
*o = TimeMs(t)
return err
}
func (o TimeMs) Std() time.Time {
return time.Time(o)
}