Skip to content

Commit

Permalink
Optimize JSON object node.
Browse files Browse the repository at this point in the history
  • Loading branch information
kingcean committed Feb 19, 2024
1 parent 4922b26 commit 19cb9dd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Core/Collection/List/ListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,34 @@ namespace Trivial.Collection;
/// </summary>
public static partial class ListExtensions
{
/// <summary>
/// Tries to get the string value.
/// </summary>
/// <param name="dict">The source dictionary.</param>
/// <param name="key">The key.</param>
/// <param name="value">The value output.</param>
/// <returns>true if exist and not empty; otherwise, false.</returns>
/// <exception cref="ArgumentNullException">dict or key was null.</exception>
public static bool TryGetNotEmptyValue(this IDictionary<string, string> dict, string key, out string value)
{
if (dict == null) throw new ArgumentNullException(nameof(dict), "dict should not be null.");
value = dict.TryGetValue(key, out var v) && !string.IsNullOrEmpty(v) ? v : null;
return v != null;
}

/// <summary>
/// Tries to get the string value.
/// </summary>
/// <param name="dict">The source dictionary.</param>
/// <param name="key">The key.</param>
/// <returns>The value; or null if not exist or empty.</returns>
/// <exception cref="ArgumentNullException">dict or key was null.</exception>
public static string TryGetNotEmptyValue(this IDictionary<string, string> dict, string key)
{
if (dict == null) throw new ArgumentNullException(nameof(dict), "dict should not be null.");
return dict.TryGetValue(key, out var v) && !string.IsNullOrEmpty(v) ? v : null;
}

/// <summary>
/// Adds a key and a value to the end of the key value pairs.
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions Core/Text/Json/ObjectNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,21 @@ public IJsonDataNode GetValue(string key)
return store[key] ?? JsonValues.Null;
}

/// <summary>
/// Gets the value of the specific property.
/// </summary>
/// <param name="key">The property key.</param>
/// <param name="undefined">true if return undefined when the property does not exist; otherwise, false.</param>
/// <returns>The value.</returns>
/// <exception cref="ArgumentNullException">The property key should not be null, empty, or consists only of white-space characters.</exception>
/// <exception cref="ArgumentOutOfRangeException">The property does not exist and argument undefined is false.</exception>
public IJsonDataNode GetValue(string key, bool undefined)
{
AssertKey(key);
if (!undefined) return store[key] ?? JsonValues.Null;
return store.TryGetValue(key, out var v) ? (v ?? JsonValues.Null) : JsonValues.Undefined;
}

/// <summary>
/// Gets the value of the specific property.
/// </summary>
Expand Down

0 comments on commit 19cb9dd

Please sign in to comment.