I've a json object that I'm trying to convert to a simple java bean;
all the fields of the bean map directly to the json object except its enum field;
I tried the EnumMorpher but without success; the FAQ said just configure the morpher but I couldn't find any examples.
tsRequest is the json string object that is to be converted.
TSBean is the target bean; TSTypes is the enum;
textStyle is the enum field in TSBean and a field in tsRequest.
the error message indicated that actual values of the enum were expected, so I tried:
textStyleMorpher.morph("HL_YELLOW");
but the error message now said that TSTypes.hl_yellow is not a value.
any help would be greatly appreciated;
thank you,
Brian
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You may want to have a look at src/test/jdk15/net/sf/json/TestJSONObjectJDK15
Is your Enum doing something fancy in its constructor? can you please send a failing testcase?
Thanks,
Andres
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
not sure where is src/test/jdk15/net/sf/json/TestJSONObjectJDK15 but I found it at
koders.com and I couldn't see anything there that specified how to configure a morpher.
the conversion worked after changing the enum field to a string so the bean and the json object are both correct;
the problem is to set the conversion process so that the textStyle field of the json object (which has as a value either "HL_YELLOW" or "STRIKE_THRU") is mapped to the textStyle field of the bean which is an enum type.
using the code above with: textStyleMorpher.morph("textStyle");
yields the following error:
java.lang.IllegalArgumentException: No enum const class marker.html.HtmlUtil$TextStyleTypes.textStyle
at java.lang.Enum.valueOf(Unknown Source)
at net.sf.json.util.EnumMorpher.morph(EnumMorpher.java:44)
at marker.ConversionFactory.createTextSelecBean(ConversionFactory.java:63)
I hope this helps
thank you,
Brian
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Seems like your enum has only two values: [HL_YELLOW,STRIKE_THRU]so a string with value "textStyle" can not be converted into a value of your enum. I'm assuming you have a property named "textStyle" which holds the actual value. Just drop the line
textStyleMorpher.morph("textStyle");
and it should work.
And you are right about not finding TestJSONObjectJdk5 in the text xref, as the only tests maven uses for reference are the jdk3, I'll see the way to fix that.
-------------
so here is the code to manually register an EnumMorpher
I tried several interpretations of your remarks and each time bean returned as null.
for json.element( "jsonEnum", "OBJECT" );
I tried: json.element("textStyle", TSTypes.HL_YELLOW);
I tried: json.element("HL_YELLOW", TSTypes.HL_YELLOW);
textStyle is the name of a field in the json object and in TSBean.
the value of the field in the json object can be either: "HL_YELLOW" or "STRIKE_THRU".
I also tried converting a bean which just had one field of type TSTypes
but that also return a null bean.
Just to be sure we aren't talking at cross purposes:
TSBean is the target bean and it several fields of type String
and one field called textStyle which is of type TSTypes; TSTypes is an enum.
thank you,
Brian
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I've a json object that I'm trying to convert to a simple java bean;
all the fields of the bean map directly to the json object except its enum field;
I tried the EnumMorpher but without success; the FAQ said just configure the morpher but I couldn't find any examples.
tsRequest is the json string object that is to be converted.
TSBean is the target bean; TSTypes is the enum;
textStyle is the enum field in TSBean and a field in tsRequest.
My attempt was:
EnumMorpher tsMorpher = new EnumMorpher(TSTypes.class);
textStyleMorpher.morph("textStyle");
JSONUtils.getMorpherRegistry().registerMorpher( textStyleMorpher);
JSONObject jsSelec = JSONObject.fromObject(tsRequest);
TSBean tsbean = (TSBean)JSONObject.toBean(jsSelec,TSBean.class);
the error message indicated that actual values of the enum were expected, so I tried:
textStyleMorpher.morph("HL_YELLOW");
but the error message now said that TSTypes.hl_yellow is not a value.
any help would be greatly appreciated;
thank you,
Brian
You may want to have a look at src/test/jdk15/net/sf/json/TestJSONObjectJDK15
Is your Enum doing something fancy in its constructor? can you please send a failing testcase?
Thanks,
Andres
not sure where is src/test/jdk15/net/sf/json/TestJSONObjectJDK15 but I found it at
koders.com and I couldn't see anything there that specified how to configure a morpher.
the conversion worked after changing the enum field to a string so the bean and the json object are both correct;
the problem is to set the conversion process so that the textStyle field of the json object (which has as a value either "HL_YELLOW" or "STRIKE_THRU") is mapped to the textStyle field of the bean which is an enum type.
using the code above with: textStyleMorpher.morph("textStyle");
yields the following error:
java.lang.IllegalArgumentException: No enum const class marker.html.HtmlUtil$TextStyleTypes.textStyle
at java.lang.Enum.valueOf(Unknown Source)
at net.sf.json.util.EnumMorpher.morph(EnumMorpher.java:44)
at marker.ConversionFactory.createTextSelecBean(ConversionFactory.java:63)
I hope this helps
thank you,
Brian
Seems like your enum has only two values: [HL_YELLOW,STRIKE_THRU]so a string with value "textStyle" can not be converted into a value of your enum. I'm assuming you have a property named "textStyle" which holds the actual value. Just drop the line
textStyleMorpher.morph("textStyle");
and it should work.
And you are right about not finding TestJSONObjectJdk5 in the text xref, as the only tests maven uses for reference are the jdk3, I'll see the way to fix that.
-------------
so here is the code to manually register an EnumMorpher
JSONUtils.getMorpherRegistry()
.registerMorpher( new EnumMorpher( JsonEnum.class ) );
JSONObject json = new JSONObject();
json.element( "jsonEnum", "OBJECT" );
EnumBean bean = (EnumBean) JSONObject.toBean( json, EnumBean.class );
assertNotNull( bean );
assertEquals( bean.getJsonEnum(), JsonEnum.OBJECT );
Starting from the next version (2.2) a default EnumMorpher will be registered for you
// yields the same result as the previous sample
JSONObject json = new JSONObject();
json.element( "jsonEnum", "OBJECT" );
EnumBean bean = (EnumBean) JSONObject.toBean( json, EnumBean.class );
assertNotNull( bean );
assertEquals( bean.getJsonEnum(), JsonEnum.OBJECT );
thank you for your reply,
I tried several interpretations of your remarks and each time bean returned as null.
for json.element( "jsonEnum", "OBJECT" );
I tried: json.element("textStyle", TSTypes.HL_YELLOW);
I tried: json.element("HL_YELLOW", TSTypes.HL_YELLOW);
textStyle is the name of a field in the json object and in TSBean.
the value of the field in the json object can be either: "HL_YELLOW" or "STRIKE_THRU".
I also tried converting a bean which just had one field of type TSTypes
but that also return a null bean.
Just to be sure we aren't talking at cross purposes:
TSBean is the target bean and it several fields of type String
and one field called textStyle which is of type TSTypes; TSTypes is an enum.
thank you,
Brian
In that case it seems to me your code should work like EnumBean/JsonEnum does but somehow it does not.
What version of Json-lib are you using?
Can you try the following?
EnumMorpher tsMorpher = new EnumMorpher(TSTypes.class);
JSONUtils.getMorpherRegistry().registerMorpher( textStyleMorpher);
JSONObject jsSelec = JSONObject.fromObject(tsRequest);
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setRootClass( TSBean.class );
TSBean tsbean = (TSBean)JSONObject.toBean(jsSelec,jsonConfig);
I'm concerned that the returned bean is null...
Hi there,
my apologies; when I got home I thought I'd try another test;
saw an error in my code and now it is working;
I was hoping to post this afore you had a chance to reply ...
thank you again,
Brian
No problem. I'm glad it finally worked for you.
Please let me know if you need further assistance =)
Cheers,
Andres