Menu

java.sql.Timestamp

Help
2007-10-05
2013-04-29
  • Mark D. Parry

    Mark D. Parry - 2007-10-05

    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?

     
    • aalmiray

      aalmiray - 2007-10-05

      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

       
    • aalmiray

      aalmiray - 2007-10-06

      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" ) );
            }
         }

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.