Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amount used decimal precision to serialize to JSON #387

Merged
merged 1 commit into from Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions specs/Qowaiv.Specs/Financial/Amount_specs.cs
Expand Up @@ -120,16 +120,24 @@ public void System_Text_JSON_deserialization_min_value()
amount.Should().Be(Amount.MinValue);
}

[Test]
public void System_Text_JSON_deserialization_max_value()
[TestCase("7.922816251426434E+28")]
[TestCase("79228162514264337593543950335")]
public void System_Text_JSON_deserialization_max_value(string json)
{
var amount = System.Text.Json.JsonSerializer.Deserialize<Amount>("7.922816251426434E+28");
var amount = System.Text.Json.JsonSerializer.Deserialize<Amount>(json);
amount.Should().Be(Amount.MaxValue);
}

[TestCase(1234.56, 1234.56)]
public void System_Text_JSON_serialization(Amount svo, object json)
=> JsonTester.Write_System_Text_JSON(svo).Should().Be(json);

[Test]
public void System_Text_JSON_serialization_Max_value()
{
var json = System.Text.Json.JsonSerializer.Serialize(Amount.MaxValue);
json.Should().Be("79228162514264337593543950335");
}
#endif
[TestCase("1234.56", 1234.56)]
[TestCase(1234.56, 1234.56)]
Expand All @@ -141,6 +149,10 @@ public void convention_based_deserialization(object json, Amount svo)
public void convention_based_serialization(Amount svo, object json)
=> JsonTester.Write(svo).Should().Be(json);

[Test]
public void convention_based_serialization_max_value()
=> JsonTester.Write(Amount.MaxValue).Should().Be(79228162514264337593543950335M);

[TestCase("Invalid input", typeof(FormatException))]
[TestCase("2017-06-11", typeof(FormatException))]
public void throws_for_invalid_json(object json, Type exceptionType)
Expand Down
12 changes: 11 additions & 1 deletion src/Qowaiv/Financial/Amount.cs
Expand Up @@ -130,7 +130,7 @@ namespace Qowaiv.Financial;
/// enough to have a <see cref="string"/> representation of -0.
/// </remarks>
[Pure]
public double ToJson() => (double)m_Value;
public decimal ToJson() => m_Value;

/// <summary>Returns a <see cref="string"/> that represents the current Amount for debug purposes.</summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
Expand All @@ -147,6 +147,16 @@ public string ToString(string? format, IFormatProvider? formatProvider)
[Pure]
private string ToXmlString() => ToString(CultureInfo.InvariantCulture);

/// <summary>Deserializes the amount from a JSON number.</summary>
/// <param name="json">
/// The JSON number to deserialize.
/// </param>
/// <returns>
/// The deserialized amount.
/// </returns>
[Pure]
public static Amount FromJson(decimal json) => new(json);

/// <summary>Deserializes the amount from a JSON number.</summary>
/// <param name="json">
/// The JSON number to deserialize.
Expand Down
4 changes: 4 additions & 0 deletions src/Qowaiv/Json/Financial/AmountJsonConverter.cs
Expand Up @@ -16,6 +16,10 @@ public class AmountJsonConverter : SvoJsonConverter<Amount>
[Pure]
protected override Amount FromJson(long json) => Amount.FromJson(json);

/// <inheritdoc />
[Pure]
protected override Amount FromJson(decimal json) => Amount.FromJson(json);

/// <inheritdoc />
[Pure]
protected override Amount FromJson(double json) => Amount.FromJson(json);
Expand Down
4 changes: 4 additions & 0 deletions src/Qowaiv/Json/Mathematics/FractionJsonConverter.cs
Expand Up @@ -16,6 +16,10 @@ public class FractionJsonConverter : SvoJsonConverter<Fraction>
[Pure]
protected override Fraction FromJson(long json) => Fraction.FromJson(json);

/// <inheritdoc />
[Pure]
protected override Fraction FromJson(decimal json) => Fraction.FromJson(json);

/// <inheritdoc />
[Pure]
protected override Fraction FromJson(double json) => Fraction.FromJson(json);
Expand Down
10 changes: 9 additions & 1 deletion src/Qowaiv/Json/SvoJsonConverter.cs
Expand Up @@ -109,6 +109,10 @@ public override void WriteAsPropertyName(Utf8JsonWriter writer, TSvo value, Json
[Pure]
protected virtual TSvo FromJson(long json) => FromJson(json.ToString(CultureInfo.InvariantCulture));

/// <summary>Creates the SVO based on its JSON (decimal) number representation.</summary>
[Pure]
protected virtual TSvo FromJson(decimal json) => FromJson((double)json);

/// <summary>Creates the SVO based on its JSON (double) number representation.</summary>
[Pure]
protected virtual TSvo FromJson(double json) => FromJson(json.ToString(CultureInfo.InvariantCulture));
Expand All @@ -123,10 +127,14 @@ private TSvo ReadNumber(ref Utf8JsonReader reader)
{
return FromJson(num);
}
else if (reader.TryGetDouble(out double dec))
else if (reader.TryGetDecimal(out decimal dec))
{
return FromJson(dec);
}
else if (reader.TryGetDouble(out double dbl))
{
return FromJson(dbl);
}
else throw new JsonException($"QowaivJsonConverter does not support writing from {reader.GetString()}.");
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/Qowaiv/Mathematics/Fraction.cs
Expand Up @@ -325,6 +325,16 @@ void ISerializable.GetObjectData(SerializationInfo info, StreamingContext contex
[Pure]
public string ToJson() => ToString(CultureInfo.InvariantCulture);

/// <summary>Deserializes the fraction from a JSON number.</summary>
/// <param name = "json">
/// The JSON number to deserialize.
/// </param>
/// <returns>
/// The deserialized fraction.
/// </returns>
[Pure]
public static Fraction FromJson(decimal json) => Cast(json);

/// <summary>Deserializes the fraction from a JSON number.</summary>
/// <param name = "json">
/// The JSON number to deserialize.
Expand Down