0

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.

dynamic property is json string

dbc
  • 104,963
  • 20
  • 228
  • 340
nikhil
  • 1,578
  • 3
  • 23
  • 52
  • There are already several questions about this including [Json.Net Serialization of Type with Polymorphic Child Object](https://stackoverflow.com/q/29528648/3744182), [How to implement custom JsonConverter in JSON.NET to deserialize a List of base class objects?](https://stackoverflow.com/q/8030538/3744182), [Deserializing polymorphic json classes without type information using json.net](https://stackoverflow.com/q/19307752/3744182), ... – dbc Jun 16 '22 at 21:06
  • ... [Json.net serialize/deserialize derived types?](https://stackoverflow.com/q/8513042/3744182) and [how to deserialize JSON into IEnumerable with Newtonsoft JSON.NET](https://stackoverflow.com/q/6348215/3744182). – dbc Jun 16 '22 at 21:07
  • You basically have two options: Use Json.NET's [`TypeNameHandling` setting](https://www.newtonsoft.com/json/help/html/serializetypenamehandling.htm) and deal with the potential [security risks](https://stackoverflow.com/q/39565954/3744182), or write a [custom converter](https://stackoverflow.com/a/8031283/3744182) that figures out the type to use based on the properties present in the JSON. – dbc Jun 16 '22 at 21:09
  • Take a look through those questions and see which is the best fit for you, there are so many pre-existing answers that there isn't much purpose in giving another nonspecific answer here. – dbc Jun 16 '22 at 21:10

0 Answers0