Skip to content

Commit 3f5efdf

Browse files
authored
Merge pull request #188 from IanKemp/bsonwriter-latest
Update code to actually use class from Newtonsoft.Json.Bson package. Update Newtonsoft.Json.Bson to latest version.
2 parents 54baf03 + 3995fc3 commit 3f5efdf

File tree

2 files changed

+7
-16
lines changed

2 files changed

+7
-16
lines changed

src/WebApiContrib.Core.Formatter.Bson/BsonOutputFormatter.cs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,16 @@ public class BsonOutputFormatter : TextOutputFormatter
1616
private readonly JsonSerializerSettings _jsonSerializerSettings;
1717

1818
private JsonSerializer _serializer;
19-
19+
2020
public BsonOutputFormatter(JsonSerializerSettings serializerSettings)
2121
{
22-
if (serializerSettings == null)
23-
{
24-
throw new ArgumentNullException(nameof(serializerSettings));
25-
}
22+
_jsonSerializerSettings = serializerSettings ?? throw new ArgumentNullException(nameof(serializerSettings));
2623

27-
28-
_jsonSerializerSettings = serializerSettings;
2924
SupportedEncodings.Add(Encoding.UTF8);
3025
SupportedEncodings.Add(Encoding.Unicode);
3126
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/bson"));
3227
}
28+
3329
public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
3430
{
3531
if (context == null)
@@ -43,22 +39,17 @@ public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext co
4339
}
4440

4541
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 })
4743
{
4844
var jsonSerializer = CreateJsonSerializer();
4945
jsonSerializer.Serialize(bsonWriter, context.Object);
5046
bsonWriter.Flush();
5147
}
5248
}
53-
49+
5450
private JsonSerializer CreateJsonSerializer()
5551
{
56-
if (_serializer == null)
57-
{
58-
_serializer = JsonSerializer.Create(_jsonSerializerSettings);
59-
}
60-
61-
return _serializer;
52+
return _serializer ?? (_serializer = JsonSerializer.Create(_jsonSerializerSettings));
6253
}
6354
}
6455
}

src/WebApiContrib.Core.Formatter.Bson/WebApiContrib.Core.Formatter.Bson.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<ItemGroup>
1515
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.1.3" />
16-
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.1" />
16+
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.2" />
1717
</ItemGroup>
1818

1919
</Project>

0 commit comments

Comments
 (0)