diff --git a/MessagePack.NodaTime.Tests/ObjectTesting.cs b/MessagePack.NodaTime.Tests/ContractlessTests.cs similarity index 98% rename from MessagePack.NodaTime.Tests/ObjectTesting.cs rename to MessagePack.NodaTime.Tests/ContractlessTests.cs index 62d6819..6cb5821 100644 --- a/MessagePack.NodaTime.Tests/ObjectTesting.cs +++ b/MessagePack.NodaTime.Tests/ContractlessTests.cs @@ -17,7 +17,7 @@ public class LDT } [Collection("ResolverCollection")] - public class ObjectTesting + public class ContractlessTests { [Fact] public void AnonType() diff --git a/MessagePack.NodaTime.Tests/Init/MessagePackTestInit.cs b/MessagePack.NodaTime.Tests/Init/MessagePackTestInit.cs index 68f206e..bba46ac 100644 --- a/MessagePack.NodaTime.Tests/Init/MessagePackTestInit.cs +++ b/MessagePack.NodaTime.Tests/Init/MessagePackTestInit.cs @@ -12,8 +12,9 @@ public ResolverFixture() { var resolver = CompositeResolver.Create(new[] { BuiltinResolver.Instance, - NodatimeResolver.Instance, AttributeFormatterResolver.Instance, + SourceGeneratedFormatterResolver.Instance, + NodatimeResolver.Instance, DynamicEnumAsStringResolver.Instance, ContractlessStandardResolver.Instance } diff --git a/MessagePack.NodaTime.Tests/SourceGeneratedContractTests.cs b/MessagePack.NodaTime.Tests/SourceGeneratedContractTests.cs new file mode 100644 index 0000000..507ed5b --- /dev/null +++ b/MessagePack.NodaTime.Tests/SourceGeneratedContractTests.cs @@ -0,0 +1,53 @@ +// Copyright (c) ARK LTD. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for +// license information. +using MessagePack; +using MessagePack.Resolvers; +using NodaTime; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using Xunit; + +namespace MessagePack.NodaTime.Tests +{ + [MessagePackObject] + public record MyClass + { + [Key(0)] + public LocalDateTime LocalDateTime { get; set; } = LocalDateTime.FromDateTime(DateTime.Now); + + [Key(1)] + public Instant Instant { get; set; } = Instant.FromDateTimeUtc(DateTime.UtcNow); + + [Key(2)] + public Period Period { get; set; } = Period.Zero; + + [Key(3)] + public LocalDate LocalDate { get; set; } = LocalDate.FromDateTime(DateTime.Now.Date); + + [Key(4)] + public OffsetDateTime OffsetDateTime { get; set; } = OffsetDateTime.FromDateTimeOffset(DateTimeOffset.Now); + + [Key(5)] + public Duration Duration { get; set; } = Duration.Zero; + + [Key(6)] + public ZonedDateTime ZonedDateTime { get; set; } = ZonedDateTime.FromDateTimeOffset(DateTimeOffset.Now); + } + + [Collection("ResolverCollection")] + public class SourceGeneratedContractTests + { + [Fact] + public void Roundtrip() + { + var o = new MyClass { LocalDateTime = LocalDateTime.FromDateTime(DateTime.Now) }; + var bin = MessagePackSerializer.Serialize(o); + var res = MessagePackSerializer.Deserialize(bin); + + Assert.Equal(o.LocalDateTime, res.LocalDateTime); // in DateTime format due to 'abc' being DateTime object + } + } +} diff --git a/MessagePack.NodaTime/DurationMessagePackFormatter.cs b/MessagePack.NodaTime/DurationMessagePackFormatter.cs index b25641a..095c884 100644 --- a/MessagePack.NodaTime/DurationMessagePackFormatter.cs +++ b/MessagePack.NodaTime/DurationMessagePackFormatter.cs @@ -10,6 +10,7 @@ namespace MessagePack.NodaTime { + [ExcludeFormatterFromSourceGeneratedResolver] public sealed class DurationAsNanosecondsMessagePackFormatter : IMessagePackFormatter { public static readonly DurationAsNanosecondsMessagePackFormatter Instance = new DurationAsNanosecondsMessagePackFormatter(); diff --git a/MessagePack.NodaTime/LocalDateMessagePackFormatter.cs b/MessagePack.NodaTime/LocalDateMessagePackFormatter.cs index 4ae50dd..506a8dc 100644 --- a/MessagePack.NodaTime/LocalDateMessagePackFormatter.cs +++ b/MessagePack.NodaTime/LocalDateMessagePackFormatter.cs @@ -8,6 +8,7 @@ namespace MessagePack.NodaTime { // Not interoperable + [ExcludeFormatterFromSourceGeneratedResolver] public sealed class LocalDateAsExtMessagePackFormatter : IMessagePackFormatter { public static readonly LocalDateAsExtMessagePackFormatter Instance = new LocalDateAsExtMessagePackFormatter(); diff --git a/MessagePack.NodaTime/MessagePack.NodaTime.csproj b/MessagePack.NodaTime/MessagePack.NodaTime.csproj index 1bbb2c3..6a3121c 100644 --- a/MessagePack.NodaTime/MessagePack.NodaTime.csproj +++ b/MessagePack.NodaTime/MessagePack.NodaTime.csproj @@ -19,7 +19,7 @@ - + diff --git a/MessagePack.NodaTime/PeriodMessagePackFormatter.cs b/MessagePack.NodaTime/PeriodMessagePackFormatter.cs index fe77487..0ad3a6f 100644 --- a/MessagePack.NodaTime/PeriodMessagePackFormatter.cs +++ b/MessagePack.NodaTime/PeriodMessagePackFormatter.cs @@ -10,6 +10,7 @@ namespace MessagePack.NodaTime { + [ExcludeFormatterFromSourceGeneratedResolver] public sealed class PeriodAsIsoStringMessagePackFormatter : IMessagePackFormatter { public static readonly PeriodAsIsoStringMessagePackFormatter Instance = new PeriodAsIsoStringMessagePackFormatter(); diff --git a/README.md b/README.md index 890b08a..229a253 100644 --- a/README.md +++ b/README.md @@ -24,11 +24,12 @@ or download directly from NuGet. To use the NodaTime resolver, you will have to add it to the composite resolver, as shown in the example below: ```csharp CompositeResolver.RegisterAndSetAsDefault( - BuiltinResolver.Instance, - NodatimeResolver.Instance, - AttributeFormatterResolver.Instance, - DynamicEnumAsStringResolver.Instance, - ContractlessStandardResolver.Instance + BuiltinResolver.Instance, + AttributeFormatterResolver.Instance, + SourceGeneratedFormatterResolver.Instance, + NodatimeResolver.Instance, + DynamicEnumAsStringResolver.Instance, + ContractlessStandardResolver.Instance ); ``` ## Quick Start