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
JsonSerializer
object is used by the client to serialize entities sent by the client to the server. - Use the
CustomizeJsonSerializer
convention to modifyJsonSerializer
by registering a serialization customization action.
Serialization = new NewtonsoftJsonSerializationConventions
{
CustomizeJsonSerializer = serializer => throw new CodeOmitted()
}
JsonContractResolver
- The default
JsonContractResolver
convention used by RavenDB will serialize all properties and all public fields. -
Change this behavior by providing your own implementation of the
IContractResolver
interface.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
DefaultRavenContractResolver
and 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
TrySerializeEntityToJsonStream
convention 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
ref
or unsafe members, the Client will throw an exception whensession.SaveChanges()
is called. - Set the
IgnoreByRefMembers
convention totrue
to simply ignoreref
members when an attempt to store an entity withref
members is made.
The entity will be uploaded to the server with all non-ref
members without throwing an exception.
The document structure on the server-side will not contain fields for thoseref
members. - Set the
IgnoreUnsafeMembers
convention totrue
to ignore all pointer members in the same manner. IgnoreByRefMembers
default value:false
IgnoreUnsafeMembers
default value:false