Skip to content

Commit

Permalink
bug fix for parse plus first number #425
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jun 7, 2022
1 parent 7281939 commit 8dcd6d4
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 0 deletions.
11 changes: 11 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderStr.java
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,8 @@ public int readInt32Value() {
if (ch == '-') {
negative = true;
ch = str.charAt(offset++);
} else if (ch == '+') {
ch = str.charAt(offset++);
}

boolean overflow = false;
Expand Down Expand Up @@ -817,6 +819,8 @@ public Integer readInt32() {
if (ch == '-') {
negative = true;
ch = str.charAt(offset++);
} else if (ch == '+') {
ch = str.charAt(offset++);
}

boolean overflow = false;
Expand Down Expand Up @@ -924,6 +928,8 @@ public long readInt64Value() {
if (ch == '-') {
negative = true;
ch = str.charAt(offset++);
} else if (ch == '+') {
ch = str.charAt(offset++);
}

boolean overflow = false;
Expand Down Expand Up @@ -1043,6 +1049,8 @@ public Long readInt64() {
if (ch == '-') {
negative = true;
ch = str.charAt(offset++);
} else if (ch == '+') {
ch = str.charAt(offset++);
}

boolean overflow = false;
Expand Down Expand Up @@ -1701,6 +1709,9 @@ public void readNumber0() {
negative = true;
ch = str.charAt(offset++);
} else {
if (ch == '+') {
ch = str.charAt(offset++);
}
limit = -2147483647; // -Integer.MAX_VALUE;
multmin = -214748364; // limit / 10;
}
Expand Down
11 changes: 11 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,8 @@ public int readInt32Value() {
if (ch == '-') {
negative = true;
ch = chars[offset++];
} else if (ch == '+') {
ch = chars[offset++];
}

boolean overflow = false;
Expand Down Expand Up @@ -1016,6 +1018,8 @@ public Integer readInt32() {
if (ch == '-') {
negative = true;
ch = chars[offset++];
} else if (ch == '+') {
ch = chars[offset++];
}

boolean overflow = false;
Expand Down Expand Up @@ -1121,6 +1125,8 @@ public long readInt64Value() {
if (ch == '-') {
negative = true;
ch = chars[offset++];
} else if (ch == '+') {
ch = chars[offset++];
}

boolean overflow = false;
Expand Down Expand Up @@ -1237,6 +1243,8 @@ public Long readInt64() {
if (ch == '-') {
negative = true;
ch = chars[offset++];
} else if (ch == '+') {
ch = chars[offset++];
}

boolean overflow = false;
Expand Down Expand Up @@ -1948,6 +1956,9 @@ public void readNumber0() {
negative = true;
ch = chars[offset++];
} else {
if (ch == '+') {
ch = chars[offset++];
}
limit = -2147483647; // -Integer.MAX_VALUE;
multmin = -214748364; // limit / 10;
}
Expand Down
11 changes: 11 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,8 @@ public final int readInt32Value() {
if (ch == '-') {
negative = true;
ch = (char) bytes[offset++];
} else if (ch == '+') {
ch = (char) bytes[offset++];
}

boolean overflow = false;
Expand Down Expand Up @@ -1142,6 +1144,8 @@ public Integer readInt32() {
if (ch == '-') {
negative = true;
ch = (char) bytes[offset++];
} else if (ch == '+') {
ch = (char) bytes[offset++];
}

boolean overflow = false;
Expand Down Expand Up @@ -1249,6 +1253,8 @@ public final long readInt64Value() {
if (ch == '-') {
negative = true;
ch = (char) bytes[offset++];
} else if (ch == '+') {
ch = (char) bytes[offset++];
}

boolean overflow = false;
Expand Down Expand Up @@ -1377,6 +1383,8 @@ public Long readInt64() {
nextIfMatch(',');
return null;
}
} else if (ch == '+') {
ch = (char) bytes[offset++];
}

boolean overflow = false;
Expand Down Expand Up @@ -2530,6 +2538,9 @@ public void readNumber0() {
negative = true;
ch = (char) bytes[offset++];
} else {
if (ch == '+') {
ch = (char) bytes[offset++];
}
limit = -2147483647; // -Integer.MAX_VALUE;
multmin = -214748364; // limit / 10;
}
Expand Down
83 changes: 83 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/JSONReaderTest1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.alibaba.fastjson2;

import com.alibaba.fastjson2_vo.*;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.math.BigInteger;

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

public class JSONReaderTest1 {
@Test
public void test() {
String str = "{\"v0000\":+000.00}";
{
JSONReader[] jsonReaders = TestUtils.createJSONReaders(str);
for (JSONReader jsonReader : jsonReaders) {
assertEquals(0, jsonReader.read(LongValue1.class).getV0000());
}
}
{
JSONReader[] jsonReaders = TestUtils.createJSONReaders(str);
for (JSONReader jsonReader : jsonReaders) {
assertEquals(0, jsonReader.read(Long1.class).getV0000());
}
}

{
JSONReader[] jsonReaders = TestUtils.createJSONReaders(str);
for (JSONReader jsonReader : jsonReaders) {
assertEquals(0, jsonReader.read(Int1.class).getV0000());
}
}
{
JSONReader[] jsonReaders = TestUtils.createJSONReaders(str);
for (JSONReader jsonReader : jsonReaders) {
assertEquals(0, jsonReader.read(Integer1.class).getV0000());
}
}
{
JSONReader[] jsonReaders = TestUtils.createJSONReaders(str);
for (JSONReader jsonReader : jsonReaders) {
assertEquals(0, jsonReader.read(FloatValue1.class).getV0000());
}
}
{
JSONReader[] jsonReaders = TestUtils.createJSONReaders(str);
for (JSONReader jsonReader : jsonReaders) {
assertEquals(0, jsonReader.read(Float1.class).getV0000());
}
}
{
JSONReader[] jsonReaders = TestUtils.createJSONReaders(str);
for (JSONReader jsonReader : jsonReaders) {
assertEquals(0, jsonReader.read(DoubleValue1.class).getV0000());
}
}
{
JSONReader[] jsonReaders = TestUtils.createJSONReaders(str);
for (JSONReader jsonReader : jsonReaders) {
assertEquals(0, jsonReader.read(Double1.class).getV0000());
}
}
}

@Test
public void testDecimal() {
String str = "{\"id\":+000.00}";
JSONReader[] jsonReaders = TestUtils.createJSONReaders(str);
for (JSONReader jsonReader : jsonReaders) {
assertEquals(new BigDecimal("0.00"), jsonReader.read(BigDecimal1.class).getId());
}
}

@Test
public void testInteger() {
String str = "{\"id\":+000.00}";
JSONReader[] jsonReaders = TestUtils.createJSONReaders(str);
for (JSONReader jsonReader : jsonReaders) {
assertEquals(BigInteger.ZERO, jsonReader.read(BigInteger1.class).getId());
}
}
}
15 changes: 15 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/issues/Issue425.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.alibaba.fastjson2.issues;

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

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

public class Issue425 {
@Test
public void test() {
String str = "{\"v0000\":+000.00}";
assertEquals(0, JSON.parseObject(str, LongValue1.class).getV0000());
}
}

0 comments on commit 8dcd6d4

Please sign in to comment.