Skip to content

Commit

Permalink
fix DuplicateKeyValueAsArray, for issue #1100
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Feb 8, 2023
1 parent a7134cc commit 1874faf
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
6 changes: 3 additions & 3 deletions core/src/main/java/com/alibaba/fastjson2/JSONReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -1560,10 +1560,10 @@ public void read(Map object, long features) {
if ((contextFeatures & JSONReader.Feature.DuplicateKeyValueAsArray.mask) != 0) {
if (origin instanceof Collection) {
((Collection) origin).add(value);
object.put(name, value);
map.put(name, origin);
} else {
JSONArray array = JSONArray.of(origin, value);
object.put(name, array);
map.put(name, array);
}
}
}
Expand Down Expand Up @@ -1611,7 +1611,7 @@ public void read(Map object, Type keyType, Type valueType, long features) {
if ((contextFeatures & JSONReader.Feature.DuplicateKeyValueAsArray.mask) != 0) {
if (origin instanceof Collection) {
((Collection) origin).add(value);
object.put(name, value);
object.put(name, origin);
} else {
JSONArray array = JSONArray.of(origin, value);
object.put(name, array);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName
if ((contextFeatures & JSONReader.Feature.DuplicateKeyValueAsArray.mask) != 0) {
if (origin instanceof Collection) {
((Collection) origin).add(value);
object.put(name, value);
object.put(name, origin);
} else {
JSONArray array = JSONArray.of(origin, value);
object.put(name, array);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName
if ((contextFeatures & JSONReader.Feature.DuplicateKeyValueAsArray.mask) != 0) {
if (origin instanceof Collection) {
((Collection) origin).add(value);
object.put(name, value);
object.put(name, origin);
} else {
JSONArray array = JSONArray.of(origin, value);
object.put(name, array);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName
if ((contextFeatures & JSONReader.Feature.DuplicateKeyValueAsArray.mask) != 0) {
if (origin instanceof Collection) {
((Collection) origin).add(value);
object.put(name, value);
object.put(name, origin);
} else {
JSONArray array = JSONArray.of(origin, value);
object.put(name, array);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.alibaba.fastjson2.issues_1000;

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 Issue1100 {
@Test
public void test() {
String str = "{\"a\":0,\"b\":1,\"b\":2,\"b\":3}";
JSONObject jsonObject = JSON.parseObject(str, JSONReader.Feature.DuplicateKeyValueAsArray);
assertEquals("[1,2,3]", jsonObject.get("b").toString());
}
}

0 comments on commit 1874faf

Please sign in to comment.