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 all commits
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
5 changes: 3 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
<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.98-beta" />
<PackageVersion Include="Nerdbank.Streams" Version="2.11.74" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.1" />
<PackageVersion Include="System.Collections.Immutable" Version="6.0.0" />
<PackageVersion Include="System.Collections.Immutable" Version="8.0.0" />
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="6.0.1" />
<PackageVersion Include="System.IO.Pipelines" Version="8.0.0" />
<PackageVersion Include="System.IO.Pipelines" Version="9.0.0" />
<PackageVersion Include="System.IO.Pipes" Version="4.3.0" />
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
Expand Down
16 changes: 16 additions & 0 deletions nuget.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,26 @@
</config>
<packageSources>
<clear />
<add key="PublicCI" value="https://pkgs.dev.azure.com/andrewarnott/OSS/_packaging/PublicCI/nuget/v3/index.json" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="msft_consumption" value="https://pkgs.dev.azure.com/azure-public/vside/_packaging/msft_consumption/nuget/v3/index.json" />
</packageSources>
<disabledPackageSources>
<!-- Defend against user or machine level disabling of sources that we list in this file. -->
<clear />
</disabledPackageSources>
<packageSourceMapping>
<!-- key value for <packageSource> should match key values from <packageSources> element -->
<packageSource key="msft_consumption">
<package pattern="*" />
</packageSource>
<packageSource key="nuget.org">
<package pattern="System.*" />
<package pattern="Microsoft.Bcl.*" />
<package pattern="PolyType" />
</packageSource>
<packageSource key="PublicCI">
<package pattern="Nerdbank.MessagePack" />
</packageSource>
</packageSourceMapping>
</configuration>
6 changes: 5 additions & 1 deletion src/StreamJsonRpc/FormatterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using System.IO.Pipelines;
using System.Reflection;
using System.Runtime.Serialization;
using Nerdbank.MessagePack;
using Nerdbank.Streams;
using PolyType;
using StreamJsonRpc.Protocol;
using StreamJsonRpc.Reflection;

Expand Down Expand Up @@ -320,7 +322,7 @@ public void Dispose()
/// <summary>
/// A base class for top-level property bags that should be declared in the derived formatter class.
/// </summary>
protected abstract class TopLevelPropertyBagBase
protected internal abstract class TopLevelPropertyBagBase
{
private readonly bool isOutbound;
private Dictionary<string, (Type, object?)>? outboundProperties;
Expand Down Expand Up @@ -436,6 +438,7 @@ protected abstract class JsonRpcErrorBase : JsonRpcError, IJsonRpcMessageBufferM
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[IgnoreDataMember]
[PropertyShape(Ignore = true)]
public TopLevelPropertyBagBase? TopLevelPropertyBag { get; set; }

void IJsonRpcMessageBufferManager.DeserializationComplete(JsonRpcMessage message)
Expand Down Expand Up @@ -480,6 +483,7 @@ protected abstract class JsonRpcResultBase : JsonRpcResult, IJsonRpcMessageBuffe
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[IgnoreDataMember]
[PropertyShape(Ignore = true)]
public TopLevelPropertyBagBase? TopLevelPropertyBag { get; set; }

void IJsonRpcMessageBufferManager.DeserializationComplete(JsonRpcMessage message)
Expand Down
2 changes: 1 addition & 1 deletion src/StreamJsonRpc/JsonRpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1697,7 +1697,7 @@ protected virtual async ValueTask<JsonRpcMessage> DispatchRequestAsync(JsonRpcRe
}

/// <summary>
/// Sends the JSON-RPC message to <see cref="IJsonRpcMessageHandler"/> intance to be transmitted.
/// Sends the JSON-RPC message to <see cref="IJsonRpcMessageHandler"/> instance to be transmitted.
/// </summary>
/// <param name="message">The message to send.</param>
/// <param name="cancellationToken">A token to cancel the send request.</param>
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>));
}
}
}
}
58 changes: 58 additions & 0 deletions src/StreamJsonRpc/NerdbankMessagePackFormatter.Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Nerdbank.MessagePack;
using StreamJsonRpc.Protocol;

namespace StreamJsonRpc;

/// <summary>
/// Serializes JSON-RPC messages using MessagePack (a fast, compact binary format).
/// </summary>
public partial class NerdbankMessagePackFormatter
{
/// <summary>
/// The constant "jsonrpc", in its various forms.
/// </summary>
private static readonly MessagePackString VersionPropertyName = new(Constants.jsonrpc);

/// <summary>
/// The constant "id", in its various forms.
/// </summary>
private static readonly MessagePackString IdPropertyName = new(Constants.id);

/// <summary>
/// The constant "method", in its various forms.
/// </summary>
private static readonly MessagePackString MethodPropertyName = new(Constants.Request.method);

/// <summary>
/// The constant "result", in its various forms.
/// </summary>
private static readonly MessagePackString ResultPropertyName = new(Constants.Result.result);

/// <summary>
/// The constant "error", in its various forms.
/// </summary>
private static readonly MessagePackString ErrorPropertyName = new(Constants.Error.error);

/// <summary>
/// The constant "params", in its various forms.
/// </summary>
private static readonly MessagePackString ParamsPropertyName = new(Constants.Request.@params);

/// <summary>
/// The constant "traceparent", in its various forms.
/// </summary>
private static readonly MessagePackString TraceParentPropertyName = new(Constants.Request.traceparent);

/// <summary>
/// The constant "tracestate", in its various forms.
/// </summary>
private static readonly MessagePackString TraceStatePropertyName = new(Constants.Request.tracestate);

/// <summary>
/// The constant "2.0", in its various forms.
/// </summary>
private static readonly MessagePackString Version2 = new("2.0");
}
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>));
}
}
}
Loading