I need to easily serialize (save as long text field in database) and deserialize (convert back to a class) a parameter list in Apex of String -> Object value pairs.
My first idea was just to write a class like this...:
public class Parameters{
private Map<String, Object> parameters = new Map<String, Object>();
public void add(String name, Object value) {
this.parameters.put(name, value);
}
public Object get(String name) {
Object result = null;
if(this.parameters.containsKey(name)) {
result = this.parameters.get(name);
}
return result;
}
}
... and use JSON.serialize and deserialize..
Parameters params = new Parameters();
params.add('Name', 'Robert');
params.add('Age', 36);
String serialized = JSON.serialize(params);
Parameters deserialized = (Parameters) JSON.deserializeStrict(serialized, Parameters.class);
System.assertEquals(params, deserialized);
but then I got this error message:
System.JSONException: Apex Type unsupported in JSON: Object
Any idea or experience on how to implement such a Parameter class that is able to at least hold all basic (non-collection) types?!