Skip to content

encoding/json: Marshal of nil net.IP fails #6339

@gopherbot

Description

@gopherbot

by liviobs:

What steps will reproduce the problem?
If possible, include a link to a program on play.golang.org.
1. Marshalling an empty net.IP used to work. For example:
http://play.golang.org/p/JQS_wUkDNr
2. With 'tip' version of golang, it fails.

What is the expected output?

null <nil>

What do you see instead?

 json: error calling MarshalJSON for type net.IP: invalid IP address


Which compiler are you using (5g, 6g, 8g, gccgo)?

6g

Which operating system are you using?

Linux

Which version are you using?  (run 'go version')

go version devel +a71616f65cb1 Tue Sep 03 21:23:52 2013 -0700 linux/amd64

Please provide any additional information below.

It looks like the following check is what changed the behavior (from changeset
17745:7c4368941249)

src/pkg/net/ip.go:

+// MarshalText implements the encoding.TextMarshaler interface.
+// The encoding is the same as returned by String.
+func (ip IP) MarshalText() ([]byte, error) {
+       if len(ip) != IPv4len && len(ip) != IPv6len {
+               return nil, errors.New("invalid IP address")
+       }

With an empty net.IP len(ip) is 0.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions