|
7 | 7 | using System.Text;
|
8 | 8 | using System.Threading;
|
9 | 9 | using System.Threading.Tasks;
|
10 |
| -using Elastic.Clients.Elasticsearch; |
11 | 10 | using Elastic.Transport;
|
12 | 11 | using Newtonsoft.Json;
|
13 | 12 |
|
14 |
| -namespace Elastic.Clients.Elasticsearch.JsonNetSerializer |
| 13 | +namespace Elastic.Clients.Elasticsearch.JsonNetSerializer; |
| 14 | + |
| 15 | +public abstract partial class ConnectionSettingsAwareSerializer : Serializer |
15 | 16 | {
|
16 |
| - public abstract partial class ConnectionSettingsAwareSerializer : Serializer |
17 |
| - { |
18 |
| - // Default buffer size of StreamWriter, which is private :( |
19 |
| - internal const int DefaultBufferSize = 1024; |
| 17 | + // Default buffer size of StreamWriter, which is private :( |
| 18 | + internal const int DefaultBufferSize = 1024; |
20 | 19 |
|
21 |
| - private static readonly Task CompletedTask = Task.CompletedTask; |
| 20 | + private static readonly Task CompletedTask = Task.CompletedTask; |
22 | 21 |
|
23 |
| - internal static readonly Encoding ExpectedEncoding = new UTF8Encoding(false); |
24 |
| - private readonly JsonSerializer _collapsedSerializer; |
| 22 | + internal static readonly Encoding ExpectedEncoding = new UTF8Encoding(false); |
| 23 | + private readonly JsonSerializer _collapsedSerializer; |
25 | 24 |
|
26 |
| - private readonly JsonSerializer _serializer; |
27 |
| - protected virtual int BufferSize => DefaultBufferSize; |
| 25 | + private readonly JsonSerializer _serializer; |
| 26 | + protected virtual int BufferSize => DefaultBufferSize; |
28 | 27 |
|
29 |
| - public override T Deserialize<T>(Stream stream) |
30 |
| - { |
31 |
| - using var streamReader = new StreamReader(stream); |
32 |
| - using var jsonTextReader = new JsonTextReader(streamReader); |
33 |
| - return _serializer.Deserialize<T>(jsonTextReader); |
34 |
| - } |
| 28 | + public override T Deserialize<T>(Stream stream) |
| 29 | + { |
| 30 | + using var streamReader = new StreamReader(stream); |
| 31 | + using var jsonTextReader = new JsonTextReader(streamReader); |
| 32 | + return _serializer.Deserialize<T>(jsonTextReader); |
| 33 | + } |
35 | 34 |
|
36 |
| - public override object Deserialize(Type type, Stream stream) |
37 |
| - { |
38 |
| - using var streamReader = new StreamReader(stream); |
39 |
| - using var jsonTextReader = new JsonTextReader(streamReader); |
40 |
| - return _serializer.Deserialize(jsonTextReader, type); |
41 |
| - } |
| 35 | + public override object Deserialize(Type type, Stream stream) |
| 36 | + { |
| 37 | + using var streamReader = new StreamReader(stream); |
| 38 | + using var jsonTextReader = new JsonTextReader(streamReader); |
| 39 | + return _serializer.Deserialize(jsonTextReader, type); |
| 40 | + } |
42 | 41 |
|
43 |
| - public override async ValueTask<T> DeserializeAsync<T>(Stream stream, CancellationToken cancellationToken = default) |
44 |
| - { |
45 |
| - using var streamReader = new StreamReader(stream); |
46 |
| - using var jsonTextReader = new JsonTextReader(streamReader); |
47 |
| - var token = await jsonTextReader.ReadTokenWithDateParseHandlingNoneAsync(cancellationToken).ConfigureAwait(false); |
48 |
| - return token.ToObject<T>(_serializer); |
49 |
| - } |
| 42 | + public override async ValueTask<T> DeserializeAsync<T>(Stream stream, CancellationToken cancellationToken = default) |
| 43 | + { |
| 44 | + using var streamReader = new StreamReader(stream); |
| 45 | + using var jsonTextReader = new JsonTextReader(streamReader); |
| 46 | + var token = await jsonTextReader.ReadTokenWithDateParseHandlingNoneAsync(cancellationToken).ConfigureAwait(false); |
| 47 | + return token.ToObject<T>(_serializer); |
| 48 | + } |
50 | 49 |
|
51 |
| - public override async ValueTask<object> DeserializeAsync(Type type, Stream stream, CancellationToken cancellationToken = default(CancellationToken)) |
52 |
| - { |
53 |
| - using var streamReader = new StreamReader(stream); |
54 |
| - using var jsonTextReader = new JsonTextReader(streamReader); |
55 |
| - var token = await jsonTextReader.ReadTokenWithDateParseHandlingNoneAsync(cancellationToken).ConfigureAwait(false); |
56 |
| - return token.ToObject(type, _serializer); |
57 |
| - } |
| 50 | + public override async ValueTask<object> DeserializeAsync(Type type, Stream stream, CancellationToken cancellationToken = default(CancellationToken)) |
| 51 | + { |
| 52 | + using var streamReader = new StreamReader(stream); |
| 53 | + using var jsonTextReader = new JsonTextReader(streamReader); |
| 54 | + var token = await jsonTextReader.ReadTokenWithDateParseHandlingNoneAsync(cancellationToken).ConfigureAwait(false); |
| 55 | + return token.ToObject(type, _serializer); |
| 56 | + } |
58 | 57 |
|
59 |
| - public override void Serialize<T>(T data, Stream stream, SerializationFormatting formatting = SerializationFormatting.None) |
60 |
| - { |
61 |
| - using var writer = new StreamWriter(stream, ExpectedEncoding, BufferSize, true); |
62 |
| - using var jsonWriter = new JsonTextWriter(writer); |
63 |
| - var serializer = formatting == SerializationFormatting.Indented ? _serializer : _collapsedSerializer; |
64 |
| - serializer.Serialize(jsonWriter, data); |
65 |
| - } |
| 58 | + public override void Serialize<T>(T data, Stream stream, SerializationFormatting formatting = SerializationFormatting.None) |
| 59 | + { |
| 60 | + using var writer = new StreamWriter(stream, ExpectedEncoding, BufferSize, true); |
| 61 | + using var jsonWriter = new JsonTextWriter(writer); |
| 62 | + var serializer = formatting == SerializationFormatting.Indented ? _serializer : _collapsedSerializer; |
| 63 | + serializer.Serialize(jsonWriter, data); |
| 64 | + } |
66 | 65 |
|
67 |
| - public override Task SerializeAsync<T>(T data, Stream stream, SerializationFormatting formatting = SerializationFormatting.None, |
68 |
| - CancellationToken cancellationToken = default) |
69 |
| - { |
70 |
| - //This makes no sense now but we need the async method on the interface in 6.x so we can start swapping this out |
71 |
| - //for an implementation that does make sense without having to wait for 7.x |
72 |
| - Serialize(data, stream, formatting); |
73 |
| - return CompletedTask; |
74 |
| - } |
| 66 | + public override Task SerializeAsync<T>(T data, Stream stream, SerializationFormatting formatting = SerializationFormatting.None, |
| 67 | + CancellationToken cancellationToken = default) |
| 68 | + { |
| 69 | + //This makes no sense now but we need the async method on the interface in 6.x so we can start swapping this out |
| 70 | + //for an implementation that does make sense without having to wait for 7.x |
| 71 | + Serialize(data, stream, formatting); |
| 72 | + return CompletedTask; |
75 | 73 | }
|
76 | 74 | }
|
0 commit comments