Skip to content

Latest commit

 

History

History
64 lines (56 loc) · 1.79 KB

DeserializeObjectCreationHandling.md

File metadata and controls

64 lines (56 loc) · 1.79 KB

ObjectCreationHandling setting

This sample deserializes JSON with Argon.ObjectCreationHandling set to Replace so that collection values aren't duplicated.

public class UserViewModel
{
    public string Name { get; set; }
    public IList<string> Offices { get; } = new List<string>
    {
        "Auckland",
        "Wellington",
        "Christchurch"
    };
}

snippet source | anchor

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

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

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

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

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

// Auckland
// Wellington
// Christchurch

snippet source | anchor