Skip to content

Commit

Permalink
fix adjust readNumber0 for decimal with suffix of B,S or L, for issue #…
Browse files Browse the repository at this point in the history
  • Loading branch information
yanxutao89 authored and wenshao committed Jul 3, 2024
1 parent e8a1910 commit 261688f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
6 changes: 3 additions & 3 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -4070,17 +4070,17 @@ public final void readNumber0() {
if (ch == 'L' || ch == 'F' || ch == 'D' || ch == 'B' || ch == 'S') {
switch (ch) {
case 'B':
if (!intOverflow) {
if (!intOverflow && valueType != JSON_TYPE_DEC) {
valueType = JSON_TYPE_INT8;
}
break;
case 'S':
if (!intOverflow) {
if (!intOverflow && valueType != JSON_TYPE_DEC) {
valueType = JSON_TYPE_INT16;
}
break;
case 'L':
if (offset - start < 19) {
if (offset - start < 19 && valueType != JSON_TYPE_DEC) {
valueType = JSON_TYPE_INT64;
}
break;
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java
Original file line number Diff line number Diff line change
Expand Up @@ -5535,17 +5535,17 @@ public final void readNumber0() {
if (ch == 'L' || ch == 'F' || ch == 'D' || ch == 'B' || ch == 'S') {
switch (ch) {
case 'B':
if (!intOverflow) {
if (!intOverflow && valueType != JSON_TYPE_DEC) {
valueType = JSON_TYPE_INT8;
}
break;
case 'S':
if (!intOverflow) {
if (!intOverflow && valueType != JSON_TYPE_DEC) {
valueType = JSON_TYPE_INT16;
}
break;
case 'L':
if (offset - start < 19) {
if (offset - start < 19 && valueType != JSON_TYPE_DEC) {
valueType = JSON_TYPE_INT64;
}
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.alibaba.fastjson2.issues_2700;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue2769 {
@Test
public void testWithSuffixB() {
String str = "{\"k1\":123.456,\"k2\":12.34B}";
test(JSON.parseObject(str));
test(JSON.parseObject(str.getBytes(StandardCharsets.UTF_8)));
}

@Test
public void testWithSuffixS() {
String str = "{\"k1\":123.456,\"k2\":12.34S}";
test(JSON.parseObject(str));
test(JSON.parseObject(str.getBytes(StandardCharsets.UTF_8)));
}

@Test
public void testWithSuffixL() {
String str = "{\"k1\":123.456,\"k2\":12.34L}";
test(JSON.parseObject(str));
test(JSON.parseObject(str.getBytes(StandardCharsets.UTF_8)));
}

private void test(JSONObject jsonObject) {
assertEquals((byte) 12, jsonObject.getByte("k2"));
assertEquals((short) 12, jsonObject.getShort("k2"));
assertEquals(12, jsonObject.getInteger("k2"));
assertEquals(12, jsonObject.getLong("k2"));
assertEquals(12.34F, jsonObject.getFloat("k2"));
assertEquals(12.34, jsonObject.getDouble("k2"));
}
}

0 comments on commit 261688f

Please sign in to comment.