Search code examples
xmljacksoncdataxml-deserializationjackson-modules

deserializing CDATA with JacksonXML - UnrecognizedPropertyException


I am using jackson libraries (2.5.2) and trying to deserialize some XML that actually has CDATA section.

It seems that Jackson 2.5 added support for CDATA.

Here is my class that has the CDATA:

public class Certificate {

@JacksonXmlProperty(localName = "name", isAttribute = true)
private String name;
@JacksonXmlCData
private String data;
@JacksonXmlProperty(localName = "date-added", isAttribute = true)
@JsonFormat(pattern = "EEE MMM d HH:mm:ss z yyyy")
private Date dateAdded;
@JsonFormat(pattern = "EEE MMM d HH:mm:ss z yyyy")
@JacksonXmlProperty(localName = "date-expires", isAttribute = true)
private Date expirationDate;


public void setData(String data) {
    this.data = data;
}

public String getData() {
    return data;
}

public String getName() {
    return name;
}

public Date getDateAdded() {
    return dateAdded;
}

public void setDateAdded(Date dateAdded) {
    this.dateAdded = dateAdded;
}

}

and I get the following exception. Is there any way at all to deserialize CDATA with jackson at all?

It seems that using JacksonXmlText works fine but when I need to serialize back I need the value to be in CDATA rather than text.

It's my first time using jackson for this so I might be missing something obvious.

Thank you,

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "" (class com.company.model.Certificate), 
not marked as ignorable (4 known properties: "date-added", "data", "name", "date-expires"])
at [Source: com.ctc.wstx.sr.ValidatingStreamReader@3895ed38; line: 23, column: 36] (through reference chain: 
com.company.model.Configuration["partner"]->java.util.HashSet[0]->com.company.model.PartnerConfiguration["config"] 
->java.util.HashSet[0]->com.company.model.HostConfiguration["cert"]->java.util.HashSet[0]->com.company.model.Certificate[""]) 
    at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:51) 
    at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:817) 
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:958) 
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1324) 
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1302) 
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:249) 
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:136) 
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245) 
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217) 
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25) 
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:523) 
    at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:101) 
    at com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap.findDeserializeAndSet(BeanPropertyMap.java:285) 
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:248) 
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:136) 
    at com.fasterxml.jackson.dataformat.xml.deser.WrapperHandlingDeserializer.deserialize(WrapperHandlingDeserializer.java:120)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:523)
    at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:101)
    at com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap.findDeserializeAndSet(BeanPropertyMap.java:285)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:248)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:136)
    at com.fasterxml.jackson.dataformat.xml.deser.WrapperHandlingDeserializer.deserialize(WrapperHandlingDeserializer.java:120)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:523)
    at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:101)
    at com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap.findDeserializeAndSet(BeanPropertyMap.java:285)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:248)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:136)
    at com.fasterxml.jackson.dataformat.xml.deser.WrapperHandlingDeserializer.deserialize(WrapperHandlingDeserializer.java:120)
    at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:3534)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1936)
    at com.fasterxml.jackson.dataformat.xml.XmlMapper.readValue(XmlMapper.java:196)
    at com.fasterxml.jackson.dataformat.xml.XmlMapper.readValue(XmlMapper.java:171)

Solution

  • Using both annotations:

    @JacksonXmlCData
    @JacksonXmlText
    

    it does the trick. That is nice!