2016-12-28 15 views
1

シリアル化可能クラスで宣言されたオブジェクト型enumをシリアル化しようとしています。私はすでにここですべての話題が赤ですが、私は私の問題に答えが見つかりませんでした。誰かが私をここで助けてくれることを願っています 私は実際にSTYPEとCbitSetを入力2つのオブジェクトをシリアル化しようとしていますが、私は以下の結果を得る:私は何か期待していシリアル化可能クラスに含まれるオブジェクト型enumのシリアル化

<rSIt> 
    <sType>S_V_INC</sType> 
    <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. 
:ここ

<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> 

は私のコードです

+0

これが私が得た結果です: –

+0

これは私が得た結果です: VI S_V_INC V株式会社 {長い[7] 5285 @} 及びIはを期待してい S_V_INC

+0

[タグの下のリンクを編集](http://stackoverflow.com/posts/41365773/edit)をクリックすると、投稿を編集できます。 – assylias

答えて

0

私も学んでいます...私は別のSOの質問(here)からの回答に同意する傾向があります

EnumのJavaは、不変なオブジェクトであることを意味します。この場合、 にはフィールドのシリアル化が行われません。あなたが指定したようにマーシャリング結果を得るために主張していること何らかの理由で、私はクラスにenumからSTypeを変更することでそれを行うことができることを発見し、それからという別のenumを埋め込むこと、ただし

EnumSTypeが入っています。

コードは次のようになります。

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> 
+1

あなたの迅速な対応のために非常にleeyuiwahありがとうございました。私がそれを行う場合、唯一の問題は、このenumクラスを使用している複数のプロジェクトを変更する必要があることです。私は別の選択肢があると思っています... –

+0

CbitSetビットオブジェクトに関する考え方はありませんか? –

+0

入力の例を教えてください。 – leeyuiwah

関連する問題