github.com/Team-Kujira/tendermint@v0.34.24-indexer/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 return nil 65 } 66 67 // Tags tests JSON tags. 68 type Tags struct { 69 JSONName string `json:"name"` 70 OmitEmpty string `json:",omitempty"` 71 Hidden string `json:"-"` 72 Tags *Tags `json:"tags,omitempty"` 73 } 74 75 // Struct tests structs with lots of contents. 76 type Struct struct { 77 Bool bool 78 Float64 float64 79 Int32 int32 80 Int64 int64 81 Int64Ptr *int64 82 String string 83 StringPtrPtr **string 84 Bytes []byte 85 Time time.Time 86 Car *Car 87 Boat Boat 88 Vehicles []Vehicle 89 Child *Struct 90 private string 91 }