chaosbot/ChaosBot/WebServer/Services/JsonElementHelper.cs
2020-10-13 01:35:48 +02:00

31 lines
814 B
C#

using System.Collections.Generic;
using System.Text.Json;
namespace ChaosBot.WebServer.Services
{
public static class JsonElementHelper
{
public static dynamic GetValueFromRequest(JsonElement requestBody, string key)
{
JsonElement prop;
try
{
prop = requestBody.GetProperty(key);
}
catch (KeyNotFoundException)
{
return null;
}
return prop.ValueKind switch
{
JsonValueKind.String => prop.GetString(),
JsonValueKind.Number => prop.GetInt64(),
JsonValueKind.True => prop.GetBoolean(),
JsonValueKind.False => prop.GetBoolean(),
_ => null
};
}
}
}