using MessagePack.Formatters; using System.Linq; using MessagePack.Internal; using MessagePack.Resolvers; namespace MessagePack.Resolvers { /// /// Default composited resolver, builtin -> attribute -> dynamic enum -> dynamic generic -> dynamic union -> dynamic object -> primitive. /// public sealed class StandardResolver : IFormatterResolver { public static readonly IFormatterResolver Instance = new StandardResolver(); #if NETSTANDARD public static readonly IMessagePackFormatter ObjectFallbackFormatter = new DynamicObjectTypeFallbackFormatter(StandardResolverCore.Instance); #endif StandardResolver() { } public IMessagePackFormatter GetFormatter() { return FormatterCache.formatter; } static class FormatterCache { public static readonly IMessagePackFormatter formatter; static FormatterCache() { if (typeof(T) == typeof(object)) { // final fallback #if NETSTANDARD formatter = (IMessagePackFormatter)ObjectFallbackFormatter; #else formatter = PrimitiveObjectResolver.Instance.GetFormatter(); #endif } else { formatter = StandardResolverCore.Instance.GetFormatter(); } } } } public sealed class ContractlessStandardResolver : IFormatterResolver { public static readonly IFormatterResolver Instance = new ContractlessStandardResolver(); #if NETSTANDARD public static readonly IMessagePackFormatter ObjectFallbackFormatter = new DynamicObjectTypeFallbackFormatter(ContractlessStandardResolverCore.Instance); #endif ContractlessStandardResolver() { } public IMessagePackFormatter GetFormatter() { return FormatterCache.formatter; } static class FormatterCache { public static readonly IMessagePackFormatter formatter; static FormatterCache() { if (typeof(T) == typeof(object)) { // final fallback #if NETSTANDARD formatter = (IMessagePackFormatter)ObjectFallbackFormatter; #else formatter = PrimitiveObjectResolver.Instance.GetFormatter(); #endif } else { formatter = ContractlessStandardResolverCore.Instance.GetFormatter(); } } } } public sealed class StandardResolverAllowPrivate : IFormatterResolver { public static readonly IFormatterResolver Instance = new StandardResolverAllowPrivate(); #if NETSTANDARD public static readonly IMessagePackFormatter ObjectFallbackFormatter = new DynamicObjectTypeFallbackFormatter(StandardResolverAllowPrivateCore.Instance); #endif StandardResolverAllowPrivate() { } public IMessagePackFormatter GetFormatter() { return FormatterCache.formatter; } static class FormatterCache { public static readonly IMessagePackFormatter formatter; static FormatterCache() { if (typeof(T) == typeof(object)) { // final fallback #if NETSTANDARD formatter = (IMessagePackFormatter)ObjectFallbackFormatter; #else formatter = PrimitiveObjectResolver.Instance.GetFormatter(); #endif } else { formatter = StandardResolverAllowPrivateCore.Instance.GetFormatter(); } } } } public sealed class ContractlessStandardResolverAllowPrivate : IFormatterResolver { public static readonly IFormatterResolver Instance = new ContractlessStandardResolverAllowPrivate(); #if NETSTANDARD public static readonly IMessagePackFormatter ObjectFallbackFormatter = new DynamicObjectTypeFallbackFormatter(ContractlessStandardResolverAllowPrivateCore.Instance); #endif ContractlessStandardResolverAllowPrivate() { } public IMessagePackFormatter GetFormatter() { return FormatterCache.formatter; } static class FormatterCache { public static readonly IMessagePackFormatter formatter; static FormatterCache() { if (typeof(T) == typeof(object)) { // final fallback #if NETSTANDARD formatter = (IMessagePackFormatter)ObjectFallbackFormatter; #else formatter = PrimitiveObjectResolver.Instance.GetFormatter(); #endif } else { formatter = ContractlessStandardResolverAllowPrivateCore.Instance.GetFormatter(); } } } } } namespace MessagePack.Internal { internal static class StandardResolverHelper { public static readonly IFormatterResolver[] DefaultResolvers = new[] { BuiltinResolver.Instance, // Try Builtin AttributeFormatterResolver.Instance, // Try use [MessagePackFormatter] #if !NETSTANDARD MessagePack.Unity.UnityResolver.Instance, #endif #if !ENABLE_IL2CPP && !UNITY_WSA && !NET_STANDARD_2_0 DynamicEnumResolver.Instance, // Try Enum DynamicGenericResolver.Instance, // Try Array, Tuple, Collection DynamicUnionResolver.Instance, // Try Union(Interface) #endif }; } internal sealed class StandardResolverCore : IFormatterResolver { public static readonly IFormatterResolver Instance = new StandardResolverCore(); static readonly IFormatterResolver[] resolvers = StandardResolverHelper.DefaultResolvers.Concat(new IFormatterResolver[] { #if !ENABLE_IL2CPP && !UNITY_WSA && !NET_STANDARD_2_0 DynamicObjectResolver.Instance, // Try Object #endif }).ToArray(); StandardResolverCore() { } public IMessagePackFormatter GetFormatter() { return FormatterCache.formatter; } static class FormatterCache { public static readonly IMessagePackFormatter formatter; static FormatterCache() { foreach (var item in resolvers) { var f = item.GetFormatter(); if (f != null) { formatter = f; return; } } } } } internal sealed class ContractlessStandardResolverCore : IFormatterResolver { public static readonly IFormatterResolver Instance = new ContractlessStandardResolverCore(); static readonly IFormatterResolver[] resolvers = StandardResolverHelper.DefaultResolvers.Concat(new IFormatterResolver[] { #if !ENABLE_IL2CPP && !UNITY_WSA && !NET_STANDARD_2_0 DynamicObjectResolver.Instance, // Try Object DynamicContractlessObjectResolver.Instance, // Serializes keys as strings #endif }).ToArray(); ContractlessStandardResolverCore() { } public IMessagePackFormatter GetFormatter() { return FormatterCache.formatter; } static class FormatterCache { public static readonly IMessagePackFormatter formatter; static FormatterCache() { foreach (var item in resolvers) { var f = item.GetFormatter(); if (f != null) { formatter = f; return; } } } } } internal sealed class StandardResolverAllowPrivateCore : IFormatterResolver { public static readonly IFormatterResolver Instance = new StandardResolverAllowPrivateCore(); static readonly IFormatterResolver[] resolvers = StandardResolverHelper.DefaultResolvers.Concat(new IFormatterResolver[] { #if !ENABLE_IL2CPP && !UNITY_WSA && !NET_STANDARD_2_0 DynamicObjectResolverAllowPrivate.Instance, // Try Object #endif }).ToArray(); StandardResolverAllowPrivateCore() { } public IMessagePackFormatter GetFormatter() { return FormatterCache.formatter; } static class FormatterCache { public static readonly IMessagePackFormatter formatter; static FormatterCache() { foreach (var item in resolvers) { var f = item.GetFormatter(); if (f != null) { formatter = f; return; } } } } } internal sealed class ContractlessStandardResolverAllowPrivateCore : IFormatterResolver { public static readonly IFormatterResolver Instance = new ContractlessStandardResolverAllowPrivateCore(); static readonly IFormatterResolver[] resolvers = StandardResolverHelper.DefaultResolvers.Concat(new IFormatterResolver[] { #if !ENABLE_IL2CPP && !UNITY_WSA && !NET_STANDARD_2_0 DynamicObjectResolverAllowPrivate.Instance, // Try Object DynamicContractlessObjectResolverAllowPrivate.Instance, // Serializes keys as strings #endif }).ToArray(); ContractlessStandardResolverAllowPrivateCore() { } public IMessagePackFormatter GetFormatter() { return FormatterCache.formatter; } static class FormatterCache { public static readonly IMessagePackFormatter formatter; static FormatterCache() { foreach (var item in resolvers) { var f = item.GetFormatter(); if (f != null) { formatter = f; return; } } } } } }