I'm trying to persist a bean containing a java.sql.Timestamp to a database via JSON. Storing to the database doesn't appear to be a problem but reconstituting my bean is causing some heartburn. In specific it would appear the problem is java.sql.Timestamp is not a "bean" itself in so much as it does not have a default constructor (one with no args). This results in the following exception.
[10/3/07 14:03:55:968 CDT] 22802280 SystemErr R net.sf.json.JSONException: java.lang.InstantiationException: java/sql/Timestamp.<init>()V
Is there a work around for this?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If the class do not follows the JavaBean convention there is nothing you can do to get it working. Perhaps adding something like 'NewInstanceStrategy' to JsonConfig will alleviate the problem. Lwt me check what are the implications of such change and get back to you.
Cheers,
Andres
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm trying to persist a bean containing a java.sql.Timestamp to a database via JSON. Storing to the database doesn't appear to be a problem but reconstituting my bean is causing some heartburn. In specific it would appear the problem is java.sql.Timestamp is not a "bean" itself in so much as it does not have a default constructor (one with no args). This results in the following exception.
[10/3/07 14:03:55:968 CDT] 22802280 SystemErr R net.sf.json.JSONException: java.lang.InstantiationException: java/sql/Timestamp.<init>()V
Is there a work around for this?
If the class do not follows the JavaBean convention there is nothing you can do to get it working. Perhaps adding something like 'NewInstanceStrategy' to JsonConfig will alleviate the problem. Lwt me check what are the implications of such change and get back to you.
Cheers,
Andres
I've added a FR for this change (1808430)
Here is the test code showing how it can be used:
public void testFR_1808430_newBeanInstance() {
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setNewBeanInstanceStrategy( new UnstandardBeanInstanceStrategy() );
JSONObject jsonObject = new JSONObject();
jsonObject.element( "id", 1 );
jsonConfig.setRootClass( UnstandardBean.class );
UnstandardBean bean = (UnstandardBean) JSONObject.toBean( jsonObject, jsonConfig );
assertNotNull( bean );
assertEquals( UnstandardBean.class, bean.getClass() );
assertEquals( 1, bean.getId() );
}
private static class UnstandardBeanInstanceStrategy extends NewBeanInstanceStrategy {
public Object newInstance( Class target, JSONObject source ) throws InstantiationException,
IllegalAccessException {
return new UnstandardBean( source.getInt( "id" ) );
}
}