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 its 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 serilialize the dynamic property back to LdrEntity instead of the json string. Please help.
Here is my class (Please 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, its a json string.
return new LdrExportObj()
{
CocktailReagents = obj.CocktailReagents,
HashCode = obj.HashCode,
};
}
When I debug, its not serializing the internal json string back to LdrEntity, instead its assigning a json string to the dynamic property, See the BciReagentEntity Property in the screenshot. I want it to be conveted back to the original LdrEntity type.