You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
195 lines
5.6 KiB
195 lines
5.6 KiB
using MessagePack.Formatters; |
|
using MessagePack.Internal; |
|
using MessagePack.Resolvers; |
|
|
|
namespace MessagePack.Resolvers |
|
{ |
|
/// <summary> |
|
/// Default composited resolver, builtin -> attribute -> dynamic enum -> dynamic generic -> dynamic union -> dynamic object -> primitive. |
|
/// </summary> |
|
public sealed class StandardResolver : IFormatterResolver |
|
{ |
|
public static readonly IFormatterResolver Instance = new StandardResolver(); |
|
|
|
#if NETSTANDARD1_4 |
|
public static readonly IMessagePackFormatter<object> ObjectFallbackFormatter = new DynamicObjectTypeFallbackFormatter(StandardResolverCore.Instance); |
|
#endif |
|
|
|
StandardResolver() |
|
{ |
|
} |
|
|
|
public IMessagePackFormatter<T> GetFormatter<T>() |
|
{ |
|
return FormatterCache<T>.formatter; |
|
} |
|
|
|
static class FormatterCache<T> |
|
{ |
|
public static readonly IMessagePackFormatter<T> formatter; |
|
|
|
static FormatterCache() |
|
{ |
|
if (typeof(T) == typeof(object)) |
|
{ |
|
// final fallback |
|
#if NETSTANDARD1_4 |
|
formatter = (IMessagePackFormatter<T>)ObjectFallbackFormatter; |
|
#else |
|
formatter = PrimitiveObjectResolver.Instance.GetFormatter<T>(); |
|
#endif |
|
} |
|
else |
|
{ |
|
formatter = StandardResolverCore.Instance.GetFormatter<T>(); |
|
} |
|
} |
|
} |
|
} |
|
|
|
public sealed class ContractlessStandardResolver : IFormatterResolver |
|
{ |
|
public static readonly IFormatterResolver Instance = new ContractlessStandardResolver(); |
|
|
|
#if NETSTANDARD1_4 |
|
public static readonly IMessagePackFormatter<object> ObjectFallbackFormatter = new DynamicObjectTypeFallbackFormatter(ContractlessStandardResolverCore.Instance); |
|
#endif |
|
|
|
ContractlessStandardResolver() |
|
{ |
|
} |
|
|
|
public IMessagePackFormatter<T> GetFormatter<T>() |
|
{ |
|
return FormatterCache<T>.formatter; |
|
} |
|
|
|
static class FormatterCache<T> |
|
{ |
|
public static readonly IMessagePackFormatter<T> formatter; |
|
|
|
static FormatterCache() |
|
{ |
|
if (typeof(T) == typeof(object)) |
|
{ |
|
// final fallback |
|
#if NETSTANDARD1_4 |
|
formatter = (IMessagePackFormatter<T>)ObjectFallbackFormatter; |
|
#else |
|
formatter = PrimitiveObjectResolver.Instance.GetFormatter<T>(); |
|
#endif |
|
} |
|
else |
|
{ |
|
formatter = ContractlessStandardResolverCore.Instance.GetFormatter<T>(); |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
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<T> GetFormatter<T>() |
|
{ |
|
return FormatterCache<T>.formatter; |
|
} |
|
|
|
static class FormatterCache<T> |
|
{ |
|
public static readonly IMessagePackFormatter<T> formatter; |
|
|
|
static FormatterCache() |
|
{ |
|
foreach (var item in resolvers) |
|
{ |
|
var f = item.GetFormatter<T>(); |
|
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<T> GetFormatter<T>() |
|
{ |
|
return FormatterCache<T>.formatter; |
|
} |
|
|
|
static class FormatterCache<T> |
|
{ |
|
public static readonly IMessagePackFormatter<T> formatter; |
|
|
|
static FormatterCache() |
|
{ |
|
foreach (var item in resolvers) |
|
{ |
|
var f = item.GetFormatter<T>(); |
|
if (f != null) |
|
{ |
|
formatter = f; |
|
return; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |