Skip to content

Commit 35c5d7b

Browse files
Copy Deploy decimal converters for time being
1 parent 96fe9ba commit 35c5d7b

7 files changed

+244
-3
lines changed

src/Umbraco.Commerce.Deploy/Artifacts/ServicePriceArtifact.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Text.Json.Serialization;
22
using Umbraco.Cms.Core;
3+
using Umbraco.Deploy.Infrastructure.Serialization;
34

45
namespace Umbraco.Commerce.Deploy.Artifacts
56
{
@@ -9,7 +10,7 @@ public class ServicePriceArtifact
910
public GuidUdi? CountryUdi { get; set; }
1011
public GuidUdi? RegionUdi { get; set; }
1112

12-
// [JsonConverter(typeof(RoundingDecimalJsonConverter), 3)]
13+
[RoundingDecimalConverter(4)]
1314
public decimal Value { get; set; }
1415
}
1516
}

src/Umbraco.Commerce.Deploy/Artifacts/TaxClassArtifact.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using System.Collections.Generic;
22
using Umbraco.Cms.Core;
33
using Umbraco.Cms.Core.Deploy;
4+
using Umbraco.Deploy.Infrastructure.Serialization;
45

56
namespace Umbraco.Commerce.Deploy.Artifacts
67
{
78
public class TaxClassArtifact(GuidUdi? udi, GuidUdi storeUdi, IEnumerable<ArtifactDependency> dependencies = null)
89
: StoreEntityArtifactBase(udi, storeUdi, dependencies)
910
{
10-
// [JsonConverter(typeof(RoundingDecimalJsonConverter), 3)]
11+
[RoundingDecimalConverter(3)]
1112
public decimal DefaultTaxRate { get; set; }
1213

1314
public IEnumerable<CountryRegionTaxRateArtifact>? CountryRegionTaxRates { get; set; }
@@ -21,7 +22,7 @@ public class CountryRegionTaxRateArtifact
2122

2223
public GuidUdi? RegionUdi { get; set; }
2324

24-
// [JsonConverter(typeof(RoundingDecimalJsonConverter), 3)]
25+
[RoundingDecimalConverter(3)]
2526
public decimal TaxRate { get; set; }
2627
}
2728
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Text.Json.Serialization;
3+
4+
namespace Umbraco.Deploy.Infrastructure.Serialization;
5+
6+
/// <summary>
7+
/// Defines an attribute for use with <see cref="RoundingDecimalJsonConverter"/> allowing a parameter
8+
/// to be passed to define the precision required.
9+
/// </summary>
10+
[AttributeUsage(AttributeTargets.Property)]
11+
public class RoundingDecimalConverterAttribute : JsonConverterAttribute
12+
{
13+
private readonly int _precision;
14+
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="RoundingDecimalConverterAttribute"/> class.
17+
/// </summary>
18+
/// <param name="precision"></param>
19+
public RoundingDecimalConverterAttribute(int precision) => _precision = precision;
20+
21+
/// <inheritdoc/>
22+
public override JsonConverter CreateConverter(Type typeToConvert)
23+
{
24+
if (typeToConvert != typeof(decimal))
25+
{
26+
throw new ArgumentException(
27+
$"This converter only works with decimal, and it was provided {typeToConvert.Name}.");
28+
}
29+
30+
return new RoundingDecimalJsonConverter(_precision);
31+
}
32+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization;
4+
5+
namespace Umbraco.Deploy.Infrastructure.Serialization;
6+
7+
/// <summary>
8+
/// Provides a <see cref="JsonConverter"/> for rounding decimal values.
9+
/// </summary>
10+
/// <remarks>
11+
/// The decimal value provided will be rounded to the precision specified.
12+
/// Unnecessary trailing zeros will be removed (e.g. 1.20 rounded to 2dp will serialize to 1.2).
13+
/// </remarks>
14+
public class RoundingDecimalJsonConverter : RoundingDecimalJsonConverterBase<decimal>
15+
{
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="RoundingDecimalJsonConverter"/> class using a default precision of 2.
18+
/// </summary>
19+
public RoundingDecimalJsonConverter()
20+
: base(2)
21+
{
22+
}
23+
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="RoundingDecimalJsonConverter"/> class using the provided precision.
26+
/// </summary>
27+
/// <param name="precision">Required rounding precision.</param>
28+
public RoundingDecimalJsonConverter(int precision)
29+
: base(precision, MidpointRounding.AwayFromZero)
30+
{
31+
}
32+
33+
/// <summary>
34+
/// Initializes a new instance of the <see cref="RoundingDecimalJsonConverter"/> classusing the provided precision and midpoint rounding
35+
/// </summary>
36+
/// <param name="precision">Required rounding precision.</param>
37+
/// <param name="rounding">Required <see cref="MidpointRounding"/>.</param>
38+
public RoundingDecimalJsonConverter(int precision, MidpointRounding rounding)
39+
: base(precision, rounding)
40+
{
41+
}
42+
43+
/// <inheritdoc />
44+
public override void Write(Utf8JsonWriter writer, decimal value, JsonSerializerOptions options)
45+
{
46+
var roundedValue = GetRoundedValue(value);
47+
48+
roundedValue = RemoveUnnecessaryTrailingZeros(roundedValue);
49+
50+
JsonSerializer.Serialize(writer, roundedValue, options);
51+
}
52+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Text.Json.Serialization;
3+
4+
namespace Umbraco.Deploy.Infrastructure.Serialization;
5+
6+
/// <summary>
7+
/// Provides base functionality for <see cref="JsonConverter"/>s for rounding decimal values.
8+
/// </summary>
9+
/// <typeparam name="T">The type of object or value handled by the converter.</typeparam>
10+
public abstract class RoundingDecimalJsonConverterBase<T> : WriteOnlyJsonConverter<T>
11+
{
12+
private readonly int _precision;
13+
private readonly MidpointRounding _rounding;
14+
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="RoundingDecimalJsonConverter"/> class using a default precision of 2.
17+
/// </summary>
18+
protected RoundingDecimalJsonConverterBase()
19+
: this(2)
20+
{
21+
}
22+
23+
/// <summary>
24+
/// Initializes a new instance of the <see cref="RoundingDecimalJsonConverter"/> class using the provided precision.
25+
/// </summary>
26+
/// <param name="precision">Required rounding precision.</param>
27+
protected RoundingDecimalJsonConverterBase(int precision)
28+
: this(precision, MidpointRounding.AwayFromZero)
29+
{ }
30+
31+
/// <summary>
32+
/// Initializes a new instance of the <see cref="RoundingDecimalJsonConverter"/> classusing the provided precision and midpoint rounding
33+
/// </summary>
34+
/// <param name="precision">Required rounding precision.</param>
35+
/// <param name="rounding">Required <see cref="MidpointRounding"/>.</param>
36+
public RoundingDecimalJsonConverterBase(int precision, MidpointRounding rounding)
37+
{
38+
_precision = precision;
39+
_rounding = rounding;
40+
}
41+
42+
/// <summary>
43+
/// Rounds a decimal value.
44+
/// </summary>
45+
protected decimal GetRoundedValue(decimal value) => Math.Round(value, _precision, _rounding);
46+
47+
/// <summary>
48+
/// Removes unnecessary trailing zeros from a provided decimal value.
49+
/// </summary>
50+
protected decimal RemoveUnnecessaryTrailingZeros(decimal roundedValue)
51+
{
52+
for (int i = _precision - 1; i > 0; i--)
53+
{
54+
var newRoundedValue = Math.Round(roundedValue, i, _rounding);
55+
if (newRoundedValue != roundedValue)
56+
{
57+
break;
58+
}
59+
60+
roundedValue = newRoundedValue;
61+
}
62+
63+
return roundedValue;
64+
}
65+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Text.Json.Serialization;
3+
4+
namespace Umbraco.Deploy.Infrastructure.Serialization;
5+
6+
/// <summary>
7+
/// Defines an attribute for use with <see cref="RoundingNullableDecimalJsonConverter"/> allowing a parameter
8+
/// to be passed to define the precision required.
9+
/// </summary>
10+
[AttributeUsage(AttributeTargets.Property)]
11+
public class RoundingNullableDecimalConverterAttribute : JsonConverterAttribute
12+
{
13+
private readonly int _precision;
14+
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="RoundingDecimalJsonConverterAttribute"/> class.
17+
/// </summary>
18+
/// <param name="precision"></param>
19+
public RoundingNullableDecimalConverterAttribute(int precision) => _precision = precision;
20+
21+
/// <inheritdoc/>
22+
public override JsonConverter CreateConverter(Type typeToConvert)
23+
{
24+
if (typeToConvert != typeof(decimal?))
25+
{
26+
throw new ArgumentException(
27+
$"This converter only works with nullable decimal, and it was provided {typeToConvert.Name}.");
28+
}
29+
30+
return new RoundingNullableDecimalJsonConverter(_precision);
31+
}
32+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization;
4+
5+
namespace Umbraco.Deploy.Infrastructure.Serialization;
6+
7+
/// <summary>
8+
/// Provides a <see cref="JsonConverter"/> for rounding decimal values.
9+
/// </summary>
10+
/// <remarks>
11+
/// The decimal value provided will be rounded to the precision specified.
12+
/// Unnecessary trailing zeros will be removed (e.g. 1.20 rounded to 2dp will serialize to 1.2).
13+
/// </remarks>
14+
public class RoundingNullableDecimalJsonConverter : RoundingDecimalJsonConverterBase<decimal?>
15+
{
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="RoundingNullableDecimalJsonConverter"/> class using a default precision of 2.
18+
/// </summary>
19+
public RoundingNullableDecimalJsonConverter()
20+
: base(2)
21+
{
22+
}
23+
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="RoundingNullableDecimalJsonConverter"/> class using the provided precision.
26+
/// </summary>
27+
/// <param name="precision">Required rounding precision.</param>
28+
public RoundingNullableDecimalJsonConverter(int precision)
29+
: base(precision, MidpointRounding.AwayFromZero)
30+
{
31+
}
32+
33+
/// <summary>
34+
/// Initializes a new instance of the <see cref="RoundingNullableDecimalJsonConverter"/> classusing the provided precision and midpoint rounding
35+
/// </summary>
36+
/// <param name="precision">Required rounding precision.</param>
37+
/// <param name="rounding">Required <see cref="MidpointRounding"/>.</param>
38+
public RoundingNullableDecimalJsonConverter(int precision, MidpointRounding rounding)
39+
: base(precision, rounding)
40+
{
41+
}
42+
43+
/// <inheritdoc />
44+
public override void Write(Utf8JsonWriter writer, decimal? value, JsonSerializerOptions options)
45+
{
46+
if (!value.HasValue)
47+
{
48+
writer.WriteNullValue();
49+
return;
50+
}
51+
52+
var roundedValue = GetRoundedValue(value.Value);
53+
54+
roundedValue = RemoveUnnecessaryTrailingZeros(roundedValue);
55+
56+
JsonSerializer.Serialize(writer, roundedValue, options);
57+
}
58+
}

0 commit comments

Comments
 (0)