1+ package tools .jackson .databind .format ;
2+
3+ import org .junit .jupiter .api .Test ;
4+
5+ import com .fasterxml .jackson .annotation .JsonFormat ;
6+ import com .fasterxml .jackson .annotation .JsonValue ;
7+
8+ import tools .jackson .databind .ObjectMapper ;
9+ import tools .jackson .databind .exc .InvalidFormatException ;
10+ import tools .jackson .databind .exc .MismatchedInputException ;
11+ import tools .jackson .databind .json .JsonMapper ;
12+ import tools .jackson .databind .testutil .DatabindTestUtil ;
13+
14+ import static org .junit .jupiter .api .Assertions .assertEquals ;
15+ import static org .junit .jupiter .api .Assertions .assertThrows ;
16+
17+ // [databind#3580] Enum (de)serialization in conjunction with JsonFormat.Shape.NUMBER_INT
18+ public class EnumNumberFormatShape3580PojoTest
19+ extends DatabindTestUtil
20+ {
21+ public static class Pojo3580 {
22+ public PojoStateInt3580 state ;
23+ public Pojo3580 () {}
24+ public Pojo3580 (PojoStateInt3580 state ) {this .state = state ;}
25+ }
26+
27+ @ JsonFormat (shape = JsonFormat .Shape .NUMBER_INT )
28+ public enum PojoStateInt3580 {
29+ OFF (17 ),
30+ ON (31 ),
31+ UNKNOWN (99 );
32+
33+ private int value ;
34+
35+ PojoStateInt3580 (int value ) { this .value = value ; }
36+
37+ @ JsonValue
38+ public int value () {return this .value ;}
39+ }
40+
41+ public static class PojoNum3580 {
42+ public PojoStateNum3580 state ;
43+ public PojoNum3580 () {}
44+ public PojoNum3580 (PojoStateNum3580 state ) {this .state = state ;}
45+ }
46+
47+ @ JsonFormat (shape = JsonFormat .Shape .NUMBER )
48+ public enum PojoStateNum3580 {
49+ OFF (17 ),
50+ ON (31 ),
51+ UNKNOWN (99 );
52+
53+ private int value ;
54+
55+ PojoStateNum3580 (int value ) { this .value = value ; }
56+
57+ @ JsonValue
58+ public int value () {return this .value ;}
59+ }
60+
61+ @ Test
62+ public void testEnumNumberIntFormatShape3580 () throws Exception
63+ {
64+ ObjectMapper mapper = JsonMapper .builder ().build ();
65+
66+ // Serialize
67+ assertEquals ("{\" state\" :17}" , mapper .writeValueAsString (new Pojo3580 (PojoStateInt3580 .OFF ))); //
68+ assertEquals ("{\" state\" :31}" , mapper .writeValueAsString (new Pojo3580 (PojoStateInt3580 .ON ))); //
69+ assertEquals ("{\" state\" :99}" , mapper .writeValueAsString (new Pojo3580 (PojoStateInt3580 .UNKNOWN ))); //
70+
71+ // Pass Deserialize
72+ assertEquals (PojoStateInt3580 .OFF , mapper .readValue ("{\" state\" :17}" , Pojo3580 .class ).state ); // Pojo[state=OFF]
73+ assertEquals (PojoStateInt3580 .ON , mapper .readValue ("{\" state\" :31}" , Pojo3580 .class ).state ); // Pojo[state=OFF]
74+ assertEquals (PojoStateInt3580 .UNKNOWN , mapper .readValue ("{\" state\" :99}" , Pojo3580 .class ).state ); // Pojo[state=OFF]
75+
76+ // Fail : Try to use ordinal number
77+ assertThrows (InvalidFormatException .class , () -> mapper .readValue ("{\" state\" :0}" , Pojo3580 .class ));
78+ }
79+
80+ @ Test
81+ public void testEnumNumberFormatShape3580 () throws Exception
82+ {
83+ ObjectMapper mapper = JsonMapper .builder ().build ();
84+
85+ // Serialize
86+ assertEquals ("{\" state\" :17}" , mapper .writeValueAsString (new PojoNum3580 (PojoStateNum3580 .OFF ))); //
87+ assertEquals ("{\" state\" :31}" , mapper .writeValueAsString (new PojoNum3580 (PojoStateNum3580 .ON ))); //
88+ assertEquals ("{\" state\" :99}" , mapper .writeValueAsString (new PojoNum3580 (PojoStateNum3580 .UNKNOWN ))); //
89+
90+ // Pass Deserialize
91+ assertEquals (PojoStateNum3580 .OFF , mapper .readValue ("{\" state\" :17}" , PojoNum3580 .class ).state ); // Pojo[state=OFF]
92+ assertEquals (PojoStateNum3580 .ON , mapper .readValue ("{\" state\" :31}" , PojoNum3580 .class ).state ); // Pojo[state=OFF]
93+ assertEquals (PojoStateNum3580 .UNKNOWN , mapper .readValue ("{\" state\" :99}" , PojoNum3580 .class ).state ); // Pojo[state=OFF]
94+
95+ // Fail : Try to use ordinal number
96+ assertThrows (MismatchedInputException .class , () -> mapper .readValue ("{\" state\" :0}" , PojoStateNum3580 .class ));
97+ }
98+ }
0 commit comments