Skip to content
This repository has been archived by the owner on Jan 20, 2025. It is now read-only.

Commit

Permalink
Fixed #50
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 2, 2014
1 parent 7b5c7b7 commit f883c5f
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 19 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
@@ -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`.

------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<HostAndPort>
public class HostAndPortDeserializer extends FromStringDeserializer<HostAndPort>
{
private static final long serialVersionUID = 1L;

Expand All @@ -22,8 +20,9 @@ public class HostAndPortDeserializer extends StdDeserializer<HostAndPort>
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");
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<InternetDomainName>
{
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
public class TestMultisets extends ModuleTestBase
{

/*
/**********************************************************************
/* Unit tests for verifying handling in absence of module registration
Expand Down

0 comments on commit f883c5f

Please sign in to comment.