Closed ian1dunn closed 1 year ago
Several javadocs, github issues, stackoverflow pages, and source code files later, I realized this is actually an issue with the DefaultPrettyPrinter
I was using. I realized PrettyPrinter
s are format-dependent and that an XmlPrettyPrinter
exists.
To fix my code, all I had to do was update this snippet of my ObjectMapper
initialization:
// PrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
PrettyPrinter prettyPrinter = new DefaultXmlPrettyPrinter();
objectMapper.setDefaultPrettyPrinter(prettyPrinter);
In the future, it might be a good idea to document more things like this because there really wasn't anything online about this. Maybe a quick start guide for custom serialization would be cool :)
@cowtowncoder I added a small section about DefaultXmlPrettyPrinter in https://github.com/FasterXML/jackson-dataformat-xml/wiki and created https://github.com/FasterXML/jackson-core/pull/1017
Some other notes:
PrettyPrinter
that is NOT an XmlPrettyPrinter
on XmlFactory
or ToXmlGenerator
(assuming this is not happening currently).
Version: 2.14.2
I am in the beginnings of creating a custom serializer for a class (the entire serializer needs to be custom made due to the nature of the application) and ran into the following issue where
ToXmlGenerator.writeStartObject()
andToXmlGenerator.writeEndObject()
write curly braces to the file instead of the expected root element.Here is the serializer method override which uses the
_initWithRootName()
method from #216Here is the output that results from serializing an instance of this class: