Click or drag to resize
Json.NET

Serialize with JsonConverters

 

This sample uses a JsonConverter to customize how JSON is serialized.

Sample
Usage
List<StringComparison> stringComparisons = new List<StringComparison>
{
    StringComparison.CurrentCulture,
    StringComparison.Ordinal
};

string jsonWithoutConverter = JsonConvert.SerializeObject(stringComparisons);

Console.WriteLine(jsonWithoutConverter);
// [0,4]

string jsonWithConverter = JsonConvert.SerializeObject(stringComparisons, new StringEnumConverter());

Console.WriteLine(jsonWithConverter);
// ["CurrentCulture","Ordinal"]

List<StringComparison> newStringComparsions = JsonConvert.DeserializeObject<List<StringComparison>>(
    jsonWithConverter,
    new StringEnumConverter());

Console.WriteLine(string.Join(", ", newStringComparsions.Select(c => c.ToString()).ToArray()));
// CurrentCulture, Ordinal