Hi
I use json-lib-1.0-jdk15 and class with enum property:
class MyBean{
private JEnum jEnum;
...
}
enum JEnum{
VAL_1, VAL_2
}
When I call
JSONObject.fromBean(new MyBean()).toString();
I get {"JEnum":"VAL_1"} string, thats seems to be ok.
But, when I try to convert this string back to MyBean calling next line
JSONObject.toBean(JSONObject.fromString("{\"JEnum\":\"VAL_1\"}"), MyBean);
I get java.lang.IllegalArgumentException: Cannot invoke MyBean.setJEnum - argument type mismatch
There is any solution for this problem?
Thanks
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You have to register an EnumMorpher that wraps the target Enum, don't worry
you can wrap and register as many target Enums as needed.
I see now that this is not intuitive and I think an explanation is missing on
the page. Also this may change in a future major release, as the type handling
mechanism is being reworked form scratch to support custom serialization.
Hope this helps you.
-- Andres
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi
I use json-lib-1.0-jdk15 and class with enum property:
class MyBean{
private JEnum jEnum;
...
}
enum JEnum{
VAL_1, VAL_2
}
When I call
JSONObject.fromBean(new MyBean()).toString();
I get {"JEnum":"VAL_1"} string, thats seems to be ok.
But, when I try to convert this string back to MyBean calling next line
JSONObject.toBean(JSONObject.fromString("{\"JEnum\":\"VAL_1\"}"), MyBean);
I get java.lang.IllegalArgumentException: Cannot invoke MyBean.setJEnum - argument type mismatch
There is any solution for this problem?
Thanks
IRedfield,
It does work but the configuration is not as you would expect, try the following
JSONUtils.getMorpherRegistry()
.registerMorpher( new EnumMorpher( JsonEnum.class ) );
JSONObject json = new JSONObject();
json.put( "jsonEnum", "OBJECT" );
EnumBean bean = (EnumBean) JSONObject.toBean( json, EnumBean.class );
assertNotNull( bean );
assertEquals( bean.getJsonEnum(), JsonEnum.OBJECT )
You have to register an EnumMorpher that wraps the target Enum, don't worry
you can wrap and register as many target Enums as needed.
I see now that this is not intuitive and I think an explanation is missing on
the page. Also this may change in a future major release, as the type handling
mechanism is being reworked form scratch to support custom serialization.
Hope this helps you.
-- Andres