Commit: 86b9fc8
Parent: 14d3105

Read numbers as quantities

Mårten Åsberg committed on 2026-07-24 at 08:42
src/JsonLdRecipeParser/Amount.cs +1 -0
diff --git a/src/JsonLdRecipeParser/Amount.cs b/src/JsonLdRecipeParser/Amount.cs
index e3320dd..3ed19cd 100644
@@ -22,6 +22,7 @@ public partial record Amount
internal static Amount? From(Quantity? quantity) =>
quantity?.Value switch
{
double d => new Amount { Value = new ExactAmountValue(d) },
string q when TryParse(q, out var amount) => amount,
QuantitativeValue q => new()
{
src/JsonLdRecipeParser/Json/Quantity.cs +2 -2
diff --git a/src/JsonLdRecipeParser/Json/Quantity.cs b/src/JsonLdRecipeParser/Json/Quantity.cs
index cf30026..a9348ba 100644
@@ -2,8 +2,8 @@ using System.Text.Json.Serialization;
namespace JsonLdRecipeParser.Json;
[JsonConverter(typeof(AlternativesJsonConverter<Quantity, string, QuantitativeValue>))]
internal class Quantity : Alternatives<string, QuantitativeValue>;
[JsonConverter(typeof(AlternativesJsonConverter<Quantity, double, string, QuantitativeValue>))]
internal class Quantity : Alternatives<double, string, QuantitativeValue>;
internal class QuantitativeValue : StructuredValue
{
src/JsonLdRecipeParser/JsonLdRecipeParser.csproj +1 -1
diff --git a/src/JsonLdRecipeParser/JsonLdRecipeParser.csproj b/src/JsonLdRecipeParser/JsonLdRecipeParser.csproj
index 6bf7fa9..23b2d51 100644
@@ -5,7 +5,7 @@
</PropertyGroup>
<PropertyGroup>
<PackageId>JsonLdRecipeParser</PackageId>
<Version>0.0.2</Version>
<Version>0.0.3</Version>
<Authors>Mårten Åsberg</Authors>
<Company>MoreTec</Company>
<Description>Parses JSON-LD to a common recipe format.</Description>
test/JsonLdRecipeParser.Tests/AmountTests/FromShould.cs +13 -0
diff --git a/test/JsonLdRecipeParser.Tests/AmountTests/FromShould.cs b/test/JsonLdRecipeParser.Tests/AmountTests/FromShould.cs
index a903e53..9a62299 100644
@@ -8,6 +8,19 @@ namespace JsonLdRecipeParser.Tests.AmountTests;
public class FromShould
{
[TestMethod]
public void MapADoubleQuantity()
{
// Arrange
var quantity = new Quantity { Value = 4.0 };
// Act
var result = Amount.From(quantity);
// Assert
result.ShouldBeEquivalentTo(new Amount { Value = new ExactAmountValue(4.0d), Unit = null });
}
[TestMethod]
public void MapAStringQuantity()
{
// Arrange