Skip to content

Commit

Permalink
fix serialize awt color
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jan 1, 2025
1 parent ea9e764 commit 3e6f981
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 3e6f981

Please sign in to comment.