Skip to content

Instantly share code, notes, and snippets.

@survivant
Created April 2, 2012 18:07
Show Gist options
  • Save survivant/2285866 to your computer and use it in GitHub Desktop.
Save survivant/2285866 to your computer and use it in GitHub Desktop.
Jacskson-xml deserialize List problem
package com.test.options;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Main {
@JsonProperty("com.test.options")
public Options options;
}
package com.test.options;
public class Option {
public String name;
public String value;
}
package com.test.options;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Options {
@JsonProperty("option")
public List<Option> option;
//public String option;
}
<?xml version="1.0" encoding="UTF-8"?>
<main>
<com.test.options>
<option name="icon1">/image1.png</option>
<option name="icon2">/image2.png</option>
</com.test.options>
</main>
package com.test.options;
import java.io.File;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class TestImporter {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
String filename = "resources/options.xml";
ObjectMapper xmlMapper = new XmlMapper();
xmlMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
xmlMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
Main pojo = xmlMapper.readValue(new File(filename), Main.class);
System.out.println(pojo);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment