public static void Write(ITestClass testObj) { string json; { testObj.Initialize(); testObj.Verify(); json = JsonSerializer.Serialize(testObj, testObj.GetType()); } { ITestClass obj = (ITestClass)JsonSerializer.Deserialize(json, testObj.GetType()); obj.Verify(); } }
public static void Write(ITestClass testObj) { var options = new JsonSerializerOptions { IncludeFields = true }; string json; { testObj.Initialize(); testObj.Verify(); json = JsonSerializer.Serialize(testObj, testObj.GetType(), options); } { ITestClass obj = (ITestClass)JsonSerializer.Deserialize(json, testObj.GetType(), options); obj.Verify(); } }
public static async Task MultipleTypes(ITestClass testObj) { Type type = testObj.GetType(); // Get the test json with the default options to avoid cache pollution of Deserialize() below. testObj.Initialize(); testObj.Verify(); var options = new JsonSerializerOptions { IncludeFields = true }; string json = JsonSerializer.Serialize(testObj, type, options); void Serialize() { ITestClass localTestObj = (ITestClass)Activator.CreateInstance(type); localTestObj.Initialize(); localTestObj.Verify(); string json = JsonSerializer.Serialize(localTestObj, type, s_options); }; void Deserialize() { ITestClass obj = (ITestClass)JsonSerializer.Deserialize(json, type, s_options); obj.Verify(); }; const int ThreadCount = 12; const int ConcurrentTestsCount = 2; Task[] tasks = new Task[ThreadCount * ConcurrentTestsCount]; for (int i = 0; i < tasks.Length; i += ConcurrentTestsCount) { tasks[i + 0] = Task.Run(() => Deserialize()); tasks[i + 1] = Task.Run(() => Serialize()); } ; await Task.WhenAll(tasks); }