4

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?

4

There are some possible questions here around what your aim is and why you need to persist this interaction, but to answer your specific question:

I would advise against attempting to serialize / deserialize a complex type such as an Interaction, especially when it's not a type of your making that you have control over. It's difficult to know what references to other types this relies on and when you deserialize from json, these will be lost - as you can see here where it is trying to deserialize the Contact property and throwing an error as it doesn't know how to recreate it.

You could go down the road of trying to create a custom serializer for this type, but I think that's far more likely to be trouble than its worth.

Instead I would follow your other line of thought, and serialize a type of your own creation that stores the information about the interaction you are needing to persist. You can then deserialize this data and create the Interaction when you need to submit it.

| improve this answer | |
  • I wouldn't try to create custom serializer for xConnect's types as it will be more tedious than just having your own serializable data model and converting it to xConnect just before it needs to be submitted. – grg Nov 30 '17 at 13:36
  • @grg, definitely agree - I'd just create them when submitting. – Kasaku Nov 30 '17 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.