Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriktsarpalis committed Jun 19, 2024
1 parent 124e54c commit 91c9d3c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public partial class JsonObject : IDictionary<string, JsonNode?>
/// </exception>
public void Add(string propertyName, JsonNode? value)
{
if (propertyName is null)
{
ThrowHelper.ThrowArgumentNullException(nameof(propertyName));
}

Dictionary.Add(propertyName, value);
value?.AssignParent(this);
}
Expand Down Expand Up @@ -74,7 +79,15 @@ public void Clear()
/// <exception cref="ArgumentNullException">
/// <paramref name="propertyName"/> is <see langword="null"/>.
/// </exception>
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);
}

/// <summary>
/// Gets the number of elements contained in <see cref="JsonObject"/>.
Expand Down Expand Up @@ -180,7 +193,15 @@ public bool Remove(string propertyName)
/// <exception cref="ArgumentNullException">
/// <paramref name="propertyName"/> is <see langword="null"/>.
/// </exception>
bool IDictionary<string, JsonNode?>.TryGetValue(string propertyName, out JsonNode? jsonNode) => Dictionary.TryGetValue(propertyName, out jsonNode);
bool IDictionary<string, JsonNode?>.TryGetValue(string propertyName, out JsonNode? jsonNode)
{
if (propertyName is null)
{
ThrowHelper.ThrowArgumentNullException(nameof(propertyName));
}

return Dictionary.TryGetValue(propertyName, out jsonNode);
}

/// <summary>
/// Returns <see langword="false"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public partial class JsonObject : IList<KeyValuePair<string, JsonNode?>>
/// <exception cref="InvalidOperationException"><paramref name="value"/> already has a parent.</exception>
public void SetAt(int index, string propertyName, JsonNode? value)
{
if (propertyName is null)
{
ThrowHelper.ThrowArgumentNullException(nameof(propertyName));
}

OrderedDictionary<string, JsonNode?> dictionary = Dictionary;
KeyValuePair<string, JsonNode?> existing = dictionary.GetAt(index);
dictionary.SetAt(index, propertyName, value);
Expand All @@ -48,7 +53,15 @@ public void SetAt(int index, JsonNode? value)
/// <param name="propertyName">The property name to locate.</param>
/// <returns>The index of <paramref name="propertyName"/> if found; otherwise, -1.</returns>
/// <exception cref="ArgumentNullException"><paramref name="propertyName"/> is null.</exception>
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);
}

/// <summary>Inserts a property into the object at the specified index.</summary>
/// <param name="index">The zero-based index at which the property should be inserted.</param>
Expand All @@ -59,6 +72,11 @@ public void SetAt(int index, JsonNode? value)
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is less than 0 or greater than <see cref="Count"/>.</exception>
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,15 @@ internal string GetPropertyName(JsonNode? node)
/// <returns>
/// <see langword="true"/> if a property with the specified name was found; otherwise, <see langword="false"/>.
/// </returns>
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);
}

/// <inheritdoc/>
public override void WriteTo(Utf8JsonWriter writer, JsonSerializerOptions? options = null)
Expand Down

0 comments on commit 91c9d3c

Please sign in to comment.