Skip to content

Instantly share code, notes, and snippets.

@AnthonyGiretti
Created January 7, 2026 19:44
Show Gist options
  • Select an option

  • Save AnthonyGiretti/acfb285b91395a3bc89679d88fb4af82 to your computer and use it in GitHub Desktop.

Select an option

Save AnthonyGiretti/acfb285b91395a3bc89679d88fb4af82 to your computer and use it in GitHub Desktop.
.NET 10 System.Text.Json duplicates rejection
using System.Text.Json;
// Payload with a duplicated "Value" key
string json = "{ \"Value\": 1, \"Value\": -1 }";
var options = new JsonSerializerOptions
{
// Reject duplicates option
AllowDuplicateProperties = false
};
try
{
var result = JsonSerializer.Deserialize<MyRecord>(json, options);
}
catch (JsonException ex)
{
Console.WriteLine(ex.Message); // Throws due to duplicate property
}
public record MyRecord(int Value);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment