Skip to content

Commit 6fcac4a

Browse files
committed
BonSerializer Tuple<,>
1 parent fc5cac2 commit 6fcac4a

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

BTDB/Serialization/BonSerializer.cs

+19
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,25 @@ public unsafe Serialize CreateSerializerForType(Type type)
258258
};
259259
}
260260

261+
if (type.SpecializationOf(typeof(Tuple<,>)) is { } tuple2)
262+
{
263+
var typeParams = tuple2.GetGenericArguments();
264+
var offsets = RawData.GetOffsets(typeParams[0], typeParams[1]);
265+
offsets = (offsets.Item1 + (uint)Unsafe.SizeOf<nint>(), offsets.Item2 + (uint)Unsafe.SizeOf<nint>());
266+
var serializer0 = CreateCachedSerializerForType(typeParams[0]);
267+
var serializer1 = CreateCachedSerializerForType(typeParams[1]);
268+
return (ref SerializerCtx ctx, ref byte value) =>
269+
{
270+
ref var builder = ref AsCtx(ref ctx).Builder;
271+
builder.StartClass("Tuple"u8);
272+
builder.WriteKey("Item1"u8);
273+
serializer0(ref ctx, ref RawData.Ref(Unsafe.As<byte, object>(ref value), offsets.Item1));
274+
builder.WriteKey("Item2"u8);
275+
serializer1(ref ctx, ref RawData.Ref(Unsafe.As<byte, object>(ref value), offsets.Item2));
276+
builder.FinishClass();
277+
};
278+
}
279+
261280
if (type.IsArray)
262281
{
263282
if (!type.IsSZArray) throw new InvalidOperationException("Only SZArray is supported");

BTDBTest/SerializationTests/BonSerializerTest.SerializeDeserializeAllSupportedTypes.approved.txt

+6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
"Item1": 1,
3232
"Item2": 2
3333
},
34+
"TupleLongString": {
35+
"__type__": "Tuple",
36+
"Item1": 123456,
37+
"Item2": "BB"
38+
},
3439
"Self": {
3540
"__type__": "BTDBTest.SerializationTests.AllSupportedTypes",
3641
"Str": null,
@@ -64,6 +69,7 @@
6469
"Item1": 0,
6570
"Item2": 0
6671
},
72+
"TupleLongString": null,
6773
"Self": null,
6874
"DoubleArray": null,
6975
"IntList": null,

BTDBTest/SerializationTests/BonSerializerTest.cs

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class AllSupportedTypes
3232
public (int A, long B) ValueTupleIntLong;
3333
public (long A, string B) ValueTupleLongString;
3434
public (uint A, uint B) ValueTupleUintUint;
35+
public Tuple<long, string> TupleLongString;
3536
public AllSupportedTypes? Self;
3637
public double[]? DoubleArray;
3738
public List<int>? IntList;
@@ -50,6 +51,7 @@ public void SerializeDeserializeAllSupportedTypes()
5051
Guid = Guid.Parse("9e251065-0873-49bc-8fd9-266cc9aa39d3"), Float16 = (Half)3.14, Float32 = 3.14f,
5152
Float64 = Math.PI, NullableFloat64 = Math.PI, ValueTupleIntLong = (42, 4242424242),
5253
ValueTupleLongString = (424242424242, "B"), ValueTupleUintUint = (1, 2),
54+
TupleLongString = new Tuple<long, string>(123456, "BB"),
5355
Self = new(), DoubleArray = [Math.E, Math.PI], IntList = [1, 20, 300], UShortSet = [666, 12345],
5456
LongIntDict = new() { { 1111, 2 }, { 3333, 4 } }
5557
};

0 commit comments

Comments
 (0)