Conventions: Deserialization
- The C# client deserializes documents loaded from the server into entities using the Newtonsoft.Json library (also known as Json.NET).
- Use the conventions described in this article to customize the deserialization (e.g., to preserve document properties that your entity class does not define).
- In this article:
CustomizeJsonDeserializer
- The client uses
JsonSerializerobjects of the Newtonsoft.Json library to deserialize documents loaded from the server into entities. - Use the
CustomizeJsonDeserializerconvention to register an action that modifies only theJsonSerializerobjects used to load documents (e.g., to keep a property's default value when the loaded document holdsnullfor this property).
Conventions ={Serialization = new NewtonsoftJsonSerializationConventions{CustomizeJsonDeserializer = serializer => throw new CodeOmitted()}}
- Note that an action registered using CustomizeJsonSerializer
also applies when loading documents, and runs before the
CustomizeJsonDeserializeraction.
A converter registered usingCustomizeJsonSerializertherefore does not need to be registered usingCustomizeJsonDeserializeras well.
DeserializeEntityFromBlittable
Use the DeserializeEntityFromBlittable convention to customize entity
deserialization from a blittable JSON.
Conventions =
{
Serialization = new NewtonsoftJsonSerializationConventions
{
DeserializeEntityFromBlittable = (type, blittable) => throw new CodeOmitted()
}
}
PreserveDocumentPropertiesNotFoundOnModel
- Some document properties are not deserialized to an object.
- Set the
PreserveDocumentPropertiesNotFoundOnModelconvention totrueto preserve such properties when the document is saved. - Set the
PreserveDocumentPropertiesNotFoundOnModelconvention tofalseto remove such properties when the document is saved. - Default:
true
Conventions =
{
PreserveDocumentPropertiesNotFoundOnModel = true
}
DefaultRavenSerializationBinder
Use the DefaultRavenSerializationBinder convention and its methods to
prevent gadgets from running RCE (Remote Code Execution) attacks while
data is deserialized by the client.
Read about this security convention and maintaining deserialization security here.
Number Deserialization
- RavenDB client supports all common numeric value types (including
int,long,double,decimal, etc.) out of the box. - Note that although deserialization of
decimalsis fully supported, there are server side limitations to numbers in this range. - Other number types, like
BigInteger, must be handled using a converter.