PMart.Enumeration.JsonNet

April 16, 2025 ยท View on GitHub

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

Installation

Add the package to your project:

dotnet add package PMart.Enumeration.JsonNet

Usage

Using Newtonsoft Json.NET, 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 CommunicationType { 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 an enum or a string value:

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

For that, you just need to use the custom converters available on the NuGet package PMart.Enumeration.JsonNet.

An example where the converter EnumerationConverter<T> is added by attribute:

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

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

public string SerializeCommunicationRecord(CommunicationRecord communicationRecord)
{
    var json = JsonConvert.SerializeObject(communicationRecord, new EnumerationConverter<CommunicationType>());

    return json;
}

For enumerations of type EnumerationDynamic, you can use the generic converter EnumerationDynamicConverter<T>.

When you have several enumeration types that you would like to register globally, instead of registering all the converters of type EnumerationConverter<T> (or EnumerationDynamicConverter<T>), one for each enumeration type, you can use the non-generic converter EnumerationConverter. This converter evaluates if the object is derived from Enumeration or EnumerationDynamic and handles it accordingly. It might be a little less performant.

public string SerializeCommunicationRecord(CommunicationRecord communicationRecord)
{
    var json = JsonConvert.SerializeObject(communicationRecord, new EnumerationConverter());

    return json;
}