using MessagePack.Formatters; 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 NETSTANDARD1_4 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 NETSTANDARD1_4 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 NETSTANDARD1_4 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 NETSTANDARD1_4 formatter = (IMessagePackFormatter)ObjectFallbackFormatter; #else formatter = PrimitiveObjectResolver.Instance.GetFormatter(); #endif } else { formatter = ContractlessStandardResolverCore.Instance.GetFormatter(); } } } } } namespace MessagePack.Internal { internal sealed class StandardResolverCore : IFormatterResolver { public static readonly IFormatterResolver Instance = new StandardResolverCore(); static readonly IFormatterResolver[] resolvers = new[] { BuiltinResolver.Instance, // Try Builtin AttributeFormatterResolver.Instance, // Try use [MessagePackFormatter] #if !NETSTANDARD1_4 MessagePack.Unity.UnityResolver.Instance, #endif #if !ENABLE_IL2CPP && !UNITY_METRO DynamicEnumResolver.Instance, // Try Enum DynamicGenericResolver.Instance, // Try Array, Tuple, Collection DynamicUnionResolver.Instance, // Try Union(Interface) DynamicObjectResolver.Instance, // Try Object #endif }; 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 = new[] { BuiltinResolver.Instance, // Try Builtin AttributeFormatterResolver.Instance, // Try use [MessagePackFormatter] #if !NETSTANDARD1_4 MessagePack.Unity.UnityResolver.Instance, #endif #if !ENABLE_IL2CPP && !UNITY_METRO DynamicEnumResolver.Instance, // Try Enum DynamicGenericResolver.Instance, // Try Array, Tuple, Collection DynamicUnionResolver.Instance, // Try Union(Interface) DynamicObjectResolver.Instance, // Try Object DynamicContractlessObjectResolver.Instance, // Serializes keys as strings #endif }; 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; } } } } } }