From 99b8ef89de0bfe9e94b94c9ff669ae252777591e Mon Sep 17 00:00:00 2001 From: yanxutao89 <910135896@qq.com> Date: Thu, 5 Dec 2024 21:56:16 +0800 Subject: [PATCH] fix remove TODO exception for issue #3192 --- .../alibaba/fastjson2/JSONPathSegment.java | 3 --- .../fastjson2/issues_3100/Issue3192.java | 20 +++++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 core/src/test/java/com/alibaba/fastjson2/issues_3100/Issue3192.java diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONPathSegment.java b/core/src/main/java/com/alibaba/fastjson2/JSONPathSegment.java index 06ef7fe0dc..bcfcd47a66 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONPathSegment.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONPathSegment.java @@ -1285,10 +1285,7 @@ public void accept(JSONReader jsonReader, JSONPath.Context context) { context.value = values; } context.eval = true; - return; } - - throw new JSONException("TODO"); } } diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_3100/Issue3192.java b/core/src/test/java/com/alibaba/fastjson2/issues_3100/Issue3192.java new file mode 100644 index 0000000000..078f894b90 --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_3100/Issue3192.java @@ -0,0 +1,20 @@ +package com.alibaba.fastjson2.issues_3100; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSONPath; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class Issue3192 { + @Test + public void test_jsonpath_array() { + String json = "{ \"array\": null }"; + assertEquals("[]", JSON.toJSONString(JSONPath.extract(json, "$.array[0].key", JSONPath.Feature.KeepNullValue, JSONPath.Feature.AlwaysReturnList))); + assertEquals("[]", JSON.toJSONString(JSONPath.extract(json, "$.array[*].key", JSONPath.Feature.KeepNullValue, JSONPath.Feature.AlwaysReturnList))); + + json = "{ \"array\": [{\"key\":1}, {\"key\":null}, {\"key\":3}] }"; + assertEquals("[1]", JSON.toJSONString(JSONPath.extract(json, "$.array[0].key", JSONPath.Feature.KeepNullValue, JSONPath.Feature.AlwaysReturnList))); + assertEquals("[1,null,3]", JSON.toJSONString(JSONPath.extract(json, "$.array[*].key", JSONPath.Feature.KeepNullValue, JSONPath.Feature.AlwaysReturnList))); + } +}