GSON workaround for enum and int

I get json response from server. And I am parsing it with the GSON library.

The key in json is an integer value. How is it possible, without changing the server response (this is the external interface of the server, there is no influence on us) in order to pass an integer value to an enumeration?

Thank.

UPD:

Answer json-Response. NOTE: we cannot change it.

"testObject":{
"id":123,
"type":42
}

Listing:

public enum ObjectTypeEnum
{
    UNKNOWN_TYPE(0),
    SIMPLE_TYPE(11),
    COMPLEX_TYPE(42);

    private int value;

    private ObjectTypeEnum(int value)
    {
        this.value = value;         
    }

    public static ObjectTypeEnum findByAbbr(int value)
    {
        for (ObjectTypeEnum currEnum : ObjectTypeEnum.values())
        {
            if (currEnum.value == value)
            {
                return currEnum;
            }
        }

        return null;
    }

    public int getValue()
    {
        return value;
    }
}

And the class of the object

public class TestObject
{
    publuc int id;
    public ObjectTypeEnum type; 
}
+8
source share
5 answers

Using the answer from Chin and the help of my neighbor, I get the following solution.

I wrote an inner class in a parser class.

private static class ObjectTypeDeserializer implements
        JsonDeserializer<ObjectTypeEnum>
{
    @Override
    public PreconditioningStatusEnum deserialize(JsonElement json,
            Type typeOfT, JsonDeserializationContext ctx)
            throws JsonParseException
    {
        int typeInt = json.getAsInt();
        return ObjectTypeEnum
                .findByAbbr(typeInt);
    }
}

and created a GSON-Object as follows:

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(ObjectTypeEnum.class, new ObjectTypeDeserializer() );
    Gson gson = gsonBuilder.create();

http://sites.google.com/site/gson/gson-user-guide#TOC-Custom-Serialization-and-Deserializ

+12
source

@SerializedName, , / . TypeAdapter.

import com.google.gson.annotations.SerializedName;

public enum ObjectTypeEnum {
    @SerializedName("0")
    UNKNOWN_TYPE(0),

    @SerializedName("11")
    SIMPLE_TYPE(11),

    @SerialziedName("42")
    COMPLEX_TYPE(42);

    private int value;

    private ObjectTypeEnum(int value) {
        this.value = value;         
    }

    public int getValue() {
        return value;
    }
}

, "" .

public enum ObjectTypeEnum {
    @SerializedName("0")
    UNKNOWN_TYPE,

    @SerializedName("11")
    SIMPLE_TYPE,

    @SerialziedName("42")
    COMPLEX_TYPE;
}
+19
public enum Color {
    GREEN(1), BLUE(2), RED(3);

    private int key;

    private Color(int key) {
        this.key = key;
    }

    public static Color findByAbbr(int key) {
        for (Color c : values()) {
            if (c.key == key) {
                return c;
            }
        }
        return null;
    }
}
+4

SO, , Mur Votema , ;

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(ObjectTypeEnum.class, new ObjectTypeDeserializer() );
Gson gson = gsonBuilder.create();

; , gsonBuilder.

, ! , .

+4

, Kotlin:

  enum class ObjectTypeEnum(val value:Int) {
    @SerializedName("0")
    UNKNOWN_TYPE(0),
    @SerializedName("11")
    SIMPLE_TYPE(11),
    @SerializedName("42")
    COMPLEX_TYPE(42)
  }

Int:

  enum class ObjectTypeEnum {
    @SerializedName("0")
    UNKNOWN_TYPE,
    @SerializedName("11")
    SIMPLE_TYPE,
    @SerializedName("42")
    COMPLEX_TYPE
  }
0

All Articles