Overlay data from JSON string to existing object instance

后端 未结 4 1500
余生分开走
余生分开走 2020-12-03 02:32

I want to deserialize a JSON string which does not necessarily contain data for every member, e.g:

public class MyStructure
{
   public string Field1;
   pub         


        
4条回答
  •  萌比男神i
    2020-12-03 03:07

    Realize - JsonConvert.PopulateObject(string,object) will NOT work for collections.

    Even with PreserveReferencesHandling = Objects/Arrays/All and an IReferenceResolver. JSON.NET will not update items in collections. Instead, it will duplicate your collection items.

    JSON.NET only uses its ("ref") Preserve Reference identifiers to reuse references read within the serialized JSON. JSON.NET will not reuse instances in existing nested object graphs. We attempted by adding an ID property to all our objects, but JSON.NET IReferenceResolver does not provide the facilities to find & match existing references within collections.

    Our solution will be to deserialize JSON into a new object instance and map properties across the 2 instances using either Fasterflect or AutoMapper.

提交回复
热议问题