-
Notifications
You must be signed in to change notification settings - Fork 5
/
types.go
43 lines (38 loc) · 1019 Bytes
/
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
package xjson
import (
bytes2 "bytes"
"fmt"
"strconv"
)
// Int64String is a go int64 type.
//
// which can marshal into json string, and unmarshal from json string or json number.
// type User struct {
// UID Int64String `json:"uid"`
// }
// so we can marshal into
// {"uid": "123456"}
// unmarshal from both
// {"uid": 123456}
// and
// {"uid": "123456"}
type Int64String int64
func (i Int64String) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%v\"", i)), nil
}
func (i *Int64String) UnmarshalJSON(bytes []byte) error {
if bytes2.HasPrefix(bytes, []byte{'"'}) {
bytes = bytes[1 : len(bytes)-1]
}
m, err := strconv.ParseInt(string(bytes), 10, 64)
if err != nil {
return err
}
*i = Int64String(m)
return nil
}
func (i Int64String) Int64() int64 { return int64(i) }
func (i Int64String) Int32() int32 { return int32(i) }
func (i Int64String) Int16() int16 { return int16(i) }
func (i Int64String) Int8() int8 { return int8(i) }
func (i Int64String) Int() int { return int(i) }