Hi Raymon,
the problem you seem to have is not related to mine directly.
However, I had something similar with dates:
You cannot expect json to deserialize Date from the string you pass in your
code.
You will need to parse it and pass it to .toBean in the correct way.
json cuts the date to integers, representing year,month,day,hour,min,sec.
Also, there is a Time part, being seconds from 1970, I think.
So either you need to pass the group of integers or the long with Time (and
Timezone).
To see the exact format just serialize your class to json.
I use json to get XML at the end, so here is how a Date looks (note that
month is zero based):
<date>
<date>3</date>
<day>2</day>
<hours>16</hours>
<minutes>26</minutes>
<month>5</month>
<seconds>29</seconds>
<time>1212524789781</time>
<timezoneOffset>240</timezoneOffset>
<year>108</year>
</date>
Also, for each embedded class other than simple types, register a Morpher
(In the example, Field is member of Filter):
JSONUtils.getMorpherRegistry().registerMorpher(new
EnumMorpher(Field.class));
Then, deserialize this way:
JSONObject jsonObject = new JSONObject();
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setRootClass(Filter.class);
jsonConfig.setIgnoreDefaultExcludes(false);
jsonConfig.setExcludes(new String[] {"class"}); //exclude the
class 'member';
jsonObject = (JSONObject)JSONSerializer.toJSON(jsonString);
this.filter =(Filter)JSONSerializer.toJava(jsonObject,
jsonConfig);
Then for each member bean, get specific Morpher:
Morpher fieldMorpher = new BeanMorpher( Field.class, morpherRegistry);
morpherRegistry.registerMorpher(fieldMorpher);
Iterate your member objects ( I have an array of them) and cast to specific
type:
List<Field> fields = new ArrayList<Field>();
for( Iterator ii = filter.getFields().iterator(); ii.hasNext();
Field field = (Field)morpherRegistry.morph(Field.class, ii.next());
fields.add(field);
}
filter.setFields(fields);
I hope this helps! Let me know if I can assist further.
Cheers,
Kiril.
On Fri, Jun 20, 2008 at 3:55 AM, Raymon Wang <rm...@gm...> wrote:
> Hi, Kiril
>
> I got your email address from json-lib mailing-list,and knew that you
> have successed in conversion with a Date class.
>
>
> would you please help me with the problem at link of
> http://groups.google.com/group/json-schema/t/e27e589ac440679e?
>
> Thanks in advance!
>
>
|