Command gencodec generates marshaling methods for Go struct types.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Felix Lange c180464f1d improve generated code 8 years ago
internal improve generated code 8 years ago
vendor use go/loader to load type-checked packages 8 years ago
LICENSE initial commit 8 years ago
README.md fix documentation typos 8 years ago
genmethod.go improve generated code 8 years ago
main.go add missing BUG: in panic calls 8 years ago
main_test.go add -formats flag 8 years ago
typeutil.go add missing BUG: in panic calls 8 years ago

README.md

Command gencodec generates marshaling methods for struct types.

When gencodec is invoked on a directory and type name, it creates a Go source file containing JSON and YAML marshaling methods for the type. The generated methods add features which the standard json package cannot offer.

gencodec -dir . -type MyType -out mytype_json.go

Struct Tags

All fields are required unless the "optional" struct tag is present. The generated unmarshaling method returns an error if a required field is missing. Other struct tags are carried over as is. The standard "json" and "yaml" tags can be used to rename a field when marshaling.

Example:

type foo struct {
	Required string
	Optional string `optional:""`
	Renamed  string `json:"otherName"`
}

Field Type Overrides

An invocation of gencodec can specify an additional 'field override' struct from which marshaling type replacements are taken. If the override struct contains a field whose name matches the original type, the generated marshaling methods will use the overridden type and convert to and from the original field type.

In this example, the specialString type implements json.Unmarshaler to enforce additional parsing rules. When json.Unmarshal is used with type foo, the specialString unmarshaler will be used to parse the value of SpecialField.

//go:generate gencodec -dir . -type foo -field-override fooMarshaling -out foo_json.go

type foo struct {
	Field        string
	SpecialField string
}

type fooMarshaling struct {
	SpecialField specialString // overrides type of SpecialField when marshaling/unmarshaling
}