Provides a System.Text.Json converter for the EmailAddress value object from
PosInformatique.Foundations.EmailAddresses. Enables seamless serialization and deserialization
of RFC 5322 compliant email addresses within JSON documents.
You can install the package from NuGet:
dotnet add package PosInformatique.Foundations.EmailAddresses.JsonThis package depends on the base package PosInformatique.Foundations.EmailAddresses.
- Provides a
JsonConverter<EmailAddress>for serialization and deserialization. - Ensures validation of RFC 5322 compliant email addresses when deserializing.
- Can be used via attributes (
[JsonConverter]) or throughJsonSerializerOptionsextension method. - Ensures consistency with the base
EmailAddressvalue object.
- Serialization: Convert value objects into JSON strings without losing semantics
- Validation: Guarantee that only valid RFC 5322 email addresses are accepted in JSON payloads
- Integration: Plug directly into
System.Text.Jsonconfiguration
using System.Text.Json;
using System.Text.Json.Serialization;
using PosInformatique.Foundations.EmailAddresses;
using PosInformatique.Foundations.EmailAddresses.Json;
public class UserDto
{
[JsonConverter(typeof(EmailAddressJsonConverter))]
public EmailAddress Email { get; set; } = default!;
}
// Serialization
var dto = new UserDto { Email = "john.doe@example.com" };
var json = JsonSerializer.Serialize(dto);
// Result: {"Email":"john.doe@example.com"}
// Deserialization
var input = "{ "Email": "alice@company.org" }";
var deserialized = JsonSerializer.Deserialize<UserDto>(input);
Console.WriteLine(deserialized!.Email); // "alice@company.org"using System.Text.Json;
using PosInformatique.Foundations.EmailAddresses;
public class CustomerDto
{
public EmailAddress Email { get; set; } = default!;
}
var options = new JsonSerializerOptions().AddEmailAddressesConverters();
// Serialization
var dto = new CustomerDto { Email = "bob@myapp.com" };
var json = JsonSerializer.Serialize(dto, options);
// Result: {"Email":"bob@myapp.com"}
// Deserialization
var input = "{ "Email": "carol@myapp.com" }";
var deserialized = JsonSerializer.Deserialize<CustomerDto>(input, options);
Console.WriteLine(deserialized!.Email); // "carol@myapp.com"