I have an XML String with an attribute that contains integer value:
<item status="2" />
There is also Simple Framework class that describes this item:
@Root
public static class Item {
@Attribute(name="status")
private int status;
public int getStatus() {
return status;
}
}
Unserialization works well, however I want to be able to change int type to the defined enum type.
public enum Status {
OK(0), PENDING(1), ERROR(2);
BetStatus(int status) {
this.status = status;
}
public int getStatus() {
return status;
}
}
So with a quick modification:
@Root
public static class Item {
@Attribute(name="status")
private Status status;
public Status getStatus() {
return status;
}
}
However now I get an error:
java.lang.IllegalArgumentException: 2 is not a constant in com.my.package.Status
Is it possible to cast int this way during unserialization ?
I bet I have to add some magic method to my Status class.
Solution:
According to Reimeus answer I leaved int type for status attribute and I modified just Item class with:
@Root
public static class Item {
@Attribute(name="status")
private int status;
public Status getStatus() {
return Status.getByOrdinal(status);
}
}
Perhaps by iterating through the types: