public async Task JsonPathIsAccurate_CaseInsensitive(string json, string expectedPath) { var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; JsonException ex = await Assert.ThrowsAsync <JsonException>(async() => await JsonSerializerWrapperForString.DeserializeWrapper <KeyValuePair <int, int> >(json, options)); Assert.Contains(expectedPath, ex.ToString()); }
public async Task JsonPathIsAccurate_PropertyNamingPolicy(string json, string expectedPath) { var options = new JsonSerializerOptions { PropertyNamingPolicy = new LeadingUnderscorePolicy() }; JsonException ex = await Assert.ThrowsAsync <JsonException>(async() => await JsonSerializerWrapperForString.DeserializeWrapper <KeyValuePair <int, int> >(json, options)); Assert.Contains(expectedPath, ex.ToString()); }
public async Task ReadClassWithStructIDictionaryWrapper_NullJson_Throws() { string json = @"{ ""Dictionary"" : null }"; await Assert.ThrowsAsync <JsonException>(async() => await JsonSerializerWrapperForString.DeserializeWrapper <ClassWithStructIDictionaryWrapper>(json)); }
public async Task InvalidJsonFail(string json) { await Assert.ThrowsAsync <JsonException>(async() => await JsonSerializerWrapperForString.DeserializeWrapper <KeyValuePair <int, int> >(json)); }
public async Task RandomReferenceMetadataNotSupported() { string json = @"{""Name"":""Jet"",""$random"":10}"; // Baseline, preserve ref feature off. var employee = await JsonSerializerWrapperForString.DeserializeWrapper <Employee>(json); Assert.Equal("Jet", employee.Name); // Metadata not supported with preserve ref feature on. var options = new JsonSerializerOptions { ReferenceHandler = ReferenceHandler.Preserve }; NotSupportedException ex = await Assert.ThrowsAsync <NotSupportedException>(() => JsonSerializerWrapperForString.DeserializeWrapper <Employee>(json, options)); string exStr = ex.ToString(); Assert.Contains("System.Text.Json.Serialization.Tests.ConstructorTests+Employee", exStr); Assert.Contains("$.$random", exStr); }
public async Task PathForChildListFails() { JsonException e = await Assert.ThrowsAsync <JsonException>(() => JsonSerializerWrapperForString.DeserializeWrapper <RootClass>(@"{""Child"":{""MyIntArray"":[1, bad]}")); Assert.Contains("$.Child.MyIntArray", e.Path); }
public Task Class_MultiplePublicParameterizedCtors_NoPublicParameterlessCtor_NoAttribute_NotSupported() { return(Assert.ThrowsAsync <NotSupportedException>(() => JsonSerializerWrapperForString.DeserializeWrapper <MultiplePublicParameterizedCtor>(@"{""MyInt"":1,""MyString"":""1""}"))); }
public async Task PathForSpecialCharacterNestedFails() { JsonException e = await Assert.ThrowsAsync <JsonException>(() => JsonSerializerWrapperForString.DeserializeWrapper <RootClass>(@"{""Child"":{""Children"":[{}, {""MyDictionary"":{""K.e.y"": {""MyInt"":bad")); Assert.Equal("$.Child.Children[1].MyDictionary['K.e.y'].MyInt", e.Path); }
public async Task NonPublicCtors_NotSupported() { async Task RunTestAsync <T>() { NotSupportedException ex = await Assert.ThrowsAsync <NotSupportedException>(() => JsonSerializerWrapperForString.DeserializeWrapper <T>("{}")); Assert.Contains("JsonConstructorAttribute", ex.ToString()); } await RunTestAsync <PrivateParameterlessCtor>(); await RunTestAsync <InternalParameterlessCtor>(); await RunTestAsync <ProtectedParameterlessCtor>(); await RunTestAsync <PrivateParameterizedCtor>(); await RunTestAsync <InternalParameterizedCtor>(); await RunTestAsync <ProtectedParameterizedCtor>(); await RunTestAsync <PrivateParameterizedCtor_WithAttribute>(); await RunTestAsync <InternalParameterizedCtor_WithAttribute>(); await RunTestAsync <ProtectedParameterizedCtor_WithAttribute>(); }
public async Task SinglePublicParameterizedCtor_SingleParameterlessCtor_NoAttribute_Supported_UseParameterlessCtor() { var obj1 = await JsonSerializerWrapperForString.DeserializeWrapper <SinglePublicParameterizedCtor>(@"{""MyInt"":1,""MyString"":""1""}"); Assert.Equal(@"{""MyInt"":0,""MyString"":null}", await JsonSerializerWrapperForString.SerializeWrapper(obj1)); }
public async Task Read_NonGeneric_NoPublicConstructor_Throws(Type type, string json) { NotSupportedException ex = await Assert.ThrowsAsync <NotSupportedException>(async() => await JsonSerializerWrapperForString.DeserializeWrapper(json, type)); Assert.Contains(type.ToString(), ex.Message); }
public async Task ReadSimpleTestClass_NonGenericWrappers_NoAddMethod_Throws(Type type, string json, Type exceptionMessageType) { NotSupportedException ex = await Assert.ThrowsAsync <NotSupportedException>(async() => await JsonSerializerWrapperForString.DeserializeWrapper(json, type)); Assert.Contains(exceptionMessageType.ToString(), ex.Message); }
public async Task ReadSimpleTestClass_StructCollectionWrappers() { SimpleTestClassWithStructCollectionWrappers obj = await JsonSerializerWrapperForString.DeserializeWrapper <SimpleTestClassWithStructCollectionWrappers>(SimpleTestClassWithStructCollectionWrappers.s_json); obj.Verify(); }
public async Task ReadKeyValuePairWithNullValues() { { KeyValuePair <string, string> kvp = await JsonSerializerWrapperForString.DeserializeWrapper <KeyValuePair <string, string> >(@"{""Key"":""key"",""Value"":null}"); Assert.Equal("key", kvp.Key); Assert.Null(kvp.Value); } { KeyValuePair <string, object> kvp = await JsonSerializerWrapperForString.DeserializeWrapper <KeyValuePair <string, object> >(@"{""Key"":""key"",""Value"":null}"); Assert.Equal("key", kvp.Key); Assert.Null(kvp.Value); } { KeyValuePair <string, SimpleClassWithKeyValuePairs> kvp = await JsonSerializerWrapperForString.DeserializeWrapper <KeyValuePair <string, SimpleClassWithKeyValuePairs> >(@"{""Key"":""key"",""Value"":null}"); Assert.Equal("key", kvp.Key); Assert.Null(kvp.Value); } { KeyValuePair <string, KeyValuePair <string, string> > kvp = await JsonSerializerWrapperForString.DeserializeWrapper <KeyValuePair <string, KeyValuePair <string, string> > >(@"{""Key"":""key"",""Value"":{""Key"":""key"",""Value"":null}}"); Assert.Equal("key", kvp.Key); Assert.Equal("key", kvp.Value.Key); Assert.Null(kvp.Value.Value); } { KeyValuePair <string, KeyValuePair <string, object> > kvp = await JsonSerializerWrapperForString.DeserializeWrapper <KeyValuePair <string, KeyValuePair <string, object> > >(@"{""Key"":""key"",""Value"":{""Key"":""key"",""Value"":null}}"); Assert.Equal("key", kvp.Key); Assert.Equal("key", kvp.Value.Key); Assert.Null(kvp.Value.Value); } { KeyValuePair <string, KeyValuePair <string, SimpleClassWithKeyValuePairs> > kvp = await JsonSerializerWrapperForString.DeserializeWrapper <KeyValuePair <string, KeyValuePair <string, SimpleClassWithKeyValuePairs> > >(@"{""Key"":""key"",""Value"":{""Key"":""key"",""Value"":null}}"); Assert.Equal("key", kvp.Key); Assert.Equal("key", kvp.Value.Key); Assert.Null(kvp.Value.Value); } }
public async Task PathForChildListFails(int bufferSize) { JsonSerializerOptions options = new() { DefaultBufferSize = bufferSize }; JsonException e = await Assert.ThrowsAsync <JsonException>(() => JsonSerializerWrapperForString.DeserializeWrapper <RootClass>(PathForChildListFails_Json, options)); Assert.Contains("$.Child.MyIntArray", e.Path); }
public async Task JsonNameConflictOnCaseInsensitiveFail() { string json = @"{""myInt"":1,""MyInt"":2}"; { var options = new JsonSerializerOptions(); options.PropertyNameCaseInsensitive = true; await Assert.ThrowsAsync <InvalidOperationException>(async() => await JsonSerializerWrapperForString.DeserializeWrapper <IntPropertyNamesDifferentByCaseOnly_TestClass>(json, options)); await Assert.ThrowsAsync <InvalidOperationException>(async() => await JsonSerializerWrapperForString.SerializeWrapper(new IntPropertyNamesDifferentByCaseOnly_TestClass(), options)); } }
public async Task PathForChildDictionaryFails(int bufferSize) { JsonSerializerOptions options = new() { DefaultBufferSize = bufferSize }; JsonException e = await Assert.ThrowsAsync <JsonException>(() => JsonSerializerWrapperForString.DeserializeWrapper <RootClass>(PathForChildDictionaryFails_Json, options)); Assert.Equal("$.Child.MyDictionary.Key", e.Path); }
public async Task EscapingFails() { JsonException e = await Assert.ThrowsAsync <JsonException>(() => JsonSerializerWrapperForString.DeserializeWrapper <Parameterized_ClassWithUnicodeProperty>("{\"A\u0467\":bad}")); Assert.Equal("$.A\u0467", e.Path); }
public async Task MultipleThreads() { // Verify the test class has >32 properties since that is a threshold for using the fallback dictionary. Assert.True(typeof(ClassWithConstructor_SimpleAndComplexParameters).GetProperties(BindingFlags.Instance | BindingFlags.Public).Length > 32); async Task DeserializeObjectAsync(string json, Type type, JsonSerializerOptions options) { var obj = await JsonSerializerWrapperForString.DeserializeWrapper(json, type, options); ((ITestClassWithParameterizedCtor)obj).Verify(); } async Task DeserializeObjectMinimalAsync(Type type, JsonSerializerOptions options) { string json = (string)type.GetProperty("s_json_minimal").GetValue(null); var obj = await JsonSerializerWrapperForString.DeserializeWrapper(json, type, options); ((ITestClassWithParameterizedCtor)obj).VerifyMinimal(); }; async Task DeserializeObjectFlippedAsync(Type type, JsonSerializerOptions options) { string json = (string)type.GetProperty("s_json_flipped").GetValue(null); await DeserializeObjectAsync(json, type, options); }; async Task DeserializeObjectNormalAsync(Type type, JsonSerializerOptions options) { string json = (string)type.GetProperty("s_json").GetValue(null); await DeserializeObjectAsync(json, type, options); }; async Task SerializeObject(Type type, JsonSerializerOptions options) { var obj = ClassWithConstructor_SimpleAndComplexParameters.GetInstance(); await JsonSerializerWrapperForString.SerializeWrapper(obj, options); }; async Task RunTestAsync(Type type) { // Use local options to avoid obtaining already cached metadata from the default options. var options = new JsonSerializerOptions(); 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(() => DeserializeObjectMinimalAsync(type, options)); tasks[i + 1] = Task.Run(() => DeserializeObjectFlippedAsync(type, options)); tasks[i + 2] = Task.Run(() => DeserializeObjectNormalAsync(type, options)); // Ensure no exceptions on serialization tasks[i + 3] = Task.Run(() => SerializeObject(type, options)); } ; await Task.WhenAll(tasks); } await RunTestAsync(typeof(ClassWithConstructor_SimpleAndComplexParameters)); await RunTestAsync(typeof(Person_Class)); await RunTestAsync(typeof(Parameterized_Class_With_ComplexTuple)); }
public async Task ExtensionDataProperty_CannotBindTo_CtorParam() { InvalidOperationException ex = await Assert.ThrowsAsync <InvalidOperationException>(() => JsonSerializerWrapperForString.DeserializeWrapper <Class_ExtData_CtorParam>("{}")); string exStr = ex.ToString(); // System.InvalidOperationException: 'The extension data property 'System.Collections.Generic.Dictionary`2[System.String,System.Text.Json.JsonElement] ExtensionData' on type 'System.Text.Json.Serialization.Tests.ConstructorTests+Class_ExtData_CtorParam' cannot bind with a parameter in constructor 'Void .ctor(System.Collections.Generic.Dictionary`2[System.String,System.Text.Json.JsonElement])'.' Assert.Contains("System.Collections.Generic.Dictionary`2[System.String,System.Text.Json.JsonElement] ExtensionData", exStr); Assert.Contains("System.Text.Json.Serialization.Tests.ConstructorTests+Class_ExtData_CtorParam", exStr); }
public async Task InvalidPropertyNameFail(Type policyType, string offendingProperty) { var options = new JsonSerializerOptions { PropertyNamingPolicy = (JsonNamingPolicy)Activator.CreateInstance(policyType) }; InvalidOperationException ex = await Assert.ThrowsAsync <InvalidOperationException>(async() => await JsonSerializerWrapperForString.DeserializeWrapper <KeyValuePair <string, string> >("{}", options)); string exAsStr = ex.ToString(); Assert.Contains(offendingProperty, exAsStr); await Assert.ThrowsAsync <InvalidOperationException>(async() => await JsonSerializerWrapperForString.SerializeWrapper(new KeyValuePair <string, string>("", ""), options)); }
public async Task PathForChildDictionaryFails() { JsonException e = await Assert.ThrowsAsync <JsonException>(() => JsonSerializerWrapperForString.DeserializeWrapper <RootClass>(@"{""Child"":{""MyDictionary"":{""Key"": bad]")); Assert.Equal("$.Child.MyDictionary.Key", e.Path); }
public async Task ReadNullableStructIDictionaryWithNullJson() { var wrapper = await JsonSerializerWrapperForString.DeserializeWrapper <StructWrapperForIDictionary?>("null"); Assert.False(wrapper.HasValue); }