PMart.Enumeration.SystemTextJson

April 16, 2025 ยท View on GitHub

This is the package to add System.Text.Json support for the Enumeration classes (more information in the main page).

Installation

Add the package to your project:

dotnet add package PMart.Enumeration.SystemTextJson

Usage

Using System.Text.Json, if you need to serialize/deserialize objects that contain properties of type Enumeration or EnumerationDynamic, without any converters, the enumeration property would act like a regular object.

For example, using this model:

public class CommunicationRecord
{
    public DateTime SentAt { get; set; }
    
    public string To { get; set; } = null!;
    
    public CommunicationType Type { get; set; } = null!;
}

The JSON without any custom JSON converters would be like:

{
  "sentAt": "0001-01-01",
  "to": "someone@email.com",
  "communicationType": {
    "value": "Email"
  }
}

Probably, you would like a JSON where the CommunicationType works like a enum or a string value:

{
  "sentAt": "0001-01-01",
  "to": "someone@email.com",
  "communicationType": "Email"
}

For that, you just need to use the JSON converter EnumerationConverterFactory available on the NuGet package PMart.Enumeration.SystemTextJson.

An example where the converter is added by attribute:

public class CommunicationRecord
{
    public DateTime SentAt { get; set; }
    
    public string To { get; set; } = null!;
    
    [JsonConverter(typeof(EnumerationConverterFactory))]
    public CommunicationType Type { get; set; } = null!;
}

An example where the converter is added on the serializer options:

public string SerializeCommunicationRecord(CommunicationRecord communicationRecord)
{
    var serializerOptions = GetSerializerOptions();

    var json = JsonSerializer.Serialize(communicationRecord, serializerOptions);

    return json;
}

private JsonSerializerOptions GetSerializerOptions()
{
    return new JsonSerializerOptions
    {
        Converters = { new EnumerationConverterFactory() }
    };
}