Re: [json-lib-user] Problems with .toJava
Brought to you by:
aalmiray
From: Andres A. <aal...@ya...> - 2008-05-10 15:42:48
|
Hi Kiril, That code would surely work but ties your domain to Json-lib, the docs (http://json-lib.sourceforge.net/usage.html) include an alternative solution to your problem: registering a BeanMorpher. 1. class MyBean{ 2. private List data; 3. // getters & setters 4. } 5. 6. class Person{ 7. private String name; 8. // getters & setters 9. } 10. 11. ... 12. 13. String json = "{'data':[{'name':'Wallace'},{'name':'Grommit'}]}"; 14. Map classMap = new HashMap(); 15. classMap.put( "data", Person.class ); 16. MyBean bean = JSONObject.toBean( JSONObject.fromObject(json), MyBean.class, classMap ); 1. Morpher dynaMorpher = new BeanMorpher( Person.class, JSONUtils.getMorpherRegistry() ); 2. morpherRegistry.registerMorpher( dynaMorpher ); 3. List output = new ArrayList(); 4. for( Iterator i = bean.getData().iterator(); i.hasNext(); ){ 5. output.add( morpherRegistry.morph( Person.class, i.next() ) ); 6. } 7. bean.setData( output ); Cheers, Andres ------------------------------------------- http://jroller.com/aalmiray http://www.linkedin.com/in/aalmiray -- What goes up, must come down. Ask any system administrator. There are 10 types of people in the world: Those who understand binary, and those who don't. To understand recursion, we must first understand recursion. ----- Original Message ---- From: Kiril <kir...@gm...> To: jso...@li... Sent: Saturday, May 10, 2008 12:54:57 AM Subject: Re: [json-lib-user] Problems with .toJava Ok, I'm not a newbie anymore :) Here is the solution, if anyone is interested. I modified the setter in the root class for the list of children (Group classes): public void setGroups(List<Group> groups) { for(int i=0; i < groups.size(); i++) { Group grp = null; BeanMorpher mrph = new BeanMorpher(Group.class, JSONUtils.getMorpherRegistry()); grp = (Group)mrph.morph(groups.get(i)); this.groups.add(grp); } } Yahoo!!!... Works! On Sat, May 10, 2008 at 2:55 AM, Kiril <kir...@gm...> wrote: May be attaching the json string will help: {"groups":[{"comparison":"And","fields":[{"comparison":"Near","field":"subject","values":["Daily","Report"]},{"comparison":"Equals","field":"message","values":["November"]}],"nextGroupComparison":"Or"},{"comparison":"Null","fields":[{"comparison":"Equals","field":"message","values":["October"]}],"nextGroupComparison":"Null"}],"revision":0,"version":1} On Sat, May 10, 2008 at 2:54 AM, Kiril <kir...@gm...> wrote: Hi all, I'm newbie to json-lib. I want to use it to serialize a class with a List<Class>, containing number of subclasses. While toJSON works fine, passing back the same string to toJava works only partially - the root class is generated Ok, but the children, in the List are MorphDynaBean's. Here is the code: JSONUtils.getMorpherRegistry().registerMorpher(new EnumMorpher(GroupComparison.class)); JSONUtils.getMorpherRegistry().registerMorpher(new EnumMorpher(NextGroupComparison.class)); JSONUtils.getMorpherRegistry().registerMorpher(new EnumMorpher(FieldComparison.class)); JSONObject jsonObject = new JSONObject(); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setRootClass( Filter.class ); jsonConfig.setEnclosedType(List.class); jsonObject = (JSONObject)JSONSerializer.toJSON(jsonString); Filter filter = (Filter)JSONSerializer.toJava(jsonObject, jsonConfig); The Filter class contains List of Group classes, but these are generated as a MorphDynaBean... Thanks in advance for any help! Kiril. ____________________________________________________________________________________ Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ |