forked from microsoft/vs-streamjsonrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessagePackFormatter.cs
123 lines (109 loc) · 5 KB
/
MessagePackFormatter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace StreamJsonRpc
{
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using MessagePack;
using Nerdbank.Streams;
using StreamJsonRpc.Protocol;
/// <summary>
/// Serializes JSON-RPC messages using MessagePack (a fast, compact binary format).
/// </summary>
/// <remarks>
/// The MessagePack implementation used here comes from https://github.com/neuecc/MessagePack-CSharp.
/// The README on that project site describes use cases and its performance compared to alternative
/// .NET MessagePack implementations and this one appears to be the best by far.
/// </remarks>
public class MessagePackFormatter : IJsonRpcMessageFormatter
{
/// <summary>
/// A value indicating whether to use LZ4 compression.
/// </summary>
private readonly bool compress;
/// <summary>
/// Initializes a new instance of the <see cref="MessagePackFormatter"/> class
/// with LZ4 compression.
/// </summary>
public MessagePackFormatter()
: this(compress: true)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="MessagePackFormatter"/> class.
/// </summary>
/// <param name="compress">A value indicating whether to use LZ4 compression.</param>
public MessagePackFormatter(bool compress)
{
this.compress = compress;
}
/// <inheritdoc/>
public JsonRpcMessage Deserialize(ReadOnlySequence<byte> contentBuffer)
{
return this.compress
? (JsonRpcMessage)LZ4MessagePackSerializer.Typeless.Deserialize(contentBuffer.AsStream())
: (JsonRpcMessage)MessagePackSerializer.Typeless.Deserialize(contentBuffer.AsStream());
}
/// <inheritdoc/>
public void Serialize(IBufferWriter<byte> contentBuffer, JsonRpcMessage message)
{
if (message is JsonRpcRequest request && request.Arguments != null && request.ArgumentsList == null && !(request.Arguments is IReadOnlyDictionary<string, object>))
{
// This request contains named arguments, but not using a standard dictionary. Convert it to a dictionary so that
// the parameters can be matched to the method we're invoking.
request.Arguments = GetParamsObjectDictionary(request.Arguments);
}
if (this.compress)
{
LZ4MessagePackSerializer.Typeless.Serialize(contentBuffer.AsStream(), message);
}
else
{
MessagePackSerializer.Typeless.Serialize(contentBuffer.AsStream(), message);
}
}
/// <inheritdoc/>
public object GetJsonText(JsonRpcMessage message) => MessagePackSerializer.ToJson<object>(message);
/// <summary>
/// Extracts a dictionary of property names and values from the specified params object.
/// </summary>
/// <param name="paramsObject">The params object.</param>
/// <returns>A dictionary, or <c>null</c> if <paramref name="paramsObject"/> is null.</returns>
/// <remarks>
/// In its present implementation, this method disregards any renaming attributes that would give
/// the properties on the parameter object a different name. The <see cref="MessagePackSerializer"/>
/// doesn't expose a simple way of doing this, so we'd have to emulate it by supporting both
/// <see cref="DataMemberAttribute.Name"/> and <see cref="KeyAttribute.StringKey"/> handling.
/// </remarks>
private static Dictionary<string, object> GetParamsObjectDictionary(object paramsObject)
{
if (paramsObject == null)
{
return null;
}
if (paramsObject is IReadOnlyDictionary<object, object> dictionary)
{
// Anonymous types are serialized this way.
return dictionary.ToDictionary(kv => (string)kv.Key, kv => kv.Value);
}
var result = new Dictionary<string, object>(StringComparer.Ordinal);
const BindingFlags bindingFlags = BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance;
foreach (var property in paramsObject.GetType().GetTypeInfo().GetProperties(bindingFlags))
{
if (property.GetMethod != null)
{
result[property.Name] = property.GetValue(paramsObject);
}
}
foreach (var field in paramsObject.GetType().GetTypeInfo().GetFields(bindingFlags))
{
result[field.Name] = field.GetValue(paramsObject);
}
return result;
}
}
}