Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NerdbankMessagePackFormatter #1100

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e934175
Update package versions and target frameworks
Dec 18, 2024
6819e6b
Continue implementing NerdbankMessagePackFormatter.
Dec 20, 2024
052592e
Refactor NerdbankMessagePackFormatter for converters
Dec 20, 2024
259558a
Add type converter registration methods to interface
Dec 21, 2024
e87da44
Enhance serialization context and type shape providers
Dec 21, 2024
ca439ea
Enhance NerdbankMessagePackFormatter with new methods
Dec 21, 2024
b12c357
Replaced the `IFormatterContextBuilder` interface with a concrete `Fo…
Dec 21, 2024
345a39c
Downgrade package versions for compatibility
Dec 21, 2024
ad436f7
User RawMessagePack from Nerdbank.MessagePack
Dec 21, 2024
1a795c7
Remove namespace aliases
Dec 21, 2024
0e6b4aa
Add unit tests
Dec 21, 2024
bca2af4
Some tests are now passing.
Dec 22, 2024
d693a1c
Cleaning up formatters. Rename `FormatterContext` to `FormatterProfile`.
Dec 22, 2024
0c06ae5
Adopt MessagePackString and configure test formatters.
Dec 24, 2024
166ae49
Adopt simpler MessagePackString write API
Dec 26, 2024
3d3cb90
Fix bug when searching property names from peek reader
Dec 26, 2024
d8430f4
Begin process of making converters stateless
Dec 27, 2024
6444f55
Remove state from converters.
Dec 27, 2024
bb22ee8
Refactor and add comments for Profile and Profile.Builder.
Dec 27, 2024
e171cdb
Add additional profile configuration to test suite.
Dec 28, 2024
d36d607
Update Benchmarks
Dec 28, 2024
949b3ab
Upgrade NB.MB version; Update tests.
Dec 29, 2024
f94bf1d
Refactor serialization to use ReadOnlySequence<byte>
Jan 1, 2025
eecd92c
Enhance NerdbankMessagePackFormatter with new types
Jan 1, 2025
edad2f8
Update Nerdbank.MessagePack and refactor converters
Jan 11, 2025
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
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<PackageVersion Include="Microsoft.VisualStudio.Threading.Analyzers" Version="$(VisualStudioThreadingVersion)" />
<PackageVersion Include="Microsoft.VisualStudio.Threading" Version="$(VisualStudioThreadingVersion)" />
<PackageVersion Include="Microsoft.VisualStudio.Validation" Version="17.8.8" />
<PackageVersion Include="Nerdbank.MessagePack" Version="0.3.84-beta" />
<PackageVersion Include="Nerdbank.MessagePack" Version="0.3.98-beta" />
<PackageVersion Include="Nerdbank.Streams" Version="2.11.74" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.1" />
<PackageVersion Include="System.Collections.Immutable" Version="8.0.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Nodes;
using Nerdbank.MessagePack;
using Nerdbank.Streams;
using PolyType.Abstractions;
using StreamJsonRpc.Reflection;

namespace StreamJsonRpc;

/// <summary>
/// Serializes JSON-RPC messages using MessagePack (a fast, compact binary format).
/// </summary>
public partial class NerdbankMessagePackFormatter
{
private static class AsyncEnumerableConverters
{
/// <summary>
/// Converts an enumeration token to an <see cref="IAsyncEnumerable{T}"/>
/// or an <see cref="IAsyncEnumerable{T}"/> into an enumeration token.
/// </summary>
#pragma warning disable CA1812
internal class PreciseTypeConverter<T> : MessagePackConverter<IAsyncEnumerable<T>>
#pragma warning restore CA1812
{
/// <summary>
/// The constant "token", in its various forms.
/// </summary>
private static readonly MessagePackString TokenPropertyName = new(MessageFormatterEnumerableTracker.TokenPropertyName);

/// <summary>
/// The constant "values", in its various forms.
/// </summary>
private static readonly MessagePackString ValuesPropertyName = new(MessageFormatterEnumerableTracker.ValuesPropertyName);

public override IAsyncEnumerable<T>? Read(ref MessagePackReader reader, SerializationContext context)
{
if (reader.TryReadNil())
{
return default;
}

NerdbankMessagePackFormatter mainFormatter = context.GetFormatter();

context.DepthStep();

RawMessagePack? token = default;
IReadOnlyList<T>? initialElements = null;
int propertyCount = reader.ReadMapHeader();
for (int i = 0; i < propertyCount; i++)
{
if (TokenPropertyName.TryRead(ref reader))
{
// The value needs to outlive the reader, so we clone it.
token = new RawMessagePack(reader.ReadRaw(context)).ToOwned();
}
else if (ValuesPropertyName.TryRead(ref reader))
{
initialElements = context.GetConverter<IReadOnlyList<T>>(context.TypeShapeProvider).Read(ref reader, context);
}
else
{
reader.Skip(context);
}
}

return mainFormatter.EnumerableTracker.CreateEnumerableProxy(token.HasValue ? token.Value : null, initialElements);
}

[SuppressMessage("Usage", "NBMsgPack031:Converters should read or write exactly one msgpack structure", Justification = "Writer is passed to helper method")]
public override void Write(ref MessagePackWriter writer, in IAsyncEnumerable<T>? value, SerializationContext context)
{
context.DepthStep();

NerdbankMessagePackFormatter mainFormatter = context.GetFormatter();
Serialize_Shared(mainFormatter, ref writer, value, context);
}

public override JsonObject? GetJsonSchema(JsonSchemaContext context, ITypeShape typeShape)
{
return CreateUndocumentedSchema(typeof(PreciseTypeConverter<T>));
}

internal static void Serialize_Shared(NerdbankMessagePackFormatter mainFormatter, ref MessagePackWriter writer, IAsyncEnumerable<T>? value, SerializationContext context)
{
if (value is null)
{
writer.WriteNil();
}
else
{
(IReadOnlyList<T> elements, bool finished) = value.TearOffPrefetchedElements();
long token = mainFormatter.EnumerableTracker.GetToken(value);

int propertyCount = 0;
if (elements.Count > 0)
{
propertyCount++;
}

if (!finished)
{
propertyCount++;
}

writer.WriteMapHeader(propertyCount);

if (!finished)
{
writer.Write(TokenPropertyName);
writer.Write(token);
}

if (elements.Count > 0)
{
writer.Write(ValuesPropertyName);
context.GetConverter<IReadOnlyList<T>>(context.TypeShapeProvider).Write(ref writer, elements, context);
}
}
}
}

/// <summary>
/// Converts an instance of <see cref="IAsyncEnumerable{T}"/> to an enumeration token.
/// </summary>
#pragma warning disable CA1812
internal class GeneratorConverter<TClass, TElement> : MessagePackConverter<TClass>
where TClass : IAsyncEnumerable<TElement>
#pragma warning restore CA1812
{
public override TClass Read(ref MessagePackReader reader, SerializationContext context)
{
throw new NotSupportedException();
}

[SuppressMessage("Usage", "NBMsgPack031:Converters should read or write exactly one msgpack structure", Justification = "Writer is passed to helper method")]
public override void Write(ref MessagePackWriter writer, in TClass? value, SerializationContext context)
{
NerdbankMessagePackFormatter mainFormatter = context.GetFormatter();

context.DepthStep();
PreciseTypeConverter<TElement>.Serialize_Shared(mainFormatter, ref writer, value, context);
}

public override JsonObject? GetJsonSchema(JsonSchemaContext context, ITypeShape typeShape)
{
return CreateUndocumentedSchema(typeof(GeneratorConverter<TClass, TElement>));
}
}
}
}
2 changes: 0 additions & 2 deletions src/StreamJsonRpc/NerdbankMessagePackFormatter.Constants.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Diagnostics;
using Nerdbank.MessagePack;
using StreamJsonRpc.Protocol;
using NBMP = Nerdbank.MessagePack;

namespace StreamJsonRpc;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Nodes;
using Nerdbank.MessagePack;
using PolyType.Abstractions;
using StreamJsonRpc.Reflection;

namespace StreamJsonRpc;

/// <summary>
/// Serializes JSON-RPC messages using MessagePack (a fast, compact binary format).
/// </summary>
public partial class NerdbankMessagePackFormatter
{
private class EnumeratorResultsConverter<T> : MessagePackConverter<MessageFormatterEnumerableTracker.EnumeratorResults<T>>
{
[SuppressMessage("Usage", "NBMsgPack031:Converters should read or write exactly one msgpack structure", Justification = "Reader is passed to user data context")]
public override MessageFormatterEnumerableTracker.EnumeratorResults<T>? Read(ref MessagePackReader reader, SerializationContext context)
{
if (reader.TryReadNil())
{
return default;
}

NerdbankMessagePackFormatter formatter = context.GetFormatter();
context.DepthStep();

Verify.Operation(reader.ReadArrayHeader() == 2, "Expected array of length 2.");
return new MessageFormatterEnumerableTracker.EnumeratorResults<T>()
{
Values = formatter.userDataProfile.Deserialize<IReadOnlyList<T>>(ref reader, context.CancellationToken),
Finished = formatter.userDataProfile.Deserialize<bool>(ref reader, context.CancellationToken),
};
}

[SuppressMessage("Usage", "NBMsgPack031:Converters should read or write exactly one msgpack structure", Justification = "Writer is passed to user data context")]
public override void Write(ref MessagePackWriter writer, in MessageFormatterEnumerableTracker.EnumeratorResults<T>? value, SerializationContext context)
{
if (value is null)
{
writer.WriteNil();
}
else
{
NerdbankMessagePackFormatter formatter = context.GetFormatter();
context.DepthStep();

writer.WriteArrayHeader(2);
formatter.userDataProfile.Serialize(ref writer, value.Values, context.CancellationToken);
formatter.userDataProfile.Serialize(ref writer, value.Finished, context.CancellationToken);
}
}

public override JsonObject? GetJsonSchema(JsonSchemaContext context, ITypeShape typeShape)
{
return CreateUndocumentedSchema(typeof(EnumeratorResultsConverter<T>));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Text.Json.Nodes;
using Nerdbank.MessagePack;
using PolyType.Abstractions;

namespace StreamJsonRpc;

/// <summary>
/// Serializes JSON-RPC messages using MessagePack (a fast, compact binary format).
/// </summary>
public partial class NerdbankMessagePackFormatter
{
/// <summary>
/// Enables formatting the default/empty <see cref="EventArgs"/> class.
/// </summary>
private class EventArgsConverter : MessagePackConverter<EventArgs>
{
internal static readonly EventArgsConverter Instance = new();

private EventArgsConverter()
{
}

/// <inheritdoc/>
public override void Write(ref MessagePackWriter writer, in EventArgs? value, SerializationContext context)
{
Requires.NotNull(value!, nameof(value));
context.DepthStep();
writer.WriteMapHeader(0);
}

/// <inheritdoc/>
public override EventArgs Read(ref MessagePackReader reader, SerializationContext context)
{
context.DepthStep();
reader.Skip(context);
return EventArgs.Empty;
}

public override JsonObject? GetJsonSchema(JsonSchemaContext context, ITypeShape typeShape)
{
return CreateUndocumentedSchema(typeof(EventArgsConverter));
}
}
}
Loading