1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
### bson extension
The bson extension implements decode from and encode to the [Binary JSON](http://bsonspec.org/) data format.
You can either parse into or serialize from a variant-like data structure, [basic_json](../basic_json.md), or your own
data structures, using [json_type_traits](../json_type_traits.md).
[decode_bson](decode_bson.md)
[basic_bson_cursor](basic_bson_cursor.md)
[encode_bson](encode_bson.md)
[basic_bson_encoder](basic_bson_encoder.md)
[bson_options](bson_options.md)
#### Mappings between BSON and jsoncons data items
BSON data item | jsoncons data item |jsoncons semantic_tag
---------------------------------|---------------|------------
null | null |
true or false | bool |
int32 or int64 | int64 |
int32 or int64 | uint64 |
UTC datetime | int64 | epoch_milli
Timestamp | uint64 |
double | double |
string | string |
binary | byte_string | ext
array | array |
embedded document | object |
decimal128 (since 0.165.0) | string | float128
ObjectId (since 0.165.0) | string | id
regex (since 0.165.0) | string | regex
JavaScript code (since 0.165.0) | string | code
min_key (since 0.165.0) | string |
max_key (since 0.165.0) | string |
undefined (since 0.165.0) | null | undefined
symbol (since 0.165.0) | string |
### Examples
[Document with string and binary](#eg1)
[Decode a BSON 128-bit decimal floating point (since 0.165.0)](#eg2)
[Encode a BSON 128-bit decimal floating point (since 0.165.0)](#eg3)
[Regular expression data (since 0.165.0)](#eg4)
[ObjectId data (since 0.165.0)](#eg5)
<div id="eg1"/>
#### Document with string and binary
```cpp
#include <jsoncons/json.hpp>
#include <jsoncons_ext/bson/bson.hpp>
#include <cassert>
using jsoncons::json;
namespace bson = jsoncons::bson; // for brevity
int main()
{
// Create some bson
std::vector<uint8_t> buffer;
bson::bson_bytes_encoder encoder(buffer);
encoder.begin_object(); // The total number of bytes comprising
// the bson document will be calculated
encoder.key("Hello");
encoder.string_value("World");
encoder.key("Data");
std::vector<uint8_t> bstr = {'f','o','o','b','a','r'};
encoder.byte_string_value(bstr); // default subtype is user defined
// or encoder.byte_string_value(bstr, 0x80);
encoder.end_object();
encoder.flush();
std::cout << "(1)\n" << jsoncons::byte_string_view(buffer) << "\n";
/*
0x27,0x00,0x00,0x00, // Total number of bytes comprising the document (40 bytes)
0x02, // URF-8 string
0x48,0x65,0x6c,0x6c,0x6f, // Hello
0x00, // trailing byte
0x06,0x00,0x00,0x00, // Number bytes in string (including trailing byte)
0x57,0x6f,0x72,0x6c,0x64, // World
0x00, // trailing byte
0x05, // binary
0x44,0x61,0x74,0x61, // Data
0x00, // trailing byte
0x06,0x00,0x00,0x00, // number of bytes
0x80, // subtype
0x66,0x6f,0x6f,0x62,0x61,0x72,
0x00
*/
ojson j = bson::decode_bson<ojson>(buffer);
std::cout << "(2)\n" << pretty_print(j) << "\n\n";
std::cout << "(3) " << j["Data"].tag() << "(" << j["Data"].ext_tag() << ")\n\n";
// Get binary value as a std::vector<uint8_t>
auto bstr2 = j["Data"].as<std::vector<uint8_t>>();
assert(bstr2 == bstr);
std::vector<uint8_t> buffer2;
bson::encode_bson(j, buffer2);
assert(buffer2 == buffer);
}
```
Output:
```
(1)
27,00,00,00,02,48,65,6c,6c,6f,00,06,00,00,00,57,6f,72,6c,64,00,05,44,61,74,61,00,06,00,00,00,80,66,6f,6f,62,61,72,00
(2)
{
"Hello": "World",
"Data": "Zm9vYmFy"
}
(3) ext(128)
```
<div id="eg2"/>
#### Decode a BSON 128-bit decimal floating point (since 0.165.0)
```cpp
#include <jsoncons/json.hpp>
#include <jsoncons_ext/bson/bson.hpp>
#include <cassert>
using jsoncons::json;
namespace bson = jsoncons::bson; // for brevity
int main()
{
std::vector<uint8_t> input = {
0x18,0x00,0x00,0x00, // Document has 24 bytes
0x13, // 128-bit decimal floating point
0x61,0x00, // "a"
0x01,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, // 1E-6176
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00 // terminating null
};
json j = bson::decode_bson<json>(input);
std::cout << "(1) " << j << "\n\n";
std::cout << "(2) " << j.at("a").tag() << "\n\n";
std::vector<char> output;
bson::encode_bson(j, output);
assert(output == input);
}
```
Output:
```
(1) {"a":"1E-6176"}
(2) float128
```
<div id="eg3"/>
#### Encode a BSON 128-bit decimal floating point (since 0.165.0)
```cpp
#include <jsoncons/json.hpp>
#include <jsoncons_ext/bson/bson.hpp>
#include <cassert>
using jsoncons::json;
namespace bson = jsoncons::bson; // for brevity
int main()
{
json j;
j.try_emplace("a", "1E-6176", jsoncons::semantic_tag::float128);
// or j["a"] = json("1E-6176", jsoncons::semantic_tag::float128);
std::cout << "(1) " << j << "\n\n";
std::cout << "(2) " << j.at("a").tag() << "\n\n";
std::vector<char> output;
bson::encode_bson(j, output);
std::cout << "(3) " << jsoncons::byte_string_view(output) << "\n\n";
/*
18,00,00,00, // document has 24 bytes
13, // 128-bit decimal floating point
13,00, // "a"
01,00,00,00,
00,00,00,00, // 1E-6176
00,00,00,00,
00,00,00,00,
00 // terminating null
*/
}
```
Output:
```
(1) {"a":"1E-6176"}
(2) float128
(3) 18,00,00,00,13,61,00,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00
```
<div id="eg4"/>
#### Regular expression data (since 0.165.0)
```cpp
#include <jsoncons/json.hpp>
#include <jsoncons_ext/bson/bson.hpp>
#include <cassert>
using jsoncons::json;
namespace bson = jsoncons::bson; // for brevity
int main()
{
std::vector<uint8_t> input = {
0x16,0x00,0x00,0x00, // Document has 22 bytes
0x0B, // Regular expression
0x72,0x65,0x67,0x65,0x78,0x00, // "regex"
0x5E,0x61,0x62,0x63,0x64,0x00, // "^abcd"
0x69,0x6C,0x78,0x00, // "ilx"
0x00 // terminating null
};
json j = bson::decode_bson<json>(input);
std::cout << "(1) " << j << "\n\n";
std::cout << "(2) " << j.at("regex").tag() << "\n\n";
std::vector<char> output;
bson::encode_bson(j, output);
assert(output == input);
}
```
Output:
```
(1) {"regex":"/^abcd/ilx"}
(2) regex
```
<div id="eg5"/>
#### ObjectId data (since 0.165.0)
```cpp
#include <jsoncons/json.hpp>
#include <jsoncons_ext/bson/bson.hpp>
#include <cassert>
using jsoncons::json;
namespace bson = jsoncons::bson; // for brevity
int main()
{
std::vector<uint8_t> input = {
0x16,0x00,0x00,0x00, // Document has 22 bytes
0x07, // ObjectId
0x6F,0x69,0x64,0x00, // "oid"
0x12,0x34,0x56,0x78,0x90,0xAB,
0xCD,0xEF,0x12,0x34,0xAB,0xCD, // (byte*12)
0x00 // terminating null
};
json j = bson::decode_bson<json>(input);
std::cout << "(1) " << j << "\n\n";
std::cout << "(2) " << j.at("oid").tag() << "\n\n";
std::vector<char> output;
bson::encode_bson(j, output);
assert(output == input);
}
```
Output:
```
(1) {"oid":"1234567890abcdef1234abcd"}
(2) id
```
### See also
[byte_string_view](../byte_string_view.md)
### Acknowledgements
The jsoncons implementations of BSON decimal128 to and from string,
and ObjectId to and from string, are based on the Apache 2 licensed [libbson](https://github.com/mongodb/mongo-c-driver/tree/master/src/libbson).
The decimal128, regex, and ObjectId examples are from the libbson test cases.
|