Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading