Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: net.IPNet should implement encoding.TextMarshaler and TextUnmarshaler #12803

Closed
gucki opened this issue Oct 1, 2015 · 4 comments

Comments

Projects
None yet
5 participants
@gucki
Copy link

commented Oct 1, 2015

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

@rakyll rakyll changed the title enable JSON encoding/ decoding of net.NetIP net: enable JSON encoding/decoding of net.NetIP Oct 1, 2015

@adg adg changed the title net: enable JSON encoding/decoding of net.NetIP net: net.IPNet should implement encoding.TextMarshaler and TextUnmarshaler Oct 2, 2015

@adg

This comment has been minimized.

Copy link
Contributor

commented Oct 2, 2015

net.IP implements encoding.TextUnmarshaler and encoding.TextMarshaler, so net.IPNet could too.

net.IPNet is a combination of net.IP and net.IPMask. The latter does not implement the encoding interfaces, so maybe IPMask should be changed also.

@nerdatmath

This comment has been minimized.

Copy link
Contributor

commented Oct 13, 2015

What should a net.IPMask look like as a JSON string? I see a few options.

  • Match IPMask.String - always a hex representation of the mask
  • Match IPNet.String after the slash (a decimal integer or hex representation depending on whether the mask is a valid CIDR or not)
  • Match 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 matching IPNet.String (as @gucki requested), or just a JSON expansion of the IPNet struct, such as this:

{ "IP": "127.0.0.1", "Mask": "8" }
@rsc

This comment has been minimized.

Copy link
Contributor

commented Oct 23, 2015

Too late, I'm afraid. That would change the current encodings.

http://play.golang.org/p/DY7Juqs9vT

package main

import (
    "encoding/json"
    "fmt"
    "log"
    "net"
)

func main() {
    js, err := json.Marshal(net.IPNet{})
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(string(js))
}

@rsc rsc closed this Oct 23, 2015

@gucki

This comment has been minimized.

Copy link
Author

commented 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?

@golang golang locked and limited conversation to collaborators Oct 24, 2016

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.