java - 可序列化类中包含的对象类型枚举的序列化

标签 java object serialization enums extending

我正在尝试序列化在可序列化类中声明的对象类型枚举。我已经在这里红色了所有主题,但我没有找到我的问题的答案。我希望有人能在这里帮助我。 我实际上正在尝试序列化 2 个对象类型 SType 和 CbitSet,但我得到以下结果:

<rSIt>
  <sType>S_V_INC</sType>
  <bits/>
  <mHave>true</mHave>
</rSIt>

我期待类似的事情:

<rSIt>
  <sType>
     <code>VI</code>
     <description>V Inc</description>
     <name>S_V_INC</name>
  </sType>
  <bits>
    <words>{long[7]@5285}</words>
    <wordsInUse>7</wordsInUse>
    <sizeIsSticky>false</sizeIsSticky>
  <bits>
  <mHave>true</mHave>
</rSIt>

这是我的代码:

    @XmlRootElement(name="rSIt")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class RSIt implements Serializable{
        private static final long serialVersionUID = -5848959699974019999L;
        @XmlElement
        private SType sType;
        @XmlElement
        private CbitSet bits = new CbitSet();
        @XmlElement
        private boolean mHave = true;

        public SType getSType() {
            return this.sType;
        }
        public void setSType(SType sType) {
            this.sType = sType;
        }
        public CbitSet getBits() {
            return this.bits;
        }

        public boolean ismHave() {
            return this.mHave;
        }
        public void setmHave(boolean mHave) {
            this.mHave = mHave;
        }

        public RSIt() {
            super();
        }

        public RSIt(SType sType, boolean mHave) {
            super();
            this.sType = sType;
            this.mHave = mHave;
        }

        public RSIt(SType sType, boolean mHave, Integer bit) {
            super();
            this.sType = sType;
            this.mHave = mHave;
            this.bits.set(bit);
        }
        }
    This is the implementation of SType  class (it is an Enum class):
    public enum SType {
        S_V_INC ("VI", "V Inc"),
        S_V_EXC ("VE", "V Exc"),
        S_RP_INC ("RI", "RP Inc"),
        S_RP_EXC ("RE", "RP Exc"),
        S_V_AN_F ("VA", "V F All");

        private final String code;
        private final String description;
        SearchType (String code, String description) {
            this.code = code;
            this.description = description;
        }
        public String getCode() {
            return code;
        }
        public String getDescription() {
            return description;
        }
    }
    This is the implementation of CbitSet class
    import java.util.BitSet;
    @XmlRootElement(name="rSIt")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class CbitSet extends BitSet implements Serializable{
        private static final long serialVersionUID = 476550000000055127L;

        private static final long longOne = 1;
        private static final long long64 = 64;

        public CbitSet() {
            super();
        }

        public long[] toLongArray() {
            long[] longs = new long[this.size() / 64];
            for (int i = 0; i < longs.length; i++)
                for (int j = 0; j < 64; j++)
                    if (this.get(i * 64 + j))
                        longs[i] |= longOne << j;
            return longs;
        }

        public void fromLongArray(long[] longs) {
            for (int i=0; i<longs.length*64; i++) {
                if ((longs[i/64]&(longOne<<(i%long64))) != 0) {
                    this.set(i);
                }
            }
        }

        public String toBitString() {
            StringBuilder sb = new StringBuilder();
            for (int x = 0; x < this.size(); x++) {
                if (this.get(x)) {
                    sb.append("1");
                } else {
                    sb.append("0");
                }
            }
            return sb.toString();       
        }

        public void fromBitString(String string) {
            int pos = 0;
            for (byte chr : string.getBytes()) {
                if ('1' == chr) {
                    this.set(pos);
                }
                pos++;
            }
        }
        public void set(List<Integer> bits) {
            set(bits,true);
        }
        public void set(List<Integer> bits, boolean value) {
            if (bits != null) {
                for (Integer bit : bits) {
                    if (bit != null) {
                        this.set(bit.intValue(), value);
                    }
                }
            }
        }
        public void set(Integer bitIndex) {
            if (bitIndex != null) {
                super.set(bitIndex);
            }
        }
        public void set(Integer bitIndex, boolean value) {
            if (bitIndex != null) {
                super.set(bitIndex, value);
            }
        }   
    }

    Thank you for your help guys.

最佳答案

我也在学习......并且我倾向于同意另一个SO问题(here)的答案

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

但是,如果出于任何原因,您坚持获取指定的编码结果,我发现可以通过将 STypeenum 更改为类,然后在其中嵌入另一个名为 EnumSTypeenum 来实现这一点。

代码如下所示:

package SerializationQuestion1;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@XmlRootElement(name="sType")
@XmlAccessorType(XmlAccessType.FIELD)   
public class SType {
    public enum EnumSType {
        S_V_INC ("VI", "V Inc"),
        S_V_EXC ("VE", "V Exc"),
        S_RP_INC ("RI", "RP Inc"),
        S_RP_EXC ("RE", "RP Exc"),
        S_V_AN_F ("VA", "V F All");

        @XmlElement
        private final String code;
        @XmlElement
        private final String description;
        EnumSType (String code, String description) {   // Bug fix 1
            this.code = code;
            this.description = description;
        }
        public String getCode() {
            return code;
        }
        public String getDescription() {
            return description;
        }
    }

    @XmlTransient
    private EnumSType sType;

    public EnumSType getsType() {
        return sType;
    }

    public void setsType(EnumSType sType) {
        this.sType = sType;
    }

    public SType () {};

    public SType (EnumSType sType) {
        this.sType = sType;
    }

    @XmlElement
    public String getCode() { return this.sType.getCode();};

    @XmlElement
    public String getDescription() { return this.sType.getDescription();};

    @XmlElement
    public String getName() { return this.sType.name();};
}

要实例化 RSIt 对象,代码现在如下所示:

RSIt rsit = new RSIt(new SType(EnumSType.S_V_INC), true, null);

以下是我的整理输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rSIt>
    <sType>
        <code>VI</code>
        <description>V Inc</description>
        <name>S_V_INC</name>
    </sType>
    <bits/>
    <mHave>true</mHave>
</rSIt>

关于java - 可序列化类中包含的对象类型枚举的序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41365773/

相关文章:

c++ - 如何将动态对象数组传递给另一个类的参数化构造函数?

c# - 如何更新序列化动态 JSON 中的元素值

java - 为什么使用 ActionListener 会破坏我的 Swing Applet?

java - 如何一次将用户输入与多个变量进行比较?

java - 当自定义事务管理器连接失败时,Spring Boot应用程序不会快速失败

python - 为什么 Django Rest Framework 不显示发布的纬度?

c++ - 使用 StrTk String Toolkit Library 编写 CSV 的示例

java - 如何将类对象作为方法参数调用

javascript - 我的代码没有正确比较两个不同的字符串

typescript 中的数组