4
4
import javax .xml .namespace .QName ;
5
5
import org .junit .jupiter .api .Test ;
6
6
7
+ import com .fasterxml .jackson .annotation .JsonFormat ;
8
+
7
9
import tools .jackson .core .json .JsonWriteFeature ;
10
+
8
11
import tools .jackson .databind .*;
12
+ import tools .jackson .databind .exc .MismatchedInputException ;
9
13
import tools .jackson .databind .testutil .DatabindTestUtil ;
10
14
import tools .jackson .databind .testutil .NoCheckSubTypeValidator ;
11
15
@@ -33,7 +37,7 @@ public class MiscJavaXMLTypesReadWriteTest
33
37
.build ();
34
38
35
39
@ Test
36
- public void testQNameSer () throws Exception
40
+ public void testQNameSerDefault () throws Exception
37
41
{
38
42
QName qn = new QName ("http://abc" , "tag" , "prefix" );
39
43
assertEquals (q (qn .toString ()),
@@ -42,6 +46,19 @@ public void testQNameSer() throws Exception
42
46
.writeValueAsString (qn ));
43
47
}
44
48
49
+ @ Test
50
+ public void testQNameSerToObject () throws Exception
51
+ {
52
+ QName qn = new QName ("http://abc" , "tag" , "prefix" );
53
+
54
+ ObjectMapper mapper = jsonMapperBuilder ()
55
+ .withConfigOverride (QName .class , cfg -> cfg .setFormat (JsonFormat .Value .forShape (JsonFormat .Shape .OBJECT )))
56
+ .disable (JsonWriteFeature .ESCAPE_FORWARD_SLASHES )
57
+ .build ();
58
+
59
+ assertEquals (a2q ("{'localPart':'tag','namespaceURI':'http://abc','prefix':'prefix'}" ), mapper .writeValueAsString (qn ));
60
+ }
61
+
45
62
@ Test
46
63
public void testDurationSer () throws Exception
47
64
{
@@ -111,6 +128,37 @@ public void testQNameDeser() throws Exception
111
128
assertEquals ("" , qn .getLocalPart ());
112
129
}
113
130
131
+ @ Test
132
+ public void testQNameDeserFromObject () throws Exception
133
+ {
134
+ String qstr = a2q ("{'namespaceURI':'http://abc','localPart':'tag','prefix':'prefix'}" );
135
+ // Ok to read with standard ObjectMapper, no `@JsonFormat` needed
136
+ QName qn = MAPPER .readValue (qstr , QName .class );
137
+
138
+ assertEquals ("http://abc" , qn .getNamespaceURI ());
139
+ assertEquals ("tag" , qn .getLocalPart ());
140
+ assertEquals ("prefix" , qn .getPrefix ());
141
+ }
142
+
143
+ @ Test
144
+ public void testQNameDeserFail () throws Exception
145
+ {
146
+ try {
147
+ MAPPER .readValue ("{}" , QName .class );
148
+ fail ("Should not pass" );
149
+ } catch (MismatchedInputException e ) {
150
+ verifyException (e , "Object value for `QName` is missing required property 'localPart'" );
151
+ }
152
+
153
+ try {
154
+ MAPPER .readValue (a2q ("{'localPart': 123}" ), QName .class );
155
+ fail ("Should not pass" );
156
+ } catch (MismatchedInputException e ) {
157
+ verifyException (e , "Object value property 'localPart'" );
158
+ verifyException (e , "must be of type STRING, not NUMBER" );
159
+ }
160
+ }
161
+
114
162
@ Test
115
163
public void testXMLGregorianCalendarDeser () throws Exception
116
164
{
0 commit comments