JSONNull cannot convert to JSONObject

Help
2008-06-04
2013-04-29
  • Andrew Hart
    Andrew Hart
    2008-06-04

    Hi Andres,

    The example of a custom bean processor on your blog page doesn't compile:

    JsonConfig jsonConfig = new JsonConfig();  
    jsonConfig.registerJsonBeanProcessor( Person.class,  
       new JsonBeanProcessor(){  
          public JSONObject processBean( Object bean, JsonConfig jsonConfig ){  
             if( !(bean instanceof Person) ){  
                return JSONNull.getInstance();  
             }  
             Person person = (Person) bean;  
             return new JSONObject()  
                .element( "name", person.getName() )  
                .element( "lastname", person.getLastname() );  
          }  
    });

    I get a type mismatch, cannot convert from JSONNull to JSONObject.  What should it return in this example?

     
    • aalmiray
      aalmiray
      2008-06-04

      Hi Andrew,

      You are absolutely right, I wonder how I managed to write that on the first place. That method should return a 'null' JSONObject instead of JSONNull by doing the following

      if( !(bean instanceof Person) ){ 
         return new JSONObject(true);  // marks the object as null

      I'll update the info on the page as well, thanks for spotting it!

      Cheers,
      Andres