-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
int64.go
71 lines (50 loc) · 877 Bytes
/
int64.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 unmarshal
import (
"strconv"
"strings"
"github.com/buger/jsonparser"
)
type Int64 int64
func (i *Int64) UnmarshalJSON(b []byte) error {
var data, dataType, _, err = jsonparser.Get(b)
if err != nil {
return err
}
if len(data) == 0 {
*i = 0
return nil
}
str := string(data)
switch dataType {
case jsonparser.String, jsonparser.Number:
if strings.Contains(str, ".") {
j, err := strconv.ParseFloat(str, 64)
if err != nil {
return err
}
*i = Int64(j)
} else {
k, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return err
}
*i = Int64(k)
}
return nil
case jsonparser.Boolean:
b, err := strconv.ParseBool(str)
if err != nil {
return err
}
if b {
*i = 1
} else {
*i = 0
}
return nil
case jsonparser.Null:
*i = 0
return nil
}
return newError(dataType, "int64")
}