Registration: a concrete element type of an object list that is not keyed, and [MessagePackFormatter] on any type or member on the id path or on the id member, are refused. The id path is a fixed point over the walk's edges. The codec refuses, once per document type and on every encode and decode path, a resolver ahead of StandardResolver that formats a type on that path. UnionArmGuardTests moves StubArm out of the list union. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
47 lines
1.6 KiB
C#
47 lines
1.6 KiB
C#
#nullable enable
|
|
using GameCult.Caching.MessagePack;
|
|
using GameCult.Caching.Tests;
|
|
using MessagePack;
|
|
using MessagePack.Formatters;
|
|
|
|
[assembly: CultCacheFormatterResolver(typeof(PairResolver))]
|
|
[assembly: CultCacheFormatterResolver(typeof(UnionArmGuardTests.StubArmResolver))]
|
|
[assembly: CultCacheFormatterResolver(typeof(UnionArmGuardTests.ClaimArmResolver))]
|
|
[assembly: CultCacheFormatterResolver(typeof(UnionArmGuardTests.OrderResolver))]
|
|
|
|
namespace GameCult.Caching.Tests
|
|
{
|
|
public struct Pair
|
|
{
|
|
public float A;
|
|
public float B;
|
|
}
|
|
|
|
public sealed class PairResolver : IFormatterResolver
|
|
{
|
|
public static readonly IFormatterResolver Instance = new PairResolver();
|
|
|
|
public IMessagePackFormatter<T>? GetFormatter<T>()
|
|
{
|
|
return typeof(T) == typeof(Pair) ? (IMessagePackFormatter<T>)(object)PairFormatter.Instance : null;
|
|
}
|
|
|
|
private sealed class PairFormatter : IMessagePackFormatter<Pair>
|
|
{
|
|
public static readonly PairFormatter Instance = new();
|
|
|
|
public void Serialize(ref MessagePackWriter writer, Pair value, MessagePackSerializerOptions options)
|
|
{
|
|
writer.WriteArrayHeader(2);
|
|
writer.Write(value.A);
|
|
writer.Write(value.B);
|
|
}
|
|
|
|
public Pair Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
|
|
{
|
|
if (reader.ReadArrayHeader() != 2) throw new MessagePackSerializationException("Pair is a two-element array.");
|
|
return new Pair { A = reader.ReadSingle(), B = reader.ReadSingle() };
|
|
}
|
|
}
|
|
}
|
|
}
|