You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
1.9 KiB

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