Skip to content

launchdarkly/j2n

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 called Overflow, of type map[string]*json.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:"name"`
	Overflow map[string]*json.RawMessage `json:"-"`
}

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)
}

About

JSON marshal/unmarshal with overflow fields

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%