Function serializeToBson

Serializes the given value to BSON.

The following types of values are supported:

Bson
Used as-is
Json
Converted to BSON
BsonBinData) $(DD Converted to Bson.Type.binData
BsonObjectID) $(DD Converted to Bson.Type.objectID
BsonDate) $(DD Converted to Bson.Type.date
BsonTimestamp) $(DD Converted to Bson.Type.timestamp
BsonRegex) $(DD Converted to Bson.Type.regex
null) $(DD Converted to Bson.Type.null_
bool) $(DD Converted to Bson.Type.bool_
float, double) $(DD Converted to Bson.Type.double_
short, ushort, int, uint, long, ulong) $(DD Converted to Bson.Type.long_
string) $(DD Converted to Bson.Type.string
ubyte[]) $(DD Converted to Bson.Type.binData
T[]) $(DD Converted to Bson.Type.array
T[string]) $(DD Converted to Bson.Type.object
struct) $(DD Converted to Bson.Type.object
class) $(DD Converted to Bson.Type.object or Bson.Type.null_

All entries of an array or an associative array, as well as all R/W properties and all fields of a struct/class are recursively serialized using the same rules.

Fields ending with an underscore will have the last underscore stripped in the serialized output. This makes it possible to use fields with D keywords as their name by simply appending an underscore.

The following methods can be used to customize the serialization of structs/classes:

Bson toBson() const;
static T fromBson(Bson src);

Json toJson() const;
static T fromJson(Json src);

string toString() const;
static T fromString(string src);

The methods will have to be defined in pairs. The first pair that is implemented by the type will be used for serialization (i.e. toBson overrides toJson).

Prototype

Bson serializeToBson(T)(
  T value,
  ubyte[] buffer = null
);

See Also

deserializeBson

Authors

Sönke Ludwig

Copyright

© 2012-2015 RejectedSoftware e.K.

License

Subject to the terms of the MIT license, as written in the included LICENSE.txt file.