Documentation ¶
Overview ¶
Package j2n allows arbitrary JSON to be marshaled into structs. Any JSON fields that are not marshaled directly into the fields of the struct are put into a field with type UnknownFields
map[string]*json_helpers.RawMessage
This means that fields that are not explicitly named in the struct will survive an Unmarshal/Marshal round trip.
To avoid recursive calls to MarshalJSON/UnmarshalJSON, use the following pattern:
type CatData struct { Name string `json_helpers:"name"` Rest UnknownFields `json_helpers:"-"` } type Cat struct { CatData } func (c *Cat) UnmarshalJSON(data []byte) error { return j2n.UnmarshalJSON(data, &c.CatData) } func (c Cat) MarshalJSON() ([]byte, error) { return j2n.MarshalJSON(c.CatData) }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MarshalJSON ¶
MarshalJSON returns the JSON encoding of v, which must be a struct.
This behaves exactly like json.Marshal, but ensures that any extra fields mentioned in v.Overflow are output alongside the explicitly named struct fields.
It expects v to contain a field named 'Overflow' of type
map[string]*json.RawMessage
func UnmarshalJSON ¶
UnmarshalJSON parses the JSON-encoded data into the struct pointed to by v.
This behaves exactly like json.Unmarshal, but any extra JSON fields that are not explicitly named in the struct are unmarshalled in the 'Overflow' field.
The struct v must contain a field 'Overflow' of type
map[string]*json.RawMessage
Types ¶
type UnknownFields ¶
type UnknownFields map[string]*json.RawMessage