3 / 5
May 2024

This code converts a BsonDocument to JSON using:

BsonDocument bson = new { Name = "someone" }.ToBsonDocument(); string json = bson.ToJson(new JsonWriterSettings { OutputMode = JsonOutputMode.RelaxedExtendedJson }); json.Should().Be("{name:\"someone\"}");

The expected JSON output is {name:"someone"}, but the actual output contains spaces as { name : "someone" }.

I am concerned about the potential impact on bandwidth usage due to these unnecessary spaces in the JSON output. How I can ensure that the output matches the expected format without unnecessary spaces?

Yep, it takes 13 percent more bandwidth. And Yep I tried everything. Also Newton BSON serializer works fine.

It’s already GZIP. Without spaces, it’s 13% smaller, give or take. Also, driver compression works between the database and the backend. The problem is in the backend and frontend.
Anyway, I wrote my own BSON stringify. Apparently, the C# driver dev team forgot to apply the open-close principle to the library.