This repository has been archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
util.go
89 lines (72 loc) · 1.72 KB
/
util.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
package stripe
import (
"strconv"
)
// Int is a special type of integer that can unmarshall a JSON value of
// "null", which cannot be parsed by the Go JSON parser as of Go v1.
//
// see http://code.google.com/p/go/issues/detail?id=2540
type Int int
func (self *Int) UnmarshalJSON(data []byte) error {
str := string(data)
if str == "null" {
return nil
}
i, err := strconv.Atoi(str)
if err != nil {
return err
}
*self = Int(i)
return nil
}
// Int64 is a special type of int64 that can unmarshall a JSON value of
// "null", which cannot be parsed by the Go JSON parser as of Go v1.
//
// see http://code.google.com/p/go/issues/detail?id=2540
type Int64 int64
func (self *Int64) UnmarshalJSON(data []byte) error {
str := string(data)
if str == "null" {
return nil
}
i, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return err
}
*self = Int64(i)
return nil
}
// Bool is a special type of bool that can unmarshall a JSON value of
// "null", which cannot be parsed by the Go JSON parser as of Go v1.
//
// see http://code.google.com/p/go/issues/detail?id=2540
type Bool bool
func (self *Bool) UnmarshalJSON(data []byte) error {
str := string(data)
if str == "null" {
return nil
}
b, err := strconv.ParseBool(str)
if err != nil {
return err
}
*self = Bool(b)
return nil
}
// String is a special type of string that can unmarshall a JSON value of
// "null", which cannot be parsed by the Go JSON parser as of Go v1.
//
// see http://code.google.com/p/go/issues/detail?id=2540
type String string
func (self *String) UnmarshalJSON(data []byte) error {
str := string(data)
if str == "null" {
return nil
}
str, err := strconv.Unquote(str)
if err != nil {
return err
}
*self = String(str)
return nil
}