Skip to content

Commit

Permalink
compatible api support non-string key, for issue #3214 (#3239)
Browse files Browse the repository at this point in the history
* compatible api support non-string key, for issue #3214
  • Loading branch information
wenshao authored Jan 1, 2025
1 parent 9728cd4 commit ea7cfb7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
9 changes: 8 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/JSONReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2246,6 +2246,7 @@ public Map<String, Object> readObject() {
throw new JSONException("level too large : " + level);
}

Map innerMap = null;
Map object;
if (context.objectSupplier == null) {
if ((context.features & Feature.UseNativeObject.mask) != 0) {
Expand All @@ -2255,6 +2256,7 @@ public Map<String, Object> readObject() {
}
} else {
object = context.objectSupplier.get();
innerMap = TypeUtils.getInnerMap(object);
}

for (int i = 0; ; ++i) {
Expand Down Expand Up @@ -2354,7 +2356,12 @@ public Map<String, Object> readObject() {
continue;
}

Object origin = object.put(name, val);
Object origin;
if (innerMap != null) {
origin = innerMap.put(name, val);
} else {
origin = object.put(name, val);
}
if (origin != null) {
if ((context.features & Feature.DuplicateKeyValueAsArray.mask) != 0) {
if (origin instanceof Collection) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.alibaba.fastjson2.issues_3200;

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

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

public class Issue3214 {
@Test
public void test() {
String json = "{\n"
+ " \"k1\":{\n"
+ " \"k2\":[\n"
+ " {\n"
+ " {} : {}\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ "}";
JSONObject jsonObject = JSON.parseObject(json);
assertNotNull(jsonObject);
}

@Test
public void test1() {
JSONReader.Context context = JSONFactory.createReadContext();
context.setObjectSupplier(() -> new com.alibaba.fastjson.JSONObject());
String json = "{\n"
+ " \"k1\":{\n"
+ " \"k2\":[\n"
+ " {\n"
+ " {} : {}\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ "}";
com.alibaba.fastjson.JSONObject jsonObject = (com.alibaba.fastjson.JSONObject) JSON.parse(json, context);
assertNotNull(jsonObject);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.alibaba.fastjson.v2issues;

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

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

public class Issue3214 {
@Test
public void test() {
String json = "{\n"
+ " \"k1\":{\n"
+ " \"k2\":[\n"
+ " {\n"
+ " {} : {}\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ "}";
JSONObject jsonObject = JSON.parseObject(json);
assertNotNull(jsonObject);
}
}

0 comments on commit ea7cfb7

Please sign in to comment.