* Extended functionalities
* Added different UIs
This commit is contained in:
39
MealPlan.Api/Services/Generators/AiRecipeJsonParser.cs
Normal file
39
MealPlan.Api/Services/Generators/AiRecipeJsonParser.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace MealPlan.Api.Services.Generators;
|
||||
|
||||
internal static class AiRecipeJsonParser
|
||||
{
|
||||
public static int GetInt32(JsonElement element, int fallback = 0)
|
||||
{
|
||||
return element.ValueKind switch
|
||||
{
|
||||
JsonValueKind.Number when element.TryGetInt32(out var value) => value,
|
||||
JsonValueKind.Number => (int)element.GetDecimal(),
|
||||
JsonValueKind.String when int.TryParse(element.GetString(), out var parsed) => parsed,
|
||||
JsonValueKind.True => 1,
|
||||
JsonValueKind.False => 0,
|
||||
_ => fallback,
|
||||
};
|
||||
}
|
||||
|
||||
public static int? GetNullableInt32(JsonElement element)
|
||||
{
|
||||
if (element.ValueKind is JsonValueKind.Null or JsonValueKind.Undefined) return null;
|
||||
return GetInt32(element);
|
||||
}
|
||||
|
||||
public static decimal? GetNullableDecimal(JsonElement element)
|
||||
{
|
||||
return element.ValueKind switch
|
||||
{
|
||||
JsonValueKind.Null or JsonValueKind.Undefined => null,
|
||||
JsonValueKind.Number => element.GetDecimal(),
|
||||
JsonValueKind.String when decimal.TryParse(element.GetString(), out var parsed) => parsed,
|
||||
_ => null,
|
||||
};
|
||||
}
|
||||
|
||||
public static string? GetString(JsonElement element) =>
|
||||
element.ValueKind == JsonValueKind.String ? element.GetString() : element.ToString();
|
||||
}
|
||||
Reference in New Issue
Block a user