vibe.d beta banner
get vibe.d
0.10.1

Asynchronous I/O that doesn’t get in your way, written in D

Function serializeToBson

Serializes the given value to BSON.

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

The following types of values are supported:

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

See Also

deserializeBson

Authors

Sönke Ludwig

Copyright

© 2012-2015 Sönke Ludwig

License

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