Thanks to visit codestin.com
Credit goes to github.com

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
27 changes: 22 additions & 5 deletions src/MessagePack/Formatters/EnumAsStringFormatter`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,42 @@
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;
using MessagePack.Internal;

namespace MessagePack.Formatters
{
// Note:This implementation is 'not' fastest, should more improve.
public sealed class EnumAsStringFormatter<T> : IMessagePackFormatter<T>
where T : struct, Enum
{
private readonly bool ignoreCase;
private readonly IReadOnlyDictionary<string, T> nameValueMapping;
private readonly IReadOnlyDictionary<T, string> valueNameMapping;
private readonly IReadOnlyDictionary<string, string>? clrToSerializationName;
private readonly IReadOnlyDictionary<string, string>? serializationToClrName;
private readonly bool isFlags;

/// <summary>
/// Initializes a new instance of the <see cref="EnumAsStringFormatter{T}"/> class
/// that is case sensitive.
/// </summary>
public EnumAsStringFormatter()
: this(ignoreCase: false)
{
this.isFlags = typeof(T).GetCustomAttribute<FlagsAttribute>() is object;
}

/// <summary>
/// Initializes a new instance of the <see cref="EnumAsStringFormatter{T}"/> class.
/// </summary>
/// <param name="ignoreCase">A value indicating whether to allow enum value names to mismatch on deserialization.</param>
public EnumAsStringFormatter(bool ignoreCase)
{
this.ignoreCase = ignoreCase;
StringComparer stringComparer = ignoreCase ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal;

this.isFlags = typeof(T).GetCustomAttribute<FlagsAttribute>() is object;
var fields = typeof(T).GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static);
var nameValueMapping = new Dictionary<string, T>(fields.Length);
var nameValueMapping = new Dictionary<string, T>(fields.Length, ignoreCase ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal);
var valueNameMapping = new Dictionary<T, string>();
Dictionary<string, string>? clrToSerializationName = null;
Dictionary<string, string>? serializationToClrName = null;
Expand All @@ -37,8 +54,8 @@ public EnumAsStringFormatter()
var attribute = enumValueMember.GetCustomAttribute<EnumMemberAttribute>();
if (attribute is { IsValueSetExplicitly: true, Value: not null })
{
clrToSerializationName ??= new();
serializationToClrName ??= new();
clrToSerializationName ??= new(stringComparer);
serializationToClrName ??= new(stringComparer);

clrToSerializationName.Add(name, attribute.Value);
serializationToClrName.Add(attribute.Value, name);
Expand Down Expand Up @@ -79,7 +96,7 @@ public T Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions
// Avoid Enum.Parse when we can because it is too slow.
if (!this.nameValueMapping.TryGetValue(name, out T value))
{
value = (T)Enum.Parse(typeof(T), this.GetClrNames(name));
value = (T)Enum.Parse(typeof(T), this.GetClrNames(name), this.ignoreCase);
}

return value;
Expand Down
64 changes: 64 additions & 0 deletions src/MessagePack/Resolvers/DynamicEnumAsStringIgnoreCaseResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) All contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Reflection;
using MessagePack.Formatters;
using MessagePack.Internal;

namespace MessagePack.Resolvers
{
public sealed class DynamicEnumAsStringIgnoreCaseResolver : IFormatterResolver
{
/// <summary>
/// The singleton instance that can be used.
/// </summary>
public static readonly DynamicEnumAsStringIgnoreCaseResolver Instance = new();

private DynamicEnumAsStringIgnoreCaseResolver()
{
}

public IMessagePackFormatter<T>? GetFormatter<T>()
{
return FormatterCache<T>.Formatter;
}

private static class FormatterCache<T>
{
private static readonly object?[] FormatterCtorArgs = new object?[] { true };

public static readonly IMessagePackFormatter<T>? Formatter;

static FormatterCache()
{
TypeInfo ti = typeof(T).GetTypeInfo();

if (ti.IsNullable())
{
// build underlying type and use wrapped formatter.
ti = ti.GenericTypeArguments[0].GetTypeInfo();
if (!ti.IsEnum)
{
return;
}

var innerFormatter = Instance.GetFormatterDynamic(ti.AsType());
if (innerFormatter == null)
{
return;
}

Formatter = (IMessagePackFormatter<T>?)Activator.CreateInstance(typeof(StaticNullableFormatter<>).MakeGenericType(ti.AsType()), new object[] { innerFormatter });
return;
}
else if (!ti.IsEnum)
{
return;
}

Formatter = (IMessagePackFormatter<T>)Activator.CreateInstance(typeof(EnumAsStringFormatter<>).MakeGenericType(typeof(T)), FormatterCtorArgs)!;
}
}
}
}
6 changes: 5 additions & 1 deletion src/MessagePack/net472/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const MessagePack.ReservedExtensionTypeCodes.UnityVector3 = 31 -> sbyte
const MessagePack.ReservedExtensionTypeCodes.UnityVector4 = 32 -> sbyte
MessagePack.CompositeResolverAttribute
MessagePack.CompositeResolverAttribute.CompositeResolverAttribute(params System.Type![]! resolvers) -> void
MessagePack.Formatters.EnumAsStringFormatter<T>.EnumAsStringFormatter(bool ignoreCase) -> void
MessagePack.Formatters.Matrix3x2Formatter
MessagePack.Formatters.Matrix3x2Formatter.Deserialize(ref MessagePack.MessagePackReader reader, MessagePack.MessagePackSerializerOptions! options) -> System.Numerics.Matrix3x2
MessagePack.Formatters.Matrix3x2Formatter.Serialize(ref MessagePack.MessagePackWriter writer, System.Numerics.Matrix3x2 value, MessagePack.MessagePackSerializerOptions! options) -> void
Expand Down Expand Up @@ -41,6 +42,8 @@ MessagePack.Internal.GeneratedAssemblyMessagePackResolverAttribute.MajorVersion.
MessagePack.Internal.GeneratedAssemblyMessagePackResolverAttribute.MinorVersion.get -> int
MessagePack.Internal.GeneratedAssemblyMessagePackResolverAttribute.ResolverType.get -> System.Type!
MessagePack.ReservedExtensionTypeCodes
MessagePack.Resolvers.DynamicEnumAsStringIgnoreCaseResolver
MessagePack.Resolvers.DynamicEnumAsStringIgnoreCaseResolver.GetFormatter<T>() -> MessagePack.Formatters.IMessagePackFormatter<T>?
MessagePack.Resolvers.SourceGeneratedFormatterResolver
MessagePack.Resolvers.SourceGeneratedFormatterResolver.GetFormatter<T>() -> MessagePack.Formatters.IMessagePackFormatter<T>?
static readonly MessagePack.Formatters.Matrix3x2Formatter.Instance -> MessagePack.Formatters.IMessagePackFormatter<System.Numerics.Matrix3x2>!
Expand All @@ -49,4 +52,5 @@ static readonly MessagePack.Formatters.QuaternionFormatter.Instance -> MessagePa
static readonly MessagePack.Formatters.Vector2Formatter.Instance -> MessagePack.Formatters.IMessagePackFormatter<System.Numerics.Vector2>!
static readonly MessagePack.Formatters.Vector3Formatter.Instance -> MessagePack.Formatters.IMessagePackFormatter<System.Numerics.Vector3>!
static readonly MessagePack.Formatters.Vector4Formatter.Instance -> MessagePack.Formatters.IMessagePackFormatter<System.Numerics.Vector4>!
static readonly MessagePack.Resolvers.SourceGeneratedFormatterResolver.Instance -> MessagePack.Resolvers.SourceGeneratedFormatterResolver!
static readonly MessagePack.Resolvers.DynamicEnumAsStringIgnoreCaseResolver.Instance -> MessagePack.Resolvers.DynamicEnumAsStringIgnoreCaseResolver!
static readonly MessagePack.Resolvers.SourceGeneratedFormatterResolver.Instance -> MessagePack.Resolvers.SourceGeneratedFormatterResolver!
4 changes: 4 additions & 0 deletions src/MessagePack/net6.0/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const MessagePack.ReservedExtensionTypeCodes.UnityVector3 = 31 -> sbyte
const MessagePack.ReservedExtensionTypeCodes.UnityVector4 = 32 -> sbyte
MessagePack.CompositeResolverAttribute
MessagePack.CompositeResolverAttribute.CompositeResolverAttribute(params System.Type![]! resolvers) -> void
MessagePack.Formatters.EnumAsStringFormatter<T>.EnumAsStringFormatter(bool ignoreCase) -> void
MessagePack.Formatters.Matrix3x2Formatter
MessagePack.Formatters.Matrix3x2Formatter.Deserialize(ref MessagePack.MessagePackReader reader, MessagePack.MessagePackSerializerOptions! options) -> System.Numerics.Matrix3x2
MessagePack.Formatters.Matrix3x2Formatter.Serialize(ref MessagePack.MessagePackWriter writer, System.Numerics.Matrix3x2 value, MessagePack.MessagePackSerializerOptions! options) -> void
Expand Down Expand Up @@ -46,6 +47,8 @@ MessagePack.Internal.GeneratedAssemblyMessagePackResolverAttribute.MajorVersion.
MessagePack.Internal.GeneratedAssemblyMessagePackResolverAttribute.MinorVersion.get -> int
MessagePack.Internal.GeneratedAssemblyMessagePackResolverAttribute.ResolverType.get -> System.Type!
MessagePack.ReservedExtensionTypeCodes
MessagePack.Resolvers.DynamicEnumAsStringIgnoreCaseResolver
MessagePack.Resolvers.DynamicEnumAsStringIgnoreCaseResolver.GetFormatter<T>() -> MessagePack.Formatters.IMessagePackFormatter<T>?
MessagePack.Resolvers.SourceGeneratedFormatterResolver
MessagePack.Resolvers.SourceGeneratedFormatterResolver.GetFormatter<T>() -> MessagePack.Formatters.IMessagePackFormatter<T>?
static readonly MessagePack.Formatters.Matrix3x2Formatter.Instance -> MessagePack.Formatters.IMessagePackFormatter<System.Numerics.Matrix3x2>!
Expand All @@ -54,4 +57,5 @@ static readonly MessagePack.Formatters.QuaternionFormatter.Instance -> MessagePa
static readonly MessagePack.Formatters.Vector2Formatter.Instance -> MessagePack.Formatters.IMessagePackFormatter<System.Numerics.Vector2>!
static readonly MessagePack.Formatters.Vector3Formatter.Instance -> MessagePack.Formatters.IMessagePackFormatter<System.Numerics.Vector3>!
static readonly MessagePack.Formatters.Vector4Formatter.Instance -> MessagePack.Formatters.IMessagePackFormatter<System.Numerics.Vector4>!
static readonly MessagePack.Resolvers.DynamicEnumAsStringIgnoreCaseResolver.Instance -> MessagePack.Resolvers.DynamicEnumAsStringIgnoreCaseResolver!
static readonly MessagePack.Resolvers.SourceGeneratedFormatterResolver.Instance -> MessagePack.Resolvers.SourceGeneratedFormatterResolver!
4 changes: 4 additions & 0 deletions src/MessagePack/net8.0/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ MessagePack.Formatters.CharListFormatter.Serialize(ref MessagePack.MessagePackWr
MessagePack.Formatters.DoubleListFormatter
MessagePack.Formatters.DoubleListFormatter.Deserialize(ref MessagePack.MessagePackReader reader, MessagePack.MessagePackSerializerOptions! options) -> System.Collections.Generic.List<double>?
MessagePack.Formatters.DoubleListFormatter.Serialize(ref MessagePack.MessagePackWriter writer, System.Collections.Generic.List<double>? value, MessagePack.MessagePackSerializerOptions! options) -> void
MessagePack.Formatters.EnumAsStringFormatter<T>.EnumAsStringFormatter(bool ignoreCase) -> void
MessagePack.Formatters.Int16ListFormatter
MessagePack.Formatters.Int16ListFormatter.Deserialize(ref MessagePack.MessagePackReader reader, MessagePack.MessagePackSerializerOptions! options) -> System.Collections.Generic.List<short>?
MessagePack.Formatters.Int16ListFormatter.Serialize(ref MessagePack.MessagePackWriter writer, System.Collections.Generic.List<short>? value, MessagePack.MessagePackSerializerOptions! options) -> void
Expand Down Expand Up @@ -92,6 +93,8 @@ MessagePack.Internal.GeneratedAssemblyMessagePackResolverAttribute.MajorVersion.
MessagePack.Internal.GeneratedAssemblyMessagePackResolverAttribute.MinorVersion.get -> int
MessagePack.Internal.GeneratedAssemblyMessagePackResolverAttribute.ResolverType.get -> System.Type!
MessagePack.ReservedExtensionTypeCodes
MessagePack.Resolvers.DynamicEnumAsStringIgnoreCaseResolver
MessagePack.Resolvers.DynamicEnumAsStringIgnoreCaseResolver.GetFormatter<T>() -> MessagePack.Formatters.IMessagePackFormatter<T>?
MessagePack.Resolvers.SourceGeneratedFormatterResolver
MessagePack.Resolvers.SourceGeneratedFormatterResolver.GetFormatter<T>() -> MessagePack.Formatters.IMessagePackFormatter<T>?
static readonly MessagePack.Formatters.BooleanListFormatter.Instance -> MessagePack.Formatters.BooleanListFormatter!
Expand All @@ -112,4 +115,5 @@ static readonly MessagePack.Formatters.UInt64ListFormatter.Instance -> MessagePa
static readonly MessagePack.Formatters.Vector2Formatter.Instance -> MessagePack.Formatters.IMessagePackFormatter<System.Numerics.Vector2>!
static readonly MessagePack.Formatters.Vector3Formatter.Instance -> MessagePack.Formatters.IMessagePackFormatter<System.Numerics.Vector3>!
static readonly MessagePack.Formatters.Vector4Formatter.Instance -> MessagePack.Formatters.IMessagePackFormatter<System.Numerics.Vector4>!
static readonly MessagePack.Resolvers.DynamicEnumAsStringIgnoreCaseResolver.Instance -> MessagePack.Resolvers.DynamicEnumAsStringIgnoreCaseResolver!
static readonly MessagePack.Resolvers.SourceGeneratedFormatterResolver.Instance -> MessagePack.Resolvers.SourceGeneratedFormatterResolver!
4 changes: 4 additions & 0 deletions src/MessagePack/netstandard2.0/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const MessagePack.ReservedExtensionTypeCodes.UnityVector3 = 31 -> sbyte
const MessagePack.ReservedExtensionTypeCodes.UnityVector4 = 32 -> sbyte
MessagePack.CompositeResolverAttribute
MessagePack.CompositeResolverAttribute.CompositeResolverAttribute(params System.Type![]! resolvers) -> void
MessagePack.Formatters.EnumAsStringFormatter<T>.EnumAsStringFormatter(bool ignoreCase) -> void
MessagePack.Formatters.Matrix3x2Formatter
MessagePack.Formatters.Matrix3x2Formatter.Deserialize(ref MessagePack.MessagePackReader reader, MessagePack.MessagePackSerializerOptions! options) -> System.Numerics.Matrix3x2
MessagePack.Formatters.Matrix3x2Formatter.Serialize(ref MessagePack.MessagePackWriter writer, System.Numerics.Matrix3x2 value, MessagePack.MessagePackSerializerOptions! options) -> void
Expand Down Expand Up @@ -41,6 +42,8 @@ MessagePack.Internal.GeneratedAssemblyMessagePackResolverAttribute.MajorVersion.
MessagePack.Internal.GeneratedAssemblyMessagePackResolverAttribute.MinorVersion.get -> int
MessagePack.Internal.GeneratedAssemblyMessagePackResolverAttribute.ResolverType.get -> System.Type!
MessagePack.ReservedExtensionTypeCodes
MessagePack.Resolvers.DynamicEnumAsStringIgnoreCaseResolver
MessagePack.Resolvers.DynamicEnumAsStringIgnoreCaseResolver.GetFormatter<T>() -> MessagePack.Formatters.IMessagePackFormatter<T>?
MessagePack.Resolvers.SourceGeneratedFormatterResolver
MessagePack.Resolvers.SourceGeneratedFormatterResolver.GetFormatter<T>() -> MessagePack.Formatters.IMessagePackFormatter<T>?
static readonly MessagePack.Formatters.Matrix3x2Formatter.Instance -> MessagePack.Formatters.IMessagePackFormatter<System.Numerics.Matrix3x2>!
Expand All @@ -49,4 +52,5 @@ static readonly MessagePack.Formatters.QuaternionFormatter.Instance -> MessagePa
static readonly MessagePack.Formatters.Vector2Formatter.Instance -> MessagePack.Formatters.IMessagePackFormatter<System.Numerics.Vector2>!
static readonly MessagePack.Formatters.Vector3Formatter.Instance -> MessagePack.Formatters.IMessagePackFormatter<System.Numerics.Vector3>!
static readonly MessagePack.Formatters.Vector4Formatter.Instance -> MessagePack.Formatters.IMessagePackFormatter<System.Numerics.Vector4>!
static readonly MessagePack.Resolvers.DynamicEnumAsStringIgnoreCaseResolver.Instance -> MessagePack.Resolvers.DynamicEnumAsStringIgnoreCaseResolver!
static readonly MessagePack.Resolvers.SourceGeneratedFormatterResolver.Instance -> MessagePack.Resolvers.SourceGeneratedFormatterResolver!
4 changes: 4 additions & 0 deletions src/MessagePack/netstandard2.1/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const MessagePack.ReservedExtensionTypeCodes.UnityVector3 = 31 -> sbyte
const MessagePack.ReservedExtensionTypeCodes.UnityVector4 = 32 -> sbyte
MessagePack.CompositeResolverAttribute
MessagePack.CompositeResolverAttribute.CompositeResolverAttribute(params System.Type![]! resolvers) -> void
MessagePack.Formatters.EnumAsStringFormatter<T>.EnumAsStringFormatter(bool ignoreCase) -> void
MessagePack.Formatters.Matrix3x2Formatter
MessagePack.Formatters.Matrix3x2Formatter.Deserialize(ref MessagePack.MessagePackReader reader, MessagePack.MessagePackSerializerOptions! options) -> System.Numerics.Matrix3x2
MessagePack.Formatters.Matrix3x2Formatter.Serialize(ref MessagePack.MessagePackWriter writer, System.Numerics.Matrix3x2 value, MessagePack.MessagePackSerializerOptions! options) -> void
Expand Down Expand Up @@ -41,6 +42,8 @@ MessagePack.Internal.GeneratedAssemblyMessagePackResolverAttribute.MajorVersion.
MessagePack.Internal.GeneratedAssemblyMessagePackResolverAttribute.MinorVersion.get -> int
MessagePack.Internal.GeneratedAssemblyMessagePackResolverAttribute.ResolverType.get -> System.Type!
MessagePack.ReservedExtensionTypeCodes
MessagePack.Resolvers.DynamicEnumAsStringIgnoreCaseResolver
MessagePack.Resolvers.DynamicEnumAsStringIgnoreCaseResolver.GetFormatter<T>() -> MessagePack.Formatters.IMessagePackFormatter<T>?
MessagePack.Resolvers.SourceGeneratedFormatterResolver
MessagePack.Resolvers.SourceGeneratedFormatterResolver.GetFormatter<T>() -> MessagePack.Formatters.IMessagePackFormatter<T>?
static readonly MessagePack.Formatters.Matrix3x2Formatter.Instance -> MessagePack.Formatters.IMessagePackFormatter<System.Numerics.Matrix3x2>!
Expand All @@ -49,4 +52,5 @@ static readonly MessagePack.Formatters.QuaternionFormatter.Instance -> MessagePa
static readonly MessagePack.Formatters.Vector2Formatter.Instance -> MessagePack.Formatters.IMessagePackFormatter<System.Numerics.Vector2>!
static readonly MessagePack.Formatters.Vector3Formatter.Instance -> MessagePack.Formatters.IMessagePackFormatter<System.Numerics.Vector3>!
static readonly MessagePack.Formatters.Vector4Formatter.Instance -> MessagePack.Formatters.IMessagePackFormatter<System.Numerics.Vector4>!
static readonly MessagePack.Resolvers.DynamicEnumAsStringIgnoreCaseResolver.Instance -> MessagePack.Resolvers.DynamicEnumAsStringIgnoreCaseResolver!
static readonly MessagePack.Resolvers.SourceGeneratedFormatterResolver.Instance -> MessagePack.Resolvers.SourceGeneratedFormatterResolver!
Loading
Loading