Skip to content

Commit

Permalink
Fix #2236
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Feb 9, 2019
1 parent 90479d3 commit a066415
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Project: jackson-databind
(reported by RightHandedMonkey@github)
#2230: `WRITE_BIGDECIMAL_AS_PLAIN` is ignored if `@JsonFormat` is used
(reported by Pavel C)
#2236: Type id not provided on `Double.NaN`, `Infinity` with `@JsonTypeInfo`
(reported by C-B-B@github)
#2241: Add `JsonPropertyNamingStrategy.LOWER_DOT_CASE` for dot-delimited names
(contributed by [email protected])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.fasterxml.jackson.annotation.JsonFormat;

import com.fasterxml.jackson.core.*;

import com.fasterxml.jackson.core.type.WritableTypeId;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
Expand Down Expand Up @@ -220,11 +220,27 @@ public void serialize(Object value, JsonGenerator gen,

// IMPORTANT: copied from `NonTypedScalarSerializerBase`
@Override
public void serializeWithType(Object value, JsonGenerator gen,
public void serializeWithType(Object value, JsonGenerator g,
SerializerProvider provider, TypeSerializer typeSer)
throws IOException {
// no type info, just regular serialization
serialize(value, gen, provider);
// 08-Feb-2018, tatu: Except that as per [databind#2236], NaN values need
// special handling
Double d = (Double) value;
if (notFinite(d)) {
WritableTypeId typeIdDef = typeSer.writeTypePrefix(g,
// whether to indicate it's number or string is arbitrary; important it is scalar
typeSer.typeId(value, JsonToken.VALUE_NUMBER_FLOAT));
g.writeNumber(d);
typeSer.writeTypeSuffix(g, typeIdDef);
} else {
g.writeNumber(d);
}
}

public static boolean notFinite(double value) {
// `jackson-core` has helper method in 3 but not yet
return Double.isNaN(value) || Double.isInfinite(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public StringWrapper(String value) {

protected static class ObjectWrapper {
final Object object;
protected ObjectWrapper(final Object object) {

public ObjectWrapper(final Object object) {
this.object = object;
}
public Object getObject() { return object; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ static class Data {
public long key;
}

// Basic `ObjectWrapper` from base uses delegating ctor, won't work well; should
// figure out why, but until then we'll use separate impl
protected static class ObjectWrapperForPoly {
Object object;

protected ObjectWrapperForPoly() { }
public ObjectWrapperForPoly(final Object o) {
object = o;
}
public Object getObject() { return object; }
}

/*
/**********************************************************************
/* Test methods
Expand Down Expand Up @@ -128,4 +140,15 @@ public void testDefaultTypingWithLong() throws Exception
assertNotNull(result);
assertEquals(2, result.size());
}

// [databind#2236]: do need type info for NaN
public void testDefaultTypingWithNaN() throws Exception
{
final ObjectWrapperForPoly INPUT = new ObjectWrapperForPoly(Double.POSITIVE_INFINITY);
final String json = DEFAULT_TYPING_MAPPER.writeValueAsString(INPUT);
final ObjectWrapperForPoly result = DEFAULT_TYPING_MAPPER.readValue(json, ObjectWrapperForPoly.class);
assertEquals(Double.class, result.getObject().getClass());
assertEquals(INPUT.getObject().toString(), result.getObject().toString());
assertTrue(((Double) result.getObject()).isInfinite());
}
}

0 comments on commit a066415

Please sign in to comment.