Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Oct 11, 2022
1 parent 15c72bb commit 919e711
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions src/Workspaces/Remote/Core/Serialization/MessagePackFormatters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ internal sealed class MessagePackFormatters
{
internal static readonly ImmutableArray<IMessagePackFormatter> Formatters = ImmutableArray.Create<IMessagePackFormatter>(
ProjectIdFormatter.Instance,
EncodingFormatter.Instance,
// ForceTypelessFormatter<T> needs to be listed here for each Roslyn abstract type T that is being serialized OOP.
// TODO: add a resolver that provides these https://github.com/dotnet/roslyn/issues/60724
new ForceTypelessFormatter<SimplifierOptions>(),
Expand All @@ -34,8 +35,7 @@ internal sealed class MessagePackFormatters
new ForceTypelessFormatter<IdeCodeStyleOptions>());

private static readonly ImmutableArray<IFormatterResolver> s_resolvers = ImmutableArray.Create<IFormatterResolver>(
StandardResolverAllowPrivate.Instance,
EncodingResolver.Instance);
StandardResolverAllowPrivate.Instance);

internal static readonly IFormatterResolver DefaultResolver = CompositeResolver.Create(Formatters, s_resolvers);

Expand Down Expand Up @@ -111,21 +111,15 @@ public void Serialize(ref MessagePackWriter writer, ProjectId? value, MessagePac
}
}

/// <summary>
/// Creates formatter for <see cref="Encoding"/> serialization.
/// </summary>
internal sealed class EncodingResolver : IFormatterResolver
{
public static readonly EncodingResolver Instance = new();

public IMessagePackFormatter<T>? GetFormatter<T>()
=> typeof(T) == typeof(Encoding) ? (IMessagePackFormatter<T>)(object)EncodingFormatter.Instance : null;
}

/// <summary>
/// Supports (de)serialization of <see cref="Encoding"/> that do not customize <see cref="Encoding.EncoderFallback"/> or <see cref="Encoding.DecoderFallback"/>.
/// The fallback will be discarded if the <see cref="Encoding"/> has any.
/// </summary>
/// <remarks>
/// Only supports (de)serializing values that are statically typed to <see cref="Encoding"/>.
/// This is important as we can't assume anything about arbitrary subtypes of <see cref="Encoding"/>
/// and can only return general <see cref="Encoding"/> from the deserializer.
/// </remarks>
internal sealed class EncodingFormatter : IMessagePackFormatter<Encoding?>
{
public static readonly EncodingFormatter Instance = new();
Expand Down

0 comments on commit 919e711

Please sign in to comment.