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.

92 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. c.Value = "custom"
  50. return nil
  51. }
  52. // Tags tests JSON tags.
  53. type Tags struct {
  54. JSONName string `json:"name"`
  55. OmitEmpty string `json:",omitempty"`
  56. Hidden string `json:"-"`
  57. Tags *Tags `json:"tags,omitempty"`
  58. }
  59. // Struct tests structs with lots of contents.
  60. type Struct struct {
  61. Bool bool
  62. Float64 float64
  63. Int32 int32
  64. Int64 int64
  65. Int64Ptr *int64
  66. String string
  67. StringPtrPtr **string
  68. Bytes []byte
  69. Time time.Time
  70. Car *Car
  71. Boat Boat
  72. Vehicles []Vehicle
  73. Child *Struct
  74. private string
  75. }