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