APEX-JSONReservedNameSerializer
APEX-JSONReservedNameSerializer copied to clipboard
A utility class that allows serialization/deserialization of reserved keywords
Reserved Keyword Serializer
A Utility class for better control over JSON serialization/deserialization in salesforce apex
Install
-
git clone
-
cd
into folder -
sfdx force:mdapi:deploy -u [username] -d ./src -w 10000
Usage
extend
For any JSON you need to Serialize data structure you need to seralize:
- implement a class that
extends JSONReservedSerializer
- pass mappings into the
super()
constructor. Mapping is defined byMap<Type, Map<String,String>>
whereType
is the top level object you are serializing. This allows for seralizaiton of multiple types in a single class.
public class MySerializer extends JSONImprovedSerializer {
private MySerializer() {
//setup mappings
super(new Map<Type,Map<String,String>>{
MyOuterDTO.class => OUTER_DTO_MAPPINGS
});
}
//define DTO's using mapped names
static final Map<String, String> OUTER_DTO_MAPPINGS = new Map<String, String> {
'obj' => 'object',
'isPrivate' => 'private'
};
public class OuterDTO {
public InnerDTO obj;
}
public class InnerDTO {
public Boolean isPrivate;
public String notReserved;
}
}
Serialize / Deserialize
String origString = '{"object":{"private":true,"notReserved":"abc"}}';
//deserialization
MySerializer json = new MySerializer();
MySerializer.OuterDTO dto = (MySerializer.OuterDTO) json.deserialize(
origString,
MySerializer.OuterDTO.class
);
//serialization
String newString = json.serialize(obj, MySerializer.OuterDTO.class);
System.assertEquals(origString, newString);
Notes
- Serialization mappings are global to the entire object
- Likely will fail due to limit exceptions with extremely large strings