From 897bf44b0bc06a8b95b5d22a801f6b35efffcd68 Mon Sep 17 00:00:00 2001 From: "shaojin.wensj" Date: Tue, 7 May 2024 20:09:36 +0800 Subject: [PATCH] enum support fastjson 1.x JSONField#unwrapped, for issue #2525 --- .../reader/ObjectReaderBaseModule.java | 7 ++ .../alibaba/fastjson/v2issues/Issue2525.java | 86 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 fastjson1-compatible/src/test/java/com/alibaba/fastjson/v2issues/Issue2525.java diff --git a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderBaseModule.java b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderBaseModule.java index 25dcd34aa0..34226e79c6 100644 --- a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderBaseModule.java +++ b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderBaseModule.java @@ -1166,6 +1166,13 @@ private void processJSONField1x(FieldInfo fieldInfo, Annotation annotation) { } break; } + case "unwrapped": { + boolean unwrapped = (Boolean) result; + if (unwrapped) { + fieldInfo.features |= FieldInfo.UNWRAPPED_MASK; + } + break; + } default: break; } diff --git a/fastjson1-compatible/src/test/java/com/alibaba/fastjson/v2issues/Issue2525.java b/fastjson1-compatible/src/test/java/com/alibaba/fastjson/v2issues/Issue2525.java new file mode 100644 index 0000000000..16529ec1be --- /dev/null +++ b/fastjson1-compatible/src/test/java/com/alibaba/fastjson/v2issues/Issue2525.java @@ -0,0 +1,86 @@ +package com.alibaba.fastjson.v2issues; + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.annotation.JSONField; +import lombok.Data; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class Issue2525 { + @Test + public void testMutated() { + StandardGetQry standardGetQry = new StandardGetQry(); + StandardBaseModel standardBaseModel = new StandardBaseModel(); + standardBaseModel.setCode("2"); + standardBaseModel.setCategory("NEW_CALL_STATUS"); + standardGetQry.setStandardBaseModel(standardBaseModel); + String jsonStr = JSON.toJSONString(standardGetQry); + assertEquals("{\"standardCategory\":\"NEW_CALL_STATUS\",\"standardCode\":\"2\"}", jsonStr); + + StandardGetQry standardGetQry2 = JSON.parseObject(jsonStr, StandardGetQry.class); + assertEquals("{\"standardCategory\":\"NEW_CALL_STATUS\",\"standardCode\":\"2\"}", JSON.toJSONString(standardGetQry2)); + } + + @Data + public static class StandardGetQry { + /** + * 基础数据 + */ + @JSONField(unwrapped = true) + private StandardBaseModel standardBaseModel; + } + + @Data + public static class StandardBaseModel { + /** + * 逻辑编码-标准KEY + */ + @JSONField(name = "standardCode") + private String code; + + /** + * 标准类别 + */ + @JSONField(name = "standardCategory") + private String category; + } + + @Test + public void testMutated1() { + StandardGetQry1 standardGetQry = new StandardGetQry1(); + StandardBaseModel1 standardBaseModel = new StandardBaseModel1(); + standardBaseModel.setCode("2"); + standardBaseModel.setCategory("NEW_CALL_STATUS"); + standardGetQry.setStandardBaseModel(standardBaseModel); + String jsonStr = com.alibaba.fastjson.JSON.toJSONString(standardGetQry); + assertEquals("{\"standardCategory\":\"NEW_CALL_STATUS\",\"standardCode\":\"2\"}", jsonStr); + + StandardGetQry1 standardGetQry2 = com.alibaba.fastjson.JSON.parseObject(jsonStr, StandardGetQry1.class); + assertEquals("{\"standardCategory\":\"NEW_CALL_STATUS\",\"standardCode\":\"2\"}", com.alibaba.fastjson.JSON.toJSONString(standardGetQry2)); + } + + @Data + public static class StandardGetQry1 { + /** + * 基础数据 + */ + @com.alibaba.fastjson.annotation.JSONField(unwrapped = true) + private StandardBaseModel1 standardBaseModel; + } + + @Data + public static class StandardBaseModel1 { + /** + * 逻辑编码-标准KEY + */ + @com.alibaba.fastjson.annotation.JSONField(name = "standardCode") + private String code; + + /** + * 标准类别 + */ + @com.alibaba.fastjson.annotation.JSONField(name = "standardCategory") + private String category; + } +}