From 9bac2832326d18bfc62d54c804f9b36d975b3001 Mon Sep 17 00:00:00 2001 From: "shaojin.wensj" Date: Sun, 17 Mar 2024 15:48:48 +0800 Subject: [PATCH] double & float support input NaN, for issue #2309 --- .../alibaba/fastjson2/JSONReaderUTF16.java | 12 +++++++ .../com/alibaba/fastjson2/JSONReaderUTF8.java | 12 +++++++ .../fastjson2/issues_2300/Issue2309.java | 34 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 core/src/test/java/com/alibaba/fastjson2/issues_2300/Issue2309.java diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java b/core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java index ed955a9409..f50f76a086 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java @@ -2802,6 +2802,12 @@ public final double readDoubleValue() { doubleValue = 0; value = true; ch = offset == end ? EOI : chars[offset++]; + } else if (ch == 'N' && chars[offset] == 'a' && chars[offset + 1] == 'N') { + valid = true; + offset += 2; + doubleValue = Double.NaN; + value = true; + ch = offset == end ? EOI : chars[offset++]; } else if (ch == '{' && quote == 0) { valid = true; this.ch = ch; @@ -3068,6 +3074,12 @@ public final float readFloatValue() { floatValue = 0; value = true; ch = offset == end ? EOI : chars[offset++]; + } else if (ch == 'N' && chars[offset] == 'a' && chars[offset + 1] == 'N') { + offset += 2; + valid = true; + value = true; + floatValue = Float.NaN; + ch = offset == end ? EOI : chars[offset++]; } else if (ch == '{' && quote == 0) { valid = true; this.ch = ch; diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java b/core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java index 2dd9d1c3e4..b57eb6199a 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java @@ -3947,6 +3947,12 @@ public final double readDoubleValue() { doubleValue = 0; value = true; ch = offset == end ? EOI : bytes[offset++]; + } else if (ch == 'N' && bytes[offset] == 'a' && bytes[offset + 1] == 'N') { + valid = true; + offset += 2; + doubleValue = Double.NaN; + value = true; + ch = offset == end ? EOI : bytes[offset++]; } else if (ch == '{' && quote == 0) { valid = true; this.ch = (char) ch; @@ -4213,6 +4219,12 @@ public final float readFloatValue() { floatValue = 0; value = true; ch = offset == end ? EOI : chars[offset++]; + } else if (ch == 'N' && chars[offset] == 'a' && chars[offset + 1] == 'N') { + offset += 2; + valid = true; + value = true; + floatValue = Float.NaN; + ch = offset == end ? EOI : chars[offset++]; } else if (ch == '{' && quote == 0) { valid = true; this.ch = (char) ch; diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_2300/Issue2309.java b/core/src/test/java/com/alibaba/fastjson2/issues_2300/Issue2309.java new file mode 100644 index 0000000000..8b5140aea9 --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_2300/Issue2309.java @@ -0,0 +1,34 @@ +package com.alibaba.fastjson2.issues_2300; + +import com.alibaba.fastjson2.JSON; +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class Issue2309 { + @Test + public void test() { + String str = "{\"value\":NaN}"; + assertEquals(Double.NaN, JSON.parseObject(str.getBytes(StandardCharsets.UTF_8), Bean.class).value); + assertEquals(Double.NaN, JSON.parseObject(str.toCharArray(), Bean.class).value); + assertEquals(Double.NaN, JSON.parseObject(str, Bean.class).value); + } + + public static class Bean { + public Double value; + } + + @Test + public void testFloat() { + String str = "{\"value\":NaN}"; + assertEquals(Float.NaN, JSON.parseObject(str.getBytes(StandardCharsets.UTF_8), Bean1.class).value); + assertEquals(Float.NaN, JSON.parseObject(str.toCharArray(), Bean1.class).value); + assertEquals(Float.NaN, JSON.parseObject(str, Bean1.class).value); + } + + public static class Bean1 { + public Float value; + } +}