Documentation ¶
Overview ¶
Package swallowjson provides a utility function for implementing the encoding/json.Unmarshaler interface's UnmarshalJSON method to decode, without discarding unknown keys. The "swallow" concept says that any keys in a JSON object which do not correspond to a known field in a (Golang) struct should not be discarded but instead put into a map.
type MyType struct { Foo string `json:"foo"` Bar int `json:"bar"` Rest map[string]interface{} `json:"-"` } func (mt *MyType) UnmarshalJSON(raw []byte) error { return swallowjson.UnmarshalWith(mt, "Rest", raw) }
The struct field to swallow fields not explicitly named must be a map keyed by string. The type of map values is handled reliably, returning a JSON error if unsuitable. Common types to use might be `interface{}` or `json.RawMessage`.
Errors are either of type swallowjson.SwallowError or are bubbled through from the json or reflect packages.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrGivenNonStringKey = SwallowError{/* contains filtered or unexported fields */} ErrMalformedJSON = SwallowError{/* contains filtered or unexported fields */} ErrMissingSpilloverField = SwallowError{/* contains filtered or unexported fields */} ErrNotGivenMutable = SwallowError{/* contains filtered or unexported fields */} ErrNotGivenStruct = SwallowError{/* contains filtered or unexported fields */} ErrNotStructHolder = SwallowError{/* contains filtered or unexported fields */} ErrSpillNotRightMap = SwallowError{/* contains filtered or unexported fields */} ErrUnsetableSpilloverField = SwallowError{/* contains filtered or unexported fields */} )
These errors may be returned by UnmarshalWith.
Functions ¶
func UnmarshalWith ¶
UnmarshalWith is used for implementing UnmarshalJSON methods to satisfy the encoding/json.Unmarshaler interface. The method becomes a one-liner returning the result of calling this function, supplying in addition to the object and input only the name of the field which should swallow unknown JSON fields.
Types ¶
type SwallowError ¶
type SwallowError struct {
// contains filtered or unexported fields
}
SwallowError is the type of all errors generated within the swallowjson package. Errors will either be of this type, or bubbled through. The string representation of the error is guaranteed to start "swallowjson:".
func (SwallowError) Error ¶
func (se SwallowError) Error() string