using Newtonsoft.Json.Converters;
using System.Text.Json.Serialization;
public static void Main()
var example = new Example{Name = "Hello World", SomeValue = 42, Terrain = Terrain.FOREST | Terrain.SWAMP | Terrain.CAVE};
NewtonsoftExample(example);
Console.WriteLine(string.Empty);
SystemTextJsonExample(example);
private static void NewtonsoftExample(Example example)
Console.WriteLine("Serializing and Deserializing with Newtonsoft.Json:");
var converter = new StringEnumConverter();
var json = JsonConvert.SerializeObject(example, Formatting.Indented, converter);
example = JsonConvert.DeserializeObject<Example>(json );
Console.WriteLine($"example.Terrain: {example.Terrain}");
private static void SystemTextJsonExample(Example example)
Console.WriteLine("Serializing and Deserializing with System.Text.Json:");
var options = new JsonSerializerOptions{WriteIndented = true, IncludeFields = true};
options.Converters.Add(new JsonStringEnumConverter());
var json = System.Text.Json.JsonSerializer.Serialize(example, options);
example = System.Text.Json.JsonSerializer.Deserialize<Example>(json , options);
Console.WriteLine($"example.Terrain: {example.Terrain}");