[CSHARP-831] Provide BsonDocument.Pretty() method for debugging purposes. Created: 02/Sep/13  Updated: 11/Mar/19  Resolved: 22/Oct/13

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: William Steinberg Assignee: Unassigned
Resolution: Done Votes: 0
Labels: bson
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

When displaying a BsonDocument, with default ToString(), it produces a nice compact linear version.
It would be nice if a BsonDocument supported the Pretty() method, and do like the mongo shell does with pretty().
Since you did it for the mongo shell, just port that to C#.



 Comments   
Comment by Robert Stam [ 22/Oct/13 ]

We are not planning to add a method called Pretty because there is already a documented way to do this.

If you want to use the name Pretty in your code you can define an extension method of your own called Pretty. For example:

public static class PrettyExtensionMethod
{
    public static string Pretty(this object obj)
    {
        return obj.ToJson(new JsonWriterSettings { Indent = true });
    }
}

Comment by William Steinberg [ 26/Sep/13 ]

Yes. I see that there is a workaround. Could we add pretty() anyway?

Comment by Robert Stam [ 26/Sep/13 ]

Instead of using ToString use ToJson which allows you to use JsonWriterSettings to control the format of the output. For example:

var bsonDocument = new BsonDocument { { "x", 1 }, { "y", 2 } };
var json = bsonDocument.ToJson(new JsonWriterSettings { Indent = true });
Console.WriteLine(json);

outputs:

{
  "x" : 1,
  "y" : 2
}

Which I think is what you are asking for (i.e. multiline output).

Generated at Wed Feb 07 21:37:57 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.