I'm attempting deserialize a list of a custom object in Apex, but got an error:
FATAL_ERROR System.JSONException: Apex Type unsupported in JSON: Object
This is because my ContactPostBack class contains the contact Map with <String, Object> which is not deserializable. Only the listed types here (bottom of page) can be deserialized:
private String url;
private Map<String, String> headers = new Map<String, String>();
private Map<String, Object> contact = new Map<String, Object>();
Changing the declaration of contact to Map<String, String> works, but Integers, Booleans, or other more complicated data structures will simply be converted to strings or throw errors.
I'd like the contact Map to contain multiple objects, like String, Integer, Boolean, or even other Maps or Lists. (I'm constructing a JSON object to post different endpoints).
Is it possible to create a "wrapper class" that contains the data types I can use?
Or is there a different implementation of JSON.deserialize that works for Object?
(Edit) - Included more code where the deserialization happens:
The ContactPostBack object:
public class ContactPostBack {
private String url;
private Map<String, String> headers = new Map<String, String>();
// This only works when: Map<String, String> contacts ...
// and the other references are changed to Map<String, String> type
private Map<String, Object> contact = new Map<String, Object>();
public ContactPostBack() {}
public ContactPostBack(
String url, Map<String, String> headers, Map<String, Object> contact
) {
this.url = url;
this.headers = headers;
this.contact = contact;
}
public void setUrl(String url) {
this.url = url;
}
public void addHeader(String key, String value) {
this.headers.put(key, value);
}
public void addField(String key, Object value) {
this.contact.put(key, value);
}
public Map<String, Object> getContact() {
return this.contact;
}
}
The actual ContactController2 class creates the List:
global class ContactController2 {
private final Contact contact;
private List<ContactPostBack> pbcList = new List<ContactPostBack>();
public ContactController2(ApexPages.StandardController sc) {
this.contact = (Contact)sc.getRecord();
// Logic to initialize class...
this.setData();
}
public String getContacts() {
return JSON.serialize(pbcList);
}
public void setData() {
// Business logic to add Contacts to pbcList
ContactPostBack pbc = new ContactPostBack();
// ...
this.pbcList.add(pbc);
}
}
Apex Trigger callout to POST to external API. This is where the error is thrown!!!
global class ContactPostBackController {
@future (callout=true)
public static void postContactChanges(String contacts) {
// Receives the JSON serialized List<ContactPostBack> from the ContactController2
postIt(contacts);
}
private static void postIt(String serializedContacts) {
// Define request, response, and http objects
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
// THIS IS WHERE THE ERROR IS THROWN
List<ContactPostBack> contacts = (List<ContactPostBack>)JSON.deserialize(
serializedContacts, List<ContactPostBack>.class
);
// Logic for sending contact to post back...
}
}
Thanks!
Map<String, Object>
.