4zh4r_s4l4ti
4zh4r_s4l4ti

Reputation: 1584

Jackson processor for java bean to json\xml serialization

Hi I am having a java bean and i need to serialize it to json. For this purpose i am using the jackson processor which is very powerful. But I also need to convert the java bean to XML. Can this be achieved using the jackson processor ?? If yes then please provide with the links where I can get the examples. Thanks!!!!

Upvotes: 1

Views: 2416

Answers (3)

StaxMan
StaxMan

Reputation: 116620

Definitely! Jackson obviously has first-class support for JSON; but there is also simple extension module to do "mini-JAXB": jackson-xml-databind. With Jackson's support for JAXB annotations (or not, if you just prefer Jackson's own annotations & xml module's couple additional ones), it's definitely possible to do both JSON and XML just using Jackson functionality.

I mostly recommend this for cases where XML support is a legacy thing (which is what most new services do). If XML is the main focus, it may make more sense to use JAXB. But even then I recommend against using conversion layers from XML to JSON; ones I have seen used have been plagues with issues when they conversion at data format layer, which IMO is completely wrong place to do it. This is also why Jackson does not try converting JSON to XML (or vice versa); rather, it only supports converting POJOs to/from external data formats.

Upvotes: 1

smp7d
smp7d

Reputation: 5032

If you want to keep Jackson and JSON out of your pojos, you can create a translation layer that can translate to a JAXB object and use pure JAXB (JAXB being one possible implementation in this case). If your domain objects map straight to the rendered JSON or you can use mixins/PropertyNamingStrategy, you will need no annotations in your pojos. I'm not sure if this is applicable in your situation but I know that many environments strive for this.

Upvotes: 2

laz
laz

Reputation: 28648

Yes it is possible. You would need to annotate your Java bean using the JAXB annotations which will give you the XML marshalling and unmarshalling support. Then, using Jackson's support for JAXB annotations you can go back and forth between Java and JSON.

Upvotes: 0

Related Questions