I'm trying to save an Interaction in a session so i have added two methods to Serialize and Deserialize the Interaction. Serializing works fine, i get a valid json with all the information. However when i try to Deserialize i get the error:
JsonSerializationException: Could not create an instance of type Sitecore.XConnect.IEntityReference`1[Sitecore.XConnect.Contact]. Type is an interface or abstract class and cannot be instantiated. Path 'Contact.Identifiers', line 1, position 685.
This is my (de)serialize code:
public static void SetObjectAsJson(this ISession session, string key, object value)
{
session.SetString(key, JsonConvert.SerializeObject(value));
}
public static T GetObjectFromJson<T>(this ISession session, string key)
{
var value = session.GetString(key);
return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
}
And i call these methods like this:
private Interaction GetInteraction(Contact c)
{
var interaction = HttpContext.Session.GetObjectFromJson<Interaction>("_Interaction");
if (interaction == null)
{
interaction = ic.CreateInteraction(c);
}
return interaction;
}
private void SetInteraction(Interaction i)
{
HttpContext.Session.SetObjectAsJson("_Interaction", i);
}
Is there a way around this error or am i going to have to create a new Interaction and fill the values myself?