diff --git a/InterlockLedger.Commons.NUnit.Tests/System/PageOfTJsonTests.cs b/InterlockLedger.Commons.NUnit.Tests/System/PageOfTJsonTests.cs new file mode 100644 index 0000000..90341a6 --- /dev/null +++ b/InterlockLedger.Commons.NUnit.Tests/System/PageOfTJsonTests.cs @@ -0,0 +1,54 @@ +// ****************************************************************************************************************************** +// +// Copyright (c) 2018-2023 InterlockLedger Network +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met +// +// * Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * Neither the name of the copyright holder nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// ****************************************************************************************************************************** + +#nullable enable + +using System.Diagnostics; + +using static System.ObjectExtensions; +using static Test.Helpers; + +namespace System; + +[TestFixture] +public class PageOfTJsonTests +{ + [Test] + public void BidirectionalJsonOfPageOfT() { + var page = new PageOf([new LimitedRange(1, 10), new LimitedRange(100, 3)], 1, 5, 2, lastToFirst: true); + string json = JsonSerializer.Serialize(page, JsonSerializerOptions.Default); + TestContext.WriteLine(json); + var resultPage = JsonSerializer.Deserialize>(json); + Assert.That(resultPage, Is.EqualTo(page)); + } + +} diff --git a/InterlockLedger.Commons/InterlockLedger.Commons.csproj b/InterlockLedger.Commons/InterlockLedger.Commons.csproj index 7d0dcd4..a940988 100644 --- a/InterlockLedger.Commons/InterlockLedger.Commons.csproj +++ b/InterlockLedger.Commons/InterlockLedger.Commons.csproj @@ -11,9 +11,9 @@ InterlockLedger git https://github.com/interlockledger/interlockledger-commons.git - 18.1.0 + 18.2.0 InterlockLedger.Commons - Fix initialization error in LimitedRange + Json deserialization fails for PageOf<T> as a record il2.png true true diff --git a/InterlockLedger.Commons/Types/System/LimitedRange.cs b/InterlockLedger.Commons/Types/System/LimitedRange.cs index 0539c04..573d56a 100644 --- a/InterlockLedger.Commons/Types/System/LimitedRange.cs +++ b/InterlockLedger.Commons/Types/System/LimitedRange.cs @@ -68,7 +68,7 @@ public LimitedRange(ulong start, ushort count) { public override int GetHashCode() => HashCode.Combine(End, Start, InvalidityCause); public override bool Equals(object? obj) => obj is LimitedRange limitedRange && Equals(limitedRange); - public bool Equals(LimitedRange other) => End == other.End && Start == other.Start; + public bool Equals(LimitedRange other) => End == other.End && Start == other.Start && string.Equals(InvalidityCause, other.InvalidityCause, StringComparison.Ordinal); public static bool operator ==(LimitedRange left, LimitedRange right) => left.Equals(right); public static bool operator !=(LimitedRange left, LimitedRange right) => !(left == right); diff --git a/InterlockLedger.Commons/Types/System/PageOf.cs b/InterlockLedger.Commons/Types/System/PageOf.cs index 736c898..01efa9f 100644 --- a/InterlockLedger.Commons/Types/System/PageOf.cs +++ b/InterlockLedger.Commons/Types/System/PageOf.cs @@ -30,13 +30,34 @@ // // ****************************************************************************************************************************** + namespace System; #pragma warning disable CA1000 // Do not declare static members on generic types -public record PageOf(IEnumerable Items, ushort Page, byte PageSize, ushort TotalNumberOfPages, bool LastToFirst) +public class PageOf(IEnumerable items, ushort page, byte pageSize, ushort totalNumberOfPages, bool lastToFirst) : IEquatable> { + public IEnumerable Items { get; set; } = items; + public ushort Page { get; set; } = page; + public byte PageSize { get; set; } = pageSize; + public ushort TotalNumberOfPages { get; set; } = totalNumberOfPages; + public bool LastToFirst { get; set; } = lastToFirst; + + public PageOf() : this([], 0, 0, 0, false) { } public PageOf(IEnumerable items, bool lastToFirst) : this(items.Required(), 0, 0, (ushort)(items.SafeAny() ? 1 : 0), lastToFirst) { } - public static PageOf Empty { get; } = new PageOf([], 0, 0, 0, false); + public static PageOf Empty { get; } = new PageOf(); + + public override bool Equals(object? obj) => Equals(obj as PageOf); + public bool Equals(PageOf? other) => + other is not null && + other.Page == Page && + other.PageSize == PageSize && + other.LastToFirst == LastToFirst && + other.TotalNumberOfPages == TotalNumberOfPages && + other.Items.SequenceEqual(Items); + public override int GetHashCode() => HashCode.Combine(Items, Page, PageSize, TotalNumberOfPages, LastToFirst); + + public static bool operator ==(PageOf? left, PageOf? right) => EqualityComparer>.Default.Equals(left, right); + public static bool operator !=(PageOf? left, PageOf? right) => !(left == right); }