// Copyright (c) All contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. #nullable enable #if !(MESSAGEPACK_FORCE_AOT || ENABLE_IL2CPP) #define DYNAMIC_GENERATION #endif using System.Linq; using MessagePack.Formatters; using MessagePack.Internal; using MessagePack.Resolvers; #pragma warning disable SA1402 // File may only contain a single type #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 DYNAMIC_GENERATION && !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 DYNAMIC_GENERATION 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 DYNAMIC_GENERATION && !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 DYNAMIC_GENERATION 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 DYNAMIC_GENERATION && !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 DYNAMIC_GENERATION 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 DYNAMIC_GENERATION && !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 DYNAMIC_GENERATION 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 //TODO::Unity_2018_3 이상버전만 사용하기 때문에 가렸음. 안가리면 빌드 오류나옴 //ImmutableCollection.ImmutableCollectionResolver.Instance, //CompositeResolver.Create(ExpandoObjectFormatter.Instance), #endif #if DYNAMIC_GENERATION DynamicGenericResolver.Instance, // Try Array, Tuple, Collection, Enum(Generic Fallback) #endif #if DYNAMIC_GENERATION && !NET_STANDARD_2_0 DynamicUnionResolver.Instance, // Try Union(Interface) #endif }; } } // Copyright (c) All contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information.