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:
Bson:
\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')
JSON | BSON | |
---|---|---|
Encoding |
UTF-8 String |
Binary |
Readability |
Human and Machine |
Machine Only |
Data Support |
String, Boolean, Number, Array |
String, Boolean, Number (Integer, Float, Long, Decimal128...), Array, Date, Raw Binary |
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 :
public partial class EntityBase<T> : INotifyPropertyChanged { private static JsonSerializer _serializer; private static JsonSerializer SerializerBson { get { if ((_serializer == null)) { _serializer = new JsonSerializer(); } return _serializer; } } public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged; public virtual void OnPropertyChanged(string propertyName) { System.ComponentModel.PropertyChangedEventHandler handler = PropertyChanged; if ((handler != null)) { handler(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName)); } } #region Serialize/Deserialize /// <summary> /// Serializes current EntityBase object into an XML string /// </summary> /// <returns>string XML value</returns> public virtual string Serialize() { MemoryStream memoryStream = null; try { memoryStream = new MemoryStream(); BsonDataWriter bsonDataWriter = new BsonDataWriter(memoryStream); SerializerBson.Serialize(bsonDataWriter, this); return Convert.ToBase64String(memoryStream.ToArray()); } finally { if ((memoryStream != null)) { memoryStream.Dispose(); } } } /// <summary> /// Deserializes EntityBase object /// </summary> /// <param name="input">string workflow markup to deserialize</param> /// <param name="obj">Output EntityBase object</param> /// <param name="exception">output Exception value if deserialize failed</param> /// <returns>true if this Serializer can deserialize the object; otherwise, false</returns> public static bool Deserialize(string input, out T obj, out Exception exception) { exception = null; obj = default(T); try { obj = Deserialize(input); return true; } catch (Exception ex) { exception = ex; return false; } } public static bool Deserialize(string input, out T obj) { Exception exception = null; return Deserialize(input, out obj, out exception); } /// <returns>string XML value</returns> public new static T Deserialize(string input) { MemoryStream memoryStream = null; try { byte[] data; data = Convert.FromBase64String(input); memoryStream = new MemoryStream(data); BsonDataReader bsonDataReader = new BsonDataReader(memoryStream); return SerializerBson.Deserialize<T>(bsonDataReader); } finally { if ((memoryStream != null)) { memoryStream.Dispose(); } } } #endregion public virtual void SaveToFile(string fileName) { StreamWriter streamWriter = null; try { string dataString = Serialize(); FileInfo outputFile = new FileInfo(fileName); streamWriter = outputFile.CreateText(); streamWriter.WriteLine(dataString); streamWriter.Close(); } finally { if ((streamWriter != null)) { streamWriter.Dispose(); } } } public new static T LoadFromFile(string fileName) { FileStream file = null; StreamReader sr = null; try { file = new FileStream(fileName, FileMode.Open, FileAccess.Read); sr = new StreamReader(file); string dataString = sr.ReadToEnd(); sr.Close(); file.Close(); return Deserialize(dataString); } finally { if ((file != null)) { file.Dispose(); } if ((sr != null)) { sr.Dispose(); } } } #endregion } #endregion