I need to serialize a list of SObject without omission null values. This string I need for a later usage in JavaScript. Structure is crucial, because I need to construct a table out of it. The structure is very dynamic. Different SObjects may lead to different fields and therefore different columns. Missing values will corrupt all the output and are unacceptable.
As it stands, in current versions JSON.serialize() will not output null values. There are already some questions about this raised, but either the answers there are incorrect or I'm doing something wrong to apply them right:
- JSON.Serialize method not returning null fields
- JSON.Serialize method not returning null fields (part deux)
As far as I understand, Apex version 28.0 and newer should have no null values and version 27.0 and older should have them. But I can't make it work. There was an issue that the JSON.serialized() was not versioned right but as to @JoshKaplan answer it should be working now.
So I've created this helper-class:
public class elfApi27 {
public static string dump0(Object o) {
return system.JSON.serializePretty(o);
}
}
Setting the version like this in the xml
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>27.0</apiVersion>
<status>Active</status>
</ApexClass>
tested it via execute anonymous like this (consider xl.log as system.debug)
Account a = new Account(name='test');
xt.log( elfApi27.dump0(a) );
I have tried all the versions from 32.0 down to 24.0 but the output stays rock-solid like this:
DB Callout Profile Code Escape
32.0 APEX_CODE,WARN;APEX_PROFILING,WARN;CALLOUT,WARN;DB,WARN;SYSTEM,WARN
19:09:02.119 (119823175)|EXECUTION_STARTED
19:09:02.119 (119833395)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
19:09:02.132 (132802344)|USER_DEBUG|[230]|WARN|"{
"attributes" : {
"type" : "Account"
},
"Name" : "test"
}"
19:09:02.132 (132848199)|CODE_UNIT_FINISHED|execute_anonymous_apex
19:09:02.134 (134336841)|EXECUTION_FINISHED
Am I misinterpreting the answers?
Is there any way to have a full verbose serialization of objects?