简体   繁体   English

列表中的枚举值的持久性注释

[英]Persistance annotation for enum values in a list

To convert Java Objects to and from JSON, we are using annotation on enum types. 为了将Java对象与JSON相互转换,我们在enum类型上使用了注释。 Suppose we have this type: 假设我们有这种类型:

public enum MyEnum {
  HELLO,
  WORLD
}

Then we are using it in this manner: 然后我们以这种方式使用它:

import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import java.io.Serializable;
import java.util.List;

public class SerializableObject implements Serializable{
  @Enumerated(EnumType.STRING)
  private MyEnum singleValue;
  // What annotation do I put here?
  private List<MyEnum> multiValue;
}

My question is: What annotation goes onto the list multiValue so that the contained enum values are properly serialized? 我的问题是:哪些注释进入了multiValue列表,以便正确枚举所包含的枚举值?

You can use EnumType.ORDINAL 您可以使用EnumType.ORDINAL

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.persistence.EnumType;
import javax.persistence.Enumerated;

enum MyEnum {
    HELLO, WORLD
}


public class SerializableObject implements Serializable {
    @Enumerated(EnumType.STRING)
    private MyEnum       singleValue;
    // What annotation do I put here?
    @Enumerated(EnumType.ORDINAL)
    private List<MyEnum> multiValue;


    public void show() {

        multiValue = new ArrayList<>(Arrays.asList(MyEnum.values()));
        multiValue.stream().forEach(
                element -> System.out.println(element.ordinal() + " " + element.toString()));
    }


    public static void main(String[] args) {

        new SerializableObject().show();
    }
}

Here is the fullcode for doing serialization and Deserialization.Hope this helps to explore more. 这是进行序列化和反序列化的完整代码,希望这有助于探索更多内容。

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;

enum MyEnum {
    HELLO, WORLD
}



public class SerializableObject implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Enumerated(EnumType.ORDINAL)
    private MyEnum       singleValue;
    // What annotation do I put here?
   @Enumerated(EnumType.STRING)
    private List<MyEnum> multiValue;


    public void show() {

        multiValue = new ArrayList<>(Arrays.asList(MyEnum.values()));
        singleValue=MyEnum.HELLO;

    }



    public static void main(String[] args) throws JsonProcessingException {

        SerializableObject a = new SerializableObject();
        a.show();
        try {
            FileOutputStream fileOut = new FileOutputStream("E:\\path\\Crunchify_Test1.txt");
            ObjectOutputStream out = new ObjectOutputStream(fileOut);

            out.writeObject(a);
            out.close();
            fileOut.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        try {
            FileInputStream fileIn = new FileInputStream("E:\\path\\Crunchify_Test1.txt");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            // List<MyEnum> multiValue=(List<MyEnum>) in.readObject();
            System.out.println("Deserialized Data: \n" + in.readObject().toString());
            in.close();
            fileIn.close();
        } catch (Exception e) {
        }
    }



    @Override
    public String toString() {

        return "SerializableObject [singleValue=" + singleValue + ", multiValue=" + multiValue
                + "]";
    }



}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM