Field Formatter - NikiforovAll/SF-Mapper GitHub Wiki
You can add additional behavior by implementing IFieldFormatter interface and passing your instance of IFieldFormatter to config.
public virtual class BaseFormatter implements IFieldFormatter{
public FormatterResult format(Object field){
Object res;
Boolean isSuccess = true;
try{
res = operation(field);
}catch(Exception ex){
isSuccess = false;
LogFormatterError(ex);
}
return new FormatterResult(res, isSuccess);
}
protected virtual Object operation(Object field){
return field;
}
}
public class MyCustomFormatter extends BaseFormatter{
protected override Object operation(Object field){
return (String)field.toUpperCase();
}
}
So you can use it:
SObjectMapper.initialize(
new MapperConfigBuilder('scheme1')
.addObjectMapping('Test__c', '*')
.addFieldMapping('test',
new FieldMappingRule(
'Text__c',
new MyCustomFormatter()
)
)
);
Map<String, Object> test = new Map<String, Object>{
'test' => 'test',
};
String data = Json.serialize(test);
Map<String, sObject> result = SObjectMapper.mapObject(
new JsonResourceProvider(data), 'scheme1'
);
// Result: {Test__c=Test__c:{Text__c=TEST}}