Re: [json-lib-user] Casting classes
Brought to you by:
aalmiray
From: Andres A. <aal...@ya...> - 2007-09-19 19:33:44
|
Hi Jerry,=0A=0AA quick test reveals that your problem is indeed a bug but h= as already=0Abeen fixed in the current development version, though an offic= ial release has not yet been made. The following code shows that when the '= nanos' property is being=0Aprocessed it will be skipped because the target = bean doesn't have a=0APropertyDescriptor for it.=0A=0A=0A=0A{"value":0,"dat= e":{"month":8,"day":3,"year":107,"nanos":0,"time":1190230433953,"seconds":5= 3,"timezoneOffset":300,"date":19,"hours":14,"minutes":33}}=0A=0A[07-09-19 1= 4:33:54] WARN [JSONObject:310] Property 'day' has no write method. SKIPPED= .=0A=0A[07-09-19 14:33:54] WARN [JSONObject:395] Tried to assign property = nanos:java.lang.Integer to bean of class java.util.Date=0A=0A[07-09-19 14:3= 3:54] WARN [JSONObject:310] Property 'timezoneOffset' has no write method.= SKIPPED.=0A=0A=0A=0AThere is no need to define exclusions or filters throu= gh JsonConfig (though you may do it to get rid of the warning).=0A=0AJson-l= ib 2.1-SNAPSHOT is available from http://json-lib.sourceforge.net/m2/repo/s= napshot/=0A=0AAn official release of 2.1 may take a little more time to all= ow for a couple of feature requests to be finished.=0A=0A=0A=0ACheers,=0A= =0AAndres =0A-------------------------------------------=0Ahttp://jroller.c= om/page/aalmiray=0Ahttp://www.linkedin.com/in/aalmiray=0A--=0AWhat goes up,= must come down. Ask any system administrator.=0AThere are 10 types of peop= le in the world: Those who understand binary, and those who don't.=0ATo= understand recursion, we must first understand recursion.=0A=0A----- Mensa= je original ----=0ADe: Gerardo Blanco <Bla...@ld...>=0APara: Andr= es Almiray <aal...@ya...>; jso...@li...=0AEnvi= ado: mi=E9rcoles, 19 de septiembre, 2007 11:36:42=0AAsunto: Re: [json-lib-u= ser] Casting classes=0A=0A=0A=0A =0A=0A=0A=0A=0A=0A=0A=0AI'm using version = 2. =0A=0A=0A =0A=0A=0AThe exception I got was =0A=0A=0A =0A=0A=0Anet.sf.jso= n.JSONException: Error while setting property=3Dnanos typeclass java.lang.I= ntegerHere's the code:JSONObject jsonObject =3D JSONObject.fromObject(myBea= n);System.out.println(myBean);MyBean b =3D (MyBean) JSONObject.toBean(jsonO= bject, MyBean.class);=0ASystem.out.println(b); Here's the bean def:import j= ava.util.Date;public class MyBean {...@Column(name=3D"PS_EFFECTIVE_DATE")= =0ADate psEffectiveDate;=0A...public Date getPsEffectiveDate() {=0A retur= n psEffectiveDate;=0A}=0A...public void setPsEffectiveDate(Date psEffective= Date) {=0A this.psEffectiveDate =3D psEffectiveDate;=0A }...}And this is t= he section of the JSON string that shows the object:,"psEffectiveDate":{"mo= nth":0,"day":1,"year":0,"nanos":0,"time":-2208963600000,"seconds":0,"timezo= neOffset":420,"date":1,"hours":0,"minutes":0},If I comment out the last two= lines in the code (where I create a new MyBean, etc), the whole process wo= rks fine. If I leave them, it fails when creating the bean b. Any comments?= -------------------------------------------------------=0AGerardo (Jerry) = R. Blanco =0ASoftware Engineer=0ALDS Platform Team=0A(801) 240-6978=0ABlanc= oG...@ld... =0A------------------------------------------------------= -=0A"I am endeavoring, ma'am, to construct a mnemonic memory circuit, using= stone knives and bearskins." -- Spock=0A>>> Andres Almiray <aalmiray@yahoo= .com> 9/19/2007 9:41 AM >>>=0A=0A=0A=0A=0A=0AHi Jerry,=0ASince version 0.9 = (I think) there is the option to specify a set of exclusions that will let = filter out some properties when using JSONObject.fromObject(), in your case= =0A=0A/* available in json-lib 1.x */=0AJSON json =3D JSONSerializer.toJSON= ( bean, new String[]{ "nanos"} );=0AJSONSerializer jsonSerializer =3D new J= SONSerializer();=0AjsonSerializer.setRootClass( Bean.class );=0ABean bean = =3D (Bean) jsonSerializer.toJava( json );=0A=0A/* the following is from 2.1= -SNAPSHOT */=0AJsonConfig jsonConfig =3D new JsonConfig();=0AjsonConfig.set= Excludes( new String[]{ "nanos"} );=0AJSON json =3D JSONSerializer.toJSON( = bean, jsonConfig );=0A...=0AjsonConfig.setRootClass( Bean.class );=0ABean b= ean =3D (Bean) JSONSerializer.toJava( json, jsonConfig );=0A =0AIn 2.1-SNAP= SHOT there is also the option to setup filters that will be enabled when se= rializing to JSON and transforming back to Java=0A=0AI'm quite surprised th= at 'nanos' gave caused a problem because the code should check if there is = a PropertyDescriptor that matches the target class and property, if so it w= ill also check if it has a proper writeMethod. Which version are you using?= =0A=0ARegards,=0AAndres=0A=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 who understand binary, and thos= e who don't.=0ATo understand recursion, we must first understand recurs= ion.=0A=0A=0A=0A=0A=0A----- Mensaje original ----=0ADe: Gerardo Blanco <Bla= nc...@ld...>=0APara: jso...@li...=0AEnviado:= mi=E9rcoles, 19 de septiembre, 2007 10:30:45=0AAsunto: [json-lib-user] Cas= ting classes=0A=0A=0A=0AHi, =0A=0A=0A =0A=0A=0AI'm doing the following:=0A= =0A=0A =0A=0A=0A1) Run a Hibernate query to populate a bean=0A=0A=0A2) usin= g JSONObject.fromObject(bean)=0A=0A=0A3) using JSONObject.toBean()=0A=0A=0A= =0A=0A=0AThe problem is that the bean contains a java.util.Date. Hibernate= casts that to a java.sql.Timestamp, which extends java.util.Date, so Hiber= nate is complying with the contract.=0A=0A=0AWhen I create my JSONObject, t= he method fromObject grabs everything the Timestamp contains, including 'na= nos' (for nanoseconds). That property belongs to Timestamp.=0A=0A=0A =0A=0A= =0AWhen toBean is called, it doesn't know what to do with 'nanos' because i= t is introspecting java.util.Date.=0A=0A=0A =0A=0A=0AI know I can code arou= nd this issue by modifying my getters/setters, but my questions is: =0A=0A= =0Ashould this behaviour be caught (and provided for) by JSON-lib somehow?= =0A=0A=0A =0A=0A=0AWhat if we had JSONObject.fromObject(bean, Bean.class)? = That way JSONObject.fromObject would introspect the class, and only include= fields for the getters/setters for the actual class, instead of extended c= lasses that might be present.=0A=0A=0A =0A=0A=0AThanks.=0A=0A=0AJerry. =0A= =0A=0A =0A=0A=0A-------------------------------------------------------=0AG= erardo (Jerry) R. Blanco =0ASoftware Engineer=0ALDS Platform Team=0A(801) 2= 40-6978=0AB...@ld... =0A---------------------------------------= ----------------=0A"I am endeavoring, ma'am, to construct a mnemonic memory= circuit, using stone knives and bearskins." -- Spock=0ANOTICE: This email = message is for the sole use of the intended recipient(s) and may contain co= nfidential and privileged information. Any unauthorized review, use, disclo= sure or distribution is prohibited. If you are not the intended recipient, = please contact the sender by reply email and destroy all copies of the orig= inal message.=0A=0A=0A=0A=0A=0A=0A=0A=0A=0A=0A=A1S=E9 un mejor fot=F3grafo!= =0APerfecciona tu t=E9cnica y encuentra las mejores fotos en:=0Ahttp://mx.y= ahoo.com/promos/mejorambientalista.html =0A=0ANOTICE: This email message is= for the sole use of the intended recipient(s) and may contain confidential= and privileged information. Any unauthorized review, use, disclosure or di= stribution is prohibited. If you are not the intended recipient, please con= tact the sender by reply email and destroy all copies of the original messa= ge.=0A=0A=0A=0A=0A=0A=0A=0A __________________________________________= __________________________________________=0A=A1S=E9 un mejor asador!=0AApr= ende todo sobre asados. =0Ahttp://mx.yahoo.com/promos/= mejorasador.html |