dotnet/runtime

Duplicate object keys shouldn't result in an ArgumentException

Open

#71,784 建立於 2022年7月7日

在 GitHub 查看
 (9 留言) (7 反應) (0 負責人)C# (5,445 fork)batch import
area-System.Text.Jsonbughelp wanted

倉庫指標

Star
 (17,886 star)
PR 合併指標
 (平均合併 12天 11小時) (30 天內合併 661 個 PR)

描述

Description

Currently, when a parsed JSON object has duplicate keys, an ArguementException is thrown from the underlying dictionary implementation. I would expect this to be thrown if I were manually attempting to add an existing key, but not from parsing.

Reproduction Steps

var json = JsonNode.Parse("[{\"foo\":1, \"bar\":2, \"foo\":3}]") as JsonArray;

var item = json[0]["bar"];

Expected behavior

Maybe throw JsonException?

Actual behavior

ArgumentException is thrown. Stack trace:

System.ArgumentException : An item with the same key has already been added. Key: {0} (Parameter 'foo')
   at System.Text.Json.ThrowHelper.ThrowArgumentException_DuplicateKey(String propertyName)
   at System.Text.Json.JsonPropertyDictionary`1.AddValue(String propertyName, T value)
   at System.Text.Json.JsonPropertyDictionary`1.Add(String propertyName, T value)
   at System.Text.Json.Nodes.JsonObject.InitializeIfRequired()
   at System.Text.Json.Nodes.JsonObject.System.Collections.Generic.IDictionary<System.String,System.Text.Json.Nodes.JsonNode>.TryGetValue(String propertyName, JsonNode& jsonNode)
   at System.Text.Json.Nodes.JsonObject.TryGetPropertyValue(String propertyName, JsonNode& jsonNode)
   at System.Text.Json.Nodes.JsonObject.GetItem(String propertyName)
   at System.Text.Json.Nodes.JsonNode.get_Item(String propertyName)

Regression?

No response

Known Workarounds

No response

Configuration

Seen in all released versions (.Net 6 and previous)

Other information

No response

貢獻者指南