Table of Contents

Class BsonSerializer

Namespace
MongoDB.Bson.Serialization
Assembly
MongoDB.Bson.dll

A static class that represents the BSON serialization functionality.

public static class BsonSerializer
Inheritance
BsonSerializer
Inherited Members

Properties

SerializerRegistry

Gets the serializer registry.

public static IBsonSerializerRegistry SerializerRegistry { get; }

Property Value

IBsonSerializerRegistry

UseNullIdChecker

Gets or sets whether to use the NullIdChecker on reference Id types that don't have an IdGenerator registered.

public static bool UseNullIdChecker { get; set; }

Property Value

bool

UseZeroIdChecker

Gets or sets whether to use the ZeroIdChecker on value Id types that don't have an IdGenerator registered.

public static bool UseZeroIdChecker { get; set; }

Property Value

bool

Methods

Deserialize(BsonDocument, Type, Action<Builder>)

Deserializes an object from a BsonDocument.

public static object Deserialize(BsonDocument document, Type nominalType, Action<BsonDeserializationContext.Builder> configurator = null)

Parameters

document BsonDocument

The BsonDocument.

nominalType Type

The nominal type of the object.

configurator Action<BsonDeserializationContext.Builder>

The configurator.

Returns

object

A deserialized value.

Deserialize(IBsonReader, Type, Action<Builder>)

Deserializes a value.

public static object Deserialize(IBsonReader bsonReader, Type nominalType, Action<BsonDeserializationContext.Builder> configurator = null)

Parameters

bsonReader IBsonReader

The BsonReader.

nominalType Type

The nominal type of the object.

configurator Action<BsonDeserializationContext.Builder>

The configurator.

Returns

object

A deserialized value.

Deserialize(byte[], Type, Action<Builder>)

Deserializes an object from a BSON byte array.

public static object Deserialize(byte[] bytes, Type nominalType, Action<BsonDeserializationContext.Builder> configurator = null)

Parameters

bytes byte[]

The BSON byte array.

nominalType Type

The nominal type of the object.

configurator Action<BsonDeserializationContext.Builder>

The configurator.

Returns

object

A deserialized value.

Deserialize(Stream, Type, Action<Builder>)

Deserializes an object from a BSON Stream.

public static object Deserialize(Stream stream, Type nominalType, Action<BsonDeserializationContext.Builder> configurator = null)

Parameters

stream Stream

The BSON Stream.

nominalType Type

The nominal type of the object.

configurator Action<BsonDeserializationContext.Builder>

The configurator.

Returns

object

A deserialized value.

Deserialize(TextReader, Type, Action<Builder>)

Deserializes an object from a JSON TextReader.

public static object Deserialize(TextReader textReader, Type nominalType, Action<BsonDeserializationContext.Builder> configurator = null)

Parameters

textReader TextReader

The JSON TextReader.

nominalType Type

The nominal type of the object.

configurator Action<BsonDeserializationContext.Builder>

The configurator.

Returns

object

A deserialized value.

Deserialize(string, Type, Action<Builder>)

Deserializes an object from a JSON string.

public static object Deserialize(string json, Type nominalType, Action<BsonDeserializationContext.Builder> configurator = null)

Parameters

json string

The JSON string.

nominalType Type

The nominal type of the object.

configurator Action<BsonDeserializationContext.Builder>

The configurator.

Returns

object

A deserialized value.

Deserialize<TNominalType>(BsonDocument, Action<Builder>)

Deserializes an object from a BsonDocument.

public static TNominalType Deserialize<TNominalType>(BsonDocument document, Action<BsonDeserializationContext.Builder> configurator = null)

Parameters

document BsonDocument

The BsonDocument.

configurator Action<BsonDeserializationContext.Builder>

The configurator.

Returns

TNominalType

A deserialized value.

Type Parameters

TNominalType

The nominal type of the object.

Deserialize<TNominalType>(IBsonReader, Action<Builder>)

Deserializes a value.

public static TNominalType Deserialize<TNominalType>(IBsonReader bsonReader, Action<BsonDeserializationContext.Builder> configurator = null)

Parameters

bsonReader IBsonReader

The BsonReader.

configurator Action<BsonDeserializationContext.Builder>

The configurator.

Returns

TNominalType

A deserialized value.

Type Parameters

TNominalType

The nominal type of the object.

Deserialize<TNominalType>(byte[], Action<Builder>)

Deserializes an object from a BSON byte array.

public static TNominalType Deserialize<TNominalType>(byte[] bytes, Action<BsonDeserializationContext.Builder> configurator = null)

Parameters

bytes byte[]

The BSON byte array.

configurator Action<BsonDeserializationContext.Builder>

The configurator.

Returns

TNominalType

A deserialized value.

Type Parameters

TNominalType

The nominal type of the object.

Deserialize<TNominalType>(Stream, Action<Builder>)

Deserializes an object from a BSON Stream.

public static TNominalType Deserialize<TNominalType>(Stream stream, Action<BsonDeserializationContext.Builder> configurator = null)

Parameters

stream Stream

The BSON Stream.

configurator Action<BsonDeserializationContext.Builder>

The configurator.

Returns

TNominalType

A deserialized value.

Type Parameters

TNominalType

The nominal type of the object.

Deserialize<TNominalType>(TextReader, Action<Builder>)

Deserializes an object from a JSON TextReader.

public static TNominalType Deserialize<TNominalType>(TextReader textReader, Action<BsonDeserializationContext.Builder> configurator = null)

Parameters

textReader TextReader

The JSON TextReader.

configurator Action<BsonDeserializationContext.Builder>

The configurator.

Returns

TNominalType

A deserialized value.

Type Parameters

TNominalType

The nominal type of the object.

Deserialize<TNominalType>(string, Action<Builder>)

Deserializes an object from a JSON string.

public static TNominalType Deserialize<TNominalType>(string json, Action<BsonDeserializationContext.Builder> configurator = null)

Parameters

json string

The JSON string.

configurator Action<BsonDeserializationContext.Builder>

The configurator.

Returns

TNominalType

A deserialized value.

Type Parameters

TNominalType

The nominal type of the object.

IsTypeDiscriminated(Type)

Returns whether the given type has any discriminators registered for any of its subclasses.

public static bool IsTypeDiscriminated(Type type)

Parameters

type Type

A Type.

Returns

bool

True if the type is discriminated.

LookupActualType(Type, BsonValue)

Looks up the actual type of an object to be deserialized.

public static Type LookupActualType(Type nominalType, BsonValue discriminator)

Parameters

nominalType Type

The nominal type of the object.

discriminator BsonValue

The discriminator.

Returns

Type

The actual type of the object.

LookupDiscriminatorConvention(Type)

Looks up the discriminator convention for a type.

public static IDiscriminatorConvention LookupDiscriminatorConvention(Type type)

Parameters

type Type

The type.

Returns

IDiscriminatorConvention

A discriminator convention.

LookupIdGenerator(Type)

Looks up an IdGenerator.

public static IIdGenerator LookupIdGenerator(Type type)

Parameters

type Type

The Id type.

Returns

IIdGenerator

An IdGenerator for the Id type.

LookupSerializer(Type)

Looks up a serializer for a Type.

public static IBsonSerializer LookupSerializer(Type type)

Parameters

type Type

The Type.

Returns

IBsonSerializer

A serializer for the Type.

LookupSerializer<T>()

Looks up a serializer for a Type.

public static IBsonSerializer<T> LookupSerializer<T>()

Returns

IBsonSerializer<T>

A serializer for type T.

Type Parameters

T

The type.

RegisterDiscriminator(Type, BsonValue)

Registers the discriminator for a type.

public static void RegisterDiscriminator(Type type, BsonValue discriminator)

Parameters

type Type

The type.

discriminator BsonValue

The discriminator.

RegisterDiscriminatorConvention(Type, IDiscriminatorConvention)

Registers the discriminator convention for a type.

public static void RegisterDiscriminatorConvention(Type type, IDiscriminatorConvention convention)

Parameters

type Type

Type type.

convention IDiscriminatorConvention

The discriminator convention.

RegisterGenericSerializerDefinition(Type, Type)

Registers a generic serializer definition for a generic type.

public static void RegisterGenericSerializerDefinition(Type genericTypeDefinition, Type genericSerializerDefinition)

Parameters

genericTypeDefinition Type

The generic type.

genericSerializerDefinition Type

The generic serializer definition.

RegisterIdGenerator(Type, IIdGenerator)

Registers an IdGenerator for an Id Type.

public static void RegisterIdGenerator(Type type, IIdGenerator idGenerator)

Parameters

type Type

The Id Type.

idGenerator IIdGenerator

The IdGenerator for the Id Type.

RegisterSerializationProvider(IBsonSerializationProvider)

Registers a serialization provider.

public static void RegisterSerializationProvider(IBsonSerializationProvider provider)

Parameters

provider IBsonSerializationProvider

The serialization provider.

RegisterSerializer(Type, IBsonSerializer)

Registers a serializer for a type.

public static void RegisterSerializer(Type type, IBsonSerializer serializer)

Parameters

type Type

The type.

serializer IBsonSerializer

The serializer.

RegisterSerializer<T>(IBsonSerializer<T>)

Registers a serializer for a type.

public static void RegisterSerializer<T>(IBsonSerializer<T> serializer)

Parameters

serializer IBsonSerializer<T>

The serializer.

Type Parameters

T

The type.

Serialize(IBsonWriter, Type, object, Action<Builder>, BsonSerializationArgs)

Serializes a value.

public static void Serialize(IBsonWriter bsonWriter, Type nominalType, object value, Action<BsonSerializationContext.Builder> configurator = null, BsonSerializationArgs args = default)

Parameters

bsonWriter IBsonWriter

The BsonWriter.

nominalType Type

The nominal type of the object.

value object

The object.

configurator Action<BsonSerializationContext.Builder>

The serialization context configurator.

args BsonSerializationArgs

The serialization args.

Serialize<TNominalType>(IBsonWriter, TNominalType, Action<Builder>, BsonSerializationArgs)

Serializes a value.

public static void Serialize<TNominalType>(IBsonWriter bsonWriter, TNominalType value, Action<BsonSerializationContext.Builder> configurator = null, BsonSerializationArgs args = default)

Parameters

bsonWriter IBsonWriter

The BsonWriter.

value TNominalType

The object.

configurator Action<BsonSerializationContext.Builder>

The serialization context configurator.

args BsonSerializationArgs

The serialization args.

Type Parameters

TNominalType

The nominal type of the object.

TryRegisterSerializer(Type, IBsonSerializer)

Tries to register a serializer for a type.

public static bool TryRegisterSerializer(Type type, IBsonSerializer serializer)

Parameters

type Type

The type.

serializer IBsonSerializer

The serializer.

Returns

bool

True if the serializer was registered on this call, false if the same serializer was already registered on a previous call, throws an exception if a different serializer was already registered.

TryRegisterSerializer<T>(IBsonSerializer<T>)

Tries to register a serializer for a type.

public static bool TryRegisterSerializer<T>(IBsonSerializer<T> serializer)

Parameters

serializer IBsonSerializer<T>

The serializer.

Returns

bool

True if the serializer was registered on this call, false if the same serializer was already registered on a previous call, throws an exception if a different serializer was already registered.

Type Parameters

T

The type.