Skip to content

Commit 2841ca4

Browse files
authored
Adopt Serde preview7 (#6)
1 parent 005f0f6 commit 2841ca4

13 files changed

+146
-11
lines changed

Directory.Packages.props

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project>
2+
<PropertyGroup>
3+
<!-- Enable central package management, https://learn.microsoft.com/en-us/nuget/consume-packages/Central-Package-Management -->
4+
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageVersion Include="Serde" Version="[0.8.0-preview7]" />
8+
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
9+
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
10+
<PackageVersion Include="xunit" Version="2.9.0" />
11+
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
12+
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" />
13+
<PackageVersion Include="MessagePack" Version="3.1.0" />
14+
</ItemGroup>
15+
</Project>

bench/bench.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
13-
<PackageReference Include="MessagePack" Version="3.1.0" />
12+
<PackageReference Include="BenchmarkDotNet" />
13+
<PackageReference Include="MessagePack" />
1414
</ItemGroup>
1515

1616
<ItemGroup>

src/Serde.MsgPack.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Serde" Version="[0.8.0-preview5]" />
11+
<PackageReference Include="Serde" />
1212
</ItemGroup>
1313

1414
</Project>

src/reader/ArrayBufReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ public void Advance(int count)
1515

1616
public bool FillBuffer(int fillCount)
1717
{
18-
return false;
18+
return _offset + fillCount <= _buffer.Length;
1919
}
2020
}

src/reader/MsgPackReader.Collections.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11

2+
using System.Buffers;
3+
24
namespace Serde.MsgPack;
35

46
partial class MsgPackReader<TReader>
57
{
68
private struct DeserializeCollection(MsgPackReader<TReader> deserializer, bool isDict, int length) : ITypeDeserializer
79
{
810
private int _index;
9-
int? ITypeDeserializer.SizeOpt => isDict switch {
11+
int? ITypeDeserializer.SizeOpt => isDict switch
12+
{
1013
true => length / 2,
1114
false => length,
1215
};
@@ -130,5 +133,18 @@ void ITypeDeserializer.SkipValue(ISerdeInfo info, int index)
130133
{
131134
throw new NotImplementedException();
132135
}
136+
137+
DateTimeOffset ITypeDeserializer.ReadDateTimeOffset(ISerdeInfo info, int index)
138+
{
139+
var next = deserializer.ReadDateTimeOffset();
140+
_index++;
141+
return next;
142+
}
143+
144+
void ITypeDeserializer.ReadBytes(ISerdeInfo info, int index, IBufferWriter<byte> writer)
145+
{
146+
deserializer.ReadBytes(writer);
147+
_index++;
148+
}
133149
}
134150
}

src/reader/MsgPackReader.ITypeDeserializer.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
using System.Buffers;
3+
24
namespace Serde.MsgPack;
35

46
partial class MsgPackReader<TReader>
@@ -71,5 +73,13 @@ int ITypeDeserializer.TryReadIndex(ISerdeInfo map, out string? errorName)
7173
return ITypeDeserializer.IndexNotFound;
7274
}
7375
}
76+
77+
DateTimeOffset ITypeDeserializer.ReadDateTimeOffset(ISerdeInfo info, int index)
78+
=> deserializer.ReadDateTimeOffset();
79+
80+
void ITypeDeserializer.ReadBytes(ISerdeInfo info, int index, IBufferWriter<byte> writer)
81+
{
82+
deserializer.ReadBytes(writer);
83+
}
7484
}
7585
}

src/reader/MsgPackReader.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11

22

3+
using System.Buffers;
34
using System.Buffers.Binary;
45
using System.Diagnostics;
56
using System.Diagnostics.CodeAnalysis;
7+
using System.Globalization;
68
using System.Runtime.CompilerServices;
79
using System.Text;
810
using Serde.IO;
@@ -349,6 +351,32 @@ private string ReadString()
349351
return str;
350352
}
351353

354+
public DateTimeOffset ReadDateTimeOffset()
355+
{
356+
return DateTime.Parse(ReadString(), styles: DateTimeStyles.RoundtripKind);
357+
}
358+
359+
public void ReadBytes(IBufferWriter<byte> writer)
360+
{
361+
var b = EatByteOrThrow();
362+
int length = b switch
363+
{
364+
0xc4 => EatByteOrThrow(),
365+
0xc5 => ReadBigEndianU16(), // 16-bit length
366+
0xc6 => checked((int)ReadBigEndianU32()), // 32-bit length
367+
_ => throw new DeserializeException($"Expected bytes, got 0x{b:x}"),
368+
};
369+
if (!_reader.FillBuffer(length))
370+
{
371+
ThrowEof();
372+
}
373+
var inputSpan = _reader.Span[..length];
374+
var outSpan = writer.GetSpan(length);
375+
inputSpan.CopyTo(outSpan);
376+
_reader.Advance(length);
377+
writer.Advance(length);
378+
}
379+
352380
private ReadOnlySpan<byte> ReadUtf8Span()
353381
{
354382
var b = EatByteOrThrow();

src/writer/MsgPackWriter.EnumSerializer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,9 @@ public void WriteValue<T>(ISerdeInfo typeInfo, int index, T value, ISerialize<T>
3535
public void WriteU16(ISerdeInfo typeInfo, int index, ushort u16) => writer.WriteU16(u16);
3636
public void WriteU32(ISerdeInfo typeInfo, int index, uint u32) => writer.WriteU32(u32);
3737
public void WriteU64(ISerdeInfo typeInfo, int index, ulong u64) => writer.WriteU64(u64);
38+
public void WriteDateTimeOffset(ISerdeInfo typeInfo, int index, DateTimeOffset dateTimeOffset)
39+
=> writer.WriteDateTimeOffset(dateTimeOffset);
40+
public void WriteBytes(ISerdeInfo typeInfo, int index, ReadOnlyMemory<byte> bytes)
41+
=> writer.WriteBytes(bytes);
3842
}
3943
}

src/writer/MsgPackWriter.ISerializeCollection.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,11 @@ public void WriteU8(ISerdeInfo typeInfo, int index, byte b)
5858
public void WriteValue<T>(ISerdeInfo typeInfo, int index, T value, ISerialize<T> serialize)
5959
where T : class?
6060
=> serialize.Serialize(value, writer);
61+
62+
public void WriteDateTimeOffset(ISerdeInfo typeInfo, int index, DateTimeOffset dateTimeOffset)
63+
=> writer.WriteDateTimeOffset(dateTimeOffset);
64+
65+
public void WriteBytes(ISerdeInfo typeInfo, int index, ReadOnlyMemory<byte> bytes)
66+
=> writer.WriteBytes(bytes);
6167
}
6268
}

src/writer/MsgPackWriter.ISerializeType.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,16 @@ void ITypeSerializer.WriteNull(ISerdeInfo typeInfo, int index)
111111
WritePropertyName(typeInfo, index);
112112
WriteNull();
113113
}
114+
115+
void ITypeSerializer.WriteDateTimeOffset(ISerdeInfo typeInfo, int index, DateTimeOffset dt)
116+
{
117+
WritePropertyName(typeInfo, index);
118+
WriteDateTimeOffset(dt);
119+
}
120+
121+
void ITypeSerializer.WriteBytes(ISerdeInfo typeInfo, int index, ReadOnlyMemory<byte> bytes)
122+
{
123+
WritePropertyName(typeInfo, index);
124+
WriteBytes(bytes);
125+
}
114126
}

0 commit comments

Comments
 (0)