Skip to content

Commit

Permalink
(CommunityToolkit#135) Made DatasyncSerializer public.
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianhall committed Nov 13, 2024
1 parent 8bedffd commit 81a7898
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,31 @@

namespace CommunityToolkit.Datasync.Client.Serialization;

internal static class DatasyncSerializer
/// <summary>
/// The serialization settings for Datasync settings.
/// </summary>
public static class DatasyncSerializer
{
private readonly static Lazy<JsonSerializerOptions> _initializer = new(GetJsonSerializerOptions);
private static JsonSerializerOptions? _userSuppliedOptions;

/// <summary>
/// Accessor for the common <see cref="JsonSerializerOptions"/> to use for serializing and deserializing
/// content in the service.
/// </summary>
internal static JsonSerializerOptions JsonSerializerOptions { get => _initializer.Value; }
public static JsonSerializerOptions JsonSerializerOptions
{
get => _userSuppliedOptions ?? _initializer.Value;
set => _userSuppliedOptions = value;
}

/// <summary>
/// Serializes an object using the serializer options.
/// </summary>
/// <typeparam name="T">The type of the object.</typeparam>
/// <param name="obj">The object.</param>
/// <returns>The serialized version of the object.</returns>
internal static string Serialize<T>(T obj)
public static string Serialize<T>(T obj)
=> JsonSerializer.Serialize(obj, JsonSerializerOptions);

/// <summary>
Expand All @@ -33,15 +41,15 @@ internal static string Serialize<T>(T obj)
/// <param name="obj">The object.</param>
/// <param name="objType">The type of the object.</param>
/// <returns>The serialized version of the object.</returns>
internal static string Serialize(object obj, Type objType)
public static string Serialize(object obj, Type objType)
=> JsonSerializer.Serialize(obj, objType, JsonSerializerOptions);

/// <summary>
/// Internal method to create a new <see cref="JsonSerializerOptions"/> object for serializing and deserializing
/// content in the service. You should never have to call this.
/// </summary>
/// <returns>A configured <see cref="JsonSerializerOptions"/> object.</returns>
internal static JsonSerializerOptions GetJsonSerializerOptions() => new(JsonSerializerDefaults.Web)
public static JsonSerializerOptions GetJsonSerializerOptions() => new(JsonSerializerDefaults.Web)
{
AllowTrailingCommas = true,
Converters =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}

0 comments on commit 81a7898

Please sign in to comment.