-
-
Notifications
You must be signed in to change notification settings - Fork 231
Closed
Milestone
Description
I want to deserialize XML using XmlMapper so that it is as mechanically similar to how we deserialize JSON. I'm doing something wrong when it comes to enums, however. Below is my enum definition and my test code.
public enum SimpleEnum {
MALE,
FEMALE,
;
}
import java.io.IOException;
import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class TestEnumSerialization {
@Test
public void testJSON() throws IOException {
SimpleEnum inputEnum = SimpleEnum.MALE;
ObjectMapper mapper = new ObjectMapper();
String serializedString;
SimpleEnum outputEnum;
//Test that ObjectMapper can serialize json
serializedString = mapper.writeValueAsString(inputEnum);
System.out.println("JSON serialized string: " + serializedString);
//Test that ObjectMapper can deserialize json
outputEnum = mapper.readValue(serializedString, SimpleEnum.class);
System.out.println("JSON deserialized enum: " + outputEnum);
}
@Test
public void testXML() throws IOException {
SimpleEnum inputEnum = SimpleEnum.MALE;
ObjectMapper mapper = new XmlMapper();
String serializedString;
SimpleEnum outputEnum;
//Test that ObjectMapper can serialize xml
serializedString = mapper.writeValueAsString(inputEnum);
System.out.println("XML serialized string: " + serializedString);
//Test that ObjectMapper can deserialize xml
outputEnum = mapper.readValue(serializedString, SimpleEnum.class);
System.out.println("XML deserialized enum: " + outputEnum);
}
}
This prints out this:
JSON serialized string: "MALE"
JSON deserialized enum: MALE
XML serialized string: MALE
and it throws a JsonMappingException
at the XmlMapper
readValue()
method, saying it can not deserialize instance of SimpleEnum out of START_OBJECT token.
I do not get this error when write a class that has an enum as a member.
Metadata
Metadata
Assignees
Labels
No labels