MismatchedInputException when deserializing XML to an enum using a factory method

36 views
Skip to first unread message

Francesco Xia

unread,
Nov 5, 2024, 8:56:43 PM11/5/24
to jackson-user
Hello,
I’m trying to serialize an Enum value to XML, then read it back, but I get an input mismatch error related to JsonCreator.
It seems the factory method was defined correctly, e.g., factory method annotated with JsonCreator and argument without JsonProperty. 

Could someone provide insights into why deserialization fails? 
Any help would is appreciated. Thank you.

Error
```java
    com.fasterxml.jackson.databind.exc.MismatchedInputException: Input mismatch reading Enum `com.xxx.yyy.Country`: properties-based `@JsonCreator` ([method com.xxx.yyy.Country#fromValue(java.lang.String)]) expects String Value, got Object value (`JsonToken.START_OBJECT`)
```

Test
```java
@Test
void aTest() throws JsonProcessingException {
XmlMapper xmlMapper = new XmlMapper();

String s = xmlMapper.writeValueAsString(Country.ITALY);
assertThat(s).isEqualTo("<Country>Italy</Country>");

Country country = xmlMapper.readValue(s, Country.class);
assertThat(country).isEqualTo(Country.ITALY);
}
```

Country.java
```java
public enum Country {
    ITALY("Italy"),
    NETHERLANDS("Netherlands");

    private String value;

    Country(String value) {
        this.value = value;
    }

    @JsonValue
    public String getValue() {
        return value;
    }

    @Override
    public String toString() {
        return String.valueOf(value);
    }

    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static Country fromValue(String value) {
        for (Country b : Country.values()) {
            if (b.value.equals(value)) {
                return b;
            }
        }
        throw new IllegalArgumentException("Unexpected value '" + value + "'");
    }
}
```

Francesco Xia

unread,
Nov 6, 2024, 4:28:54 AM11/6/24
to jackson-user
sorry, I mistakenly duplicated the same conversation here: https://groups.google.com/g/jackson-user/c/kD_CVvK-3Xg

Tatu Saloranta

unread,
Nov 6, 2024, 9:44:48 PM11/6/24
to jackso...@googlegroups.com
I would file a bug report (issue on jackson-dataformat-xml).

The problem itself is that due to differences between logical content
model of XML and JSON, XmlMapper has to provide work-arounds to cases
like this -- since module exposes this:

<Country>Italy</Country>

as basically equivalent to same token sequence as:

{ "Country": "Italy" }

there's a need to "unwrap" String value. This should happen for plain
`String` type (you can try it), and probably even default Enums. But I
think this won't be done for custom constructors.
So that's missing handling.

A possible work-around would be to take in `JsonNode` (instead of
`String`) and handle `ObjectNode` case (for XML), and if necessary,
regular `TextNode` for other formats like JSON.

-+ Tatu +-
> --
> You received this message because you are subscribed to the Google Groups "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
> To view this discussion visit https://groups.google.com/d/msgid/jackson-user/439271ea-1295-42fd-ac25-e1f363d1f5a0n%40googlegroups.com.

Francesco Xia

unread,
Nov 13, 2024, 8:12:05 AM11/13/24
to jackson-user
for completeness this is the bug report opened on jackson-dataformat-xml
https://github.com/FasterXML/jackson-dataformat-xml/issues/682

Thank you for you assistance @tatu
Reply all
Reply to author
Forward
0 new messages