2

I have the following class:

package dictionary;

import java.io.Serializable;
import java.util.Objects;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlEnum;
import javax.xml.bind.annotation.XmlTransient;

public class Definition implements Serializable {

        private static final long serialVersionUID = 1L;

        @XmlEnum
        public enum GrammaticalCategory {
                MALE("m."),
                FEMALE("f."),
                ADJECTIVE("adj."),
                ALSO_NOUN("Ú. t. c. s."),
                ALSO_PLURAL("Ú. t. en pl."),
                MORE_PLURAL("Ú. m. en pl."),
                ADVERB("adv."),
                ARTICLE("art."),
                CONJUNCTION("conj."),
                PRONOMINAL_VERB("prnl."),
                FIGURATIVE_PHRASE("fr. fig."),
                ADVERBIAL_PHRASE("loc. adv."),
                CONJUNCTIVE_PHRASE("loc. conj."),
                TRANSITIVE_VERB("tr."),
                INTRANSITIVE_VERB("intr."),
                ALSO_PRONOMINAL("ú. t. c. prnl."),
                INTERJECTION("interj."),
                ONOMATOPEIA("onom.");

                // Category abbreviation in spanish.
                @XmlTransient
                public final String esAbbrev;

                GrammaticalCategory(String esAbbrev) {
                        this.esAbbrev = esAbbrev;
                }

                /*
                 * Returns the GrammaticalCategory object determined by its spanish abbreviation.
                 * Returns null if esAbbrev == null or the spanish abbreviation is unknown.
                 */
                public static GrammaticalCategory fromEsAbbrev(String esAbbrev) {
                        if (esAbbrev == null) {
                                return null;
                        }

                        for (GrammaticalCategory cat : values()) {
                                if (cat.esAbbrev.equalsIgnoreCase(esAbbrev)) {
                                        return cat;
                                }
                        }

                        return null;
                }
        }

        private String definition;

        private GrammaticalCategory category;

        public Definition() {
                this("");
        }

        public Definition(String string) throws IllegalArgumentException {
                this(string, null);
        }

        public Definition(String string, GrammaticalCategory category) {
                if (string == null) {
                        throw new IllegalArgumentException("string for the definition cannot be null");
                }
                this.definition = string;
                this.category = category;
        }

        @XmlElement(name = "definition")
        public String getDefinition() {
                return definition;
        }

        public void setDefinition(String definition) {
                this.definition = definition;
        }

        // Returns the category of the definition, or null if there is no category associated.
        @XmlElement(name = "cat")
        public GrammaticalCategory getCategory() {
                return category;
        }

        public void setCategory(GrammaticalCategory cat) {
                category = cat;
        }

        public String toString() {
                return "Definition[definition=" + definition + (category != null ? ",cat=" + category.name() : "");
        }

        public boolean equals(Object o) {
                if (o instanceof Definition) {
                        Definition other = (Definition) o;
                        return definition.equals(definition) && Objects.equals(category, other.category);
                }

                return false;
        }
}

And I am trying to serialize it using JAXB. The enum field is not being written. Why?

I have annoted as @XmlTransient the String field inside the enum because I just want the name of the enum to be serialized.

José D.
  • 4,175
  • 7
  • 28
  • 47

1 Answers1

2

Enums in Java are meant to be immutable objects, in which case there is no point in serializing any of their fields.

JAXB serializes enums only by writing out their name, nothing else. You do not need to use the @XmlTransient annotation on the fields of an enum, they won't be serialized even if you omit it.

Your class is serialized properly for me, with or without the @XmlTransient.

Serialization code:

Definition d = new Definition();
d.setCategory(GrammaticalCategory.ADJECTIVE);
d.setDefinition("testdefinition");

JAXB.marshal(d, new File("out.xml"));

Output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definition>
    <cat>ADJECTIVE</cat>
    <definition>testdefinition</definition>
</definition>

My guess is that you haven't set the category with the setCategory() method, and if a property is null, it will not appear in the output XML.

icza
  • 389,944
  • 63
  • 907
  • 827