Re: [json-lib-user] Converting GregorianCalendar object, Java to JSON and back
Brought to you by:
aalmiray
From: Andres A. <aal...@ya...> - 2008-06-11 05:09:14
|
What about this (pseudo) Java code ;-) class Bean { String name } class BeanFactory { Bean newBean( String name ){ new Bean(name: name) } } class MyNewBeanInstanceFactory extends NewBeanInstanceFactory.DEFAULT { BeanFactory factory = new BeanFactory() public Object newInstance( Class target, JSONObject source ){ if( Bean.class.isAssignableFrom(target) ) { return factory.newBean( source.get("name") ) }else{ super.newInstance(target,source) } } } JsonConfig jsonConfig = new JsonConfig() jsonConfig.setNewBeanInstanceStrategy( new MyNewBeanInstanceFactory() ) ... ----- Original Message ---- From: Darlene Wong <dar...@gm...> To: Andres Almiray <aal...@ya...> Cc: jso...@li... Sent: Tuesday, June 10, 2008 6:52:27 PM Subject: Re: [json-lib-user] Converting GregorianCalendar object, Java to JSON and back Thanks Andres. I will give this a shot. Can you point me to some sample code that creates a custom strategy? thanks, Darlene On Fri, Jun 6, 2008 at 4:15 PM, Andres Almiray <aal...@ya...> wrote: Hi Darlene, There is indeed a restriction on the classes that can be used when serializing back to Java, they must adhere to the JavaBeans conventions (no-arg constructor, getters/setters, etc). It looks like GregorianCalendar follows suit still it throws an exception. Well no worries as you may register a NewBeanInstanceStrategy with JsonConfig. The default strategy follows the JavaBeans convention (no-arg constructor), with a custom strategy you are free to instantiate the class as you see fit, for example using a factory. http://json-lib.sourceforge.net/xref/net/sf/json/util/NewBeanInstanceStrategy.html 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: Darlene Wong <dar...@gm...> To: jso...@li... Sent: Friday, June 6, 2008 3:08:30 PM Subject: [json-lib-user] Converting GregorianCalendar object, Java to JSON and back Hi, I'm new to json-lib. I want to convert a Java class into JSONObject and then back again. I am able to do this conversion with a Date class. However, the class I want to convert contains a nested XMLGregorianCalendar object, which has issues when converting from JSONObject to Java. I've tried to create a simple test case that converts only a GregorianCalendar object (http://java.sun.com/javase/6/docs/api/java/util/GregorianCalendar.html). This works fine when converting from Java to JSONObject, but the reverse conversion gives an InstantiationException error when calling toJava(). Here is my sample code: GregorianCalendar gc = DateUtil.getCalendar(-7); System.out.println(gc.toString()); // Convert object to JSON JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(gc); System.out.println(jsonObject.toString()); // Convert JSON object back into Java object GregorianCalendar gc2; JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setRootClass(GregorianCalendar.class); gc2 = (GregorianCalendar) JSONSerializer.toJava(jsonObject, jsonConfig); System.out.println(gc2.toString()); The output is as follows: java.util.GregorianCalendar[time=1212788878236,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=java.util.SimpleTimeZone[id=GMT-07:00,offset=-25200000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=3,startDay=1,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=7200000,endTimeMode=0],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2008,MONTH=5,WEEK_OF_YEAR=23,WEEK_OF_MONTH=1,DAY_OF_MONTH=6,DAY_OF_YEAR=158,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=3,HOUR_OF_DAY=15,MINUTE=47,SECOND=58,MILLISECOND=236,ZONE_OFFSET=-25200000,DST_OFFSET=3600000] {"firstDayOfWeek":1,"gregorianChange":{"date":4,"day":4,"hours":16,"minutes":0,"month":9,"seconds":0,"time":-12219292800000,"timezoneOffset":480,"year":-318},"lenient":true,"minimalDaysInFirstWeek":1,"time":{"date":6,"day":5,"hours":14,"minutes":47,"month":5,"seconds":58,"time":1212788878236,"timezoneOffset":420,"year":108},"timeInMillis":1212788878236,"timeZone":{"DSTSavings":3600000,"ID":"GMT-07:00","displayName":"GMT-07:00","rawOffset":-25200000}} net.sf.json.JSONException: java.lang.InstantiationException at net.sf.json.JSONObject.toBean(JSONObject.java:304) at net.sf.json.JSONObject.toBean(JSONObject.java:420) at net.sf.json.JSONSerializer.toJava(JSONSerializer.java:69) ... Like I said, I am able to get the same code to work on other objects, such as Date objects, so what am I missing? Is there a limitation on the types of objects that can be converted to JSONObject and back? Any help or pointers is greatly appreciated! Darlene |