1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
#if NETSTANDARD || NETFRAMEWORK
using System;
using System.Buffers;
namespace MessagePack.Formatters
{
<# for(var i = 1; i <= 8; i++) {
Func<int, string> toT = x => "T" + ((x == 8) ? "Rest" : x.ToString());
Func<int, string> toItem = x => ((x == 8) ? "Rest" : "Item" + x);
var ts = string.Join(", ", Enumerable.Range(1, i).Select(x => toT(x)));
var t = "ValueTuple<" + ts + ">";
#>
public sealed class ValueTupleFormatter<<#= ts #>> : IMessagePackFormatter<<#= t #>><#= (t.Contains("TRest") ? " where TRest : struct" : "") #>
{
public void Serialize(IBufferWriter<byte> writer, <#= t #> value, IFormatterResolver formatterResolver)
{
MessagePackBinary.WriteArrayHeader(writer, <#= i #>);
<# for(var j = 1; j <= i; j++) { #>
formatterResolver.GetFormatterWithVerify<<#= toT(j) #>>().Serialize(writer, value.<#= toItem(j) #>, formatterResolver);
<# } #>
}
public <#= t #> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver)
{
if (MessagePackBinary.IsNil(byteSequence))
{
throw new InvalidOperationException("Data is Nil, ValueTuple can not be null.");
}
else
{
var count = MessagePackBinary.ReadArrayHeader(ref byteSequence);
if (count != <#= i #>) throw new InvalidOperationException("Invalid ValueTuple count");
<# for(var j = 1; j <= i; j++) { #>
var item<#= j #> = formatterResolver.GetFormatterWithVerify<<#= toT(j) #>>().Deserialize(ref byteSequence, formatterResolver);
<# } #>
return new ValueTuple<<#= ts #>>(<#= string.Join(", ", Enumerable.Range(1, i).Select(x => "item" + x)) #>);
}
}
}
<# } #>
}
#endif
|