Skip to content

Commit 595e69f

Browse files
committed
issue FasterXML#205 - add the xml untyped deser as an option
Signed-off-by: joaovarandas <[email protected]>
1 parent 56dbc88 commit 595e69f

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

src/main/java/com/fasterxml/jackson/dataformat/xml/JacksonXmlModule.java

-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ public void setupModule(SetupContext context)
6767
m.setXMLTextElementName(_cfgNameForTextElement);
6868
}
6969

70-
// fix the duplicated elements bug in an untyped Object
71-
addDeserializer(Object.class, new XmlUntypedObjectDeserializer());
72-
7370
/* Usually this would be the first call; but here anything added will
7471
* be stuff user may has added, so do it afterwards instead.
7572
*/

src/main/java/com/fasterxml/jackson/dataformat/xml/XmlMapper.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@
77
import javax.xml.stream.XMLStreamReader;
88
import javax.xml.stream.XMLStreamWriter;
99

10-
import com.fasterxml.jackson.core.*;
10+
import com.fasterxml.jackson.core.PrettyPrinter;
11+
import com.fasterxml.jackson.core.Version;
1112
import com.fasterxml.jackson.core.type.TypeReference;
12-
import com.fasterxml.jackson.databind.*;
13+
import com.fasterxml.jackson.databind.AnnotationIntrospector;
14+
import com.fasterxml.jackson.databind.JavaType;
15+
import com.fasterxml.jackson.databind.Module;
16+
import com.fasterxml.jackson.databind.ObjectMapper;
17+
import com.fasterxml.jackson.databind.module.SimpleModule;
1318
import com.fasterxml.jackson.dataformat.xml.deser.FromXmlParser;
19+
import com.fasterxml.jackson.dataformat.xml.deser.XmlUntypedObjectDeserializer;
1420
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
1521
import com.fasterxml.jackson.dataformat.xml.ser.XmlSerializerProvider;
1622
import com.fasterxml.jackson.dataformat.xml.util.DefaultXmlPrettyPrinter;
@@ -129,6 +135,15 @@ public XmlMapper setDefaultUseWrapper(boolean state) {
129135
}
130136
return this;
131137
}
138+
139+
public XmlMapper setUseXmlUntypedObjectDeserModule(boolean state) {
140+
if (state == true) {
141+
Module xmlUntypedObjectDeserModule = new SimpleModule().addDeserializer(Object.class, new XmlUntypedObjectDeserializer());
142+
registerModule(xmlUntypedObjectDeserModule);
143+
}
144+
145+
return this;
146+
}
132147

133148
/*
134149
/**********************************************************

src/test/java/com/fasterxml/jackson/dataformat/xml/stream/XmlParserTest.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,15 @@ public void testSimpleNested() throws Exception
6868
public void testDuplicatedElementsSwallowing() throws Exception
6969
{
7070
// 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>"));
71+
assertEquals("{\"id\":\"10\",\"name\":\"John\",\"parent\":[\"Jose\",\"Maria\"],\"dogs\":{\"count\":\"3\",\"dog\":[{\"name\":\"Spike\",\"age\":\"12\"},{\"name\":\"Brutus\",\"age\":\"9\"},{\"name\":\"Bob\",\"age\":\"14\"}]},\"addresses\":{\"address\":[\"Brazil\",\"US\"]}}",
72+
_readXmlToMapToJson("<person id=\"10\"><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><addresses><address>Brazil</address><address>US</address></addresses></person>", true));
73+
74+
// using 'false' should keep the deser the way it was before ...
75+
assertEquals("{\"id\":\"10\",\"name\":\"John\",\"parent\":\"Maria\",\"dogs\":{\"count\":\"3\",\"dog\":{\"name\":\"Bob\",\"age\":\"14\"}},\"addresses\":{\"address\":\"US\"}}",
76+
_readXmlToMapToJson("<person id=\"10\"><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><addresses><address>Brazil</address><address>US</address></addresses></person>", false));
77+
7378
}
79+
7480

7581
/**
7682
* Unit test that verifies that we can write sample document from JSON
@@ -286,8 +292,10 @@ private String _readXmlWriteJson(String xml) throws IOException
286292
return w.toString();
287293
}
288294

289-
private String _readXmlToMapToJson(String xml) throws IOException
295+
private String _readXmlToMapToJson(String xml, boolean state) throws IOException
290296
{
291-
return _objectWriter.writeValueAsString(_xmlMapper.readValue(xml, Object.class));
297+
XmlMapper _xmlMapperWithOption = new XmlMapper().setUseXmlUntypedObjectDeserModule(state);
298+
299+
return _objectWriter.writeValueAsString(_xmlMapperWithOption.readValue(xml, Object.class));
292300
}
293301
}

0 commit comments

Comments
 (0)