Skip to content

Commit 29963a7

Browse files
committed
Fix #411 for 2.3.3 as well
1 parent 20d2fc9 commit 29963a7

File tree

4 files changed

+45
-16
lines changed

4 files changed

+45
-16
lines changed

release-notes/VERSION

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ Version: 2.3.2 (xx-xxx-2014)
99
#398: Should deserialize empty (not null) URI from empty String
1010
(reported by pgieser@github)
1111
#406: @JsonTypeIdResolver not working with external type ids
12-
(repoted by Martin T)
12+
(reported by Martin T)
13+
#411: NumberDeserializers throws exception with NaN and +/- Infinity
14+
(reported by clarkbreyman@github)
1315
- Added `BeanSerializerBase._serializeObjectId()` needed by modules that
1416
override standard BeanSerializer; specifically, XML module.
1517

src/main/java/com/fasterxml/jackson/databind/deser/std/NumberDeserializers.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.fasterxml.jackson.core.JsonParser;
99
import com.fasterxml.jackson.core.JsonProcessingException;
1010
import com.fasterxml.jackson.core.JsonToken;
11-
1211
import com.fasterxml.jackson.databind.DeserializationContext;
1312
import com.fasterxml.jackson.databind.DeserializationFeature;
1413
import com.fasterxml.jackson.databind.JsonDeserializer;
@@ -453,15 +452,28 @@ public Number deserialize(JsonParser jp, DeserializationContext ctxt)
453452
*/
454453
if (t == JsonToken.VALUE_STRING) { // let's do implicit re-parse
455454
String text = jp.getText().trim();
455+
if (text.length() == 0) {
456+
return getEmptyValue();
457+
}
458+
if (_hasTextualNull(text)) {
459+
return getNullValue();
460+
}
461+
if (_isPosInf(text)) {
462+
return Double.POSITIVE_INFINITY;
463+
}
464+
if (_isNegInf(text)) {
465+
return Double.NEGATIVE_INFINITY;
466+
}
467+
if (_isNaN(text)) {
468+
return Double.NaN;
469+
}
456470
try {
457471
if (text.indexOf('.') >= 0) { // floating point
458-
// as per [JACKSON-72]:
459472
if (ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
460473
return new BigDecimal(text);
461474
}
462475
return new Double(text);
463476
}
464-
// as per [JACKSON-100]:
465477
if (ctxt.isEnabled(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS)) {
466478
return new BigInteger(text);
467479
}

src/main/java/com/fasterxml/jackson/databind/deser/std/StdDeserializer.java

+20-11
Original file line numberDiff line numberDiff line change
@@ -478,17 +478,17 @@ protected final float _parseFloatPrimitive(JsonParser jp, DeserializationContext
478478
}
479479
switch (text.charAt(0)) {
480480
case 'I':
481-
if ("Infinity".equals(text) || "INF".equals(text)) {
481+
if (_isPosInf(text)) {
482482
return Float.POSITIVE_INFINITY;
483483
}
484484
break;
485485
case 'N':
486-
if ("NaN".equals(text)) {
486+
if (_isNaN(text)) {
487487
return Float.NaN;
488488
}
489489
break;
490490
case '-':
491-
if ("-Infinity".equals(text) || "-INF".equals(text)) {
491+
if (_isNegInf(text)) {
492492
return Float.NEGATIVE_INFINITY;
493493
}
494494
break;
@@ -523,17 +523,17 @@ protected final Double _parseDouble(JsonParser jp, DeserializationContext ctxt)
523523
}
524524
switch (text.charAt(0)) {
525525
case 'I':
526-
if ("Infinity".equals(text) || "INF".equals(text)) {
526+
if (_isPosInf(text)) {
527527
return Double.POSITIVE_INFINITY;
528528
}
529529
break;
530530
case 'N':
531-
if ("NaN".equals(text)) {
531+
if (_isNaN(text)) {
532532
return Double.NaN;
533533
}
534534
break;
535535
case '-':
536-
if ("-Infinity".equals(text) || "-INF".equals(text)) {
536+
if (_isNegInf(text)) {
537537
return Double.NEGATIVE_INFINITY;
538538
}
539539
break;
@@ -567,17 +567,17 @@ protected final double _parseDoublePrimitive(JsonParser jp, DeserializationConte
567567
}
568568
switch (text.charAt(0)) {
569569
case 'I':
570-
if ("Infinity".equals(text) || "INF".equals(text)) {
570+
if (_isPosInf(text)) {
571571
return Double.POSITIVE_INFINITY;
572572
}
573573
break;
574574
case 'N':
575-
if ("NaN".equals(text)) {
575+
if (_isNaN(text)) {
576576
return Double.NaN;
577577
}
578578
break;
579579
case '-':
580-
if ("-Infinity".equals(text) || "-INF".equals(text)) {
580+
if (_isNegInf(text)) {
581581
return Double.NEGATIVE_INFINITY;
582582
}
583583
break;
@@ -661,10 +661,19 @@ protected final String _parseString(JsonParser jp, DeserializationContext ctxt)
661661
*
662662
* @since 2.3
663663
*/
664-
protected boolean _hasTextualNull(String value)
665-
{
664+
protected boolean _hasTextualNull(String value) {
666665
return "null".equals(value);
667666
}
667+
668+
protected final boolean _isNegInf(String text) {
669+
return "-Infinity".equals(text) || "-INF".equals(text);
670+
}
671+
672+
protected final boolean _isPosInf(String text) {
673+
return "Infinity".equals(text) || "INF".equals(text);
674+
}
675+
676+
protected final boolean _isNaN(String text) { return "NaN".equals(text); }
668677

669678
/*
670679
/****************************************************

src/test/java/com/fasterxml/jackson/databind/deser/TestNumbers.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,17 @@ public MyBeanValue deserialize(JsonParser jp, DeserializationContext ctxt)
5959
/**********************************************************************
6060
*/
6161

62-
public void testFloatNaN() throws Exception
62+
public void testNaN() throws Exception
6363
{
6464
ObjectMapper m = new ObjectMapper();
6565
Float result = m.readValue(" \"NaN\"", Float.class);
6666
assertEquals(Float.valueOf(Float.NaN), result);
67+
68+
Double d = m.readValue(" \"NaN\"", Double.class);
69+
assertEquals(Double.valueOf(Double.NaN), d);
70+
71+
Number num = m.readValue(" \"NaN\"", Number.class);
72+
assertEquals(Double.valueOf(Double.NaN), num);
6773
}
6874

6975
public void testDoubleInf() throws Exception

0 commit comments

Comments
 (0)