I have a class that I am converting into a JSON string and saving. The class contains 2 dynamic properties which are of some type T. In my case it's LdrEntity. Now when I am deserializing and getting the object back, it is assigning a JSON string to that dynamic property. Is there a way to specify to serialize the dynamic property back to LdrEntity instead of the JSON string.
Here is my class (see LdrReagentEntity and BciReagentEntity properties):
using Domain.Model.Shared.Entity.Panel; using System.Collections.Generic; namespace Authoring.DataAccess.DataModel { /// <summary> /// Cocktail reagent structure to coordinate data. /// </summary> public class CocktailReagent { /// <summary> /// Id. /// </summary> public string Id { get; set; } /// <summary> /// Reagent type. /// </summary> public ReagentType Type { get; set; } /// <summary> /// Reagent name. /// </summary> public string ActualName { get; set; } /// <summary> /// Reagent name. /// </summary> public int Version { get; set; } /// <summary> /// Stability days. /// </summary> public int StabilityDays { get; set; } /// <summary> /// Number of tests. /// </summary> public int NumberOfTests { get; set; } /// <summary> /// Reagent comments. /// </summary> public string Comments { get; set; } /// <summary> /// Cocktail reagent Items. /// </summary> public List<CocktailReagentItem> SelectedReagents { get; set; } } public class CocktailReagentItem { public string Id { get; set; } public uint Index { get; set; } public uint Volume { get; set; } public dynamic LdrReagentEntity { get; set; } //The original type is LdrEntity public dynamic BciReagentEntity { get; set; } //The original type is LdrEntity } } The two dynamic properties when converted are of type LdrEntity.
So when I convert to json string, I do the following.
public class LdrExportObj { public List<CocktailReagent> CocktailReagents { get; set; } public string HashCode { get; set; } } public void Export() { var exportObj = new LdrExportObj { CocktailReagents = listOfCocktailReagents, //These are the list of items I am saving HashCode = HashCodeHelper.GenerateHashCode(str), }; var exportStr = JsonConvert.SerializeObject(exportObj, Formatting.Indented); //I get the json string and save it. //Save to file } When I Import I say
public void Import() { var obj = JsonConvert.DeserializeObject<LdrExportObj>(str); var cocktailsStr = JsonConvert.SerializeObject(obj.CocktailReagents, Formatting.Indented); //If I expand the object and see the dynamic properties, it's a JSON string. return new LdrExportObj() { CocktailReagents = obj.CocktailReagents, HashCode = obj.HashCode, }; } When I debug, it's not serializing the internal JSON string back to LdrEntity, instead it's assigning a JSON string to the dynamic property, See the BciReagentEntity Property in the screenshot. I want it to be converted back to the original LdrEntity type.

TypeNameHandlingsetting and deal with the potential security risks, or write a custom converter that figures out the type to use based on the properties present in the JSON.