Default value handling with Json-lib
Up to version 2.2 of the library there was no way to solve this problem by handling Integer-nulls differently in a nice way. Sure, you could still go through your JSONObject or JSONArray afterwards and replace the values one by one, but that would more or less render the whole library useless to me. This situation changed with 2.2, though. Now you can associate a so-called DefaultValueProcess (and a DefaultValueProcessorMatcher for that matter) with a JsonConfig instance and let for instance String-null be represented as null instead of '' or Integer null values represented as null instead of 0 (which might be invalid depending on how you interpret the JSON specs but sometimes it's the easiest solution). Here a small example:
import net.sf.json.JsonConfig;
import net.sf.json.processors.DefaultValueProcessor;
...
JsonConfig conf = new JsonConfig();
conf.registerDefaultValueProcessor(Integer.class,
new DefaultValueProcessor(){
public Object getDefaultValue(Class type){
return JSONNull.getInstance();
}
});
This example does exactly that, it registers a new DefaultValueProcessor for the Integer class and returns null if the Integer instance is a null-pointer. Note that you can basically ignore the type argument of the getDefaultValue() method since the class this processor is bound to is already set in the registerDefaultValueProcessor() method.
The following example would now take a null String and replace it with "I am empty!":
conf.registerDefaultValueProcessor(String.class,
new DefaultValueProcessor(){
public Object getDefaultValue(Class type){
return "I am empty!";
}
});
You can find a complete example packaged here and versioned here
