Menu

JSONObject.toBean( jsonObject, jsonConfig)

Mino Togna
2007-10-14
2013-04-29
  • Mino Togna

    Mino Togna - 2007-10-14

    Hi,
    I am using the json-lib-2.1-jdk15.jar api in my application.
    I was trying to convert a jsonObject into my java bean which is an instance of HashMap (My bean extends java.util.HashMap).
    It means i can put everything in my bean as well as instances of java.util.List.
    If my JsonObject contains an instance of JSONArray I have a problem at the line 323 of the JSONObject:
    if( List.class.isAssignableFrom( pd.getPropertyType() ) )
    I have a nullPointerException because the pd (PropertyDescriptor) is null.

    In fact the property descriptor is taken at the line 314:
    PropertyDescriptor pd = Map.class.isAssignableFrom( beanClass ) ? null
                      : PropertyUtils.getPropertyDescriptor( bean, key );
    My java bean can be assignable from Map because it's an instance of hashMap.

    If I change the line 314 with:
    PropertyDescriptor pd = Map.class.isAssignableFrom( beanClass ) && !( value instanceof JSONArray ) ? null
                      : PropertyUtils.getPropertyDescriptor( bean, key );
    I got the PropertyDescriptor and the List that represents the JsonArray can be put into the map.

    Please let me know if the little code i added is wrong either there is another way to fix it.
    You can write me: mino.togna at gmail dot com

    I look forward to hear from you.

    Thanks very much.
    Mino

     
    • aalmiray

      aalmiray - 2007-10-14

      Hi Mino,

      I think that you may have found an untested corner case. If I understand correctly your bean may have "dynamic" properties, much like a DynaBean, because it follows the Map contract, and at the same time some "normal" properties like a JavaBean.
      I'll appreciate if you can send a failing testcase so that I can get a better understanding of your problem and fix the bug if any =)

      Cheers,
      Andres

       
    • Mino Togna

      Mino Togna - 2007-10-14

      Hi Andres,

      I sent you via email a very simple failing test example.
      I have created only one package 'testjson' which contains the TestJson class with the main method.
      The UserInfobean can have more then one AffiliationBean (as java.util.List object).

      The exception I got is:
      net.sf.json.JSONException: Error while setting property=affiliations type interface java.util.List
          at net.sf.json.JSONObject.toBean(JSONObject.java:468)
          at testjson.TestJson.createMainBeanObject(TestJson.java:54)
          at testjson.TestJson.main(TestJson.java:28)
      Caused by: java.lang.NullPointerException
          at net.sf.json.JSONObject.toBean(JSONObject.java:323)
          ... 2 more

      I look forward to get your reply.

      Cheers
      -Mino

       

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.