22
33import com .alibaba .fastjson2 .JSON ;
44import com .alibaba .fastjson2 .JSONWriter ;
5+ import com .alibaba .fastjson2 .annotation .JSONType ;
56import com .alibaba .fastjson2 .filter .ValueFilter ;
67import org .junit .jupiter .api .Test ;
78
89import static org .junit .jupiter .api .Assertions .assertEquals ;
10+ import static org .junit .jupiter .api .Assertions .assertTrue ;
911
1012public class WriteClassNameWithFilterTest {
1113 @ Test
@@ -18,7 +20,48 @@ public void test() {
1820 assertEquals ("{\" @type\" :\" com.alibaba.fastjson2.features.WriteClassNameWithFilterTest$Bean\" ,\" id\" :101}" , str );
1921 }
2022
23+ @ Test
24+ public void testAnnotationSerializeFeaturesWithFilter () {
25+ Circle circle = new Circle ();
26+ circle .setType ("circle" );
27+ circle .setRadius (10 );
28+
29+ ValueFilter filter = (Object object , String name , Object value ) -> value ;
30+
31+ String str = JSON .toJSONString (circle , filter );
32+
33+ assertTrue (str .contains ("\" type\" :\" circle\" " ), "Should contain type:circle, actual: " + str );
34+ assertTrue (str .contains ("\" radius\" :10" ), "Should contain radius:10, actual: " + str );
35+ }
36+
2137 public static class Bean {
2238 public Integer id ;
2339 }
40+
41+ @ JSONType (typeKey = "type" )
42+ public static class Shape {
43+ private String type ;
44+
45+ public String getType () {
46+ return this .type ;
47+ }
48+
49+ public void setType (String type ) {
50+ this .type = type ;
51+ }
52+ }
53+
54+ @ JSONType (typeKey = "type" , typeName = "circle" , serializeFeatures = JSONWriter .Feature .WriteClassName )
55+ public static class Circle
56+ extends Shape {
57+ private int radius ;
58+
59+ public int getRadius () {
60+ return radius ;
61+ }
62+
63+ public void setRadius (int radius ) {
64+ this .radius = radius ;
65+ }
66+ }
2467}
0 commit comments