forked from chanxuehong/wechat
/
types.go
58 lines (48 loc) · 1.19 KB
/
types.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
package util
import (
"strconv"
"strings"
)
// Uint64 support string quoted number in json
type Uint64 uint64
// UnmarshalJSON implement json Unmarshal interface
func (u64 *Uint64) UnmarshalJSON(b []byte) (err error) {
if b[0] == '"' && b[len(b)-1] == '"' {
b = b[1 : len(b)-1]
}
i, _ := strconv.ParseUint(string(b), 10, 64)
*u64 = Uint64(i)
return
}
func (u64 Uint64) Uint64() uint64 {
return uint64(u64)
}
// Int64 support string quoted number in json
type Int64 int64
// UnmarshalJSON implement json Unmarshal interface
func (i64 *Int64) UnmarshalJSON(b []byte) (err error) {
if b[0] == '"' && b[len(b)-1] == '"' {
b = b[1 : len(b)-1]
}
i, _ := strconv.ParseInt(string(b), 10, 64)
*i64 = Int64(i)
return
}
func (i64 Int64) Int64() int64 {
return int64(i64)
}
// MoneyFloat support string quoted number in json
type MoneyFloat float64
// UnmarshalJSON implement json Unmarshal interface
func (f64 *MoneyFloat) UnmarshalJSON(b []byte) (err error) {
if b[0] == '"' && b[len(b)-1] == '"' {
b = b[1 : len(b)-1]
}
str := strings.TrimPrefix(string(b), "¥")
i, _ := strconv.ParseFloat(str, 64)
*f64 = MoneyFloat(i)
return
}
func (f64 MoneyFloat) Float64() float64 {
return float64(f64)
}