Loading…

BitBadger.Npgsql.Documents
Advanced Usage: Custom Serialization

JSON documents are sent to and received from PostgreSQL as strings; the translation to and from your domain objects (commonly called POCOs) is handled via .NET. By default, the serializer used by the library is based on System.Text.Json with converters for common F# types.

Implementing a Custom Serializer

IDocumentSerializer (found in the BitBadger.Npgsql.Documents namespace in both libraries) specifies two methods. Serialize<T> takes a T and returns a string; Deserialize<T> takes a string and returns an instance of T. (These show as 'T in F#.) While implementing those two methods is required, the custom implementation can use whatever library you desire, and contain converters for custom types.

Once this serializer is implemented and constructed, provide it to the library:

// C#
    var serializer = /* constructed serializer */;
    Configuration.UseSerializer(serializer);
// F#
    let serializer = (* constructed serializer *)
    Configuration.useSerializer serializer

The biggest benefit to registering a serializer (apart from control) is that all JSON operations will use the same serializer. This is most important for JSON containment queries; the object you pass as the criteria will be translated properly before PostgreSQL compares it.

Uses for Custom Serialization

« Back to Advanced Usage