Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public void serialize(InetSocketAddress value, JsonGenerator g, SerializerProvid
} else { // otherwise use name
str = str.substring(0, ix);
}
} else if (addr == null && str.indexOf(':') >= 0 && !str.startsWith("[")) {
// Unresolved address exposes no `InetAddress`, so an IPv6 literal has to be
// recognized from the host name; without brackets the port is not separable
str = "[" + str + "]";
}

g.writeString(str + ":" + value.getPort());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,30 @@ public void testInetSocketAddress() throws IOException
MAPPER.writeValueAsString(new InetSocketAddress("2001:db8:85a3:8d3:1319:8a2e:370:7348", 443)));
}

// Unresolved addresses expose no `InetAddress`, so the IPv6 literal has to be
// bracketed based on the host name alone; otherwise the port is not separable.
@Test
public void testInetSocketAddressUnresolvedIPv6() throws IOException
{
final String ip6 = "2001:db8:85a3:8d3:1319:8a2e:370:7348";

assertEquals(q("[" + ip6 + "]:443"),
MAPPER.writeValueAsString(InetSocketAddress.createUnresolved(ip6, 443)));
// Already bracketed host name must not be bracketed twice
assertEquals(q("[" + ip6 + "]:443"),
MAPPER.writeValueAsString(InetSocketAddress.createUnresolved("[" + ip6 + "]", 443)));

// Deserialization yields unresolved addresses, so a value read back has to
// serialize into something that reads back the same way
InetSocketAddress addr = MAPPER.readValue(q(ip6), InetSocketAddress.class);
String json = MAPPER.writeValueAsString(addr);
assertEquals(q("[" + ip6 + "]:0"), json);

InetSocketAddress rt = MAPPER.readValue(json, InetSocketAddress.class);
assertEquals(0, rt.getPort());
assertEquals(json, MAPPER.writeValueAsString(rt));
}

// [JACKSON-597]
@Test
public void testClass() throws IOException
Expand Down
Loading