u
This commit is contained in:
@@ -1,20 +1,45 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"strconv"
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Test struct {
|
type TheLong struct {
|
||||||
Name string `json:"name"`
|
Value int64
|
||||||
Name2 string `json:"name2"`
|
|
||||||
Name3 string `json:"name3,omitempty"`
|
|
||||||
Age int32 `json:"age"`
|
|
||||||
NotSer string `json:"-"`
|
|
||||||
Bool bool `json:"bool"`
|
|
||||||
BoolInStr bool `json:"bool_in_str,string"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Test struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Name2 string `json:"name2"`
|
||||||
|
Name3 string `json:"name3,omitempty"`
|
||||||
|
Age int32 `json:"age"`
|
||||||
|
NotSer string `json:"-"`
|
||||||
|
Bool bool `json:"bool"`
|
||||||
|
BoolInStr bool `json:"bool_in_str,string"`
|
||||||
|
Long int64 `json:"long"`
|
||||||
|
Long2 TheLong `json:"long2"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://www.cnblogs.com/yorkyang/p/8990570.html
|
||||||
|
func (l *TheLong) MarshalJSON() (data []byte, err error) {
|
||||||
|
fmt.Println("------")//??
|
||||||
|
if l != nil {
|
||||||
|
data = []byte(strconv.FormatInt(l.Value, 10))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
func (l *TheLong) UnmarshalJSON(data []byte) error {
|
||||||
|
v, err := strconv.ParseInt(string(data), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
l.Value = v
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
t := Test {
|
t := Test {
|
||||||
Name: "hatter",
|
Name: "hatter",
|
||||||
@@ -24,6 +49,8 @@ func main() {
|
|||||||
NotSer: "not ser!",
|
NotSer: "not ser!",
|
||||||
Bool: true,
|
Bool: true,
|
||||||
BoolInStr: true,
|
BoolInStr: true,
|
||||||
|
Long: 342438204823084023,
|
||||||
|
Long2: TheLong { Value: 1111111111111111111 },
|
||||||
}
|
}
|
||||||
s, err := json.Marshal(t)
|
s, err := json.Marshal(t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user