33using System . IO ;
44using System . Reflection ;
55using System . Text ;
6+ using System . Threading . Tasks ;
67using System . Xml ;
78using System . Xml . Serialization ;
89using Shuttle . Core . Contract ;
@@ -26,6 +27,7 @@ public DefaultSerializer()
2627 {
2728 _xmlWriterSettings = new XmlWriterSettings
2829 {
30+ Async = true ,
2931 Encoding = Encoding . UTF8 ,
3032 OmitXmlDeclaration = true ,
3133 Indent = true
@@ -42,9 +44,18 @@ public DefaultSerializer()
4244 }
4345
4446 public string Name => "Xml" ;
45- public byte Id => 1 ;
4647
4748 public Stream Serialize ( object instance )
49+ {
50+ return SerializeAsync ( instance , true ) . GetAwaiter ( ) . GetResult ( ) ;
51+ }
52+
53+ public async Task < Stream > SerializeAsync ( object instance )
54+ {
55+ return await SerializeAsync ( instance , false ) . ConfigureAwait ( false ) ;
56+ }
57+
58+ private async Task < Stream > SerializeAsync ( object instance , bool sync )
4859 {
4960 Guard . AgainstNull ( instance , nameof ( instance ) ) ;
5061
@@ -53,38 +64,59 @@ public Stream Serialize(object instance)
5364
5465 var xml = new StringBuilder ( ) ;
5566
56- using ( var writer = XmlWriter . Create ( xml , _xmlWriterSettings ) )
57- {
58- serializer . Serialize ( writer , instance , _namespaces ) ;
67+ using var writer = XmlWriter . Create ( xml , _xmlWriterSettings ) ;
68+
69+ serializer . Serialize ( writer , instance , _namespaces ) ;
5970
71+ if ( sync )
72+ {
6073 writer . Flush ( ) ;
6174 }
75+ else
76+ {
77+ await writer . FlushAsync ( ) . ConfigureAwait ( false ) ;
78+ }
6279
6380 var data = Encoding . UTF8 . GetBytes ( xml . ToString ( ) ) ;
81+
6482 return new MemoryStream ( data , 0 , data . Length , false , true ) ;
6583 }
6684
6785 public object Deserialize ( Type type , Stream stream )
86+ {
87+ return DeserializeAsync ( type , stream , true ) . GetAwaiter ( ) . GetResult ( ) ;
88+ }
89+
90+ public async Task < object > DeserializeAsync ( Type type , Stream stream )
91+ {
92+ return await DeserializeAsync ( type , stream , false ) . ConfigureAwait ( false ) ;
93+ }
94+
95+ private async Task < object > DeserializeAsync ( Type type , Stream stream , bool sync )
6896 {
6997 Guard . AgainstNull ( type , nameof ( type ) ) ;
7098 Guard . AgainstNull ( stream , nameof ( stream ) ) ;
7199
72- using ( var copy = new MemoryStream ( ) )
73- {
74- var position = stream . Position ;
100+ using var copy = new MemoryStream ( ) ;
101+ var position = stream . Position ;
75102
76- stream . Position = 0 ;
103+ stream . Position = 0 ;
104+
105+ if ( sync )
106+ {
77107 stream . CopyTo ( copy ) ;
108+ }
109+ else
110+ {
111+ await stream . CopyToAsync ( copy ) . ConfigureAwait ( false ) ;
112+ }
78113
79- stream . Position = position ;
80- copy . Position = 0 ;
114+ stream . Position = position ;
115+ copy . Position = 0 ;
81116
82-
83- using ( var reader = XmlDictionaryReader . CreateTextReader ( copy , Encoding . UTF8 , _xmlDictionaryReaderQuotas , null ) )
84- {
85- return GetSerializer ( type ) . Deserialize ( reader ) ;
86- }
87- }
117+ using var reader = XmlDictionaryReader . CreateTextReader ( copy , Encoding . UTF8 , _xmlDictionaryReaderQuotas , null ) ;
118+
119+ return GetSerializer ( type ) . Deserialize ( reader ) ;
88120 }
89121
90122 public void AddSerializerType ( Type root , Type contained )
0 commit comments