Question

I have some questions about BSON and first I want to understand correctly some things about it.

  1. I have seen cases of programs where JSON files were used as configuration files. This is what I would like to attempt. The fact is that .INIs for example are not suited for my use. I must input in my program through a parser a list of parameters. Some are ASCII strings, some are arrays of ASCII strings, some are 32 bit integers, some 64 bit, doubles, arrays of doubles, etc. I have read some BSON documentation. But before continuing. I Wonder... is BSON suitable for this use?

  2. Say I have an array of strings [ "hello", "meow", "whatsup" ], for example... Are there functions to access specifically each element of the array e.g. similar to array[1] in C for example.

  3. It seems the BSON Library documentation contains an API to create BSON objects and all, but I do not see any parser. The point of using BSON is not to implement a parser in my case. So I wonder... is there a Library to parse BSON files, so I only have to access the objects without dealing with parsing?

  4. Is the BSON Library disponible on Windows? Can I include it in a C++ MSVC project? I would like to statically link it since my program must be only a .DLL, is it possible?

  5. I see that BSON contains data types like int32 and int64. In a BSON file, how can I declarate an instance to be int32? Something like this? :

    { "variable" : (int32) 82730 }

Thank you!

Thanks!

Was it helpful?

Solution

  1. BSON is not suitable, because it is a binary format. Configuration file stored in BSON will not be human-readable.
    You can, however, use the C++ BSONObj to work with a configuration stored in JSON. I rather like that option, I'm using BSON to work with JSON, although I had to rewrite the JSON parser and emitter because the ones MongoDB provide have some limitations (can't parse top-level arrays, 64-bit integers are encoded in a way specific to MongoDB).
  2. That depends on the library, there are several BSON libraries: the C++ BSON library, the Java BSON library, the PHP BSON library and so on. I'm only familiar with the C++ BSON library. In that library there is an operator[] (int field) in BSONArray so yes, you can do the random access.
    You can also get a vector copy of the array and you can get an iterator. The vector obviously has the random access and it's trivial to move the iterator to the needed position as well.
  3. That's the beauty of it, BSON doesn't need a parser. You can mmap a huge BSON file and start using it without any parsing, for example.
  4. A large part of the library is header-only, you might have luck using the header-only part of the library without any linking. Compiling and linking the MongoDB DLL should be possible, but I've never tried it.
  5. BSON file is binary, it's not human-readable.
    If you're parsing JSON into BSON then the MongoDB JSON parser supports the "variable" : { "$numberLong" : "82730" } notation. By default a number is parsed as int32 if it fits and as int64 if not.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top