[SalesForce] JSON.Serialize method not returning null fields (part deux)

My question is related to the question JSON.Serialize method not returning null fields in that I'm having the same issue but based on the solution, I'm unable to get my code to use version 27 (maybe I'm doing something wrong). I've tried to change the Version Settings for all of the classes used on the page and the (visual force) page to use 27.0 (I even tried 26.0) to no avail. The JSON being returned still references v28.0 and my null properties are being removed. Here's a snippet in case I'm missing something…

In my Apex class…

webService static String getPriceListItems(String filters) {
   ...
   string soql = 'SELECT Id, Name, Price__c, Catalog_UOM__c, ... FROM PriceListItem__c WHERE ...';
   List<Object> result = Database.query(soql); // also tried with List<PriceListItem__c>
   String JSONString = JSON.serialize(result);
   System.debug('result[0].Catalog_UOM__c ' + result[0].Catalog_UOM__c); // shows null value
   System.debug('JSONString : ' + JSONString  ); // does not contain Catalog_UOM__c field
   return JSONString;

}

And the serialized JSON is like this (still with all of the null properties being omitted)…

[{
"attributes": {
"type": "PriceListItem__c",
"url": "/services/data/**v28.0**/sobjects/PriceListItem__c/a0Hi00000027tstEAA"
},
...
}]

Other than setting the version to 27.0 in the Version Settings while editing the class, is there anything else that I need to do? Note that there are still some classes in my package (that I don't believe that I am using) that are set to v28.0 but that shouldn't matter, should it?

It seems like you should be able to specify what serialization settings you want to use instead of being blindly forced to use a specific setting under a particular version. This is especially important if there is some new functionality in a current version that you need to use but still need to serialize and return null values.

I can understand not wanting to implement a full blown JSON configuration object, but since this is not default behavior (on any other platform that I've dealt with and that you need to specifically configure JSON to omit nulls), that the JSON.serialize method in Apex should perhaps take an optional boolean parameter to omit nulls (defaulted to false). That would solve everyone's problem as it wouldn't break any existing code and those that want that functionality can just add "true".

Best Answer

We are also facing the same problem.

There is some answer from APEX team on this at below link : JSON.Serialize method not returning null fields

But it looks like that question is still open.