File tree Expand file tree Collapse file tree 2 files changed +23
-4
lines changed Expand file tree Collapse file tree 2 files changed +23
-4
lines changed Original file line number Diff line number Diff line change 1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Globalization ;
4
+ using System . IO ;
4
5
using System . Linq ;
5
6
using System . Text ;
6
7
@@ -11,11 +12,22 @@ internal static class Extensions
11
12
private const long MillisecondsInAWeek = MillisecondsInADay * 7 ;
12
13
private const long MillisecondsInADay = MillisecondsInAnHour * 24 ;
13
14
private const long MillisecondsInAnHour = MillisecondsInAMinute * 60 ;
14
-
15
- internal static string Utf8String ( this byte [ ] bytes ) => bytes == null ? null : Encoding . UTF8 . GetString ( bytes , 0 , bytes . Length ) ;
16
15
private const long MillisecondsInAMinute = MillisecondsInASecond * 60 ;
17
16
private const long MillisecondsInASecond = 1000 ;
18
17
18
+ internal static string Utf8String ( this byte [ ] bytes ) => bytes == null ? null : Encoding . UTF8 . GetString ( bytes , 0 , bytes . Length ) ;
19
+
20
+ internal static string Utf8String ( this MemoryStream ms )
21
+ {
22
+ if ( ms is null )
23
+ return null ;
24
+
25
+ if ( ! ms . TryGetBuffer ( out ArraySegment < byte > buffer ) || buffer . Array is null )
26
+ return Encoding . UTF8 . GetString ( ms . ToArray ( ) ) ;
27
+
28
+ return Encoding . UTF8 . GetString ( buffer . Array , buffer . Offset , buffer . Count ) ;
29
+ }
30
+
19
31
internal static byte [ ] Utf8Bytes ( this string s ) => s . IsNullOrEmpty ( ) ? null : Encoding . UTF8 . GetBytes ( s ) ;
20
32
21
33
internal static string NotNull ( this string @object , string parameterName )
Original file line number Diff line number Diff line change @@ -44,7 +44,14 @@ public static string SerializeToString<T>(
44
44
T data ,
45
45
IMemoryStreamFactory memoryStreamFactory = null ,
46
46
SerializationFormatting formatting = SerializationFormatting . Indented
47
- ) =>
48
- serializer . SerializeToBytes ( data , memoryStreamFactory , formatting ) . Utf8String ( ) ;
47
+ )
48
+ {
49
+ memoryStreamFactory = memoryStreamFactory ?? RecyclableMemoryStreamFactory . Default ;
50
+ using ( var ms = memoryStreamFactory . Create ( ) )
51
+ {
52
+ serializer . Serialize ( data , ms , formatting ) ;
53
+ return ms . Utf8String ( ) ;
54
+ }
55
+ }
49
56
}
50
57
}
You can’t perform that action at this time.
0 commit comments