@@ -16,20 +16,16 @@ public class BsonOutputFormatter : TextOutputFormatter
16
16
private readonly JsonSerializerSettings _jsonSerializerSettings ;
17
17
18
18
private JsonSerializer _serializer ;
19
-
19
+
20
20
public BsonOutputFormatter ( JsonSerializerSettings serializerSettings )
21
21
{
22
- if ( serializerSettings == null )
23
- {
24
- throw new ArgumentNullException ( nameof ( serializerSettings ) ) ;
25
- }
22
+ _jsonSerializerSettings = serializerSettings ?? throw new ArgumentNullException ( nameof ( serializerSettings ) ) ;
26
23
27
-
28
- _jsonSerializerSettings = serializerSettings ;
29
24
SupportedEncodings . Add ( Encoding . UTF8 ) ;
30
25
SupportedEncodings . Add ( Encoding . Unicode ) ;
31
26
SupportedMediaTypes . Add ( MediaTypeHeaderValue . Parse ( "application/bson" ) ) ;
32
27
}
28
+
33
29
public override async Task WriteResponseBodyAsync ( OutputFormatterWriteContext context , Encoding selectedEncoding )
34
30
{
35
31
if ( context == null )
@@ -43,22 +39,17 @@ public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext co
43
39
}
44
40
45
41
var response = context . HttpContext . Response ;
46
- using ( var bsonWriter = new BsonWriter ( response . Body ) { CloseOutput = false } )
42
+ using ( var bsonWriter = new BsonDataWriter ( response . Body ) { CloseOutput = false } )
47
43
{
48
44
var jsonSerializer = CreateJsonSerializer ( ) ;
49
45
jsonSerializer . Serialize ( bsonWriter , context . Object ) ;
50
46
bsonWriter . Flush ( ) ;
51
47
}
52
48
}
53
-
49
+
54
50
private JsonSerializer CreateJsonSerializer ( )
55
51
{
56
- if ( _serializer == null )
57
- {
58
- _serializer = JsonSerializer . Create ( _jsonSerializerSettings ) ;
59
- }
60
-
61
- return _serializer ;
52
+ return _serializer ?? ( _serializer = JsonSerializer . Create ( _jsonSerializerSettings ) ) ;
62
53
}
63
54
}
64
55
}
0 commit comments