Skip to content

Commit

Permalink
Merge pull request #392 from AArnott/msgpackUpdate
Browse files Browse the repository at this point in the history
Update MessagePack version
  • Loading branch information
AArnott authored Dec 3, 2019
2 parents eb0da41 + 1b49b5f commit f16d000
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 34 deletions.
42 changes: 10 additions & 32 deletions src/StreamJsonRpc/MessagePackFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,6 @@ public class MessagePackFormatter : IJsonRpcMessageFormatter, IJsonRpcInstanceCo
/// </summary>
private MessagePackSerializerOptions userDataSerializationOptions = MessagePackSerializerOptions.Standard;

/// <summary>
/// The formatter to use when serializing user data that we only see typed as <see cref="object"/>.
/// </summary>
private DynamicObjectTypeFallbackFormatter dynamicObjectTypeFormatterForUserSuppliedResolver;

/// <summary>
/// Backing field for the <see cref="IJsonRpcInstanceContainer.Rpc"/> property.
/// </summary>
Expand All @@ -109,26 +104,9 @@ public class MessagePackFormatter : IJsonRpcMessageFormatter, IJsonRpcInstanceCo
/// Initializes a new instance of the <see cref="MessagePackFormatter"/> class.
/// </summary>
public MessagePackFormatter()
: this(compress: false)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MessagePackFormatter"/> class.
/// </summary>
/// <param name="compress">A value indicating whether to use LZ4 compression.</param>
private MessagePackFormatter(bool compress)
{
if (compress)
{
// Before we enable this, we need a way to ensure that the LZ4-expanded buffers stick around long enough for our deferred deserialization.
// See https://github.com/neuecc/MessagePack-CSharp/issues/109#issuecomment-551370773
throw new NotSupportedException();
}

// Set up initial options for our own message types.
this.messageSerializationOptions = MessagePackSerializerOptions.Standard
.WithLZ4Compression(useLZ4Compression: compress)
.WithResolver(this.CreateTopLevelMessageResolver());

// Create the specialized formatters/resolvers that we will inject into the chain for user data.
Expand All @@ -137,8 +115,7 @@ private MessagePackFormatter(bool compress)
this.pipeFormatterResolver = new PipeFormatterResolver(this);

// Set up default user data resolver.
this.SetMessagePackSerializerOptions(StandardResolverAllowPrivate.Options);
(this.userDataSerializationOptions, this.dynamicObjectTypeFormatterForUserSuppliedResolver) = this.MassageUserDataOptions(StandardResolverAllowPrivate.Options);
this.userDataSerializationOptions = this.MassageUserDataOptions(StandardResolverAllowPrivate.Options);
}

private interface IJsonRpcMessagePackRetention
Expand Down Expand Up @@ -236,7 +213,7 @@ public void SetMessagePackSerializerOptions(MessagePackSerializerOptions options
{
Requires.NotNull(options, nameof(options));

(this.userDataSerializationOptions, this.dynamicObjectTypeFormatterForUserSuppliedResolver) = this.MassageUserDataOptions(options);
this.userDataSerializationOptions = this.MassageUserDataOptions(options);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -354,7 +331,7 @@ private static ReadOnlySequence<byte> GetSliceForNextToken(ref MessagePackReader
/// </summary>
/// <param name="userSuppliedOptions">The options for user data that is supplied by the user (or the default).</param>
/// <returns>The <see cref="MessagePackSerializerOptions"/> to use for all user data (args, return values and error data) and a special formatter to use when all we have is <see cref="object"/> for this user data.</returns>
private (MessagePackSerializerOptions UserDataOptions, DynamicObjectTypeFallbackFormatter DynamicObjectTypeFormatter) MassageUserDataOptions(MessagePackSerializerOptions userSuppliedOptions)
private MessagePackSerializerOptions MassageUserDataOptions(MessagePackSerializerOptions userSuppliedOptions)
{
var formatters = new IMessagePackFormatter[]
{
Expand All @@ -376,10 +353,10 @@ private static ReadOnlySequence<byte> GetSliceForNextToken(ref MessagePackReader
IFormatterResolver userDataResolver = CompositeResolver.Create(formatters, resolvers);

MessagePackSerializerOptions userDataOptions = userSuppliedOptions
.WithLZ4Compression(false) // If/when we support LZ4 compression, it will be at the message level -- not the user-data level.
.WithCompression(MessagePackCompression.None) // If/when we support LZ4 compression, it will be at the message level -- not the user-data level.
.WithResolver(userDataResolver);

return (userDataOptions, new DynamicObjectTypeFallbackFormatter(userDataResolver));
return userDataOptions;
}

private IFormatterResolver CreateTopLevelMessageResolver()
Expand Down Expand Up @@ -1099,12 +1076,13 @@ public void Serialize(ref MessagePackWriter writer, Protocol.JsonRpcRequest valu
writer.Write(value.Method);

writer.Write(ParamsPropertyName);

if (value.ArgumentsList != null)
{
writer.WriteArrayHeader(value.ArgumentsList.Count);
foreach (var arg in value.ArgumentsList)
{
this.formatter.dynamicObjectTypeFormatterForUserSuppliedResolver.Serialize(ref writer, arg, this.formatter.userDataSerializationOptions);
DynamicObjectTypeFallbackFormatter.Instance.Serialize(ref writer, arg, this.formatter.userDataSerializationOptions);
}
}
else if (value.NamedArguments != null)
Expand All @@ -1113,7 +1091,7 @@ public void Serialize(ref MessagePackWriter writer, Protocol.JsonRpcRequest valu
foreach (KeyValuePair<string, object?> entry in value.NamedArguments)
{
writer.Write(entry.Key);
this.formatter.dynamicObjectTypeFormatterForUserSuppliedResolver.Serialize(ref writer, entry.Value, this.formatter.userDataSerializationOptions);
DynamicObjectTypeFallbackFormatter.Instance.Serialize(ref writer, entry.Value, this.formatter.userDataSerializationOptions);
}
}
else
Expand Down Expand Up @@ -1170,7 +1148,7 @@ public void Serialize(ref MessagePackWriter writer, Protocol.JsonRpcResult value
options.Resolver.GetFormatterWithVerify<RequestId>().Serialize(ref writer, value.RequestId, options);

writer.Write(ResultPropertyName);
this.formatter.dynamicObjectTypeFormatterForUserSuppliedResolver.Serialize(ref writer, value.Result, this.formatter.userDataSerializationOptions);
DynamicObjectTypeFallbackFormatter.Instance.Serialize(ref writer, value.Result, this.formatter.userDataSerializationOptions);
}
}

Expand Down Expand Up @@ -1272,7 +1250,7 @@ public void Serialize(ref MessagePackWriter writer, Protocol.JsonRpcError.ErrorD
writer.Write(value.Message);

writer.Write(DataPropertyName);
this.formatter.dynamicObjectTypeFormatterForUserSuppliedResolver.Serialize(ref writer, value.Data, this.formatter.userDataSerializationOptions);
DynamicObjectTypeFallbackFormatter.Instance.Serialize(ref writer, value.Data, this.formatter.userDataSerializationOptions);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/StreamJsonRpc/StreamJsonRpc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<PackageReference Include="MessagePack" Version="2.0.231-rc" />
<PackageReference Include="MessagePackAnalyzer" Version="2.0.231-rc" PrivateAssets="all" />
<PackageReference Include="MessagePack" Version="2.0.299-rc" />
<PackageReference Include="MessagePackAnalyzer" Version="2.0.299-rc" PrivateAssets="all" />
<!-- <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.4" PrivateAssets="all" /> -->
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="16.4.16" PrivateAssets="all" />
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="16.4.16" />
Expand Down

0 comments on commit f16d000

Please sign in to comment.