Skip to content

Commit ed71d7e

Browse files
committed
Add additional constructors for json serialization and add JsonConstructor attributes
1 parent 4dcf856 commit ed71d7e

File tree

9 files changed

+71
-40
lines changed

9 files changed

+71
-40
lines changed

src/ImageSharp/Formats/Webp/Lossy/Vp8BandProbas.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using System.Text.Json.Serialization;
5+
46
namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
57

68
/// <summary>
@@ -20,6 +22,13 @@ public Vp8BandProbas()
2022
}
2123
}
2224

25+
/// <summary>
26+
/// Initializes a new instance of the <see cref="Vp8BandProbas"/> class.
27+
/// Only used for unit tests.
28+
/// </summary>
29+
[JsonConstructor]
30+
public Vp8BandProbas(Vp8ProbaArray[] probabilities) => this.Probabilities = probabilities;
31+
2332
/// <summary>
2433
/// Gets the Probabilities.
2534
/// </summary>

src/ImageSharp/Formats/Webp/Lossy/Vp8CostArray.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using System.Text.Json.Serialization;
5+
46
namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
57

68
internal class Vp8CostArray
@@ -10,5 +12,12 @@ internal class Vp8CostArray
1012
/// </summary>
1113
public Vp8CostArray() => this.Costs = new ushort[67 + 1];
1214

15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="Vp8CostArray"/> class.
17+
/// Only used for unit tests.
18+
/// </summary>
19+
[JsonConstructor]
20+
public Vp8CostArray(ushort[] costs) => this.Costs = costs;
21+
1322
public ushort[] Costs { get; }
1423
}

src/ImageSharp/Formats/Webp/Lossy/Vp8Costs.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using System.Text.Json.Serialization;
5+
46
namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
57

68
internal class Vp8Costs
@@ -17,6 +19,13 @@ public Vp8Costs()
1719
}
1820
}
1921

22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="Vp8Costs"/> class.
24+
/// Only used for unit tests.
25+
/// </summary>
26+
[JsonConstructor]
27+
public Vp8Costs(Vp8CostArray[] costs) => this.Costs = costs;
28+
2029
/// <summary>
2130
/// Gets the Costs.
2231
/// </summary>

src/ImageSharp/Formats/Webp/Lossy/Vp8ProbaArray.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using System.Text.Json.Serialization;
5+
46
namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
57

68
/// <summary>
@@ -13,6 +15,13 @@ internal class Vp8ProbaArray
1315
/// </summary>
1416
public Vp8ProbaArray() => this.Probabilities = new byte[WebpConstants.NumProbas];
1517

18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="Vp8ProbaArray"/> class.
20+
/// Only used for unit tests.
21+
/// </summary>
22+
[JsonConstructor]
23+
public Vp8ProbaArray(byte[] probabilities) => this.Probabilities = probabilities;
24+
1625
/// <summary>
1726
/// Gets the probabilities.
1827
/// </summary>

src/ImageSharp/Formats/Webp/Lossy/Vp8Residual.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Runtime.InteropServices;
88
using System.Runtime.Intrinsics;
99
using System.Runtime.Intrinsics.X86;
10+
using System.Text.Json.Serialization;
1011

1112
namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
1213

@@ -15,6 +16,22 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
1516
/// </summary>
1617
internal class Vp8Residual
1718
{
19+
public Vp8Residual()
20+
{
21+
}
22+
23+
[JsonConstructor]
24+
public Vp8Residual(int first, int last, int coeffType, short[] coeffs, Vp8BandProbas[] prob, Vp8Stats[] stats, Vp8Costs[] costs)
25+
{
26+
this.First = first;
27+
this.Last = last;
28+
this.CoeffType = coeffType;
29+
this.Coeffs = coeffs;
30+
this.Prob = prob;
31+
this.Stats = stats;
32+
this.Costs = costs;
33+
}
34+
1835
public int First { get; set; }
1936

2037
public int Last { get; set; }

src/ImageSharp/Formats/Webp/Lossy/Vp8Stats.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using System.Text.Json.Serialization;
5+
46
namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
57

68
internal class Vp8Stats
@@ -17,5 +19,12 @@ public Vp8Stats()
1719
}
1820
}
1921

22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="Vp8Stats"/> class.
24+
/// Only used for unit tests.
25+
/// </summary>
26+
[JsonConstructor]
27+
public Vp8Stats(Vp8StatsArray[] stats) => this.Stats = stats;
28+
2029
public Vp8StatsArray[] Stats { get; }
2130
}

src/ImageSharp/Formats/Webp/Lossy/Vp8StatsArray.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using System.Text.Json.Serialization;
5+
46
namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
57

68
internal class Vp8StatsArray
@@ -10,5 +12,12 @@ internal class Vp8StatsArray
1012
/// </summary>
1113
public Vp8StatsArray() => this.Stats = new uint[WebpConstants.NumProbas];
1214

15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="Vp8StatsArray"/> class.
17+
/// Only used for unit tests.
18+
/// </summary>
19+
[JsonConstructor]
20+
public Vp8StatsArray(uint[] stats) => this.Stats = stats;
21+
1322
public uint[] Stats { get; }
1423
}

tests/ImageSharp.Tests/Formats/WebP/Serialization/Vp8CostArrayJsonConverter.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
// Licensed under the Six Labors Split License.
33

44
using System.Runtime.Intrinsics.X86;
5-
using System.Text.Json;
65
using SixLabors.ImageSharp.Formats.Webp;
76
using SixLabors.ImageSharp.Formats.Webp.Lossy;
8-
using SixLabors.ImageSharp.Tests.Formats.WebP.Serialization;
97
using SixLabors.ImageSharp.Tests.TestUtilities;
108
using JsonSerializer = System.Text.Json.JsonSerializer;
119

@@ -14,29 +12,6 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp;
1412
[Trait("Format", "Webp")]
1513
public class Vp8ResidualTests
1614
{
17-
[Fact]
18-
public void Vp8CostArray_Serialization_Works()
19-
{
20-
// arrange
21-
Vp8CostArray expected = new();
22-
for (ushort i = 0; i < expected.Costs.Length; i++)
23-
{
24-
expected.Costs[i] = i;
25-
}
26-
27-
JsonSerializerOptions options = new()
28-
{
29-
Converters = { new Vp8CostArrayJsonConverter() }
30-
};
31-
32-
// act
33-
string jsonString = JsonSerializer.Serialize(expected);
34-
Vp8CostArray actual = JsonSerializer.Deserialize<Vp8CostArray>(jsonString, options);
35-
36-
// assert
37-
Assert.Equal(expected.Costs, actual.Costs);
38-
}
39-
4015
[Fact]
4116
public void Vp8Residual_Serialization_Works()
4217
{

0 commit comments

Comments
 (0)