Skip to content
Advertisement

Jackson serializes Enum to NAME not value (XML) Java

I have this Enum defined:

public enum OutputFormatEnum {
    PDF("pdf"),

    DOCX("docx"),

    XLSX("xlsx"),

    PPTX("pptx"),

    HTML("html"),

    PRN("prn"),

    CSV("csv"),

    RTF("rtf"),

    JPG("jpg"),

    PNG("png"),

    SVG("svg"),

    EPS("eps"),

    BMP("bmp"),

    GIF("gif"),

    TXT("txt");

    private String value;

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

    public String getValue() {
        return value;
    }

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

    public static OutputFormatEnum fromValue(String value) {
        for (OutputFormatEnum b : OutputFormatEnum.values())
            if (b.value.equals(value))
                return b;
        throw new IllegalArgumentException("Unexpected value '" + value + "'");
    }
}

I have a model object that has a OutputFormatEnum variable called OutputFormat. When I set this variable to OutputFormatEnum.PDF, it is initially registered as “pdf” which is what it should be. When I then execute the following code:

ByteArrayOutputStream streamTemplate = new ByteArrayOutputStream();
xmlMapper.writeValue(streamTemplate, {model_object_with_enum_variable});

It sets the value of OutputFormat in streamTemplate to “PDF”, where it should be “pdf” (the value of OutputFormatEnum.PDF). Any idea as to why this is happening?

Advertisement

Answer

You need to add @JsonValue to the getValue() method so that this method is used by Jackson to serialize the instance:

public enum OutputFormatEnum {
    PDF("pdf"),

    DOCX("docx"),

    XLSX("xlsx"),

    PPTX("pptx"),

    HTML("html"),

    PRN("prn"),

    CSV("csv"),

    RTF("rtf"),

    JPG("jpg"),

    PNG("png"),

    SVG("svg"),

    EPS("eps"),

    BMP("bmp"),

    GIF("gif"),

    TXT("txt");

    private String value;

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

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

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

    public static OutputFormatEnum fromValue(String value) {
        for (OutputFormatEnum b : OutputFormatEnum.values())
            if (b.value.equals(value))
                return b;
        throw new IllegalArgumentException("Unexpected value '" + value + "'");
    }
}

Given that you are serializing this to XML, you might need to use @XmlValue instead:

public enum OutputFormatEnum {
    PDF("pdf"),

    DOCX("docx"),

    XLSX("xlsx"),

    PPTX("pptx"),

    HTML("html"),

    PRN("prn"),

    CSV("csv"),

    RTF("rtf"),

    JPG("jpg"),

    PNG("png"),

    SVG("svg"),

    EPS("eps"),

    BMP("bmp"),

    GIF("gif"),

    TXT("txt");

    private String value;

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

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

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

    public static OutputFormatEnum fromValue(String value) {
        for (OutputFormatEnum b : OutputFormatEnum.values())
            if (b.value.equals(value))
                return b;
        throw new IllegalArgumentException("Unexpected value '" + value + "'");
    }
}

In addition to this, you need to enable the support for the standard JAXB annotations as follows:

xmlMapper.registerModule(new JaxbAnnotationModule());
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement