Closed ScanMountGoat closed 3 years ago
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace JsonConsole
{
class Program
{
enum MyEnum
{
One,
Two,
Three
}
class MyData
{
public string Name { get; set; } = "ABC";
public int Value { get; set; } = 3;
public MyEnum Variant { get; set; } = MyEnum.Two;
}
static void Main(string[] args)
{
var options = new JsonSerializerOptions { Converters = { new JsonStringEnumConverter() } };
var json = JsonSerializer.Serialize(new MyData(), options);
// This should be faster.
//JsonSerializer.SerializeToUtf8Bytes
Console.WriteLine(json);
var data = JsonSerializer.Deserialize<MyData>(json, options);
// This should be faster.
//JsonSerializer.Deserialize<MyData>(System.IO.File.ReadAllBytes("path.txt"));
Console.WriteLine($"{data.Name} {data.Value} {data.Variant}");
}
}
}
In theory, this should improve performance and memory usage on startup.