Newtonsoft unified deserialization elements when deserializing a list inside another list

I have a list of objects (A), each of which contains a list of objects (B). I did the serialization of the list as no problem, but when I did the deserialization of As. The list of Bs inside each A has twice the original number of Bs. Why is this happening?

        var sample = new List<A>
        {
            new A
            {
                Flag = true,
                Amount = 10,
                Bs = new List<B>
                {
                    new B {Amount = 4, Id = Guid.NewGuid()},
                    new B {Amount = 6, Id = Guid.NewGuid()}
                }
            },
            new A
            {
                Flag = true,
                Amount = 20,
                Bs = new List<B>
                {
                    new B {Amount = 4, Id = Guid.NewGuid()},
                    new B {Amount = 6, Id = Guid.NewGuid()}
                }
            },
            new A
            {
                Flag = false,
                Amount = 30,
                Bs = new List<B>
                {
                    new B {Amount = 4, Id = Guid.NewGuid()},
                    new B {Amount = 6, Id = Guid.NewGuid()}
                }
            }
        };

        var serialized = JsonConvert.SerializeObject(sample, ContractResolver.AllMembersSettings);
        var deserialized = JsonConvert.DeserializeObject<List<A>>(serialized, ContractResolver.AllMembersSettings);

class A
{
    public bool Flag { get; set; }
    public decimal Amount { get; set; }
    public List<B> Bs { get; set; }
}

class B
{
    public Guid Id { get; set; }
    public decimal Amount { get; set; }
}

public class ContractResolver : DefaultContractResolver
{
    public static readonly JsonSerializerSettings AllMembersSettings =
        new JsonSerializerSettings
        {
            TypeNameHandling = TypeNameHandling.All,
            ContractResolver = new ContractResolver()
        };

    protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
    {
        var props =
            type
                .GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                .Where(p => p.CanRead && p.CanWrite)
                .Select(p => base.CreateProperty(p, memberSerialization))
            .Union(
            type
                .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                .Select(f => base.CreateProperty(f, memberSerialization)))
            .ToList();

        props.ForEach(p => { p.Writable = true; p.Readable = true; });

        return props;
    }
}
+4
source share
3 answers

This is because the C # compiler generates support fields under the hood for properties.

You can either remove the user-created Resolver and let Json.NET do its magic, or use a small hack at the end.

Authorized Properties

( ) . , . , . , , . - .

, , , .
, , BindingFlags.NonPublic, , , .

type
    .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
    .Where(field => !field.Name.EndsWith("k__BackingField"))
    .Select(f => base.CreateProperty(f, memberSerialization))
)
+2

, , ContractResolver, :

string sampleData = Newtonsoft.Json.JsonConvert.SerializeObject(sample);
List<A> test = Newtonsoft.Json.JsonConvert.DeserializeObject<List<A>>(sampleData);

, .

+1

, ObjectCreationHandling Replace

Newtonsoft:

https://www.newtonsoft.com/json/help/html/DeserializeObjectCreationHandling.htm

:

public class UserViewModel
{
    public string Name { get; set; }
    public IList<string> Offices { get; private set; }

    public UserViewModel()
    {
        Offices = new List<string>
        {
            "Auckland",
            "Wellington",
            "Christchurch"
        };
    }
}

:

string json = @"{
  'Name': 'James',
  'Offices': [
    'Auckland',
    'Wellington',
    'Christchurch'
  ]
}";

UserViewModel model1 = JsonConvert.DeserializeObject<UserViewModel>(json);

foreach (string office in model1.Offices)
{
    Console.WriteLine(office);
}
// Auckland
// Wellington
// Christchurch
// Auckland
// Wellington
// Christchurch

UserViewModel model2 = JsonConvert.DeserializeObject<UserViewModel>(json, new JsonSerializerSettings
{
    ObjectCreationHandling = ObjectCreationHandling.Replace
});

foreach (string office in model2.Offices)
{
    Console.WriteLine(office);
}

// Auckland
// Wellington
// Christchurch

, , , - , .

0

Source: https://habr.com/ru/post/1629068/


All Articles