I have a field in json that is either abc
or def
and I want to make sure that when I unmarshal the data it checks that the field only contains either of the 2 valid values, is there a way to do that in golang without adhoc checking?
I know I can do it if I have the json in byte
const(
Enum1 = "abc"
Enum1 = "def"
)
func (s *MyJsonStruct) UnmarshalJSON(data []byte) error {
type Aux MyJsonStruct;
var a *Aux = (*Aux)(s);
err := json.Unmarshal(data, &a)
if err != nil {
return err
}
if s.Key != Enum1 && s.Key != Enum2 {
s.Key = ""
return errors.New("invalid value for Key")
}
}
How to override UnmarshalJSON
if the input is io.Reader