diff --git a/src/CommunityToolkit.Datasync.Client/Serialization/DatasyncSerializer.cs b/src/CommunityToolkit.Datasync.Client/Serialization/DatasyncSerializer.cs index 7b3b45d..a50a206 100644 --- a/src/CommunityToolkit.Datasync.Client/Serialization/DatasyncSerializer.cs +++ b/src/CommunityToolkit.Datasync.Client/Serialization/DatasyncSerializer.cs @@ -8,15 +8,23 @@ namespace CommunityToolkit.Datasync.Client.Serialization; -internal static class DatasyncSerializer +/// +/// The serialization settings for Datasync settings. +/// +public static class DatasyncSerializer { private readonly static Lazy _initializer = new(GetJsonSerializerOptions); + private static JsonSerializerOptions? _userSuppliedOptions; /// /// Accessor for the common to use for serializing and deserializing /// content in the service. /// - internal static JsonSerializerOptions JsonSerializerOptions { get => _initializer.Value; } + public static JsonSerializerOptions JsonSerializerOptions + { + get => _userSuppliedOptions ?? _initializer.Value; + set => _userSuppliedOptions = value; + } /// /// Serializes an object using the serializer options. @@ -24,7 +32,7 @@ internal static class DatasyncSerializer /// The type of the object. /// The object. /// The serialized version of the object. - internal static string Serialize(T obj) + public static string Serialize(T obj) => JsonSerializer.Serialize(obj, JsonSerializerOptions); /// @@ -33,7 +41,7 @@ internal static string Serialize(T obj) /// The object. /// The type of the object. /// The serialized version of the object. - internal static string Serialize(object obj, Type objType) + public static string Serialize(object obj, Type objType) => JsonSerializer.Serialize(obj, objType, JsonSerializerOptions); /// @@ -41,7 +49,7 @@ internal static string Serialize(object obj, Type objType) /// content in the service. You should never have to call this. /// /// A configured object. - internal static JsonSerializerOptions GetJsonSerializerOptions() => new(JsonSerializerDefaults.Web) + public static JsonSerializerOptions GetJsonSerializerOptions() => new(JsonSerializerDefaults.Web) { AllowTrailingCommas = true, Converters = diff --git a/tests/CommunityToolkit.Datasync.Client.Test/Authentication/GenericAuthenticationProvider_Tests.cs b/tests/CommunityToolkit.Datasync.Client.Test/Authentication/GenericAuthenticationProvider_Tests.cs index 0f595dd..1e5bf2e 100644 --- a/tests/CommunityToolkit.Datasync.Client.Test/Authentication/GenericAuthenticationProvider_Tests.cs +++ b/tests/CommunityToolkit.Datasync.Client.Test/Authentication/GenericAuthenticationProvider_Tests.cs @@ -68,7 +68,6 @@ public void Ctor_WhitespaceHeader_Throws(string headerName) [InlineData(" ")] [InlineData("\t")] [InlineData(" \t ")] - [Trait("Method", "Ctor")] public void Ctor_Authorization_RequiresType(string authType) { Action act = () => _ = new GenericAuthenticationProvider(_ => Task.FromResult(ValidAuthenticationToken), "Authorization", authType); diff --git a/tests/CommunityToolkit.Datasync.Client.Test/Serialization/DatasyncSerializer_Tests.cs b/tests/CommunityToolkit.Datasync.Client.Test/Serialization/DatasyncSerializer_Tests.cs index f6d9b97..25674ad 100644 --- a/tests/CommunityToolkit.Datasync.Client.Test/Serialization/DatasyncSerializer_Tests.cs +++ b/tests/CommunityToolkit.Datasync.Client.Test/Serialization/DatasyncSerializer_Tests.cs @@ -4,7 +4,7 @@ using CommunityToolkit.Datasync.Client.Serialization; using CommunityToolkit.Datasync.TestCommon.Databases; - +using System.Text.Json; using TestData = CommunityToolkit.Datasync.TestCommon.TestData; namespace CommunityToolkit.Datasync.Client.Test.Serialization; @@ -31,4 +31,18 @@ public void Serializer_Tests() actual = DatasyncSerializer.Serialize(movie, typeof(ClientMovie)); actual.Should().Be(expected); } + + [Fact] + public void CanSetSerializerOptions() + { + JsonSerializerOptions options = new(JsonSerializerDefaults.Web); + JsonSerializerOptions sut = DatasyncSerializer.JsonSerializerOptions; + sut.Should().NotBeNull().And.NotBe(options); + + DatasyncSerializer.JsonSerializerOptions = options; + DatasyncSerializer.JsonSerializerOptions.Should().Be(options); + + DatasyncSerializer.JsonSerializerOptions = null; + DatasyncSerializer.JsonSerializerOptions.Should().Be(sut); + } }