Java : Convert Object consisting enum to Json Object

后端 未结 5 612
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-30 03:13

I am using org.json library to convert Object to Json format. Kindly check the below code snippet.

public enum JobStatus implements Serializable{
     INCOMP         


        
5条回答
  •  我在风中等你
    2020-12-30 03:44

    for me, i made an interface that shuold be implemented by any enum i will have to use in Json, this interface forces the enum to know the proper enum itself from a value, and also it should return a value ... so every enum.CONSTANT is mapped to a value of any type (weather a number or a String)

    so when i want to put this enum in a Json Object, i ask the enum.CONSTANT to give me it's value, and when i have this value (from Json), i can request from the enum to give me the proper enum.CONSTANT mapped to this value

    the interface is as follows (you can copy it as it is) :

    /**
     * 
     * this interface is intended for {@code enums} (or similar classes that needs
     * to be identified by a value) who are based on a value for each constant,
     * where it has the utility methods to identify the type ({@code enum} constant)
     * based on the value passed, and can declare it's value in the interface as
     * well
     * 
     * @param 
     *            the type of the constants (pass the {@code enum} as a type)
     * @param 
     *            the type of the value which identifies this constant
     */
    public interface Valueable, V> {
    
        /**
         * get the Type based on the passed value
         * 
         * @param value
         *            the value that identifies the Type
         * @return the Type
         */
        T getType(V value);
    
        /**
         * get the value that identifies this type
         * 
         * @return a value that can be used later in {@link #getType(Object)}
         */
        V getValue();
    }
    

    now here is an example for a small enum implementing this interface:

    public enum AreaType implements Valueable {
        NONE(0),
        AREA(1),
        NEIGHBORHOOD(2);
    
        private int value;
    
        AreaType(int value) {
            this.value = value;
        }
    
        @Override
        public AreaType getType(Integer value) {
    
            if(value == null){
                // assume this is the default
                return NONE;
            }
    
            for(AreaType a : values()){
                if(a.value == value){ // or you can use value.equals(a.value)
                    return a;
                }
            }
            // assume this is the default
            return NONE;
        }
    
        @Override
        public Integer getValue() {
            return value;
        }
    
    }
    

    to save this enum in a Json :

    AreaType areaType = ...;
    jsonObject.put(TAG,areaType.getValue());
    

    now to get your value from a Json Object :

    int areaValue = jsonObject.optInt(TAG,-1);
    AreaType areaType = AreaType.NONE.getType(areaValue);
    

    so if the areaValue is 1 for example, the AreaType will be "Area", and so on

提交回复
热议问题