Skip to content

Commit

Permalink
double & float support input NaN, for issue #2309
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Mar 17, 2024
1 parent df46051 commit 9bac283
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 9bac283

Please sign in to comment.