44 using Azure . Storage . Blobs ;
55 using Configuration ;
66 using Extensions ;
7+ using Newtonsoft . Json ;
78 using Properties ;
89 using Query ;
910 using Query . Strategies ;
1011 using System ;
1112 using System . Collections . Generic ;
1213 using System . Data ;
14+ using System . IO ;
1315 using System . Linq ;
1416 using System . Linq . Expressions ;
17+ using System . Text ;
1518 using System . Threading ;
1619 using System . Threading . Tasks ;
1720 using Utility ;
@@ -22,17 +25,19 @@ internal class AzureStorageBlobRepositoryContext : LinqEnumerableRepositoryConte
2225
2326 private readonly IAzureStorageBlobContainerNameBuilder _containerNameBuilder ;
2427 private readonly bool _createContainerIfNotExists ;
28+ private readonly JsonSerializerSettings _serializerSettings ;
2529
2630 #endregion
2731
2832 #region Constructors
2933
30- public AzureStorageBlobRepositoryContext ( string connectionString , IAzureStorageBlobContainerNameBuilder containerNameBuilder = null , bool createIfNotExists = false )
34+ public AzureStorageBlobRepositoryContext ( string connectionString , IAzureStorageBlobContainerNameBuilder containerNameBuilder = null , bool createIfNotExists = false , JsonSerializerSettings serializerSettings = null )
3135 {
3236 Guard . NotEmpty ( connectionString , nameof ( connectionString ) ) ;
3337
3438 _containerNameBuilder = containerNameBuilder ?? new DefaultContainerNameBuilder ( ) ;
3539 _createContainerIfNotExists = createIfNotExists ;
40+ _serializerSettings = serializerSettings ;
3641
3742 Client = new BlobServiceClient ( connectionString ) ;
3843 }
@@ -41,6 +46,46 @@ public AzureStorageBlobRepositoryContext(string connectionString, IAzureStorageB
4146
4247 #region Private Methods
4348
49+ private JsonSerializer GetJsonSerializer ( )
50+ {
51+ var settings = _serializerSettings ?? new JsonSerializerSettings
52+ {
53+ Formatting = Formatting . Indented ,
54+ ContractResolver = new DefaultJsonSerializeContractResolver ( ) ,
55+ PreserveReferencesHandling = PreserveReferencesHandling . None
56+ } ;
57+
58+ return JsonSerializer . Create ( settings ) ;
59+ }
60+
61+ private Stream Serialize < TEntity > ( TEntity entity )
62+ {
63+ var serializer = GetJsonSerializer ( ) ;
64+ var stream = new MemoryStream ( ) ;
65+
66+ using ( var sw = new StreamWriter ( stream : stream , encoding : Encoding . UTF8 , bufferSize : 4096 , leaveOpen : true ) )
67+ using ( var jsonTextWriter = new JsonTextWriter ( sw ) )
68+ {
69+ serializer . Serialize ( jsonTextWriter , entity ) ;
70+
71+ sw . Flush ( ) ;
72+ stream . Seek ( 0 , SeekOrigin . Begin ) ;
73+
74+ return stream ;
75+ }
76+ }
77+
78+ private TEntity Deserialize < TEntity > ( Stream stream )
79+ {
80+ var serializer = GetJsonSerializer ( ) ;
81+
82+ using ( var sr = new StreamReader ( stream ) )
83+ using ( var jsonTextReader = new JsonTextReader ( sr ) )
84+ {
85+ return serializer . Deserialize < TEntity > ( jsonTextReader ) ;
86+ }
87+ }
88+
4489 private BlobContainerClient GetBlobContainer < TEntity > ( )
4590 {
4691 var container = _containerNameBuilder . Build < TEntity > ( ) ;
@@ -72,7 +117,8 @@ private TEntity DownloadEntity<TEntity>(BlobContainerClient blobContainer, strin
72117 {
73118 var blob = blobContainer . GetBlobClient ( blobName ) ;
74119 var contentResult = blob . DownloadContent ( ) ;
75- var result = contentResult . Value . Content . ToObjectFromJson < TEntity > ( ) ;
120+ var contentStream = contentResult . Value . Content . ToStream ( ) ;
121+ var result = Deserialize < TEntity > ( contentStream ) ;
76122
77123 return result ;
78124 }
@@ -117,7 +163,8 @@ private async Task<TEntity> DownloadEntityAsync<TEntity>(BlobContainerClient blo
117163 {
118164 var blob = blobContainer . GetBlobClient ( blobName ) ;
119165 var contentResult = await blob . DownloadContentAsync ( cancellationToken ) ;
120- var result = contentResult . Value . Content . ToObjectFromJson < TEntity > ( ) ;
166+ var contentStream = contentResult . Value . Content . ToStream ( ) ;
167+ var result = Deserialize < TEntity > ( contentStream ) ;
121168
122169 return result ;
123170 }
@@ -139,17 +186,23 @@ private async IAsyncEnumerable<TEntity> DownloadEntitiesAsync<TEntity>() where T
139186 private void UploadEntity < TEntity > ( TEntity entity ) where TEntity : class
140187 {
141188 var blob = GetBlobClient ( entity ) ;
142- var binaryData = BinaryData . FromObjectAsJson < TEntity > ( entity ) ;
189+ using ( var stream = Serialize < TEntity > ( entity ) )
190+ {
191+ var binaryData = BinaryData . FromStream ( stream ) ;
143192
144- blob . Upload ( binaryData , overwrite : true ) ;
193+ blob . Upload ( binaryData , overwrite : true ) ;
194+ }
145195 }
146196
147197 private async Task UploadEntityAsync < TEntity > ( TEntity entity , CancellationToken cancellationToken = new CancellationToken ( ) ) where TEntity : class
148198 {
149199 var blob = await GetBlobClientAsync ( entity ) ;
150- var binaryData = BinaryData . FromObjectAsJson < TEntity > ( entity ) ;
200+ using ( var stream = Serialize < TEntity > ( entity ) )
201+ {
202+ var binaryData = await BinaryData . FromStreamAsync ( stream , cancellationToken ) ;
151203
152- await blob . UploadAsync ( binaryData , overwrite : true , cancellationToken ) ;
204+ await blob . UploadAsync ( binaryData , overwrite : true , cancellationToken ) ;
205+ }
153206 }
154207
155208 #endregion
0 commit comments