-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathJTokenExtensions.cs
31 lines (29 loc) · 1.02 KB
/
JTokenExtensions.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
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System.IO;
using Elastic.Transport;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Elastic.Clients.Elasticsearch.JsonNetSerializer
{
internal static class JTokenExtensions
{
/// <summary>
/// Writes a <see cref="JToken" /> to a <see cref="MemoryStream" /> using <see cref="ConnectionSettingsAwareSerializer.ExpectedEncoding" />
/// </summary>
public static MemoryStream ToStream(this JToken token, MemoryStreamFactory memoryStreamFactory)
{
var ms = memoryStreamFactory.Create();
using (var streamWriter = new StreamWriter(ms, ConnectionSettingsAwareSerializer.ExpectedEncoding,
ConnectionSettingsAwareSerializer.DefaultBufferSize, true))
using (var writer = new JsonTextWriter(streamWriter))
{
token.WriteTo(writer);
writer.Flush();
ms.Position = 0;
return ms;
}
}
}
}