11package tools .jackson .dataformat .xml .ser ;
22
3- import tools . jackson . core . JsonGenerator ;
3+ import org . junit . jupiter . api . Test ;
44
5+ import tools .jackson .core .JsonGenerator ;
6+ import tools .jackson .core .JsonParser ;
7+ import tools .jackson .core .Version ;
8+ import tools .jackson .databind .DeserializationContext ;
9+ import tools .jackson .databind .JsonNode ;
510import tools .jackson .databind .SerializationContext ;
11+ import tools .jackson .databind .deser .std .StdDeserializer ;
612import tools .jackson .databind .module .SimpleModule ;
713
814import tools .jackson .databind .ser .std .StdScalarSerializer ;
15+ import tools .jackson .databind .ser .std .StdSerializer ;
916import tools .jackson .dataformat .xml .XmlMapper ;
1017import tools .jackson .dataformat .xml .XmlTestUtil ;
1118
1219import static org .junit .jupiter .api .Assertions .assertEquals ;
20+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
1321
14- import org . junit . jupiter . api . Test ;
22+ import com . fasterxml . jackson . annotation . JsonPropertyOrder ;
1523
1624public class CustomSerializerTest extends XmlTestUtil
1725{
18- static class CustomSerializer extends StdScalarSerializer <String >
26+ // for [dataformat-xml#41]
27+ static class CustomSerializer41 extends StdScalarSerializer <String >
1928 {
20- public CustomSerializer () { super (String .class ); }
29+ public CustomSerializer41 () { super (String .class ); }
2130
2231 @ Override
2332 public void serialize (String value , JsonGenerator g ,
@@ -26,15 +35,93 @@ public void serialize(String value, JsonGenerator g,
2635 }
2736 }
2837
38+ // [dataformat-xml#42]
39+ @ JsonPropertyOrder ({ "name" , "obj" })
40+ public static class Item42 {
41+ public String name ;
42+ public Foo obj ;
43+ public Item42 (String name , Foo obj ) {
44+ this .name = name ;
45+ this .obj = obj ;
46+ }
47+ }
48+
49+ public static class Foo {
50+ public String name ;
51+ protected Foo () { }
52+ public Foo (String name ) {
53+ this .name = name ;
54+ }
55+ }
56+
57+ // [dataformat-xml#42]
58+ static class ItemDeserializer42 extends StdDeserializer <Item42 > {
59+ public ItemDeserializer42 () {
60+ super (Item42 .class );
61+ }
62+
63+ @ Override
64+ public Item42 deserialize (JsonParser p , DeserializationContext ctxt ) {
65+ JsonNode json = ctxt .readTree (p );
66+ JsonNode foo = json .get ("obj" );
67+ if (foo == null ) {
68+ throw new IllegalStateException ("missing foo property" );
69+ }
70+ return new Item42 (json .path ("name" ).asString (),
71+ new Foo (foo .path ("name" ).asString ()));
72+ }
73+ }
74+
75+ // [dataformat-xml#42]
76+ public class ItemSerializer42 extends StdSerializer <Item42 > {
77+ public ItemSerializer42 () {
78+ super (Item42 .class );
79+ }
80+
81+ @ Override
82+ public void serialize (Item42 value , JsonGenerator g , SerializationContext ctxt ) {
83+ g .writeStartObject ();
84+ g .writePOJOProperty ("obj" , value .obj );
85+ g .writeStringProperty ("name" , value .name );
86+ g .writeEndObject ();
87+ }
88+ }
89+
90+ /*
91+ /**********************************************************************
92+ /* Test methods
93+ /**********************************************************************
94+ */
95+
2996 // for [dataformat-xml#41]
3097 @ Test
3198 public void testCustomSerializer ()
3299 {
33100 SimpleModule module = new SimpleModule ();
34- module .addSerializer (String .class , new CustomSerializer ());
101+ module .addSerializer (String .class , new CustomSerializer41 ());
35102 final XmlMapper mapper = XmlMapper .builder ()
36103 .addModule (module )
37104 .build ();
38105 assertEquals ("<String>custom:foo</String>" , mapper .writeValueAsString ("foo" ));
39106 }
107+
108+ // [dataformat-xml#42]
109+ @ Test
110+ public void testCustomSerializer42 () throws Exception
111+ {
112+ SimpleModule m = new SimpleModule ("module" , new Version (1 ,0 ,0 ,null ,null ,null ));
113+ m .addSerializer (Item42 .class , new ItemSerializer42 ());
114+ m .addDeserializer (Item42 .class , new ItemDeserializer42 ());
115+ XmlMapper xmlMapper = XmlMapper .builder ()
116+ .addModule (m )
117+ .build ();
118+ Item42 value = new Item42 ("itemName" , new Foo ("fooName" ));
119+ String xml = xmlMapper .writeValueAsString (value );
120+
121+ Item42 result = xmlMapper .readValue (xml , Item42 .class );
122+ assertNotNull (result );
123+ assertEquals ("itemName" , result .name );
124+ assertNotNull (result .obj );
125+ assertEquals ("fooName" , result .obj .name );
126+ }
40127}
0 commit comments