From 91c9d3ce86ae0732aebe12aa91175fe72a2a2155 Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Wed, 19 Jun 2024 22:18:22 +0100 Subject: [PATCH] Fix #103715. --- .../Text/Json/Nodes/JsonObject.IDictionary.cs | 25 +++++++++++++++++-- .../Text/Json/Nodes/JsonObject.IList.cs | 20 ++++++++++++++- .../src/System/Text/Json/Nodes/JsonObject.cs | 11 ++++++-- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.IDictionary.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.IDictionary.cs index 8756b93366f90..3de8d933a1b07 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.IDictionary.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.IDictionary.cs @@ -25,6 +25,11 @@ public partial class JsonObject : IDictionary /// public void Add(string propertyName, JsonNode? value) { + if (propertyName is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(propertyName)); + } + Dictionary.Add(propertyName, value); value?.AssignParent(this); } @@ -74,7 +79,15 @@ public void Clear() /// /// is . /// - public bool ContainsKey(string propertyName) => Dictionary.ContainsKey(propertyName); + public bool ContainsKey(string propertyName) + { + if (propertyName is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(propertyName)); + } + + return Dictionary.ContainsKey(propertyName); + } /// /// Gets the number of elements contained in . @@ -180,7 +193,15 @@ public bool Remove(string propertyName) /// /// is . /// - bool IDictionary.TryGetValue(string propertyName, out JsonNode? jsonNode) => Dictionary.TryGetValue(propertyName, out jsonNode); + bool IDictionary.TryGetValue(string propertyName, out JsonNode? jsonNode) + { + if (propertyName is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(propertyName)); + } + + return Dictionary.TryGetValue(propertyName, out jsonNode); + } /// /// Returns . diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.IList.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.IList.cs index d7032fd5125ed..372eccdcfe016 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.IList.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.IList.cs @@ -23,6 +23,11 @@ public partial class JsonObject : IList> /// already has a parent. public void SetAt(int index, string propertyName, JsonNode? value) { + if (propertyName is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(propertyName)); + } + OrderedDictionary dictionary = Dictionary; KeyValuePair existing = dictionary.GetAt(index); dictionary.SetAt(index, propertyName, value); @@ -48,7 +53,15 @@ public void SetAt(int index, JsonNode? value) /// The property name to locate. /// The index of if found; otherwise, -1. /// is null. - public int IndexOf(string propertyName) => Dictionary.IndexOf(propertyName); + public int IndexOf(string propertyName) + { + if (propertyName is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(propertyName)); + } + + return Dictionary.IndexOf(propertyName); + } /// Inserts a property into the object at the specified index. /// The zero-based index at which the property should be inserted. @@ -59,6 +72,11 @@ public void SetAt(int index, JsonNode? value) /// is less than 0 or greater than . public void Insert(int index, string propertyName, JsonNode? value) { + if (propertyName is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(propertyName)); + } + Dictionary.Insert(index, propertyName, value); value?.AssignParent(this); } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.cs index 8b32efee897e2..62dcaf2fee8c7 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.cs @@ -116,8 +116,15 @@ internal string GetPropertyName(JsonNode? node) /// /// if a property with the specified name was found; otherwise, . /// - public bool TryGetPropertyValue(string propertyName, out JsonNode? jsonNode) => - Dictionary.TryGetValue(propertyName, out jsonNode); + public bool TryGetPropertyValue(string propertyName, out JsonNode? jsonNode) + { + if (propertyName is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(propertyName)); + } + + return Dictionary.TryGetValue(propertyName, out jsonNode); + } /// public override void WriteTo(Utf8JsonWriter writer, JsonSerializerOptions? options = null)