1- using System . IO ;
21using System . Reflection ;
2+ using Elasticsearch . Net ;
3+ using Newtonsoft . Json ;
4+ using System ;
5+ using System . IO ;
36using System . Text ;
47using System . Threading ;
58using System . Threading . Tasks ;
6- using Elasticsearch . Net ;
7- using Newtonsoft . Json ;
89
910namespace AM . Extensions . Logging . ElasticSearch
1011{
@@ -15,7 +16,7 @@ public class ElasticsearchJsonNetSerializer : IElasticsearchSerializer
1516 /// <summary>
1617 /// The size of the buffer to use when writing the serialized request
1718 /// to the request stream
18- /// Performance tests as part of https://github.com/elastic/elasticsearch-net/issues/1899 indicate this
19+ /// Performance tests as part of https://github.com/elastic/elasticsearch-net/issues/1899 indicate this
1920 /// to be a good compromise buffer size for performance throughput and bytes allocated.
2021 /// </summary>
2122 protected virtual int BufferSize => 1024 ;
@@ -40,31 +41,41 @@ private JsonSerializerSettings CreateSettings()
4041 return settings ;
4142 }
4243
44+ public object Deserialize ( Type type , Stream stream )
45+ {
46+ var settings = this . _settings ;
47+ return _Deserialize ( type , stream , settings ) ;
48+ }
4349
4450 public T Deserialize < T > ( Stream stream )
4551 {
4652 var settings = this . _settings ;
47- return _Deserialize < T > ( stream , settings ) ;
53+ return ( T ) _Deserialize ( typeof ( T ) , stream , settings ) ;
4854 }
4955
50- public async Task < T > DeserializeAsync < T > ( Stream responseStream , CancellationToken cancellationToken = new CancellationToken ( ) )
56+ public Task < object > DeserializeAsync ( Type type , Stream stream , CancellationToken cancellationToken = default ( CancellationToken ) )
5157 {
52- return this . Deserialize < T > ( responseStream ) ;
58+ return Task . FromResult ( _Deserialize ( type , stream ) ) ;
5359 }
5460
55- protected internal T _Deserialize < T > ( Stream stream , JsonSerializerSettings settings = null )
61+ protected internal object _Deserialize ( Type type , Stream stream , JsonSerializerSettings settings = null )
5662 {
5763 settings = settings ?? this . _settings ;
5864 var serializer = JsonSerializer . Create ( settings ) ;
5965 var jsonTextReader = new JsonTextReader ( new StreamReader ( stream ) ) ;
60- var t = ( T ) serializer . Deserialize ( jsonTextReader , typeof ( T ) ) ;
66+ var t = serializer . Deserialize ( jsonTextReader , type ) ;
6167 return t ;
6268 }
6369
70+ public Task < T > DeserializeAsync < T > ( Stream stream , CancellationToken cancellationToken = default ( CancellationToken ) )
71+ {
72+ var result = this . Deserialize < T > ( stream ) ;
73+ return Task . FromResult ( result ) ;
74+ }
6475
65- public void Serialize ( object data , Stream writableStream , SerializationFormatting formatting = SerializationFormatting . Indented )
76+ public void Serialize < T > ( T data , Stream stream , SerializationFormatting formatting = SerializationFormatting . Indented )
6677 {
67- using ( var writer = new StreamWriter ( writableStream , ExpectedEncoding , BufferSize , leaveOpen : true ) )
78+ using ( var writer = new StreamWriter ( stream , ExpectedEncoding , BufferSize , leaveOpen : true ) )
6879 using ( var jsonWriter = new JsonTextWriter ( writer ) )
6980 {
7081 _defaultSerializer . Serialize ( jsonWriter , data ) ;
@@ -73,6 +84,10 @@ public void Serialize(object data, Stream writableStream, SerializationFormattin
7384 }
7485 }
7586
76- public IPropertyMapping CreatePropertyMapping ( MemberInfo memberInfo ) => null ;
87+ public Task SerializeAsync < T > ( T data , Stream stream , SerializationFormatting formatting = SerializationFormatting . Indented , CancellationToken cancellationToken = default ( CancellationToken ) )
88+ {
89+ Serialize ( data , stream , formatting ) ;
90+ return Task . CompletedTask ;
91+ }
7792 }
7893}
0 commit comments