Skip to content

Commit

Permalink
fix zero or negative number key map deserialize, for issue #2570
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 14, 2024
1 parent 62d5e15 commit ff2a1a3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/src/main/java/com/alibaba/fastjson2/JSONReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -1993,7 +1993,7 @@ public void read(Map object, long features) {

Object name;
if (match || typeRedirect) {
if (ch >= '1' && ch <= '9') {
if ((ch >= '0' && ch <= '9') || ch == '-') {
name = null;
} else {
name = readFieldName();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.alibaba.fastjson2.issues_2500;

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

import java.util.Map;

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

public class Issue2570 {
@Test
public void test() {
String json = "{0:12,1:13,2:14,\"date\":\"2024-05-14\"}";
Map<Object, Object> map = JSON.parseObject(json, Map.class);

Integer date0 = (Integer) map.get(0);
assertNotNull(date0);

Integer date1 = (Integer) map.get(1);
assertNotNull(date1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.alibaba.fastjson.v2issues;

import com.alibaba.fastjson.JSON;
import org.junit.jupiter.api.Test;

import java.util.Map;

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

public class Issue2570 {
@Test
public void test() {
String json = "{0:12,1:13,2:14,\"date\":\"2024-05-14\"}";
Map<Object, Object> map = JSON.parseObject(json, Map.class);

Integer date0 = (Integer) map.get(0);
assertNotNull(date0);
assertEquals(12, date0);

Integer date1 = (Integer) map.get(1);
assertNotNull(date1);
}

@Test
public void test1() {
String json = "{-1:12,1:13,2:14,\"date\":\"2024-05-14\"}";
Map<Object, Object> map = JSON.parseObject(json, Map.class);

Integer date0 = (Integer) map.get(-1);
assertNotNull(date0);
assertEquals(12, date0);

Integer date1 = (Integer) map.get(1);
assertNotNull(date1);
}
}

0 comments on commit ff2a1a3

Please sign in to comment.