Re: [json-lib-user] JSONObject.toBean() function fails on Bean with Bean[] setter/getter
Brought to you by:
aalmiray
From: Andres A. <aal...@ya...> - 2007-09-29 15:24:15
|
Hi Lian,=0A=0AIt seems to me that you are using Json-lib 2.0, which has a b= ug when serializing back to Java and the bean has an array property of non-= primitive type. It has been fixed for 2.1-SNAPSHOT. You may download a copy= from cvs and build yourself the jars or head to=0Ahttp://json-lib.sourcefo= rge.net/m2/repo/snapshot and download the latest snapshot.=0A=0APlease let = us know if it didn't work for you.=0A=0ACheers,=0AAndres=0A =0A------------= -------------------------------=0Ahttp://jroller.com/page/aalmiray=0Ahttp:/= /www.linkedin.com/in/aalmiray=0A--=0AWhat goes up, must come down. Ask any = system administrator.=0AThere are 10 types of people in the world: Those wh= o understand binary, and those who don't.=0ATo understand recursion, we= must first understand recursion.=0A=0A----- Mensaje original ----=0ADe: Yu= en Chi Lian <yue...@gm...>=0APara: jso...@li...urceforg= e.net=0AEnviado: s=E1bado, 29 de septiembre, 2007 0:21:42=0AAsunto: [json-l= ib-user] JSONObject.toBean() function fails on Bean with Bean[] setter/gett= er=0A=0AHi guys,=0A=0AI am choosing in between SOJO and JSON-lib for my new= project. JSON-lib seems to be well-maintained however I am having a diffic= ulty to convert from JSON to a bean with this structure:=0A=0ABeanA{=0A ge= t/set: BeanB[]=0A=0A get/set: String, whatever=0A}=0A=0ABeanB{=0A get/set= : String, whatever=0A}=0A=0ASample code and error:=0A=0AJSON json =3D JSONS= erializer.toJSON(REMOTE_ISSUE_STRING_1);=0AMap map =3D new HashMap();=0Amap= .put("customFieldValues", =0ARemoteCustomFieldValue.class);=0AJSONObject.to= Bean((JSONObject) json, RemoteIssue.class, map);=0A=0Anet.sf.json.JSONExcep= tion: Error while setting property=3DcustomfieldId typeclass java.lang.Stri= ng=0A at =0Anet.sf.json.JSONObject.toBean(JSONObject.java:453)=0A at = net.sf.json.JSONArray.toArray(JSONArray.java:294)=0A at net.sf.json.JSON= Object.toBean(JSONObject.java:386)=0A at org.mule.providers.jira.util.So= joUtilsTestCase.testJsonLibTypeFunctionalities=0A(SojoUtilsTestCase.java:48= )=0A at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)=0A = at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.j= ava:39)=0A at sun.reflect.DelegatingMethodAccessorImpl.invoke=0A(Delegat= ingMethodAccessorImpl.java:25)=0A at java.lang.reflect.Method.invoke(Met= hod.java:585)=0A at junit.framework.TestCase.runTest(TestCase.java:154)= =0A at junit.framework.TestCase.runBare(TestCase.java:127)=0A=0A at j= unit.framework.TestResult$1.protect(TestResult.java:106)=0A at junit.fra= mework.TestResult.runProtected(TestResult.java:124)=0A at junit.framewor= k.TestResult.run(TestResult.java:109)=0A at junit.framework.TestCase.run= =0A(TestCase.java:118)=0A at org.eclipse.jdt.internal.junit.runner.junit= 3.JUnit3TestReference.run(JUnit3TestReference.java:128)=0A at org.eclips= e.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)=0A=0A = at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(Remot= eTestRunner.java:460)=0A at org.eclipse.jdt.internal.junit.runner.Remote= TestRunner.runTests(RemoteTestRunner.java:673)=0A at org.eclipse.jdt.int= ernal.junit.runner.RemoteTestRunner.run=0A(RemoteTestRunner.java:386)=0A = at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestR= unner.java:196)=0ACaused by: java.lang.NoSuchMethodException: Unknown prope= rty 'customfieldId'=0A at =0Aorg.apache.commons.beanutils.PropertyUtilsB= ean.setSimpleProperty(PropertyUtilsBean.java:1741)=0A at org.apache.comm= ons.beanutils.PropertyUtils.setSimpleProperty(PropertyUtils.java:577)=0A = at net.sf.json.JSONObject.setProperty=0A(JSONObject.java:950)=0A at net= .sf.json.JSONObject.toBean(JSONObject.java:418)=0A ... 19 more=0A=0ABy d= oing some debugging and tracing, I found these lines in JSONObject:=0A=0Aif= ( List.class.isAssignableFrom( =0Apd.getPropertyType() ) ){=0A = Class targetClass =3D findTargetClass( key, classMap );=0A = targetClass =3D targetClass =3D=3D null ? findTargetClass( name= , classMap )=0A : targetClass;=0A=0A...=0A = }else{=0A Object array =3D JSONArray.toArray= ( (JSONArray) value, beanClass, classMap );=0A=0AIt is obvious that beanCla= ss as RemoteIssue is not going to work. I have tried putting RemoteFieldVal= ue[].class into the classMap too, but I knew that's not going to change any= thing.=0A=0A=0AIs there any workaround that you guys can suggest? Or should= I raise a feature/bug request on this?=0A=0A-- =0ACheers,=0AYuen-Chi Lian= =0A=0A"I do not seek; I find." - Pablo Picasso=0A=0A=0A=0A=0A=0A=0A __= ___________________________________________________________________________= _______=0A=A1S=E9 un mejor fot=F3grafo!=0APerfecciona tu t=E9cnica y encuen= tra las mejores fotos. =0Ahttp://mx.yahoo.com/promos/= mejorfotografo.html |