diff --git a/extension/src/main/java/com/alibaba/fastjson2/support/AwtWriterModule.java b/extension/src/main/java/com/alibaba/fastjson2/support/AwtWriterModule.java index 02d4c533ff..acd92a97a5 100644 --- a/extension/src/main/java/com/alibaba/fastjson2/support/AwtWriterModule.java +++ b/extension/src/main/java/com/alibaba/fastjson2/support/AwtWriterModule.java @@ -18,7 +18,10 @@ public class AwtWriterModule public ObjectWriter getObjectWriter(Type objectType, Class objectClass) { if (objectType == Color.class) { return objectWriter(Color.class, - fieldWriter("rgb", Color::getRGB) + fieldWriter("r", Color::getRed), + fieldWriter("g", Color::getGreen), + fieldWriter("b", Color::getBlue), + fieldWriter("alpha", Color::getAlpha) ); } diff --git a/fastjson1-compatible/src/test/java/com/alibaba/fastjson/v2issues/Issue2739.java b/fastjson1-compatible/src/test/java/com/alibaba/fastjson/v2issues/Issue2739.java new file mode 100644 index 0000000000..8b89a83476 --- /dev/null +++ b/fastjson1-compatible/src/test/java/com/alibaba/fastjson/v2issues/Issue2739.java @@ -0,0 +1,19 @@ +package com.alibaba.fastjson.v2issues; + +import com.alibaba.fastjson.JSON; +import org.junit.jupiter.api.Test; + +import java.awt.*; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class Issue2739 { + @Test + public void test() { + Color blue = Color.BLUE; + String colorStr = JSON.toJSONString(blue); + assertEquals("{\"r\":0,\"g\":0,\"b\":255,\"alpha\":255}", colorStr); + Color color = JSON.parseObject(colorStr, Color.class); + assertEquals(blue, color); + } +}