Conventions: Serialization
Use the methods described in this page to customize the conventions by which entities are serialized as they are sent by the client to the server.
Serialization
CustomizeJsonSerializer
- The
JsonSerializerobject is used by the client to serialize entities sent by the client to the server. - Use the
CustomizeJsonSerializerconvention to modifyJsonSerializerby registering a serialization customization action.
Serialization = new NewtonsoftJsonSerializationConventions
{
CustomizeJsonSerializer = serializer => throw new CodeOmitted()
}
JsonContractResolver
- The default
JsonContractResolverconvention used by RavenDB will serialize all properties and all public fields. - Change this behavior by providing your own implementation of the
IContractResolverinterface.
Serialization = new NewtonsoftJsonSerializationConventions
{
JsonContractResolver = new CustomJsonContractResolver()
}
public class CustomJsonContractResolver : IContractResolver
{
public JsonContract ResolveContract(Type type)
{
throw new CodeOmitted();
}
}
- You can also customize the behavior of the default resolver by inheriting
from
DefaultRavenContractResolverand overriding specific methods.
public class CustomizedRavenJsonContractResolver : DefaultRavenContractResolver
{
public CustomizedRavenJsonContractResolver(ISerializationConventions conventions) : base(conventions)
{
}
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
throw new CodeOmitted();
}
}
BulkInsert.TrySerializeEntityToJsonStream
- Adjust Bulk Insert
behavior by using the
TrySerializeEntityToJsonStreamconvention to register a custom serialization implementation.
BulkInsert =
{
TrySerializeEntityToJsonStream = (entity, metadata, writer) => throw new CodeOmitted(),
}
IgnoreByRefMembers and IgnoreUnsafeMembers
- By default, if you try to store an entity with
refor unsafe members, the Client will throw an exception whensession.SaveChanges()is called. - Set the
IgnoreByRefMembersconvention totrueto simply ignorerefmembers when an attempt to store an entity withrefmembers is made.
The entity will be uploaded to the server with all non-refmembers without throwing an exception.
The document structure on the server-side will not contain fields for thoserefmembers. - Set the
IgnoreUnsafeMembersconvention totrueto ignore all pointer members in the same manner. IgnoreByRefMembersdefault value:falseIgnoreUnsafeMembersdefault value:false