github.com/516108736/tendermint@v0.36.0/libs/json/helpers_test.go (about) 1 package json_test 2 3 import ( 4 "time" 5 6 "github.com/tendermint/tendermint/libs/json" 7 ) 8 9 // Register Car, an instance of the Vehicle interface. 10 func init() { 11 json.RegisterType(&Car{}, "vehicle/car") 12 json.RegisterType(Boat{}, "vehicle/boat") 13 json.RegisterType(PublicKey{}, "key/public") 14 json.RegisterType(PrivateKey{}, "key/private") 15 } 16 17 type Vehicle interface { 18 Drive() error 19 } 20 21 // Car is a pointer implementation of Vehicle. 22 type Car struct { 23 Wheels int32 24 } 25 26 func (c *Car) Drive() error { return nil } 27 28 // Boat is a value implementation of Vehicle. 29 type Boat struct { 30 Sail bool 31 } 32 33 func (b Boat) Drive() error { return nil } 34 35 // These are public and private encryption keys. 36 type PublicKey [8]byte 37 type PrivateKey [8]byte 38 39 // Custom has custom marshalers and unmarshalers, taking pointer receivers. 40 type CustomPtr struct { 41 Value string 42 } 43 44 func (c *CustomPtr) MarshalJSON() ([]byte, error) { 45 return []byte("\"custom\""), nil 46 } 47 48 func (c *CustomPtr) UnmarshalJSON(bz []byte) error { 49 c.Value = "custom" 50 return nil 51 } 52 53 // CustomValue has custom marshalers and unmarshalers, taking value receivers (which usually doesn't 54 // make much sense since the unmarshaler can't change anything). 55 type CustomValue struct { 56 Value string 57 } 58 59 func (c CustomValue) MarshalJSON() ([]byte, error) { 60 return []byte("\"custom\""), nil 61 } 62 63 func (c CustomValue) UnmarshalJSON(bz []byte) error { 64 c.Value = "custom" 65 return nil 66 } 67 68 // Tags tests JSON tags. 69 type Tags struct { 70 JSONName string `json:"name"` 71 OmitEmpty string `json:",omitempty"` 72 Hidden string `json:"-"` 73 Tags *Tags `json:"tags,omitempty"` 74 } 75 76 // Struct tests structs with lots of contents. 77 type Struct struct { 78 Bool bool 79 Float64 float64 80 Int32 int32 81 Int64 int64 82 Int64Ptr *int64 83 String string 84 StringPtrPtr **string 85 Bytes []byte 86 Time time.Time 87 Car *Car 88 Boat Boat 89 Vehicles []Vehicle 90 Child *Struct 91 private string 92 }