Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project>
<PropertyGroup>
<!-- Enable central package management, https://learn.microsoft.com/en-us/nuget/consume-packages/Central-Package-Management -->
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Serde" Version="[0.8.0-preview7]" />
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageVersion Include="xunit" Version="2.9.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" />
<PackageVersion Include="MessagePack" Version="3.1.0" />
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions bench/bench.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
<PackageReference Include="MessagePack" Version="3.1.0" />
<PackageReference Include="BenchmarkDotNet" />
<PackageReference Include="MessagePack" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Serde.MsgPack.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serde" Version="[0.8.0-preview5]" />
<PackageReference Include="Serde" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/reader/ArrayBufReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public void Advance(int count)

public bool FillBuffer(int fillCount)
{
return false;
return _offset + fillCount <= _buffer.Length;
}
}
18 changes: 17 additions & 1 deletion src/reader/MsgPackReader.Collections.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@

using System.Buffers;

namespace Serde.MsgPack;

partial class MsgPackReader<TReader>
{
private struct DeserializeCollection(MsgPackReader<TReader> deserializer, bool isDict, int length) : ITypeDeserializer
{
private int _index;
int? ITypeDeserializer.SizeOpt => isDict switch {
int? ITypeDeserializer.SizeOpt => isDict switch
{
true => length / 2,
false => length,
};
Expand Down Expand Up @@ -130,5 +133,18 @@ void ITypeDeserializer.SkipValue(ISerdeInfo info, int index)
{
throw new NotImplementedException();
}

DateTimeOffset ITypeDeserializer.ReadDateTimeOffset(ISerdeInfo info, int index)
{
var next = deserializer.ReadDateTimeOffset();
_index++;
return next;
}

void ITypeDeserializer.ReadBytes(ISerdeInfo info, int index, IBufferWriter<byte> writer)
{
deserializer.ReadBytes(writer);
_index++;
}
}
}
10 changes: 10 additions & 0 deletions src/reader/MsgPackReader.ITypeDeserializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

using System.Buffers;

namespace Serde.MsgPack;

partial class MsgPackReader<TReader>
Expand Down Expand Up @@ -71,5 +73,13 @@ int ITypeDeserializer.TryReadIndex(ISerdeInfo map, out string? errorName)
return ITypeDeserializer.IndexNotFound;
}
}

DateTimeOffset ITypeDeserializer.ReadDateTimeOffset(ISerdeInfo info, int index)
=> deserializer.ReadDateTimeOffset();

void ITypeDeserializer.ReadBytes(ISerdeInfo info, int index, IBufferWriter<byte> writer)
{
deserializer.ReadBytes(writer);
}
}
}
28 changes: 28 additions & 0 deletions src/reader/MsgPackReader.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@


using System.Buffers;
using System.Buffers.Binary;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;
using Serde.IO;
Expand Down Expand Up @@ -349,6 +351,32 @@ private string ReadString()
return str;
}

public DateTimeOffset ReadDateTimeOffset()
{
return DateTime.Parse(ReadString(), styles: DateTimeStyles.RoundtripKind);
}

public void ReadBytes(IBufferWriter<byte> writer)
{
var b = EatByteOrThrow();
int length = b switch
{
0xc4 => EatByteOrThrow(),
0xc5 => ReadBigEndianU16(), // 16-bit length
0xc6 => checked((int)ReadBigEndianU32()), // 32-bit length
_ => throw new DeserializeException($"Expected bytes, got 0x{b:x}"),
};
if (!_reader.FillBuffer(length))
{
ThrowEof();
}
var inputSpan = _reader.Span[..length];
var outSpan = writer.GetSpan(length);
inputSpan.CopyTo(outSpan);
_reader.Advance(length);
writer.Advance(length);
}

private ReadOnlySpan<byte> ReadUtf8Span()
{
var b = EatByteOrThrow();
Expand Down
4 changes: 4 additions & 0 deletions src/writer/MsgPackWriter.EnumSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,9 @@ public void WriteValue<T>(ISerdeInfo typeInfo, int index, T value, ISerialize<T>
public void WriteU16(ISerdeInfo typeInfo, int index, ushort u16) => writer.WriteU16(u16);
public void WriteU32(ISerdeInfo typeInfo, int index, uint u32) => writer.WriteU32(u32);
public void WriteU64(ISerdeInfo typeInfo, int index, ulong u64) => writer.WriteU64(u64);
public void WriteDateTimeOffset(ISerdeInfo typeInfo, int index, DateTimeOffset dateTimeOffset)
=> writer.WriteDateTimeOffset(dateTimeOffset);
public void WriteBytes(ISerdeInfo typeInfo, int index, ReadOnlyMemory<byte> bytes)
=> writer.WriteBytes(bytes);
}
}
6 changes: 6 additions & 0 deletions src/writer/MsgPackWriter.ISerializeCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,11 @@ public void WriteU8(ISerdeInfo typeInfo, int index, byte b)
public void WriteValue<T>(ISerdeInfo typeInfo, int index, T value, ISerialize<T> serialize)
where T : class?
=> serialize.Serialize(value, writer);

public void WriteDateTimeOffset(ISerdeInfo typeInfo, int index, DateTimeOffset dateTimeOffset)
=> writer.WriteDateTimeOffset(dateTimeOffset);

public void WriteBytes(ISerdeInfo typeInfo, int index, ReadOnlyMemory<byte> bytes)
=> writer.WriteBytes(bytes);
}
}
12 changes: 12 additions & 0 deletions src/writer/MsgPackWriter.ISerializeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,16 @@ void ITypeSerializer.WriteNull(ISerdeInfo typeInfo, int index)
WritePropertyName(typeInfo, index);
WriteNull();
}

void ITypeSerializer.WriteDateTimeOffset(ISerdeInfo typeInfo, int index, DateTimeOffset dt)
{
WritePropertyName(typeInfo, index);
WriteDateTimeOffset(dt);
}

void ITypeSerializer.WriteBytes(ISerdeInfo typeInfo, int index, ReadOnlyMemory<byte> bytes)
{
WritePropertyName(typeInfo, index);
WriteBytes(bytes);
}
}
37 changes: 37 additions & 0 deletions src/writer/MsgPackWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,43 @@ public void WriteNull()
_out.Add(0xc0);
}

public void WriteDateTimeOffset(DateTimeOffset dt)
{
WriteString(dt.ToString("O"));
}

public void WriteBytes(ReadOnlyMemory<byte> bytes)
{
byte code = bytes.Length switch
{
<= 0xff => 0xc4,
<= 0xffff => 0xc5,
_ => 0xc6
};
var prefixLen = code switch
{
0xc4 => 2,
0xc5 => 3,
_ => 5
};
var span = _out.GetAppendSpan(prefixLen + bytes.Length);
_out.Count += prefixLen + bytes.Length;
span[0] = code;
switch (prefixLen)
{
case 2:
span[1] = (byte)bytes.Length;
break;
case 3:
WriteBigEndian((ushort)bytes.Length);
break;
case 5:
WriteBigEndian((uint)bytes.Length);
break;
}
bytes.Span.CopyTo(span[prefixLen..]);
}

public void WriteI8(sbyte b) => WriteI64(b);

private static readonly Encoding _utf8 = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
Expand Down
7 changes: 7 additions & 0 deletions tests/RoundTripTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ public void TestFloat()
AssertRoundTrip(float.PositiveInfinity, F32Proxy.Instance);
}

[Fact]
public void TestBytes()
{
var bytes = new byte[] { 1, 2, 3, 4, 5 };
AssertRoundTrip(bytes, ByteArrayProxy.Instance);
}

private static void AssertRoundTrip<T>(T expected)
where T : ISerializeProvider<T>, IDeserializeProvider<T>, IEquatable<T>
{
Expand Down
12 changes: 6 additions & 6 deletions tests/Serde.MsgPack.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="MessagePack" Version="2.5.192" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Serde" Version="[0.8.0-preview5]" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageReference Include="coverlet.collector" />
<PackageReference Include="MessagePack" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Serde" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
</ItemGroup>

<ItemGroup>
Expand Down