nfsense/internal/definitions/ipnet.go
2023-03-26 18:50:18 +02:00

30 lines
452 B
Go

package definitions
import (
"encoding/json"
"net"
)
type IPNet struct {
net.IPNet
}
// MarshalJSON for IPNet
func (i IPNet) MarshalJSON() ([]byte, error) {
return json.Marshal(i.String())
}
// UnmarshalJSON for IPNet
func (i *IPNet) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
_, ipnet, err := net.ParseCIDR(s)
if err != nil {
return err
}
i.IPNet = *ipnet
return nil
}