Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson XML Deserialisation Issue

I am able to write a XML document, however am unable to deserialise the XML which is created. Original code is in Kotlin, however I've posted the Java equivalent.

@JacksonXmlRootElement(localName = "assets")
public class Assets {
    @JacksonXmlElementWrapper(useWrapping = false)
    private Asset[] asset;

    public Assets(Asset[] asset) {
        this.asset = asset;
    }

    public Asset[] getAsset() {
        return asset;
    }

    public void setAsset(Asset[] asset) {
        this.asset = asset;
    }
}

public class Asset {
    @JacksonXmlProperty(isAttribute = true)
    private String type;
    @JacksonXmlProperty(isAttribute = true)
    private String name;
    @JacksonXmlProperty(isAttribute = true)
    private String displayName;
    @JacksonXmlElementWrapper(localName = "permissions")
    private Permission[] permission;

    public Asset(String type, String name, String displayName, Permission[] permission) {
        this.type = type;
        this.name = name;
        this.displayName = displayName;
        this.permission = permission;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public Permission[] getPermission() {
        return permission;
    }

    public void setPermission(Permission[] permission) {
        this.permission = permission;
    }
}

public class Permission {
    @JacksonXmlProperty(isAttribute = true)
    private String name;
    private Group[] group;

    public Permission(String name, Group[] group) {
        this.name = name;
        this.group = group;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Group[] getGroup() {
        return group;
    }

    public void setGroup(Group[] group) {
        this.group = group;
    }
}

Instantiated with (Kotlin):

fun writeToXml(obj: Any) : String {
    val xmlMapper = XmlMapper()
    xmlMapper.enable(SerializationFeature.INDENT_OUTPUT)
    return xmlMapper.writeValueAsString(obj)
}

val assets = Assets(arrayOf(
            Asset("bdo", "approval-matrix", "Approval Matrix", arrayOf(Permission("Create", arrayOf(Group(arrayOf("Admin", "Approver")))))),
            Asset("bdo", "approval-matrix-2", "Approval Matrix 2", arrayOf(Permission("Delete", arrayOf(Group(arrayOf("Admin", "Approver")), Group(arrayOf("Admin-2", "Approver-2"))))))))

val xmlModule = JacksonXmlModule()
val objectMapper = XmlMapper(xmlModule)
println(writeToXml(assets))

The generated XML is as expected:

<assets>
  <asset type="bdo" name="approval-matrix" displayName="Approval Matrix">
    <permissions>
      <permission name="Create">
        <groups>
          <group>Admin</group>
          <group>Approver</group>
        </groups>
      </permission>
    </permissions>
  </asset>
  <asset type="bdo" name="approval-matrix-2" displayName="Approval Matrix 2">
    <permissions>
      <permission name="Delete">
        <groups>
          <group>Admin</group>
          <group>Approver</group>
        </groups>
        <groups>
          <group>Admin-2</group>
          <group>Approver-2</group>
        </groups>
      </permission>
    </permissions>
  </asset>
</assets>

However when trying to deserialize:

val generated = objectMapper.readValue<Assets>(xmlAssets)

I receive the following error:

Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `uk.co.processflows.configuration.entities.Assets` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: (StringReader); line: 2, column: 3]
    at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
    at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1451)
    at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1027)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1290)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
    at com.fasterxml.jackson.dataformat.xml.deser.WrapperHandlingDeserializer.deserialize(WrapperHandlingDeserializer.java:113)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3011)
    at uk.co.processflows.module.EntryPoint.main(EntryPoint.kt:58)

Any assistance would be appreciated.

like image 216
Wayne Hunter Avatar asked May 16 '18 15:05

Wayne Hunter


2 Answers

Just to clarify the answer:

I included

dependencies {
    ...
    compile(kotlin("reflect"))
    compile("com.fasterxml.jackson.module:jackson-module-kotlin:2.9.+")
    compile("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.+")
    ...
}

in build.gradle.kts

Then had the same issue. Changing my data class by adding default values to the constructor seemed to fix it. IE:

data class HelloWorld(val hello: Boolean, val world: Boolean) <- Errors

becomes

data class HelloWorld(val hello: Boolean = false, val world: Boolean = false) <- Working

then:

const val xml = """
<HelloWorld>
    <hello>true</hello>
    <world>true</world>
</HelloWorld>
"""

Will serialize and deserialize normally in kotlin via data classes. Thanks!

like image 158
Brooks DuBois Avatar answered Sep 23 '22 14:09

Brooks DuBois


There is now a Jackson module which allows it to deserialise to Kotlin data classes without the need to add a default constructor.

See https://github.com/FasterXML/jackson-module-kotlin

compile "com.fasterxml.jackson.module:jackson-module-kotlin:2.9.+"

Try adding this to your Kotlin project so that you can keep your domain classes 'pure'.

like image 30
hexkid Avatar answered Sep 23 '22 14:09

hexkid