Tarator
Tarator

Reputation: 1524

Jackson XML Deserialiation fails when there is a XML-Comment instead of elements

I have the following XML:

<licenseSummary>
    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.4</version>
            <licenses>
                <license>
                   <name>The Apache Software License, Version 2.0</name>
                   <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
                   <distribution>repo</distribution>
                </license>
            </licenses>
        </dependency>
        <dependency>
            <groupId>somegroup</groupId>
            <artifactId>someartifact</artifactId>
            <version>2.43.0-SNAPSHOT</version>
            <licenses>
                <!--No license information available. -->
            </licenses>
        </dependency>
    </dependencies>
</licenseSummary>

When trying to deserialize this XML-File Jackson fails because there is a comment instead of elements in the <licenses /> part of the second <dependency />-Element with the following error:

Cannot deserialize instance of `java.util.ArrayList` out of VALUE_STRING token

I tried to configure the XMLMapper with but this doesn't help:

ObjectMapper xmlMapper = new XmlMapper();
xmlMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

This is how the relevant POJO looks like:

public class Dependency {
        private String groupId;
        private String artifactId;
        private String version;
        private List<License> licenses = new ArrayList<License>(1);

        public String getGroupId() {
                return groupId;
        }
        public void setGroupId(String groupId) {
                this.groupId = groupId;
        }
        public String getArtifactId() {
                return artifactId;
        }
        public void setArtifactId(String artifactId) {
                this.artifactId = artifactId;
        }
        public String getVersion() {
                return version;
        }
        public void setVersion(String version) {
                this.version = version;
        }
        public List<License> getLicenses() {
                return licenses;
        }
        public void setLicenses(List<License> licenses) {
                this.licenses = licenses;
        }
}

Does anybody know how I can ignore the comment. (btw: I can't remove the comment in the XML-file)

jackson-dataformat-xml Version 2.9.4

Upvotes: 1

Views: 1042

Answers (1)

Tarator
Tarator

Reputation: 1524

I ended up implementing a DeserializationProblemHandler and registering it to the xmlMapper like this:

xmlMapper.addHandler(new LicenceDeserializationProblemHandler());

Here is the Handler:

import java.io.IOException;
import java.util.ArrayList;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;

public class LicenceDeserializationProblemHandler extends DeserializationProblemHandler {

        @Override
        public Object handleUnexpectedToken(DeserializationContext ctxt, Class<?> targetType, JsonToken t, JsonParser p,
                        String failureMsg) throws IOException {
                if(ArrayList.class.isAssignableFrom(targetType) && 
                                JsonToken.VALUE_STRING.equals(t)) {
                        // If there is no license a XML-Comment is in the licenses token.
                        return null;
                }else {
                        return super.handleUnexpectedToken(ctxt, targetType, t, p, failureMsg);
                }
        }   
}

Upvotes: 2

Related Questions