// 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.Linq; using MessagePack.Formatters; using MessagePack.Internal; using MessagePack.Resolvers; #pragma warning disable SA1403 // File may only contain a single namespace namespace MessagePack.Resolvers { /// /// Default composited resolver, builtin -> attribute -> dynamic enum -> dynamic generic -> dynamic union -> dynamic object -> primitive. /// public sealed class StandardResolver : IFormatterResolver { /// /// The singleton instance that can be used. /// public static readonly StandardResolver Instance; /// /// A instance with this formatter pre-configured. /// public static readonly MessagePackSerializerOptions Options; private static readonly IFormatterResolver[] Resolvers = StandardResolverHelper.DefaultResolvers.Concat(new IFormatterResolver[] { #if !ENABLE_IL2CPP && !NET_STANDARD_2_0 DynamicObjectResolver.Instance, // Try Object #endif }).ToArray(); static StandardResolver() { Instance = new StandardResolver(); Options = new MessagePackSerializerOptions(Instance); } private StandardResolver() { } public IMessagePackFormatter GetFormatter() { return FormatterCache.Formatter; } private static class FormatterCache { public static readonly IMessagePackFormatter Formatter; static FormatterCache() { if (typeof(T) == typeof(object)) { // final fallback #if !ENABLE_IL2CPP Formatter = (IMessagePackFormatter)DynamicObjectTypeFallbackFormatter.Instance; #else Formatter = PrimitiveObjectResolver.Instance.GetFormatter(); #endif } else { foreach (IFormatterResolver item in Resolvers) { IMessagePackFormatter f = item.GetFormatter(); if (f != null) { Formatter = f; return; } } } } } } public sealed class ContractlessStandardResolver : IFormatterResolver { /// /// The singleton instance that can be used. /// public static readonly ContractlessStandardResolver Instance; /// /// A instance with this formatter pre-configured. /// public static readonly MessagePackSerializerOptions Options; private static readonly IFormatterResolver[] Resolvers = StandardResolverHelper.DefaultResolvers.Concat(new IFormatterResolver[] { #if !ENABLE_IL2CPP && !NET_STANDARD_2_0 DynamicObjectResolver.Instance, // Try Object DynamicContractlessObjectResolver.Instance, // Serializes keys as strings #endif }).ToArray(); static ContractlessStandardResolver() { Instance = new ContractlessStandardResolver(); Options = new MessagePackSerializerOptions(Instance); } private ContractlessStandardResolver() { } public IMessagePackFormatter GetFormatter() { return FormatterCache.Formatter; } private static class FormatterCache { public static readonly IMessagePackFormatter Formatter; static FormatterCache() { if (typeof(T) == typeof(object)) { // final fallback #if !ENABLE_IL2CPP Formatter = (IMessagePackFormatter)DynamicObjectTypeFallbackFormatter.Instance; #else Formatter = PrimitiveObjectResolver.Instance.GetFormatter(); #endif } else { foreach (IFormatterResolver item in Resolvers) { IMessagePackFormatter f = item.GetFormatter(); if (f != null) { Formatter = f; return; } } } } } } public sealed class StandardResolverAllowPrivate : IFormatterResolver { /// /// The singleton instance that can be used. /// public static readonly StandardResolverAllowPrivate Instance; /// /// A instance with this formatter pre-configured. /// public static readonly MessagePackSerializerOptions Options; private static readonly IFormatterResolver[] Resolvers = StandardResolverHelper.DefaultResolvers.Concat(new IFormatterResolver[] { #if !ENABLE_IL2CPP && !NET_STANDARD_2_0 DynamicObjectResolverAllowPrivate.Instance, // Try Object #endif }).ToArray(); static StandardResolverAllowPrivate() { Instance = new StandardResolverAllowPrivate(); Options = new MessagePackSerializerOptions(Instance); } private StandardResolverAllowPrivate() { } public IMessagePackFormatter GetFormatter() { return FormatterCache.Formatter; } private static class FormatterCache { public static readonly IMessagePackFormatter Formatter; static FormatterCache() { if (typeof(T) == typeof(object)) { // final fallback #if !ENABLE_IL2CPP Formatter = (IMessagePackFormatter)DynamicObjectTypeFallbackFormatter.Instance; #else Formatter = PrimitiveObjectResolver.Instance.GetFormatter(); #endif } else { foreach (IFormatterResolver item in Resolvers) { IMessagePackFormatter f = item.GetFormatter(); if (f != null) { Formatter = f; return; } } } } } } public sealed class ContractlessStandardResolverAllowPrivate : IFormatterResolver { /// /// The singleton instance that can be used. /// public static readonly ContractlessStandardResolverAllowPrivate Instance; /// /// A instance with this formatter pre-configured. /// public static readonly MessagePackSerializerOptions Options; private static readonly IFormatterResolver[] Resolvers = StandardResolverHelper.DefaultResolvers.Concat(new IFormatterResolver[] { #if !ENABLE_IL2CPP && !NET_STANDARD_2_0 DynamicObjectResolverAllowPrivate.Instance, // Try Object DynamicContractlessObjectResolverAllowPrivate.Instance, // Serializes keys as strings #endif }).ToArray(); static ContractlessStandardResolverAllowPrivate() { Instance = new ContractlessStandardResolverAllowPrivate(); Options = new MessagePackSerializerOptions(Instance); } private ContractlessStandardResolverAllowPrivate() { } public IMessagePackFormatter GetFormatter() { return FormatterCache.Formatter; } private static class FormatterCache { public static readonly IMessagePackFormatter Formatter; static FormatterCache() { if (typeof(T) == typeof(object)) { // final fallback #if !ENABLE_IL2CPP Formatter = (IMessagePackFormatter)DynamicObjectTypeFallbackFormatter.Instance; #else Formatter = PrimitiveObjectResolver.Instance.GetFormatter(); #endif } else { foreach (IFormatterResolver item in Resolvers) { IMessagePackFormatter f = item.GetFormatter(); if (f != null) { Formatter = f; return; } } } } } } } namespace MessagePack.Internal { internal static class StandardResolverHelper { public static readonly IFormatterResolver[] DefaultResolvers = new IFormatterResolver[] { BuiltinResolver.Instance, // Try Builtin AttributeFormatterResolver.Instance, // Try use [MessagePackFormatter] #if UNITY_2018_3_OR_NEWER MessagePack.Unity.UnityResolver.Instance, #else ImmutableCollection.ImmutableCollectionResolver.Instance, CompositeResolver.Create(ExpandoObjectFormatter.Instance), #endif #if !ENABLE_IL2CPP DynamicGenericResolver.Instance, // Try Array, Tuple, Collection, Enum(Generic Fallback) #endif #if !ENABLE_IL2CPP && !NET_STANDARD_2_0 DynamicUnionResolver.Instance, // Try Union(Interface) #endif }; } }