Add Windows desktop version
This commit is contained in:
63
windows/StreamPlayer.Desktop/JsonExtensions.cs
Normal file
63
windows/StreamPlayer.Desktop/JsonExtensions.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace StreamPlayer.Desktop;
|
||||
|
||||
public static class JsonExtensions
|
||||
{
|
||||
public static string GetPropertyOrDefault(this JsonElement element, string propertyName)
|
||||
{
|
||||
if (element.ValueKind == JsonValueKind.Object && element.TryGetProperty(propertyName, out var property))
|
||||
{
|
||||
return property.ValueKind switch
|
||||
{
|
||||
JsonValueKind.String => property.GetString() ?? string.Empty,
|
||||
JsonValueKind.Number => property.GetRawText(),
|
||||
JsonValueKind.True => "true",
|
||||
JsonValueKind.False => "false",
|
||||
_ => property.GetRawText()
|
||||
};
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public static int GetPropertyOrDefaultInt(this JsonElement element, string propertyName, int fallback = 0)
|
||||
{
|
||||
if (element.ValueKind == JsonValueKind.Object && element.TryGetProperty(propertyName, out var property))
|
||||
{
|
||||
if (property.TryGetInt32(out var value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
public static long GetPropertyOrDefaultLong(this JsonElement element, string propertyName, long fallback = 0)
|
||||
{
|
||||
if (element.ValueKind == JsonValueKind.Object && element.TryGetProperty(propertyName, out var property))
|
||||
{
|
||||
if (property.TryGetInt64(out var value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
public static bool GetPropertyOrDefaultBool(this JsonElement element, string propertyName, bool fallback = false)
|
||||
{
|
||||
if (element.ValueKind == JsonValueKind.Object && element.TryGetProperty(propertyName, out var property))
|
||||
{
|
||||
if (property.ValueKind == JsonValueKind.True)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (property.ValueKind == JsonValueKind.False)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user