public static async Task JsonSerializerOptionsUpdateHandler_ClearingDoesntPreventSerialization() { // This test uses reflection to: // - Access JsonSerializerOptions._classes // - Access JsonSerializerOptionsUpdateHandler.ClearCache // // If either of them changes, this test will need to be kept in sync. var options = new JsonSerializerOptions(); FieldInfo classesField = options.GetType().GetField("_classes", BindingFlags.NonPublic | BindingFlags.Instance); Assert.NotNull(classesField); IDictionary classes = (IDictionary)classesField.GetValue(options); Assert.Equal(0, classes.Count); SimpleTestClass testObj = new SimpleTestClass(); testObj.Initialize(); await JsonSerializer.SerializeAsync <SimpleTestClass>(new MemoryStream(), testObj, options); Assert.NotEqual(0, classes.Count); Type updateHandler = typeof(JsonSerializerOptions).Assembly.GetType("System.Text.Json.JsonSerializerOptionsUpdateHandler", throwOnError: true, ignoreCase: false); MethodInfo clearCache = updateHandler.GetMethod("ClearCache"); Assert.NotNull(clearCache); clearCache.Invoke(null, new object[] { null }); Assert.Equal(0, classes.Count); await JsonSerializer.SerializeAsync <SimpleTestClass>(new MemoryStream(), testObj, options); Assert.NotEqual(0, classes.Count); }
public static void JsonSerializerOptionsUpdateHandler_ClearingDoesntPreventSerialization() { // This test uses reflection to: // - Access JsonSerializerOptions._cachingContext.Count // - Access JsonSerializerOptionsUpdateHandler.ClearCache // // If either of them changes, this test will need to be kept in sync. RemoteExecutor.Invoke(() => { var options = new JsonSerializerOptions(); Func <JsonSerializerOptions, int> getCount = CreateCacheCountAccessor(); Assert.Equal(0, getCount(options)); SimpleTestClass testObj = new SimpleTestClass(); testObj.Initialize(); JsonSerializer.Serialize <SimpleTestClass>(testObj, options); Assert.NotEqual(0, getCount(options)); Type updateHandler = typeof(JsonSerializerOptions).Assembly.GetType("System.Text.Json.JsonSerializerOptionsUpdateHandler", throwOnError: true, ignoreCase: false); MethodInfo clearCache = updateHandler.GetMethod("ClearCache"); Assert.NotNull(clearCache); clearCache.Invoke(null, new object[] { null }); Assert.Equal(0, getCount(options)); JsonSerializer.Serialize <SimpleTestClass>(testObj, options); Assert.NotEqual(0, getCount(options)); }).Dispose();
public static void MultipleThreads_SameType_DifferentJson() { // Use local options to avoid obtaining already cached metadata from the default options. var options = new JsonSerializerOptions(); // Verify the test class has >64 properties since that is a threshold for using the fallback dictionary. Assert.True(typeof(SimpleTestClass).GetProperties(BindingFlags.Instance | BindingFlags.Public).Length > 64); void DeserializeObjectMinimal() { SimpleTestClass obj = JsonSerializer.Deserialize <SimpleTestClass>(@"{""MyDecimal"" : 3.3}", options); }; void DeserializeObjectFlipped() { SimpleTestClass obj = JsonSerializer.Deserialize <SimpleTestClass>(SimpleTestClass.s_json_flipped, options); obj.Verify(); }; void DeserializeObjectNormal() { SimpleTestClass obj = JsonSerializer.Deserialize <SimpleTestClass>(SimpleTestClass.s_json, options); obj.Verify(); }; void SerializeObject() { var obj = new SimpleTestClass(); obj.Initialize(); JsonSerializer.Serialize(obj, options); }; const int ThreadCount = 8; const int ConcurrentTestsCount = 4; Task[] tasks = new Task[ThreadCount * ConcurrentTestsCount]; for (int i = 0; i < tasks.Length; i += ConcurrentTestsCount) { // Create race condition to populate the sorted property cache with different json ordering. tasks[i + 0] = Task.Run(() => DeserializeObjectMinimal()); tasks[i + 1] = Task.Run(() => DeserializeObjectFlipped()); tasks[i + 2] = Task.Run(() => DeserializeObjectNormal()); // Ensure no exceptions on serialization tasks[i + 3] = Task.Run(() => SerializeObject()); } ; Task.WaitAll(tasks); }
public void Initialize() { SimpleTestClass obj1 = new SimpleTestClass(); obj1.Initialize(); SimpleTestClass obj2 = new SimpleTestClass(); obj2.Initialize(); MyData = new SimpleTestClass[] { obj1, obj2 }; }
static byte[] GenerateJsonArray(int count) { SimpleTestClass[] collection = new SimpleTestClass[count]; for (int i = 0; i < collection.Length; i++) { var obj = new SimpleTestClass(); obj.Initialize(); obj.MyInt32 = i; // verify order correctness collection[i] = obj; } return(JsonSerializer.SerializeToUtf8Bytes(collection)); }
public void Initialize() { MyStack = new Stack <SimpleTestClass>(); { SimpleTestClass obj = new SimpleTestClass(); obj.Initialize(); MyStack.Push(obj); } { SimpleTestClass obj = new SimpleTestClass(); obj.Initialize(); MyStack.Push(obj); } MyQueue = new Queue <SimpleTestClass>(); { SimpleTestClass obj = new SimpleTestClass(); obj.Initialize(); MyQueue.Enqueue(obj); } { SimpleTestClass obj = new SimpleTestClass(); obj.Initialize(); MyQueue.Enqueue(obj); } MyHashSet = new HashSet <SimpleTestClass>(); { SimpleTestClass obj = new SimpleTestClass(); obj.Initialize(); MyHashSet.Add(obj); } { SimpleTestClass obj = new SimpleTestClass(); obj.Initialize(); MyHashSet.Add(obj); } MyLinkedList = new LinkedList <SimpleTestClass>(); { SimpleTestClass obj = new SimpleTestClass(); obj.Initialize(); MyLinkedList.AddLast(obj); } { SimpleTestClass obj = new SimpleTestClass(); obj.Initialize(); MyLinkedList.AddLast(obj); } }
public static void ReadSimpleTestClassWithSimpleTestStruct() { SimpleTestClass testObject = new SimpleTestClass(); testObject.Initialize(); testObject.MySimpleTestStruct = new SimpleTestStruct { MyInt64 = 64, MyString = "Hello", MyInt32Array = new int[] { 32 } }; string json = JsonSerializer.ToString(testObject); SimpleTestClass parsedObject = JsonSerializer.Parse <SimpleTestClass>(json); parsedObject.Verify(); }
public void Initialize() { MyData = new List <SimpleTestClass>(); { SimpleTestClass obj = new SimpleTestClass(); obj.Initialize(); MyData.Add(obj); } { SimpleTestClass obj = new SimpleTestClass(); obj.Initialize(); MyData.Add(obj); } }
public static void ReadSimpleTestClassWithSimpleTestStruct() { SimpleTestClass testObject = new SimpleTestClass(); testObject.Initialize(); testObject.MySimpleTestStruct = new SimpleTestStruct { MyInt64 = 64, MyString = "Hello", MyInt32Array = new int[] { 32 } }; string json = JsonSerializer.Serialize(testObject); SimpleTestClass parsedObject = JsonSerializer.Deserialize <SimpleTestClass>(json); parsedObject.Verify(); Assert.Equal(64, parsedObject.MySimpleTestStruct.MyInt64); Assert.Equal("Hello", parsedObject.MySimpleTestStruct.MyString); Assert.Equal(32, parsedObject.MySimpleTestStruct.MyInt32Array[0]); }
public static void PropertyCacheWithMinInputsLast() { // Use local options to avoid obtaining already cached metadata from the default options. var options = new JsonSerializerOptions(); SimpleTestClass testObj = new SimpleTestClass(); testObj.Initialize(); testObj.Verify(); string json = JsonSerializer.Serialize(testObj, options); testObj = JsonSerializer.Deserialize <SimpleTestClass>(json, options); testObj.Verify(); json = "{}"; JsonSerializer.Deserialize <SimpleTestClass>(json, options); }
public static void PropertyCacheWithMinInputsLast() { // Use localized caching // Todo: localized caching not implemented yet. When implemented, add a run-time attribute to JsonSerializerOptions as that will create a separate cache held by JsonSerializerOptions. var options = new JsonSerializerOptions(); SimpleTestClass testObj = new SimpleTestClass(); testObj.Initialize(); testObj.Verify(); string json = JsonSerializer.ToString(testObj, options); testObj = JsonSerializer.Parse <SimpleTestClass>(json, options); testObj.Verify(); json = "{}"; JsonSerializer.Parse <SimpleTestClass>(json, options); }
public static void MultipleThreads() { // Ensure no exceptions are thrown due to caching or other issues. void SerializeAndDeserializeObject(bool useEmptyJson) { // Use localized caching // Todo: localized caching not implemented yet. When implemented, add a run-time attribute to JsonSerializerOptions as that will create a separate cache held by JsonSerializerOptions. var options = new JsonSerializerOptions(); string json; if (useEmptyJson) { json = "{}"; } else { SimpleTestClass testObj = new SimpleTestClass(); testObj.Initialize(); testObj.Verify(); json = JsonSerializer.ToString(testObj, options); } SimpleTestClass testObjDeserialized = JsonSerializer.Parse <SimpleTestClass>(json, options); testObjDeserialized.Verify(); }; Task[] tasks = new Task[8]; bool useEmptyJson = false; for (int i = 0; i < tasks.Length; i++) { tasks[i] = Task.Run(() => SerializeAndDeserializeObject(useEmptyJson)); useEmptyJson = !useEmptyJson; } ; Task.WaitAll(tasks); }
public void Initialize() { MyData = new SimpleTestClass(); MyData.Initialize(); }
public void Initialize() { { SimpleTestClass obj1 = new SimpleTestClass(); obj1.Initialize(); SimpleTestClass obj2 = new SimpleTestClass(); obj2.Initialize(); MyIImmutableList = ImmutableList.CreateRange(new List <SimpleTestClass> { obj1, obj2 }); } { SimpleTestClass obj1 = new SimpleTestClass(); obj1.Initialize(); SimpleTestClass obj2 = new SimpleTestClass(); obj2.Initialize(); MyIImmutableStack = ImmutableStack.CreateRange(new List <SimpleTestClass> { obj1, obj2 }); } { SimpleTestClass obj1 = new SimpleTestClass(); obj1.Initialize(); SimpleTestClass obj2 = new SimpleTestClass(); obj2.Initialize(); MyIImmutableQueue = ImmutableQueue.CreateRange(new List <SimpleTestClass> { obj1, obj2 }); } { SimpleTestClass obj1 = new SimpleTestClass(); obj1.Initialize(); SimpleTestClass obj2 = new SimpleTestClass(); obj2.Initialize(); MyIImmutableSet = ImmutableHashSet.CreateRange(new List <SimpleTestClass> { obj1, obj2 }); } { SimpleTestClass obj1 = new SimpleTestClass(); obj1.Initialize(); SimpleTestClass obj2 = new SimpleTestClass(); obj2.Initialize(); MyImmutableHashSet = ImmutableHashSet.CreateRange(new List <SimpleTestClass> { obj1, obj2 }); } { SimpleTestClass obj1 = new SimpleTestClass(); obj1.Initialize(); SimpleTestClass obj2 = new SimpleTestClass(); obj2.Initialize(); MyImmutableList = ImmutableList.CreateRange(new List <SimpleTestClass> { obj1, obj2 }); } { SimpleTestClass obj1 = new SimpleTestClass(); obj1.Initialize(); SimpleTestClass obj2 = new SimpleTestClass(); obj2.Initialize(); MyImmutableStack = ImmutableStack.CreateRange(new List <SimpleTestClass> { obj1, obj2 }); } { SimpleTestClass obj1 = new SimpleTestClass(); obj1.Initialize(); SimpleTestClass obj2 = new SimpleTestClass(); obj2.Initialize(); MyImmutableQueue = ImmutableQueue.CreateRange(new List <SimpleTestClass> { obj1, obj2 }); } }