ObjectCreationHandling setting

September 8, 2025 ยท View on GitHub

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; } =
    [
        "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