Integrations
editAn ID is read and written as its underlying value everywhere: a number or a string in JSON, a value in a BSON document or a database column, a segment of a URL. The generator writes a converter for each serializer, nested in the ID and registered by an attribute on it, so most libraries use them without configuration.
| Library | What the generator provides | Configuration |
|---|---|---|
| System.Text.Json | A JsonConverter<T>, also for dictionary keys | None, except with a source-generated JsonSerializerContext: register the converters in its options |
| Newtonsoft.Json | A JsonConverter, when the project references Newtonsoft.Json | None |
| MongoDB | A BSON serializer, when the project references MongoDB.Bson | None |
| TypeConverter and configuration | A TypeConverter, used by Microsoft.Extensions.Configuration and UI frameworks | None |
| ASP.NET Core | TryParse and IParsable<T> for route and query values, and the JSON converter for bodies | None |
| Entity Framework Core | Nothing specific | A value converter that you write once per ID type |
| YAML | A YamlConverter<T>, when the project references Meziantou.Framework.Yaml | None, except with a source-generated YamlSerializerContext: register the converters in its options |
| TOML | A TomlConverter<T>, when the project references Meziantou.Framework.Toml | None, except with a source-generated TomlSerializerContext: register the converters in options that use it |
Each converter can be turned off with an option.
Use a converter directly
The converters are public classes nested in the ID, named after it: ProjectId.ProjectIdJsonConverter, ProjectId.ProjectIdNewtonsoftJsonConverter, ProjectId.ProjectIdBsonConverter, ProjectId.ProjectIdYamlConverter, ProjectId.ProjectIdTomlConverter and ProjectId.ProjectIdTypeConverter. Create one when a library does not read the attributes, for instance to add it to JsonSerializerOptions.Converters for a source-generated context, to wrap it in your own converter, or to register it in another type.
To choose another accessibility, declare the converter in the ID with the accessibility you want. The generator writes the rest of the class and keeps your modifiers:
[StronglyTypedId<int>]
public partial struct WarehouseId
{
// Declare the converter to choose its accessibility. The generator writes the rest of the type.
internal sealed partial class WarehouseIdJsonConverter;
}