Skip to content

Commit

Permalink
Support nested duplicate keys when enable DuplicateKeyValueAsArray #1786
Browse files Browse the repository at this point in the history
  • Loading branch information
cxzl25 authored and wenshao committed Aug 25, 2023
1 parent daf4873 commit b75e14a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
13 changes: 12 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/JSONReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2339,7 +2339,18 @@ public Map<String, Object> readObject() {
default:
throw new JSONException(info("illegal input " + ch));
}
object.put(name, val);
Object origin = object.put(name, val);
if (origin != null) {
if ((context.getFeatures() & JSONReader.Feature.DuplicateKeyValueAsArray.mask) != 0) {
if (origin instanceof Collection) {
((Collection) origin).add(val);
object.put(name, origin);
} else {
JSONArray array = JSONArray.of(origin, val);
object.put(name, array);
}
}
}
}

if (comma = (ch == ',')) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.alibaba.fastjson2.issues_1700;

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

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

public class Issue1786 {
@Test
public void test() {
String json = "{\"c1\":{\"a\":1,\"a\":2}}";
JSONObject jsonObject = JSON.parseObject(json, JSONReader.Feature.DuplicateKeyValueAsArray);
assertEquals("[1,2]", ((JSONObject) jsonObject.get("c1")).get("a").toString());
}
}

0 comments on commit b75e14a

Please sign in to comment.