Skip to content

proposal: net: add MarshalText/UnmarshalText to IPMask #40436

@ChrisSwanson

Description

@ChrisSwanson

What version of Go are you using (go version)?

$ go version
1.14.6

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/cswanson/Library/Caches/go-build"
GOENV="/Users/cswanson/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/cswanson/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/cswanson/go/src/github.com/golang/go/src/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/wb/vtqrcvns4h7ghfrl_m75_z7c0000gn/T/go-build009589323=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I'm trying to json marshal and unmarshal net.IPNet objects, but the net.IPMask object currently does not support marshalling to text formats in a human readable/parsable format. When a net.IPMask object is attempted to be json marshalled, it is currently being transformed from a byte slice to a base64 string (as per the encode/json standards).

Here's an example:
https://play.golang.org/p/EGjSJeVQ9W-

Right now I am working around this, through type aliasing and methods, but it would be nice if this were either in its hex format, that it is displayed as in when using the String method to keep with backwards compatibility.
This seems to be a somewhat known issue to some regard as it is mentioned within a previous issue, although not yet followed up upon from what I was able to see: #29678 (comment)
Ideally in a future update, it would be great if the IPMask String method and potentially this text marshal were in a more well known format such as dot-decimal notation, for IPv4 instead of encoded to hex, as it is currently displayed, but that sounds like another issue aside from the json marshaling issue I am currently filing for.

What did you expect to see?

I expect net.IPMask to be marshaled to a text format, and consistent with net.IP and enable net.IPNet to be json marshaled leaving all objects in a human readable/parsable format.

What did you see instead?

A json marshaled version of these items in a non human readable fashion (base64 encoded translation of the byte slice).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions