Re: [json-lib-user] Problems with .toJava
Brought to you by:
aalmiray
From: Kiril <kir...@gm...> - 2008-05-10 07:54:58
|
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. >> > > |