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

Commit

Permalink
Fixed #29
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 22, 2013
1 parent e01df0a commit 3d0f6a2
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 34 deletions.
6 changes: 4 additions & 2 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
Project: jackson-datatype-guava
Version: 2.2.3 (25-Aug-2013)
Version: 2.3.0 (xx-Oct-2013)

No functional changes.
#29: Empty ImmutableMap not deserialized correctly, when type info included
(reported by pbergn@github)

------------------------------------------------------------------------
=== History: ===
------------------------------------------------------------------------

2.2.3 (25-Aug-2013)
2.2.2 (27-May-2013)
2.2.1 (03-May-2013)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ public Object deserializeWithType(JsonParser jp, DeserializationContext ctxt,
TypeDeserializer typeDeserializer)
throws IOException, JsonProcessingException
{
return typeDeserializer.deserializeTypedFromArray(jp, ctxt);
// note: call "...FromObject" because expected output structure
// for value is JSON Object (regardless of contortions used for type id)
return typeDeserializer.deserializeTypedFromObject(jp, ctxt);
}

@Override
Expand All @@ -115,10 +117,8 @@ public T deserialize(JsonParser jp, DeserializationContext ctxt)
JsonToken t = jp.getCurrentToken();
if (t == JsonToken.START_OBJECT) { // If START_OBJECT, move to next; may also be END_OBJECT
t = jp.nextToken();
if (t != JsonToken.FIELD_NAME && t != JsonToken.END_OBJECT) {
throw ctxt.mappingException(_mapType.getRawClass());
}
} else if (t != JsonToken.FIELD_NAME) {
}
if (t != JsonToken.FIELD_NAME && t != JsonToken.END_OBJECT) {
throw ctxt.mappingException(_mapType.getRawClass());
}
return _deserializeEntries(jp, ctxt);
Expand All @@ -132,11 +132,4 @@ public T deserialize(JsonParser jp, DeserializationContext ctxt)

protected abstract T _deserializeEntries(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException;

/*
/**********************************************************************
/* Helper methods
/**********************************************************************
*/

}
113 changes: 93 additions & 20 deletions src/test/java/com/fasterxml/jackson/datatype/guava/TestImmutables.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Iterator;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
Expand All @@ -17,6 +18,18 @@
*/
public class TestImmutables extends BaseTest
{
private final ObjectMapper MAPPER = mapperWithModule();

static class Holder {
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
public Object value;

public Holder() { }
public Holder(Object v) {
value = v;
}
}

/*
/**********************************************************************
/* Unit tests for verifying handling in absence of module registration
Expand All @@ -31,16 +44,15 @@ public void testWithoutSerializers() throws Exception
{
ImmutableList<Integer> list = ImmutableList.<Integer>builder()
.add(1).add(2).add(3).build();
ObjectMapper mapper = new ObjectMapper();
assertEquals("[1,2,3]", mapper.writeValueAsString(list));
assertEquals("[1,2,3]", MAPPER.writeValueAsString(list));

ImmutableSet<String> set = ImmutableSet.<String>builder()
.add("abc").add("def").build();
assertEquals("[\"abc\",\"def\"]", mapper.writeValueAsString(set));
assertEquals("[\"abc\",\"def\"]", MAPPER.writeValueAsString(set));

ImmutableMap<String,Integer> map = ImmutableMap.<String,Integer>builder()
.put("a", 1).put("b", 2).build();
assertEquals("{\"a\":1,\"b\":2}", mapper.writeValueAsString(map));
assertEquals("{\"a\":1,\"b\":2}", MAPPER.writeValueAsString(map));
}

/**
Expand All @@ -49,7 +61,6 @@ public void testWithoutSerializers() throws Exception
public void testWithoutDeserializers() throws Exception
{
ObjectMapper mapper = new ObjectMapper();

try {
mapper.readValue("[1,2,3]",
new TypeReference<ImmutableList<Integer>>() { });
Expand Down Expand Up @@ -88,8 +99,7 @@ public void testWithoutDeserializers() throws Exception

public void testImmutableList() throws Exception
{
ObjectMapper mapper = mapperWithModule();
ImmutableList<Integer> list = mapper.readValue("[1,2,3]", new TypeReference<ImmutableList<Integer>>() { });
ImmutableList<Integer> list = MAPPER.readValue("[1,2,3]", new TypeReference<ImmutableList<Integer>>() { });
assertEquals(3, list.size());
assertEquals(Integer.valueOf(1), list.get(0));
assertEquals(Integer.valueOf(2), list.get(1));
Expand All @@ -98,19 +108,54 @@ public void testImmutableList() throws Exception

public void testImmutableSet() throws Exception
{
ObjectMapper mapper = mapperWithModule();
ImmutableSet<Integer> set = mapper.readValue("[3,7,8]", new TypeReference<ImmutableSet<Integer>>() { });
ImmutableSet<Integer> set = MAPPER.readValue("[3,7,8]",
new TypeReference<ImmutableSet<Integer>>() { });
assertEquals(3, set.size());
Iterator<Integer> it = set.iterator();
assertEquals(Integer.valueOf(3), it.next());
assertEquals(Integer.valueOf(7), it.next());
assertEquals(Integer.valueOf(8), it.next());

set = MAPPER.readValue("[ ]",
new TypeReference<ImmutableSet<Integer>>() { });
assertEquals(0, set.size());
}

public void testTypedImmutableset() throws Exception
{
ImmutableSet<Integer> set;
Holder h;
String json;
Holder result;

// First, with one entry
set = new ImmutableSet.Builder<Integer>()
.add(1).build();
h = new Holder(set);
json = MAPPER.writeValueAsString(h);

// so far so good. and back?
result = MAPPER.readValue(json, Holder.class);
assertNotNull(result.value);
if (!(result.value instanceof ImmutableSet<?>)) {
fail("Expected ImmutableSet, got "+result.value.getClass());
}
assertEquals(1, ((ImmutableSet<?>) result.value).size());
// and then an empty version:
set = new ImmutableSet.Builder<Integer>().build();
h = new Holder(set);
json = MAPPER.writeValueAsString(h);
result = MAPPER.readValue(json, Holder.class);
assertNotNull(result.value);
if (!(result.value instanceof ImmutableSet<?>)) {
fail("Expected ImmutableSet, got "+result.value.getClass());
}
assertEquals(0, ((ImmutableSet<?>) result.value).size());
}

public void testImmutableSortedSet() throws Exception
{
ObjectMapper mapper = mapperWithModule();
ImmutableSortedSet<Integer> set = mapper.readValue("[5,1,2]", new TypeReference<ImmutableSortedSet<Integer>>() { });
ImmutableSortedSet<Integer> set = MAPPER.readValue("[5,1,2]", new TypeReference<ImmutableSortedSet<Integer>>() { });
assertEquals(3, set.size());
Iterator<Integer> it = set.iterator();
assertEquals(Integer.valueOf(1), it.next());
Expand All @@ -120,32 +165,60 @@ public void testImmutableSortedSet() throws Exception

public void testImmutableMap() throws Exception
{
ObjectMapper mapper = mapperWithModule();
final JavaType type = mapper.getTypeFactory().constructType(new TypeReference<ImmutableMap<Integer,Boolean>>() { });
ImmutableMap<Integer,Boolean> map = mapper.readValue("{\"12\":true,\"4\":false}", type);
final JavaType type = MAPPER.getTypeFactory().constructType(new TypeReference<ImmutableMap<Integer,Boolean>>() { });
ImmutableMap<Integer,Boolean> map = MAPPER.readValue("{\"12\":true,\"4\":false}", type);
assertEquals(2, map.size());
assertEquals(Boolean.TRUE, map.get(Integer.valueOf(12)));
assertEquals(Boolean.FALSE, map.get(Integer.valueOf(4)));

// [Issue#29]
map = mapper.readValue("{}", type);
map = MAPPER.readValue("{}", type);
assertNotNull(map);
assertEquals(0, map.size());
}

public void testTypedImmutableMap() throws Exception
{
ImmutableMap<String,Integer> map;
Holder h;
String json;
Holder result;

// First, with one entry
map = new ImmutableMap.Builder<String,Integer>()
.put("a", 1).build();
h = new Holder(map);
json = MAPPER.writeValueAsString(h);

// so far so good. and back?
result = MAPPER.readValue(json, Holder.class);
assertNotNull(result.value);
if (!(result.value instanceof ImmutableMap<?,?>)) {
fail("Expected ImmutableMap, got "+result.value.getClass());
}
assertEquals(1, ((ImmutableMap<?,?>) result.value).size());
// and then an empty version:
map = new ImmutableMap.Builder<String,Integer>().build();
h = new Holder(map);
json = MAPPER.writeValueAsString(h);
result = MAPPER.readValue(json, Holder.class);
assertNotNull(result.value);
if (!(result.value instanceof ImmutableMap<?,?>)) {
fail("Expected ImmutableMap, got "+result.value.getClass());
}
assertEquals(0, ((ImmutableMap<?,?>) result.value).size());
}

public void testImmutableSortedMap() throws Exception
{
ObjectMapper mapper = mapperWithModule();
ImmutableSortedMap<Integer,Boolean> map = mapper.readValue("{\"12\":true,\"4\":false}", new TypeReference<ImmutableSortedMap<Integer,Boolean>>() { });
ImmutableSortedMap<Integer,Boolean> map = MAPPER.readValue("{\"12\":true,\"4\":false}", new TypeReference<ImmutableSortedMap<Integer,Boolean>>() { });
assertEquals(2, map.size());
assertEquals(Boolean.TRUE, map.get(Integer.valueOf(12)));
assertEquals(Boolean.FALSE, map.get(Integer.valueOf(4)));
}

public void testImmutableBiMap() throws Exception
{
ObjectMapper mapper = mapperWithModule();
ImmutableBiMap<Integer,Boolean> map = mapper.readValue("{\"12\":true,\"4\":false}", new TypeReference<ImmutableBiMap<Integer,Boolean>>() { });
ImmutableBiMap<Integer,Boolean> map = MAPPER.readValue("{\"12\":true,\"4\":false}", new TypeReference<ImmutableBiMap<Integer,Boolean>>() { });
assertEquals(2, map.size());
assertEquals(Boolean.TRUE, map.get(12));
assertEquals(Boolean.FALSE, map.get(4));
Expand Down

0 comments on commit 3d0f6a2

Please sign in to comment.