diff --git a/release-notes/VERSION b/release-notes/VERSION index f6da555..ef9c4c6 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION @@ -1,6 +1,8 @@ Project: jackson-datatype-guava Version: 2.4.3 (xx-xxx-2014) +#50: Add support for `InternetDomainName` + (suggested by sdavids@github) - Improved serialization, type handling, schema-access for `Range` and `Optional`. ------------------------------------------------------------------------ diff --git a/src/main/java/com/fasterxml/jackson/datatype/guava/GuavaDeserializers.java b/src/main/java/com/fasterxml/jackson/datatype/guava/GuavaDeserializers.java index 71bd9b0..673704a 100644 --- a/src/main/java/com/fasterxml/jackson/datatype/guava/GuavaDeserializers.java +++ b/src/main/java/com/fasterxml/jackson/datatype/guava/GuavaDeserializers.java @@ -1,5 +1,10 @@ package com.fasterxml.jackson.datatype.guava; +import com.google.common.base.Optional; +import com.google.common.collect.*; +import com.google.common.net.HostAndPort; +import com.google.common.net.InternetDomainName; + import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.deser.Deserializers; import com.fasterxml.jackson.databind.jsontype.TypeDeserializer; @@ -12,9 +17,6 @@ import com.fasterxml.jackson.datatype.guava.deser.multimap.list.LinkedListMultimapDeserializer; import com.fasterxml.jackson.datatype.guava.deser.multimap.set.HashMultimapDeserializer; import com.fasterxml.jackson.datatype.guava.deser.multimap.set.LinkedHashMultimapDeserializer; -import com.google.common.base.Optional; -import com.google.common.collect.*; -import com.google.common.net.HostAndPort; /** * Custom deserializers module offers. @@ -232,6 +234,9 @@ public JsonDeserializer findBeanDeserializer(final JavaType type, Deserializa if (raw == HostAndPort.class) { return HostAndPortDeserializer.std; } + if (raw == InternetDomainName.class) { + return InternetDomainNameDeserializer.std; + } return super.findBeanDeserializer(type, config, beanDesc); } } diff --git a/src/main/java/com/fasterxml/jackson/datatype/guava/GuavaSerializers.java b/src/main/java/com/fasterxml/jackson/datatype/guava/GuavaSerializers.java index 41d1842..d192df4 100644 --- a/src/main/java/com/fasterxml/jackson/datatype/guava/GuavaSerializers.java +++ b/src/main/java/com/fasterxml/jackson/datatype/guava/GuavaSerializers.java @@ -6,6 +6,7 @@ import com.google.common.collect.Multimap; import com.google.common.collect.Range; import com.google.common.net.HostAndPort; +import com.google.common.net.InternetDomainName; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import com.fasterxml.jackson.databind.ser.Serializers; @@ -31,6 +32,10 @@ public JsonSerializer findSerializer(SerializationConfig config, JavaType typ if (HostAndPort.class.isAssignableFrom(raw)) { return ToStringSerializer.instance; } + // since 2.4.3 + if (InternetDomainName.class.isAssignableFrom(raw)) { + return ToStringSerializer.instance; + } // not sure how useful, but why not? if (CacheBuilderSpec.class.isAssignableFrom(raw) || CacheBuilder.class.isAssignableFrom(raw)) { return ToStringSerializer.instance; diff --git a/src/main/java/com/fasterxml/jackson/datatype/guava/deser/HostAndPortDeserializer.java b/src/main/java/com/fasterxml/jackson/datatype/guava/deser/HostAndPortDeserializer.java index 09aa262..e1d8f77 100644 --- a/src/main/java/com/fasterxml/jackson/datatype/guava/deser/HostAndPortDeserializer.java +++ b/src/main/java/com/fasterxml/jackson/datatype/guava/deser/HostAndPortDeserializer.java @@ -3,14 +3,12 @@ import java.io.IOException; import com.fasterxml.jackson.core.*; - import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.deser.std.StdDeserializer; - +import com.fasterxml.jackson.databind.deser.std.FromStringDeserializer; import com.google.common.net.HostAndPort; -public class HostAndPortDeserializer extends StdDeserializer +public class HostAndPortDeserializer extends FromStringDeserializer { private static final long serialVersionUID = 1L; @@ -22,8 +20,9 @@ public class HostAndPortDeserializer extends StdDeserializer public HostAndPort deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { - JsonToken t = jp.getCurrentToken(); - if (t == JsonToken.START_OBJECT) { // old style + // 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(); JsonNode n = root.get("port"); @@ -32,11 +31,12 @@ public HostAndPort deserialize(JsonParser jp, DeserializationContext ctxt) } return HostAndPort.fromParts(host, n.asInt()); } - if (t == JsonToken.VALUE_STRING) { - return HostAndPort.fromString(jp.getText().trim()); - } - // could also support arrays? - throw ctxt.wrongTokenException(jp, JsonToken.VALUE_STRING, "(or JSON Object)"); + return super.deserialize(jp, ctxt); } + @Override + protected HostAndPort _deserialize(String value, DeserializationContext ctxt) + throws IOException { + return HostAndPort.fromString(value); + } } diff --git a/src/main/java/com/fasterxml/jackson/datatype/guava/deser/InternetDomainNameDeserializer.java b/src/main/java/com/fasterxml/jackson/datatype/guava/deser/InternetDomainNameDeserializer.java new file mode 100644 index 0000000..9fb2100 --- /dev/null +++ b/src/main/java/com/fasterxml/jackson/datatype/guava/deser/InternetDomainNameDeserializer.java @@ -0,0 +1,24 @@ +package com.fasterxml.jackson.datatype.guava.deser; + +import java.io.IOException; + +import com.google.common.net.HostAndPort; +import com.google.common.net.InternetDomainName; + +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.deser.std.FromStringDeserializer; + +public class InternetDomainNameDeserializer extends FromStringDeserializer +{ + private static final long serialVersionUID = 1L; + + public final static InternetDomainNameDeserializer std = new InternetDomainNameDeserializer(); + + public InternetDomainNameDeserializer() { super(HostAndPort.class); } + + @Override + protected InternetDomainName _deserialize(String value, DeserializationContext ctxt) + throws IOException { + return InternetDomainName.from(value); + } +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/guava/HostAndPortTest.java b/src/test/java/com/fasterxml/jackson/datatype/guava/HostAndPortTest.java index d562363..e409d1c 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/guava/HostAndPortTest.java +++ b/src/test/java/com/fasterxml/jackson/datatype/guava/HostAndPortTest.java @@ -30,12 +30,12 @@ public void testDeserialization() throws Exception assertEquals("localhost", result.getHostText()); assertEquals(7070, result.getPort()); - // and ... error + // and ... error (note: numbers, booleans may all be fine) try { - MAPPER.readValue("false", HostAndPort.class); - fail("Should not deserialize from boolean"); + result = MAPPER.readValue("[ ]", HostAndPort.class); + fail("Should not deserialize from boolean: got "+result); } catch (JsonProcessingException e) { - verifyException(e, "Unexpected token"); + verifyException(e, "Can not deserialize"); } } } diff --git a/src/test/java/com/fasterxml/jackson/datatype/guava/ScalarTypesTest.java b/src/test/java/com/fasterxml/jackson/datatype/guava/ScalarTypesTest.java new file mode 100644 index 0000000..9835e99 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/guava/ScalarTypesTest.java @@ -0,0 +1,25 @@ +package com.fasterxml.jackson.datatype.guava; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.net.InternetDomainName; + +public class ScalarTypesTest extends ModuleTestBase +{ + private final ObjectMapper MAPPER = mapperWithModule(); + + public void testInternetDomainNameSerialization() throws Exception + { + final String INPUT = "google.com"; + InternetDomainName name = InternetDomainName.from(INPUT); + assertEquals(quote(INPUT), MAPPER.writeValueAsString(name)); + } + + public void testInternetDomainNameDeserialization() throws Exception + { + final String INPUT = "google.com"; +// InternetDomainName name = MAPPER.readValue(quote(INPUT), InternetDomainName.class); + InternetDomainName name = new ObjectMapper().readValue(quote(INPUT), InternetDomainName.class); + assertNotNull(name); + assertEquals(INPUT, name.toString()); + } +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/guava/TestMultisets.java b/src/test/java/com/fasterxml/jackson/datatype/guava/TestMultisets.java index 07c23c5..7e98b90 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/guava/TestMultisets.java +++ b/src/test/java/com/fasterxml/jackson/datatype/guava/TestMultisets.java @@ -13,7 +13,6 @@ */ public class TestMultisets extends ModuleTestBase { - /* /********************************************************************** /* Unit tests for verifying handling in absence of module registration