From 6602de981fbf308d0c1988e596c356c05342cf8c Mon Sep 17 00:00:00 2001 From: "shaojin.wensj" Date: Sun, 13 Aug 2023 23:44:41 +0800 Subject: [PATCH] fix serialize field priority, for issue #1727 --- .../alibaba/fastjson2/writer/FieldWriter.java | 26 ++++++++- .../fastjson2/issues_1700/Issue1727.java | 55 +++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 core/src/test/java/com/alibaba/fastjson2/issues_1700/Issue1727.java diff --git a/core/src/main/java/com/alibaba/fastjson2/writer/FieldWriter.java b/core/src/main/java/com/alibaba/fastjson2/writer/FieldWriter.java index 308cdbe990..d737ff7161 100644 --- a/core/src/main/java/com/alibaba/fastjson2/writer/FieldWriter.java +++ b/core/src/main/java/com/alibaba/fastjson2/writer/FieldWriter.java @@ -402,11 +402,17 @@ public int compareTo(Object o) { } } - if (thisMember instanceof Field && otherMember instanceof Method) { + if (thisMember instanceof Field + && otherMember instanceof Method + && ((Field) thisMember).getType() == ((Method) otherMember).getReturnType() + ) { return -1; } - if (thisMember instanceof Method && otherMember instanceof Field) { + if (thisMember instanceof Method + && otherMember instanceof Field + && ((Method) thisMember).getReturnType() == ((Field) otherMember).getType() + ) { return 1; } @@ -454,6 +460,22 @@ public int compareTo(Object o) { } } + if (thisFieldClass.isPrimitive() && !otherFieldClass.isPrimitive()) { + return -1; + } + + if (!thisFieldClass.isPrimitive() && otherFieldClass.isPrimitive()) { + return 1; + } + + if (thisFieldClass.getName().startsWith("java.") && !otherFieldClass.getName().startsWith("java.")) { + return -1; + } + + if (!thisFieldClass.getName().startsWith("java.") && otherFieldClass.getName().startsWith("java.")) { + return 1; + } + return nameCompare; } diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_1700/Issue1727.java b/core/src/test/java/com/alibaba/fastjson2/issues_1700/Issue1727.java new file mode 100644 index 0000000000..afbe7955f6 --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_1700/Issue1727.java @@ -0,0 +1,55 @@ +package com.alibaba.fastjson2.issues_1700; + +import com.alibaba.fastjson2.JSON; +import lombok.Getter; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class Issue1727 { + @Test + public void test() { + final Bean bean = new Bean(); + bean.color = Color.BLUE; + + assertEquals("{\"color\":5}", JSON.toJSONString(bean)); + } + + public enum Color { + RED(1), + YELLOW(3), + BLUE(5); + + final Integer value; + + Color(Integer value) { + this.value = value; + } + } + + @Getter + public static class Bean { + public Color color; + + public Integer getColor() { + return color.value; + } + } + + @Test + public void test1() { + final Bean1 bean = new Bean1(); + bean.color = Color.BLUE; + + assertEquals("{\"color\":5}", JSON.toJSONString(bean)); + } + + @Getter + public static class Bean1 { + public Color color; + + public int getColor() { + return color.value; + } + } +}