Skip to content

Commit

Permalink
Fix #827 with minor modifications
Browse files Browse the repository at this point in the history
cowtowncoder committed Jun 13, 2015
1 parent dd9e751 commit 429f70f
Showing 5 changed files with 48 additions and 17 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
@@ -71,6 +71,8 @@ Project: jackson-databind
`java.util.Calendar` serialization
#826: Replaced synchronized HashMap with ConcurrentHashMap in TypeDeserializerBase._findDeserializer
(contributed by Lars P)
#827: Fix for polymorphic custom map key serializer
(reported by mjr6140@gitgub)
- Remove old cglib compatibility tests; cause problems in Eclipse

2.5.4 (09-Jun-2015)
20 changes: 12 additions & 8 deletions src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java
Original file line number Diff line number Diff line change
@@ -875,11 +875,12 @@ public void writeValue(JsonGenerator gen, Object value)
&& (value instanceof Closeable)) {
_writeCloseableValue(gen, value, _config);
} else {
if (_prefetch.valueSerializer != null) {
_serializerProvider(_config).serializeValue(gen, value, _prefetch.rootType,
_prefetch.valueSerializer);
JsonSerializer<Object> ser = _prefetch.valueSerializer;
if (ser != null) {
_serializerProvider(_config).serializeValue(gen, value, _prefetch.rootType, ser);
} else if (_prefetch.typeSerializer != null) {
_serializerProvider(_config).serializePolymorphic(gen, value, _prefetch.typeSerializer);
_serializerProvider(_config).serializePolymorphic(gen, value,
_prefetch.rootType, _prefetch.typeSerializer);
} else {
_serializerProvider(_config).serializeValue(gen, value);
}
@@ -888,7 +889,7 @@ public void writeValue(JsonGenerator gen, Object value)
}
}
}

/*
/**********************************************************
/* Serialization methods, others
@@ -1086,7 +1087,8 @@ protected final void _configAndWriteValue(JsonGenerator gen, Object value) throw
_serializerProvider(_config).serializeValue(gen, value, _prefetch.rootType,
_prefetch.valueSerializer);
} else if (_prefetch.typeSerializer != null) {
_serializerProvider(_config).serializePolymorphic(gen, value, _prefetch.typeSerializer);
_serializerProvider(_config).serializePolymorphic(gen, value,
_prefetch.rootType, _prefetch.typeSerializer);
} else {
_serializerProvider(_config).serializeValue(gen, value);
}
@@ -1121,7 +1123,8 @@ private final void _writeCloseable(JsonGenerator gen, Object value, Serializatio
_serializerProvider(cfg).serializeValue(gen, value, _prefetch.rootType,
_prefetch.valueSerializer);
} else if (_prefetch.typeSerializer != null) {
_serializerProvider(cfg).serializePolymorphic(gen, value, _prefetch.typeSerializer);
_serializerProvider(cfg).serializePolymorphic(gen, value,
_prefetch.rootType, _prefetch.typeSerializer);
} else {
_serializerProvider(cfg).serializeValue(gen, value);
}
@@ -1165,7 +1168,8 @@ private final void _writeCloseableValue(JsonGenerator gen, Object value, Seriali
_serializerProvider(cfg).serializeValue(gen, value, _prefetch.rootType,
_prefetch.valueSerializer);
} else if (_prefetch.typeSerializer != null) {
_serializerProvider(cfg).serializePolymorphic(gen, value, _prefetch.typeSerializer);
_serializerProvider(cfg).serializePolymorphic(gen, value,
_prefetch.rootType, _prefetch.typeSerializer);
} else {
_serializerProvider(cfg).serializeValue(gen, value);
}
Original file line number Diff line number Diff line change
@@ -268,25 +268,39 @@ public void serializeValue(JsonGenerator gen, Object value, JavaType rootType, J
* Alternate serialization call used for polymorphic types, when {@link TypeSerializer}
* is already known, but not actual value serializer.
*
* @since 2.5
* @since 2.6
*/
public void serializePolymorphic(JsonGenerator gen, Object value, TypeSerializer typeSer)
throws IOException
public void serializePolymorphic(JsonGenerator gen, Object value,
JavaType rootType, TypeSerializer typeSer)
throws IOException
{
if (value == null) {
_serializeNull(gen);
return;
}
final Class<?> type = value.getClass();
JsonSerializer<Object> ser = findValueSerializer(type, null);
// Let's ensure types are compatible at this point
if ((rootType != null) && !rootType.getRawClass().isAssignableFrom(value.getClass())) {
_reportIncompatibleRootType(value, rootType);
}
JsonSerializer<Object> ser;
/* 12-Jun-2015, tatu: nominal root type is necessary for Maps at least;
* possibly collections, but can cause problems for other polymorphic
* types. We really need to distinguish between serialization type,
* base type; but right we don't. Hence this check
*/
if ((rootType != null) && rootType.isContainerType()) {
ser = findValueSerializer(rootType, null);
} else {
ser = findValueSerializer(value.getClass(), null);
}

final boolean wrap;
PropertyName rootName = _config.getFullRootName();
if (rootName == null) {
wrap = _config.isEnabled(SerializationFeature.WRAP_ROOT_VALUE);
if (wrap) {
gen.writeStartObject();
PropertyName pname = _config.findRootName(type);
PropertyName pname = _config.findRootName(value.getClass());
gen.writeFieldName(pname.simpleAsEncoded(_config));
}
} else if (rootName.isEmpty()) {
@@ -311,7 +325,18 @@ public void serializePolymorphic(JsonGenerator gen, Object value, TypeSerializer
throw new JsonMappingException(msg, e);
}
}


/**
* @deprecated since 2.6; remove from 2.7 or later
*/
@Deprecated
public void serializePolymorphic(JsonGenerator gen, Object value, TypeSerializer typeSer)
throws IOException
{
JavaType t = (value == null) ? null : _config.constructType(value.getClass());
serializePolymorphic(gen, value, t, typeSer);
}

/**
* Helper method called when root value to serialize is null
*
Original file line number Diff line number Diff line change
@@ -93,7 +93,7 @@ public void testObjectWriterWithNode() throws Exception
assertEquals("{\"a\":5}", json);
}

public void testPolymorpicWithTyping() throws Exception
public void testPolymorphicWithTyping() throws Exception
{
ObjectWriter writer = MAPPER.writerFor(PolyBase.class);
String json;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fasterxml.jackson.failing;
package com.fasterxml.jackson.databind.seq;

import java.io.IOException;
import java.util.HashMap;

0 comments on commit 429f70f

Please sign in to comment.