package zwavejs import ( "bytes" "encoding/json" "errors" "github.com/sirupsen/logrus" ) type AnyTypeType int const ( AnyTypeString AnyTypeType = iota AnyTypeInt AnyTypeBool AnyTypeList ) type AnyType struct { Type AnyTypeType String string Int int Bool bool List []AnyType } func (n *AnyType) UnmarshalJSON(data []byte) error { if bytes.HasPrefix(data, []byte("\"")) { n.Type = AnyTypeString return json.Unmarshal(data, &n.String) } if bytes.Equal(data, []byte("true")) || bytes.Equal(data, []byte("false")) { n.Type = AnyTypeBool return json.Unmarshal(data, &n.Bool) } if bytes.HasPrefix(data, []byte("[")) { n.Type = AnyTypeList return json.Unmarshal(data, &n.List) } n.Type = AnyTypeInt if err := json.Unmarshal(data, &n.Int); err != nil { logrus.WithField("value", string(data)).Debug("error while parsing node property value of ambiguous type") return err } return nil } func (n AnyType) MarshalJSON() ([]byte, error) { switch n.Type { case AnyTypeString: return json.Marshal(n.String) case AnyTypeBool: return json.Marshal(n.Bool) case AnyTypeList: return json.Marshal(n.List) case AnyTypeInt: return json.Marshal(n.Int) default: return nil, errors.New("anytype of unknown type") } }