Skip to content

Commit

Permalink
Fix #357 properly. Finally!
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Dec 10, 2016
1 parent c799b42 commit 9434f51
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 55 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Project: jackson-databind
#291: @JsonTypeInfo with As.EXTERNAL_PROPERTY doesn't work if external type property
is referenced more than once
(reported by Starkom@github)
#357: StackOverflowError with contentConverter that returns array type
(reported by Florian S)
#476: Allow "Serialize as POJO" using `@JsonFormat(shape=Shape.OBJECT)` class annotation
#507: Support for default `@JsonView` for a class
(suggested by Mark W)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,11 @@ public void serialize(Object value, JsonGenerator gen, SerializerProvider provid
provider.defaultSerializeNull(gen);
return;
}
System.err.println(" delegating.serialize. value: "+value.getClass());

// 02-Apr-2015, tatu: As per [databind#731] may need to do dynamic lookup
JsonSerializer<Object> ser = _delegateSerializer;
if (ser == null) {
System.err.println("DEBUG: dynamic lookup for delegator... ");
ser = _findSerializer(delegateValue, provider);
}
System.err.println(" delegating.serialize with -> "+ser);
ser.serialize(delegateValue, gen, provider);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.Set;
import java.util.IdentityHashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
Expand Down Expand Up @@ -34,6 +34,14 @@ public abstract class StdSerializer<T>
{
private static final long serialVersionUID = 1L;

/**
* Key used for storing a lock object to prevent infinite recursion when
* constructing converting serializers.
*
* @since 2.9
*/
private final static Object KEY_CONTENT_CONVERTER_LOCK = new Object();

/**
* Nominal type supported, usually declared type of
* property for which serializer is used.
Expand Down Expand Up @@ -355,30 +363,29 @@ protected JsonSerializer<?> findContextualConvertingSerializer(SerializerProvide
{
// 08-Dec-2016, tatu: to fix [databind#357], need to prevent recursive calls for
// same property
/*
@SuppressWarnings("unchecked")
Set<Object> conversions = (Set<Object>) provider.getAttribute(CONTENT_CONVERTER_LOCK);
Map<Object,Object> conversions = (Map<Object,Object>) provider.getAttribute(KEY_CONTENT_CONVERTER_LOCK);
if (conversions != null) {
if (conversions.contains(property)) {
Object lock = conversions.get(property);
if (lock != null) {
return existingSerializer;
}
} else {
conversions = new HashSet<>();
provider.setAttribute(CONTENT_CONVERTER_LOCK, provider);
conversions = new IdentityHashMap<>();
provider.setAttribute(KEY_CONTENT_CONVERTER_LOCK, conversions);
}
conversions.put(property, Boolean.TRUE);
try {
} finally {
}
*/
JsonSerializer<?> ser = findConvertingContentSerializer(provider, property, existingSerializer);
if (ser != null) {
return provider.handleSecondaryContextualization(ser, property);
}
} finally {
conversions.remove(property);
}
return existingSerializer;
}

// private final static Object CONTENT_CONVERTER_LOCK = new Object();

/**
* @deprecated Since 2.9 use {link {@link #findContextualConvertingSerializer} instead
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.*;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.util.StdConverter;

public class TestConvertingDeserializer
Expand Down Expand Up @@ -99,6 +100,21 @@ static class LowerCaseTextArray {
public String[] texts;
}

// [databind#357]
static class Value { }

static class ListWrapper {
@JsonSerialize(contentConverter = ValueToStringListConverter.class)
public List<Value> list = Arrays.asList(new Value());
}

static class ValueToStringListConverter extends StdConverter<Value, List<String>> {
@Override
public List<String> convert(Value value) {
return Arrays.asList("Hello world!");
}
}

// for [databind#795]

static class ToNumberConverter extends StdConverter<String,Number>
Expand All @@ -114,7 +130,7 @@ static class Issue795Bean
@JsonDeserialize(converter=ToNumberConverter.class)
public Number value;
}

/*
/**********************************************************
/* Test methods
Expand Down Expand Up @@ -198,6 +214,12 @@ public void testPropertyAnnotationForMaps() throws Exception
assertEquals(2, p.y);
}

// [databind#357]
public void testConverterForList357() throws Exception {
String json = objectWriter().writeValueAsString(new ListWrapper());
assertEquals("{\"list\":[[\"Hello world!\"]]}", json);
}

// [databind#795]
public void testConvertToAbstract() throws Exception
{
Expand Down

This file was deleted.

0 comments on commit 9434f51

Please sign in to comment.