Documentation ¶
Overview ¶
Package jsontime gives simple marshalling directly to and from time objects
This is a simple package that provides wrapper types for time.Time and time.Duration that can be marshalled to and from json and yaml.
time.Time already can marshal to and from json, but not yaml. Whereas time.Duration cannot marshal to and from either. These wrapper types fill in this gap.
This is extremely helpful for configuration and REST apis communicating via json. For example
Unmarshalling yaml config:
type Config struct { StartTime jsontime.StartTime `yaml:"startTime"` RotationPeriod jsontime.Duration `yaml:"retryPeriod"` } var config Config if err := yaml.Unmarshal(responseBodybody, &config); err != nil { return err } // Get the actual time objects, not the jsontime type aliases startTime := target.StartTime.Time() rotationPeriod := target.RotationPeriod.Duration()
Unmarshalling a response:
type ResponseType struct { RetryPeriod jsontime.Duration `json:"retryPeriod"` } var target ResponseType if err := json.Unmarshal(responseBody, &target); err != nil { // Error captures any parse error on the retryPeriod value // rather than having to do that error check yourself return err } retryPeriod := target.RetryPeriod.Duration()
Marshalling a json request/response:
type RequestBody struct { RefreshRate jsontime.Duration `json:"refreshRate"` } requestBody := RequestBody{ RefreshRate: jsontime.Duration(2 * time.Minute), } marshalled, _ := json.Marshal(requestBody) resp, err := http.Do(url, method, bytes.NewBuffer(marshalled))
Index ¶
- func DurationMapstructureDecodeHookFunc(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error)
- type Duration
- func (d Duration) Duration() time.Duration
- func (d Duration) MarshalJSON() ([]byte, error)
- func (d Duration) MarshalYAML() (interface{}, error)
- func (d Duration) String() string
- func (d *Duration) UnmarshalJSON(data []byte) error
- func (d *Duration) UnmarshalYAML(unmarshal func(interface{}) error) error
- type Time
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Duration ¶
Duration is an alias of time.Duration
Can be marshalled to and from json and yaml.
Example (Json) ¶
package main import ( "encoding/json" "fmt" "github.com/anz-bank/sysl-go/jsontime" ) func main() { type MyStruct struct { // Use the yaml tag for marshalling from yaml Duration *jsontime.Duration `mapstructure:"duration"` } const raw = `{"duration": "10m"}` var m MyStruct _ = json.Unmarshal([]byte(raw), &m) fmt.Println(m.Duration) }
Output: 10m0s
Example (Yaml) ¶
type MyStruct struct { // Use the yaml tag for marshalling from yaml Duration *jsontime.Duration `mapstructure:"duration"` } const raw = "duration: 10m" var m MyStruct _ = yaml.Unmarshal([]byte(raw), &m) fmt.Println(m.Duration)
Output: 10m0s
func (Duration) MarshalJSON ¶
MarshalJSON implements json.Marshaller.
func (Duration) MarshalYAML ¶
MarshalYAML implements yaml.Marshaller.
func (*Duration) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaller.
func (*Duration) UnmarshalYAML ¶
UnmarshalYAML implements yaml.Unmarshaller.
type Time ¶
Time is an alias of time.Time.
Can marshal to and from json and yaml.
func (Time) MarshalJSON ¶
MarshalJSON implements json.Marshaller.
func (Time) MarshalYAML ¶
MarshalYAML implements yaml.Marshaller.
func (*Time) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaller.
func (*Time) UnmarshalYAML ¶
UnmarshalYAML implements yaml.Unmarshaller.