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 SimpleObjectWithReflectionMaterializer() { var options = new JsonSerializerOptions(); options.ClassMaterializer = JsonClassMaterializer.Reflection; SimpleTestClass obj = JsonSerializer.Read <SimpleTestClass>(SimpleTestClass.s_data, options); obj.Verify(); }
public static void ReadSimpleClassIgnoresLeadingOrTrailingTrivia(string leadingTrivia, string trailingTrivia) { var options = new JsonSerializerOptions(); options.ReadCommentHandling = JsonCommentHandling.Skip; SimpleTestClass obj = JsonSerializer.Deserialize <SimpleTestClass>(leadingTrivia + SimpleTestClass.s_json + trailingTrivia, options); obj.Verify(); }
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); }
static void VerifyReadNull(SimpleTestClass obj, bool isNull) { if (isNull) { Assert.Null(obj); } else { obj.Verify(); } }
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 static async Task ReadSimpleObjectAsync() { using (MemoryStream stream = new MemoryStream(SimpleTestClass.s_data)) { JsonSerializerOptions options = new JsonSerializerOptions { DefaultBufferSize = 1 }; SimpleTestClass obj = await JsonSerializer.DeserializeAsync <SimpleTestClass>(stream, options); obj.Verify(); } }
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 static async Task ReadSimpleObjectWithTrailingTriviaAsync() { byte[] data = Encoding.UTF8.GetBytes(SimpleTestClass.s_json + " /* Multi\r\nLine Comment */\t"); using (MemoryStream stream = new MemoryStream(data)) { JsonSerializerOptions options = new JsonSerializerOptions { DefaultBufferSize = 1, ReadCommentHandling = JsonCommentHandling.Skip, }; SimpleTestClass obj = await JsonSerializer.DeserializeAsync <SimpleTestClass>(stream, options); obj.Verify(); } }
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 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 static void DictionaryOfClasses() { { IDictionary obj; { string json = @"{""Key1"":" + SimpleTestClass.s_json + @",""Key2"":" + SimpleTestClass.s_json + "}"; obj = JsonSerializer.Deserialize <IDictionary>(json); Assert.Equal(2, obj.Count); if (obj["Key1"] is JsonElement element) { SimpleTestClass result = JsonSerializer.Deserialize <SimpleTestClass>(element.GetRawText()); result.Verify(); } else { ((SimpleTestClass)obj["Key1"]).Verify(); ((SimpleTestClass)obj["Key2"]).Verify(); } } { // We can't compare against the json string above because property ordering is not deterministic (based on reflection order) // so just round-trip the json and compare. string json = JsonSerializer.Serialize(obj); obj = JsonSerializer.Deserialize <IDictionary>(json); Assert.Equal(2, obj.Count); if (obj["Key1"] is JsonElement element) { SimpleTestClass result = JsonSerializer.Deserialize <SimpleTestClass>(element.GetRawText()); result.Verify(); } else { ((SimpleTestClass)obj["Key1"]).Verify(); ((SimpleTestClass)obj["Key2"]).Verify(); } } { string json = JsonSerializer.Serialize <object>(obj); obj = JsonSerializer.Deserialize <IDictionary>(json); Assert.Equal(2, obj.Count); if (obj["Key1"] is JsonElement element) { SimpleTestClass result = JsonSerializer.Deserialize <SimpleTestClass>(element.GetRawText()); result.Verify(); } else { ((SimpleTestClass)obj["Key1"]).Verify(); ((SimpleTestClass)obj["Key2"]).Verify(); } } } { Dictionary <string, SimpleTestClass> obj; { string json = @"{""Key1"":" + SimpleTestClass.s_json + @",""Key2"":" + SimpleTestClass.s_json + "}"; obj = JsonSerializer.Deserialize <Dictionary <string, SimpleTestClass> >(json); Assert.Equal(2, obj.Count); obj["Key1"].Verify(); obj["Key2"].Verify(); } { // We can't compare against the json string above because property ordering is not deterministic (based on reflection order) // so just round-trip the json and compare. string json = JsonSerializer.Serialize(obj); obj = JsonSerializer.Deserialize <Dictionary <string, SimpleTestClass> >(json); Assert.Equal(2, obj.Count); obj["Key1"].Verify(); obj["Key2"].Verify(); } { string json = JsonSerializer.Serialize <object>(obj); obj = JsonSerializer.Deserialize <Dictionary <string, SimpleTestClass> >(json); Assert.Equal(2, obj.Count); obj["Key1"].Verify(); obj["Key2"].Verify(); } } { ImmutableSortedDictionary <string, SimpleTestClass> obj; { string json = @"{""Key1"":" + SimpleTestClass.s_json + @",""Key2"":" + SimpleTestClass.s_json + "}"; obj = JsonSerializer.Deserialize <ImmutableSortedDictionary <string, SimpleTestClass> >(json); Assert.Equal(2, obj.Count); obj["Key1"].Verify(); obj["Key2"].Verify(); } { // We can't compare against the json string above because property ordering is not deterministic (based on reflection order) // so just round-trip the json and compare. string json = JsonSerializer.Serialize(obj); obj = JsonSerializer.Deserialize <ImmutableSortedDictionary <string, SimpleTestClass> >(json); Assert.Equal(2, obj.Count); obj["Key1"].Verify(); obj["Key2"].Verify(); } { string json = JsonSerializer.Serialize <object>(obj); obj = JsonSerializer.Deserialize <ImmutableSortedDictionary <string, SimpleTestClass> >(json); Assert.Equal(2, obj.Count); obj["Key1"].Verify(); obj["Key2"].Verify(); } } }
public static void ReadSimpleClass() { SimpleTestClass obj = JsonSerializer.Deserialize <SimpleTestClass>(SimpleTestClass.s_json); obj.Verify(); }
public static void ReadGenericApi() { SimpleTestClass obj = JsonSerializer.Parse <SimpleTestClass>(SimpleTestClass.s_data); obj.Verify(); }