Re: [json-lib-user] Casting classes
Brought to you by:
aalmiray
|
From: Andres A. <aal...@ya...> - 2007-09-19 15:41:30
|
Hi Jerry,=0A=0ASince version 0.9 (I think) there is the option to specify a=
set of=0Aexclusions that will let filter out some properties when using=0A=
JSONObject.fromObject(), in your case=0A=0A=0A=0A/* available in json-lib 1=
.x */=0A=0AJSON json =3D JSONSerializer.toJSON( bean, new String[]{ "nanos"=
} );=0A=0AJSONSerializer jsonSerializer =3D new JSONSerializer();=0A=0Ajson=
Serializer.setRootClass( Bean.class );=0A=0ABean bean =3D (Bean) jsonSerial=
izer.toJava( json );=0A=0A=0A=0A/* the following is from 2.1-SNAPSHOT */=0A=
=0AJsonConfig jsonConfig =3D new JsonConfig();=0A=0AjsonConfig.setExcludes(=
new String[]{ "nanos"} );=0A=0AJSON json =3D JSONSerializer.toJSON( bean, =
jsonConfig );=0A=0A...=0A=0AjsonConfig.setRootClass( Bean.class );=0A=0ABea=
n bean =3D (Bean) JSONSerializer.toJava( json, jsonConfig );=0A =0A=0AIn 2.=
1-SNAPSHOT there is also the option to setup filters that will be=0Aenabled=
when serializing to JSON and transforming back to Java=0A=0A=0A=0AI'm quit=
e surprised that 'nanos' gave caused a problem because the code=0Ashould ch=
eck if there is a PropertyDescriptor that matches the target=0Aclass and pr=
operty, if so it will also check if it has a proper=0AwriteMethod. Which ve=
rsion are you using?=0A=0A=0A=0ARegards,=0A=0AAndres =0A-------------------=
------------------------=0Ahttp://jroller.com/page/aalmiray=0Ahttp://www.li=
nkedin.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 under=
stand binary, and those who don't.=0ATo understand recursion, we must f=
irst understand recursion.=0A=0A----- Mensaje original ----=0ADe: Gerardo B=
lanco <Bla...@ld...>=0APara: jso...@li...=
=0AEnviado: mi=E9rcoles, 19 de septiembre, 2007 10:30:45=0AAsunto: [json-li=
b-user] Casting classes=0A=0A=0A=0A =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) using JSONObject.fromObject(bean)=0A=0A=0A3) using JSONO=
bject.toBean()=0A=0A=0A =0A=0A=0AThe problem is that the bean contains a ja=
va.util.Date. Hibernate casts that to a java.sql.Timestamp, which extends j=
ava.util.Date, so Hibernate is complying with the contract.=0A=0A=0AWhen I =
create my JSONObject, the method fromObject grabs everything the Timestamp =
contains, including 'nanos' (for nanoseconds). That property belongs to Tim=
estamp.=0A=0A=0A =0A=0A=0AWhen toBean is called, it doesn't know what to do=
with 'nanos' because it is introspecting java.util.Date.=0A=0A=0A =0A=0A=
=0AI know I can code around this issue by modifying my getters/setters, but=
my questions is: =0A=0A=0Ashould this behaviour be caught (and provided fo=
r) by JSON-lib somehow?=0A=0A=0A =0A=0A=0AWhat if we had JSONObject.fromObj=
ect(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 classes that might be present.=0A=0A=0A =0A=0A=0AThan=
ks.=0A=0A=0AJerry. =0A=0A=0A =0A=0A=0A-------------------------------------=
------------------=0AGerardo (Jerry) R. Blanco =0ASoftware Engineer=0ALDS P=
latform Team=0A(801) 240...@ld... =0A-----------------=
--------------------------------------=0A"I am endeavoring, ma'am, to const=
ruct a mnemonic memory circuit, using stone knives and bearskins." -- Spock=
=0A=0ANOTICE: This email message is for the sole use of the intended recipi=
ent(s) and may contain confidential and privileged information. Any unautho=
rized review, use, disclosure or distribution is prohibited. If you are not=
the intended recipient, please contact the sender by reply email and destr=
oy all copies of the original message.=0A=0A=0A=0A=0A=0A=0A=0A _______=
___________________________________________________________________________=
__=0A=A1S=E9 un mejor ambientalista!=0AEncuentra consejos para cuidar el lu=
gar donde vivimos. =0Ahttp://mx.yahoo.com/promos/mejo=
rambientalista.html |