1
1
package com .fasterxml .jackson .dataformat .xml .stream ;
2
2
3
- import java .io .*;
4
-
5
- import com .fasterxml .jackson .core .*;
3
+ import java .io .IOException ;
4
+ import java .io .StringReader ;
5
+ import java .io .StringWriter ;
6
+
7
+ import com .fasterxml .jackson .core .JsonFactory ;
8
+ import com .fasterxml .jackson .core .JsonGenerator ;
9
+ import com .fasterxml .jackson .core .JsonParser ;
10
+ import com .fasterxml .jackson .core .JsonToken ;
6
11
import com .fasterxml .jackson .databind .JsonNode ;
7
12
import com .fasterxml .jackson .databind .ObjectMapper ;
13
+ import com .fasterxml .jackson .databind .ObjectWriter ;
8
14
import com .fasterxml .jackson .dataformat .xml .XmlFactory ;
9
15
import com .fasterxml .jackson .dataformat .xml .XmlMapper ;
10
16
import com .fasterxml .jackson .dataformat .xml .XmlTestBase ;
@@ -21,6 +27,7 @@ public class XmlParserTest extends XmlTestBase
21
27
protected JsonFactory _jsonFactory ;
22
28
protected XmlFactory _xmlFactory ;
23
29
protected XmlMapper _xmlMapper ;
30
+ protected ObjectWriter _objectWriter ;
24
31
25
32
// let's actually reuse XmlMapper to make things bit faster
26
33
@ Override
@@ -29,6 +36,7 @@ public void setUp() throws Exception {
29
36
_jsonFactory = new JsonFactory ();
30
37
_xmlFactory = new XmlFactory ();
31
38
_xmlMapper = new XmlMapper ();
39
+ _objectWriter = new ObjectMapper ().writer ();
32
40
}
33
41
34
42
/*
@@ -57,6 +65,13 @@ public void testSimpleNested() throws Exception
57
65
_readXmlWriteJson ("<root><a><b><c>xyz</c></b></a></root>" ));
58
66
}
59
67
68
+ public void testDuplicatedElementsSwallowing () throws Exception
69
+ {
70
+ // 04-Aug-2016, jpvarandas: ensure that duplicated elements get wrapped into an array and do not get swallowed (deser and ser)
71
+ assertEquals ("{\" name\" :\" John\" ,\" parent\" :[\" Jose\" ,\" Maria\" ],\" dogs\" :{\" count\" :\" 3\" ,\" dog\" :[{\" name\" :\" Spike\" ,\" age\" :\" 12\" },{\" name\" :\" Brutus\" ,\" age\" :\" 9\" },{\" name\" :\" Bob\" ,\" age\" :\" 14\" }]}}" ,
72
+ _readXmlToMapToJson ("<person><name>John</name><parent>Jose</parent><parent>Maria</parent><dogs><count>3</count><dog><name>Spike</name><age>12</age></dog><dog><name>Brutus</name><age>9</age></dog><dog><name>Bob</name><age>14</age></dog></dogs></person>" ));
73
+ }
74
+
60
75
/**
61
76
* Unit test that verifies that we can write sample document from JSON
62
77
* specification as XML, and read it back in "as JSON", with
@@ -270,4 +285,9 @@ private String _readXmlWriteJson(String xml) throws IOException
270
285
jg .close ();
271
286
return w .toString ();
272
287
}
288
+
289
+ private String _readXmlToMapToJson (String xml ) throws IOException
290
+ {
291
+ return _objectWriter .writeValueAsString (_xmlMapper .readValue (xml , Object .class ));
292
+ }
273
293
}
0 commit comments