A package of custom types for Gripmock that solves JSON serialization/deserialization problems.
In issue #648, a problem was identified with deserializing string time values into time.Duration. When trying to use JSON like:
{
"output": {
"delay": "100ms"
}
}The following error occurred:
json: cannot unmarshal number { into Go struct field Output.Delay of type time.Duration
The types package provides a custom Duration type that correctly handles string time values in JSON.
A custom type alias for time.Duration that provides JSON marshaling/unmarshaling support for string values.
Only string values are supported:
{
"delay": "100ms"
}Valid duration formats:
"100ms"- milliseconds"2s"- seconds"1m"- minutes"1h"- hours"1h30m"- combined formats
type Output struct {
Delay types.Duration `json:"delay"`
Data map[string]interface{} `json:"data"`
}
var output Output
json.Unmarshal([]byte(`{"delay": "100ms"}`), &output)
// Access the duration value
duration := time.Duration(output.Delay)go get github.com/gripmock/typesgo test ./...MIT License - see LICENSE file.