custom json ser
This commit is contained in:
@@ -19,25 +19,24 @@ type Test struct {
|
|||||||
Bool bool `json:"bool"`
|
Bool bool `json:"bool"`
|
||||||
BoolInStr bool `json:"bool_in_str,string"`
|
BoolInStr bool `json:"bool_in_str,string"`
|
||||||
Long int64 `json:"long"`
|
Long int64 `json:"long"`
|
||||||
Long2 TheLong `json:"long2"`
|
Long2 *TheLong `json:"long2"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://www.cnblogs.com/yorkyang/p/8990570.html
|
// https://www.cnblogs.com/yorkyang/p/8990570.html
|
||||||
func (l *TheLong) MarshalJSON() (data []byte, err error) {
|
func (l *TheLong) MarshalJSON() (data []byte, err error) {
|
||||||
fmt.Println("------")//??
|
|
||||||
if l != nil {
|
if l != nil {
|
||||||
data = []byte(strconv.FormatInt(l.Value, 10))
|
data = []byte("\""+strconv.FormatInt(l.Value, 10) + "\"")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
func (l *TheLong) UnmarshalJSON(data []byte) error {
|
// func (l *TheLong) UnmarshalJSON(data []byte) error {
|
||||||
v, err := strconv.ParseInt(string(data), 10, 64)
|
// v, err := strconv.ParseInt(string(data), 10, 64)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
l.Value = v
|
// l.Value = v
|
||||||
return nil
|
// return nil
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -50,7 +49,7 @@ func main() {
|
|||||||
Bool: true,
|
Bool: true,
|
||||||
BoolInStr: true,
|
BoolInStr: true,
|
||||||
Long: 342438204823084023,
|
Long: 342438204823084023,
|
||||||
Long2: TheLong { Value: 1111111111111111111 },
|
Long2: &TheLong { Value: 342438204823084023 },
|
||||||
}
|
}
|
||||||
s, err := json.Marshal(t)
|
s, err := json.Marshal(t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user