-1

I have a weird issue and am confused. I have 3 class:

public class ProductInfo
{
    public List<PartInfo> PartInfos { get; set; }
    public string Name { get; set; }
}
public class SubProductInfo : PartInfo
{
    public ProductInfo ProductInfo { get; set; }
}
public class PartInfo
{
    public string Name { get; set; }
    public string Desc { get; set; }
}

I have some information for industrial products. each product has some part and some sub product. for each product I have an object from type 'ProductInfo'. for each part related to this product I have an object of type 'PartInfo' and for each sub product related to this product I have an object of type 'SubProductInfo'. this 'ProductInfo' object serialized with 'JsonConvert.SerializeObject' method. Example for serialized information:

{"PartInfos":[{"Name":"Part1","Desc":"Part1's Desc"},{"ProductInfo":{"PartInfos":[{"Name":"SubProduct1's Part1","Desc":"SubProduct1's Part1 desc"},{"Name":"SubProduct1's Part2","Desc":"SubProduct1's Part2 desc"}],"Name":"SubProduct1"},"Name":"SubProduct1 Details","Desc":"SubProduct1's Desc"}],"Name":"Product1"}

now I want to deserialize these JSONs to orginal object. I used:

JsonConvert.DeserializeObject<ProductInfo>(serializedObject)

but in deserialization 'SubProductInfo' objects will deserialize to 'PartInfo' objects. how I can deserialize these JSONs to Exactly orginal objects?

Mohsen Koorani
  • 241
  • 2
  • 10

2 Answers2

0

Like I commented, I really don't undestand what you are trying to achieve.

I think you want to make a list of SubProductInfo instead of PartInfo, as PartInfo is the base of SubProductInfo. Like this:

public class ProductInfo
{
    public List<SubProductInfo> PartInfos { get; set; }
    public string Name { get; set; }
}
public class SubProductInfo : PartInfo
{
    public ProductInfo ProductInfo { get; set; }
}
public class PartInfo
{
    public string Name { get; set; }
    public string Desc { get; set; }
}

If a SubProductInfo is something different than a PartInfo and both need to be part of you ProductInfo class, you should keep seperate lists:

public class ProductInfo
{
    public List<PartInfo> PartInfos { get; set; }
    public List<SubProductInfo> SubProductInfos { get; set; }
    public string Name { get; set; }
}
public class SubProductInfo : PartInfo
{
    public ProductInfo ProductInfo { get; set; }
}
public class PartInfo
{
    public string Name { get; set; }
    public string Desc { get; set; }
}

Maybe you should avoid inheritence, it helps visualizing your data model.

Edit

The second option won't (de)serialize as SubProductInfo has a circular connection to ProductInfo. You can mark the ProductInfo property as not serializeble and fill it after reading it from json. Or just remove it, since the code that wants to use it should have access to the parent ProductInfo class.

ikwillem
  • 1,044
  • 1
  • 12
  • 24
0

Check out this: https://www.newtonsoft.com/json/help/html/SerializeTypeNameHandling.htm

When you're serializing, the protocol you're using usually has manners to solve problems like this, usually by type hinting. Basically your serialized product contains data that tells the deserializre which type to use when deserializing.

Glubus
  • 2,819
  • 1
  • 12
  • 26