-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjson.go
120 lines (97 loc) · 2.16 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package otils
import (
"encoding/json"
"strconv"
"strings"
"time"
)
// NullableString represents a string that is sent
// back by some APIs as null, in JSON unquoted which
// makes them un-unmarshalable in Go.
// NullableString interprets null as "".
type NullableString string
var _ json.Unmarshaler = (*NullableString)(nil)
func (ns *NullableString) UnmarshalJSON(b []byte) error {
str := string(b)
// Special case when we encounter `null`, modify it to the empty string
if str == "null" || str == "" {
*ns = ""
return nil
}
unquoted, err := strconv.Unquote(str)
if err != nil {
return err
}
*ns = NullableString(unquoted)
return nil
}
type NullableFloat64 float64
var _ json.Unmarshaler = (*NullableFloat64)(nil)
func (nf64 *NullableFloat64) UnmarshalJSON(b []byte) error {
str := string(b)
if strings.HasPrefix(str, "\"") {
unquoted, err := strconv.Unquote(str)
if err == nil {
str = unquoted
}
}
f64, err := strconv.ParseFloat(str, 64)
if err == nil {
*nf64 = NullableFloat64(f64)
return nil
}
// Otherwise trying checking if it was null
var ns NullableString
if err := json.Unmarshal(b, &ns); err != nil {
return err
}
if ns == "" {
*nf64 = 0.0
return nil
}
f64, err = strconv.ParseFloat(str, 64)
if err != nil {
return err
}
*nf64 = NullableFloat64(f64)
return nil
}
type NumericBool bool
func (nb *NumericBool) UnmarshalJSON(blob []byte) error {
if len(blob) < 1 {
*nb = false
return nil
}
s := string(blob)
// Try first parsing an integer.
pBool, err := strconv.ParseBool(s)
if err == nil {
*nb = NumericBool(pBool)
return nil
}
pInt, err := strconv.ParseInt(s, 10, 32)
if err != nil {
*nb = pInt != 0
return nil
}
return err
}
type NullableTime time.Time
var _ json.Unmarshaler = (*NullableTime)(nil)
func (nt *NullableTime) UnmarshalJSON(b []byte) error {
var ns NullableString
if err := json.Unmarshal(b, &ns); err != nil {
return err
}
if ns == "" {
return nil
}
// To parse the time, we need to quote the value
quotedStr := strconv.Quote(string(ns))
t := new(time.Time)
if err := json.Unmarshal([]byte(quotedStr), t); err != nil {
return err
}
*nt = NullableTime(*t)
return nil
}