Created
January 7, 2026 19:44
-
-
Save AnthonyGiretti/acfb285b91395a3bc89679d88fb4af82 to your computer and use it in GitHub Desktop.
.NET 10 System.Text.Json duplicates rejection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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