You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
631 B
25 lines
631 B
1 year ago
|
using System.Text.Json;
|
||
|
using System.Text.Json.Nodes;
|
||
|
|
||
|
namespace StabilityMatrix.Core.Extensions;
|
||
|
|
||
|
public static class JsonObjectExtensions
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// Returns the value of a property with the specified name, or the specified default value if not found.
|
||
|
/// </summary>
|
||
|
public static T GetPropertyValueOrDefault<T>(
|
||
|
this JsonObject jsonObject,
|
||
|
string propertyName,
|
||
|
T defaultValue
|
||
|
)
|
||
|
{
|
||
|
if (!jsonObject.TryGetPropertyValue(propertyName, out var node))
|
||
|
{
|
||
|
return defaultValue;
|
||
|
}
|
||
|
|
||
|
return node.Deserialize<T>() ?? defaultValue;
|
||
|
}
|
||
|
}
|