Re: [json-lib-user] Using JSON -LIB for JSON to Java Serialization
Brought to you by:
aalmiray
|
From: Andres A. <aal...@ya...> - 2007-12-09 03:43:48
|
Json-lib has the option of transforming JSONObject/JSONArray directly into =
an specific class without DynaBeans in the middle.=0AYou just have to confi=
gure a rootClass and optionally a classMap with jsonConfig to get it done. =
I'm preparing more examples that=0Awill be available at the site when the n=
ext release is ready (2.2), in the meantime you may inspect the test source=
s to get an idea on how this feature works.=0A=0ACheers,=0AAndres=0A=0A=0A-=
---- Original Message ----=0AFrom: Haritha Juturu <har...@ya...=
>=0ATo: jso...@li...=0ASent: Friday, December 7, 200=
7 12:08:28 PM=0ASubject: [json-lib-user] Using JSON -LIB for JSON to Java S=
erialization=0A=0A=0AHi All,=0AI have a complicated JSON string which i am =
trying to serialize to Java Objects using the Json-Lib library.=0AI thought=
i will post a code snippet here to know if i am doing the right thing or t=
heres a simpler way to serialize json to java.=0ACalling JSONObject.toBean(=
) returns a bunch of MorphDynaBeans which i morph to the objects in my appl=
ication using EZMorph library.=0APlease have a look and send in your commen=
ts.=0A=0AThis is my JSON string=0A{=0A"name":"Nameless=0A One",=0A"pages":[=
=0A{"name":"Page1",=0A"canvas":"{"objectType":"EditorCanvas",=0A"id":"edito=
rCanvas",=0A"name":"",=0A"sizeWidth":940,=0A"cssClass":"",=0A"style":"width=
:940px;",=0A"children":[=0A{"objectType":"TextObject",=0A"id":"Text_1196907=
790312",=0A"name":"",=0A"cssClass":"",=0A"style":"left:40px;top:60px;width:=
620px;height:100px;",=0A"text":""[Winning the Nobel Peace Prize] ." =97Moha=
mmad Yunus"},=0A{"objectType":"ImageObject",=0A"id":"Image_1196907800312",=
=0A"name":"",=0A"position":({left:40, top:220}),=0A"size":({width:300, heig=
ht:300}),=0A"cssClass":"",=0A"style":"left:40px;top:220px;width:300px;heigh=
t:300px;",=0A}=0A]=0A}"=0A}=0A]=0A}=0A=0AJSON json =3D JSONSerializer.toJSO=
N(jsonStr);=0A=0AMorpherRegistry morpherRegistry =3D new MorpherRegistry();=
=0AmorpherRegistry.registerMorpher( new BeanMorpher( BaseObject.class,morph=
erRegistry));=0AmorpherRegistry.registerMorpher( new BeanMorpher(SplashNote=
Object.class,=0A morpherRegistry));=0AmorpherRegistry.registerMorpher( new =
BeanMorpher( PageObject.class,morpherRegistry));=0AmorpherRegistry.register=
Morpher( new BeanMorpher(EditorCanvas.class, morpherRegistry));=0A=0ASplash=
NoteObject snObject =3D (SplashNoteObject) JSONObject.toBean((JSONObject) j=
son, SplashNoteObject.class );=0APageObject page =3D(PageObject)morpherRegi=
stry.morph(PageObject.class,snObject.getPages().get(0));=0A=0AEditorCanvas =
bean =3D (EditorCanvas)page.getCanvas();=0A=0Ai havent included the code fo=
r any further levels like TextObject and ImageObject=0A=0AIs this the only =
was i can serialize json to java for java objects which have other java obj=
ects in them. Do i need=0Ato morph the objects at every level to the bean i=
need.=0A=0ACheers=0AHaritha=0A=0A=0A=0A=0A=0A Never miss a thing. M=
ake Yahoo your homepage.=0A=0A=0A=0A=0A=0A=0A=0A _____________________=
_______________________________________________________________=0ANever mis=
s a thing. Make Yahoo your home page. =0Ahttp://www.yahoo.com/r/hs |