Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XmlMapper not deserializing root-level Enums #121

Closed
bhkjersten opened this issue Oct 2, 2014 · 6 comments
Closed

XmlMapper not deserializing root-level Enums #121

bhkjersten opened this issue Oct 2, 2014 · 6 comments
Milestone

Comments

@bhkjersten
Copy link

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.

@cowtowncoder
Copy link
Member

My first suggestion would actually be to always use a (JSON) Object as the root value, and not try to serialize "bare" Lists, arrays, Maps or Enums as root value. This avoid many of the problems.

But let me see what is the issue here: the only problem with Enums, XML, that I remember was that type id handling was problematic for some inclusion methods. But the non-typed handling should be fine.

@cowtowncoder
Copy link
Member

Interesting. I can reproduce the issue, and there was no test for this case.

So upside is that this is not a known problem (which often means "more difficult to fix") but rather "as of yet unknown". I'll add a unit test, see what gives.

Thank you for reporting this!

@cowtowncoder
Copy link
Member

Hmmh. Ok, this may be tricky to handle. Problem is with structural impedance between XML, JSON, and that the fix at parser/databinder level relies on POJO properties to allow defining element contents as scalars.

Will add a test, at any rate.

cowtowncoder added a commit that referenced this issue Oct 6, 2014
cowtowncoder added a commit that referenced this issue Oct 6, 2014
@bhkjersten
Copy link
Author

Thanks a lot for your comments yesterday. That cleared up what was going on.

@cowtowncoder
Copy link
Member

Ok no problem. We can still try to make that work in future; but for now at least this is documented (I added a note on README)

@cowtowncoder cowtowncoder reopened this Jun 30, 2020
@cowtowncoder cowtowncoder changed the title XmlMapper not deserializing enums XmlMapper not deserializing root-level Enums Jun 30, 2020
@cowtowncoder
Copy link
Member

Turns out I can finally fix this for 2.12, now that "extract scalar from Object" logic can be plugged in.

@cowtowncoder cowtowncoder added this to the 2.12.0 milestone Jun 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants