Most of us know that a JSON object can be unmarshaled with JSON tags:
var jsonData = `{"name": "Brown Bear"}`
type Elephant struct {
Name string `json:"name"`
}
This is because string
is a built in types. But what if Name
were not a built in type, and we wanted to use this type across different structs?
var jsonData = `{"name": "Brown Bear"}`
type Elephant struct {
Name Name `json:"name"` // Unmarshalling fails here
}
type Feline struct {
Name Name `json:"name"` // Unmarshalling fails here
}
type Bear struct {
Name Name `json:"name"` // Unmarshalling fails here
}
type Name struct {
CommonName string
ScientificName string // This field should intentionally be blank
}
Is there a way we can define the Name
type so that the json
unmarshaller knows how to unmarshal it?
PS: The solution I want to avoid is creating UnmarshalJSON
methods for Elephant
, Feline
, and Bear
above. It would be better to create a method just for the Name
type.