Click or drag to resize
Json.NETSerialize with JsonConverters
 

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

Sample
Usage
 1List<StringComparison> stringComparisons = new List<StringComparison>
 2{
 3    StringComparison.CurrentCulture,
 4    StringComparison.Ordinal
 5};
 6
 7string jsonWithoutConverter = JsonConvert.SerializeObject(stringComparisons);
 8
 9Console.WriteLine(jsonWithoutConverter);
10// [0,4]
11
12string jsonWithConverter = JsonConvert.SerializeObject(stringComparisons, new StringEnumConverter());
13
14Console.WriteLine(jsonWithConverter);
15// ["CurrentCulture","Ordinal"]
16
17List<StringComparison> newStringComparsions = JsonConvert.DeserializeObject<List<StringComparison>>(
18    jsonWithConverter,
19    new StringEnumConverter());
20
21Console.WriteLine(string.Join(", ", newStringComparsions.Select(c => c.ToString()).ToArray()));
22// CurrentCulture, Ordinal