[json-lib-user] JSONObject.toBean() function fails on Bean with Bean[] setter/getter
Brought to you by:
aalmiray
From: Yuen C. L. <yue...@gm...> - 2007-09-29 05:21:46
|
Hi guys, I am choosing in between SOJO and JSON-lib for my new project. JSON-lib seems to be well-maintained however I am having a difficulty to convert from JSON to a bean with this structure: BeanA{ get/set: BeanB[] get/set: String, whatever } BeanB{ get/set: String, whatever } Sample code and error: JSON json = JSONSerializer.toJSON(REMOTE_ISSUE_STRING_1); Map map = new HashMap(); map.put("customFieldValues", RemoteCustomFieldValue.class); JSONObject.toBean((JSONObject) json, RemoteIssue.class, map); net.sf.json.JSONException: Error while setting property=customfieldId typeclass java.lang.String at net.sf.json.JSONObject.toBean(JSONObject.java:453) at net.sf.json.JSONArray.toArray(JSONArray.java:294) at net.sf.json.JSONObject.toBean(JSONObject.java:386) at org.mule.providers.jira.util.SojoUtilsTestCase.testJsonLibTypeFunctionalities (SojoUtilsTestCase.java:48) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke( NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke( DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at junit.framework.TestCase.runTest(TestCase.java:154) at junit.framework.TestCase.runBare(TestCase.java:127) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:118) at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run( JUnit3TestReference.java:128) at org.eclipse.jdt.internal.junit.runner.TestExecution.run( TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests( RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests( RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run( RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main( RemoteTestRunner.java:196) Caused by: java.lang.NoSuchMethodException: Unknown property 'customfieldId' at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty( PropertyUtilsBean.java:1741) at org.apache.commons.beanutils.PropertyUtils.setSimpleProperty( PropertyUtils.java:577) at net.sf.json.JSONObject.setProperty(JSONObject.java:950) at net.sf.json.JSONObject.toBean(JSONObject.java:418) ... 19 more By doing some debugging and tracing, I found these lines in JSONObject: if( List.class.isAssignableFrom( pd.getPropertyType() ) ){ Class targetClass = findTargetClass( key, classMap ); targetClass = targetClass == null ? findTargetClass( name, classMap ) : targetClass; ... }else{ Object array = JSONArray.toArray( (JSONArray) value, beanClass, classMap ); It is obvious that beanClass as RemoteIssue is not going to work. I have tried putting RemoteFieldValue[].class into the classMap too, but I knew that's not going to change anything. Is there any workaround that you guys can suggest? Or should I raise a feature/bug request on this? -- Cheers, Yuen-Chi Lian "I do not seek; I find." - Pablo Picasso |