Reads and writes BSON documents to and from Streams and Bufs.



Str print(Obj? val, Int? maxWidth := null, Str? indent := null)

Pretty prints MongoDB documents to a JSON-esque string. Useful for debugging.

Note PrettyPrinter only pretty prints if the resulting text string if greater than maxWidth. So if PrettyPrinter appears not to be working, then try setting a smaller maxWidth.

str := BsonIO.print(doc, 20)

maxWidth defaults to 80, and indent to 2 spaces.



Str:Obj? readDoc(InStream in, TimeZone tz := TimeZone.cur())

Reads a BSON document from an InStream.


  • BINARY objects with a subtype of BIN_GENERIC are returned as a Buf.
  • CODE objects are returned as Strs.
  • INTEGER_32 values are returned as 64-bit Ints.
  • REGEX flags are converted to embedded character flags.
  • Deprecated BSON objects are returned as null.

All DATE objects are returned in the given TimeZone.

This does not change the instant in the date time continuum, just time zone it is reported in. This lets a stored date time of 12 Dec 2012 18:00 UTC be returned as 12 Dec 2012 13:00 New_York.



Buf writeDoc(Str:Obj? document, Buf? buf := null)

Writes the BSON document to a Buf.

As per BSON spec, the returned Buf is set to be little endian.