Home  /  Blog  /  Documentation  /  Code Generator   /  Settings  /  BSon Serialization



BSON simply stands for “Binary JSON,” and that’s exactly what it was invented to be. BSON’s binary structure encodes type and length information, which allows it to be parsed much more quickly.

JSON and BSON are close cousins, as their nearly identical names imply, but you wouldn’t know it by looking at them side-by-side. JSON, or JavaScript Object Notation, is the wildly popular standard for data interchange on the web, on which BSON (Binary JSON) is based.

BSON simply stands for "Binary JSON", and that is exactly what it was invented to be. BSON's binary structure encodes type and length information, which allows for much faster parsing.

With xsd2code++ producing JSON or BSON represents exactly the same steps. The only thing to consider is the choice of the serializer in the JSonSerializer or BSonSerializer options. The steps to follow are described in the page dedicated to JSON serialization.


A document such as {"hello":"world"} will be stored as:

\x16\x00\x00\x00 // total document size
\x02 // 0x02 = type String
hello\x00 // field name
\x06\x00\x00\x00world\x00 // field value (size of value, value, null terminator)
\x00 // 0x00 = type EOO ('end of object')


UTF-8 String
Human and Machine
Machine Only
Data Support
String, Boolean, Number, Array
String, Boolean, Number (Integer, Float, Long, Decimal128...), Array, Date, Raw Binary

How to serialize in BSon format

To serialize to BSon format, it is exactly the same option as for JSON with the only difference being that the DefaultSerializer must be set to BSonSerializer.
Here is the code which will be generated in your classes or in a base class according to your options :