Skip to content

Commit

Permalink
Fix #45
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Apr 6, 2019
1 parent fa0d16b commit 680d347
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,23 @@ public class HostAndPortDeserializer extends FromStringDeserializer<HostAndPort>
public HostAndPortDeserializer() { super(HostAndPort.class); }

@Override
public HostAndPort deserialize(JsonParser jp, DeserializationContext ctxt)
public HostAndPort deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException
{
// Need to override this method, which otherwise would work just fine,
// since we have legacy JSON Object format to support too:
if (jp.getCurrentToken() == JsonToken.START_OBJECT) { // old style
JsonNode root = jp.readValueAsTree();
String host = root.path("hostText").asText();
if (p.hasToken(JsonToken.START_OBJECT)) { // old style
JsonNode root = p.readValueAsTree();
// [datatypes-collections#45]: we actually have 2 possibilities depending on Guava version
JsonNode hostNode = root.get("host");
final String host = (hostNode == null) ? root.path("hostText").asText() : hostNode.textValue();
JsonNode n = root.get("port");
if (n == null) {
return HostAndPort.fromString(host);
}
return HostAndPort.fromParts(host, n.asInt());
}
return super.deserialize(jp, ctxt);
return super.deserialize(p, ctxt);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public void testDeserialization() throws Exception
assertEquals("localhost", result.getHostText());
assertEquals(9090, result.getPort());

// and Alt Old too:
result = MAPPER.readValue(aposToQuotes("{'port':8080, 'host':'foobar.com'}"),
HostAndPort.class);
assertEquals("foobar.com", result.getHostText());
assertEquals(8080, result.getPort());

// and new:
result = MAPPER.readValue(quote("localhost:7070"), HostAndPort.class);
assertEquals("localhost", result.getHostText());
Expand Down

0 comments on commit 680d347

Please sign in to comment.