Check Float NaN and Infinity
MODIFY
analytics/src/main/java/com/segment/analytics/Cartographer.java
analytics/src/main/java/com/segment/analytics/Cartographer.java
@@ -216,6 +216,10 @@ public class Cartographer {
&& (Double.isNaN((Double) value) || Double.isInfinite((Double) value))) {
value = 0.0;
}
+ if (value instanceof Float
+ && (Float.isNaN((Float) value) || Float.isInfinite((Float) value))) {
+ value = 0.0;
+ }
writer.value((Number) value);
} else if (value instanceof Boolean) {
writer.value((Boolean) value);

MODIFY
analytics/src/test/java/com/segment/analytics/CartographerTest.kt
analytics/src/test/java/com/segment/analytics/CartographerTest.kt
@@ -319,6 +319,27 @@ class CartographerTest {
)
}
+ @Test
+ @Throws(IOException::class)
+ fun encodesInfiniteAndNanFloats() {
+ val map = ImmutableMap.builder<String, Any>()
+ .put("nan", Float.NaN)
+ .put("positive_infinity", Float.POSITIVE_INFINITY)
+ .put("negative_infinity", Float.NEGATIVE_INFINITY)
+ .build()
+
+ assertThat(cartographer.toJson(map))
+ .isEqualTo(
+ """
+ |{
+ | "nan": 0.0,
+ | "positive_infinity": 0.0,
+ | "negative_infinity": 0.0
+ |}
+ """.trimMargin()
+ )
+ }
+
@Test
@Throws(IOException::class)
fun encodesPrimitiveArrays() {

Check Float NaN and Infinity (#755)