Deserializing an enum with int values

Tom VH
  • Tom VH

    Tom VH - 2013-01-16

    Hi, we are creating a JSON REST client application that has to communicate with a service written in C#.
    Most things like difference in dates etc are solved pretty easily with the FlexJson library.

    But one thing doesn't: Enum values that are sent as an integer, which is the value received from the service, and that have to be mapped to their Java Enum value.

    The Java enum is ok, we can convert integers to the enum as long as the value exists of course.
    But we do not succeed to get the Flexjson to convert the value.

    One of the Enums is called FormState

    We wrote a custom Transformer let's call it OurEnumTransformer which extends AbstractTransformer and implements ObjectFactory.
    Upon deserialization we add the .use(Enum.class, OurEnumTransformer), if we don't we get an error like:
    Don't know how to convert 4 to enumerated constant of FormState, which makes sense as it is an integer and not a name of an enum value

    But we add the .use(…) we keep getting an error on deserialization:
    FormState lacks a no argument constructor.  Flexjson will instantiate any protected, private, or public no-arg constructor.

    But it does actually have a private parameterless constructor.

    Another thing is that a breakpoint that is set in the OurEnumTransformer is never hit.

    So can anyone help me why .use(Enum.class, OurEnumTransformer) does not work with an enum that has integer values?

    The code of the enum and factory is below

    public enum FormState {
        private int value;
        private FormState() {
            this.value= -1;
        private FormState(int value) {
            this.value= value;
        public int getValue()
            return value;
        private static final Map<Integer, FormState> intToTypeMap = new HashMap<Integer, FormState>();
            for(FormState type: FormState.values())
                intToTypeMap.put(type.value, type); 
        public static FormState fromInt(int i) {
            FormState type = intToTypeMap.get(Integer.valueOf(i));
            if (type == null) 
                return FormState.DLT_UNKNOWN;
            return type;


    public final class InnectisEnumTransformer extends AbstractTransformer implements
            ObjectFactory {
        public Object instantiate(ObjectBinder context, Object value, Type targetType,
                Class targetClass) 
                return FormState.fromInt((Integer)value);
            else if(targetClass.equals(TrajectState.class))
                return TrajectState.fromInt((Integer)value);
                throw new JSONException(String.format("%s:  Don't know how to convert %s to enumerated constant of %s",
                                             context.getCurrentPath(), value, targetType));
        public void transform(Object arg0) {
            // TODO Auto-generated method stub
  • Charlie Hubbard

    Charlie Hubbard - 2013-01-16

    So I tried this out in a unit test and the good news is that it worked.  However, the bad news is I think you've stumbled onto a bug that is fixed in the main branch, but hasn't been released yet.  I told someone last week we'd release very soon, and I'm getting ready to do that now.  I'll probably release tonight or tomorrow morning so you can upgrade and get the latest and have this fixed very shortly.


Log in to post a comment.