Closed
Description
package main
import (
"encoding/json"
"fmt"
"net"
)
var input = `
{
"network": "127.0.0.1/24"
}
`
type A struct {
Network net.IPNet `json:"network"`
}
func main() {
var val A
if err := json.Unmarshal([]byte(input), &val); err != nil {
panic(err)
}
fmt.Println(val)
}
Fails with "panic: json: cannot unmarshal string into Go value of type net.IPNet".
go version go1.5.1 linux/amd64
Activity
[-]enable JSON encoding/ decoding of net.NetIP[/-][+]net: enable JSON encoding/decoding of net.NetIP[/+][-]net: enable JSON encoding/decoding of net.NetIP[/-][+]net: net.IPNet should implement encoding.TextMarshaler and TextUnmarshaler[/+]adg commentedon Oct 2, 2015
net.IP
implementsencoding.TextUnmarshaler
andencoding.TextMarshaler
, sonet.IPNet
could too.net.IPNet
is a combination ofnet.IP
andnet.IPMask
. The latter does not implement the encoding interfaces, so maybeIPMask
should be changed also.nerdatmath commentedon Oct 13, 2015
What should a
net.IPMask
look like as a JSON string? I see a few options.IPMask.String
- always a hex representation of the maskIPNet.String
after the slash (a decimal integer or hex representation depending on whether the mask is a valid CIDR or not)IPNet.String
, including the slash and following characters (always start with a slash).Then, independently of the above, should
net.IPNet
marshal/unmarshal a format matchingIPNet.String
(as @gucki requested), or just a JSON expansion of theIPNet
struct, such as this:rsc commentedon Oct 23, 2015
Too late, I'm afraid. That would change the current encodings.
http://play.golang.org/p/DY7Juqs9vT
gucki commentedon Oct 24, 2015
@rsc Sorry, I don't get why parsing "127.0.0.1/24" into a net.IPNET could not be implemented anyway?