1

I'm searching for a way to copy a BSON document to a buffer or into a file using the C-library libbson.

I think I'm missing something - any help is appreciated.

zx485
  • 28,498
  • 28
  • 50
  • 59
zille
  • 95
  • 10
  • 1
    What format do you have the document in? – user253751 Mar 06 '18 at 22:27
  • 1
    I added two links to the relevant source pages. – zx485 Mar 06 '18 at 23:15
  • @immibis the document is on heap accessed via a bson_t pointer. Is that what you meant with format? – zille Mar 07 '18 at 08:40
  • bson_as_canonical_extended_json() may do the trick. Is there a good way to export bson in binary format as e.g. mongodump does? – zille Mar 07 '18 at 09:25
  • @zille yep. I was thinking you might've had it as a char array and be barking up the wrong tree :P – user253751 Mar 07 '18 at 20:57
  • @immibis okay. I tried half a day playing around with bson_writer_t. Wasn't successful though. canonical extended json is 1kb larger but has no information loss unlike json itself. So I'm sticking to that now. – zille Mar 08 '18 at 01:58

2 Answers2

1

bson_as_canonical_extended_json exports BSON into UTF-8 without loss of information.

zille
  • 95
  • 10
0

If you only need buffer data, you can use bson_get_data :

bson_t * bson = bson_new();
///// Fill with data

// Get data buffer
const char * data = reinterpret_cast<const char*>(bson_get_data(bson));
int length = bson->len;
Richard
  • 11
  • 3