diff --git a/Src/Newtonsoft.Json.Tests/Issues/Issue1725.cs b/Src/Newtonsoft.Json.Tests/Issues/Issue1725.cs new file mode 100644 index 00000000..8a8a7f58 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Issues/Issue1725.cs @@ -0,0 +1,131 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Reflection; +using System.Reflection.Emit; +using System.Runtime.Serialization; +using Newtonsoft.Json.Converters; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json.Serialization; +using Newtonsoft.Json.Utilities; +#if DNXCORE50 +using Xunit; +using Test = Xunit.FactAttribute; +using Assert = Newtonsoft.Json.Tests.XUnitAssert; +#else +using NUnit.Framework; +#endif + +namespace Newtonsoft.Json.Tests.Issues +{ + [TestFixture] + public class Issue1725 : TestFixtureBase + { + [Test] + public void Test_In() + { + var p1 = new InPerson("some name"); + string json = JsonConvert.SerializeObject(p1); + + var p2 = JsonConvert.DeserializeObject(json); + Assert.AreEqual("some name", p2.Name); + } + + [Test] + public void Test_Ref() + { + string value = "some name"; + var p1 = new RefPerson(ref value); + string json = JsonConvert.SerializeObject(p1); + + var p2 = JsonConvert.DeserializeObject(json); + Assert.AreEqual("some name", p2.Name); + } + + [Test] + public void Test_InNullable() + { + var p1 = new InNullablePerson(1); + string json = JsonConvert.SerializeObject(p1); + + var p2 = JsonConvert.DeserializeObject(json); + Assert.AreEqual(1, p2.Age); + } + + [Test] + public void Test_RefNullable() + { + int? value = 1; + var p1 = new RefNullablePerson(ref value); + string json = JsonConvert.SerializeObject(p1); + + var p2 = JsonConvert.DeserializeObject(json); + Assert.AreEqual(1, p2.Age); + } + + public class InPerson + { + public InPerson(in string name) + { + Name = name; + } + + public string Name { get; } + } + + public class RefPerson + { + public RefPerson(ref string name) + { + Name = name; + } + + public string Name { get; } + } + + public class InNullablePerson + { + public InNullablePerson(in int? age) + { + Age = age; + } + + public int? Age { get; } + } + + public class RefNullablePerson + { + public RefNullablePerson(ref int? age) + { + Age = age; + } + + public int? Age { get; } + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Utilities/DynamicReflectionDelegateFactoryTests.cs b/Src/Newtonsoft.Json.Tests/Utilities/DynamicReflectionDelegateFactoryTests.cs index 0d8ce1d6..cfbfc43c 100644 --- a/Src/Newtonsoft.Json.Tests/Utilities/DynamicReflectionDelegateFactoryTests.cs +++ b/Src/Newtonsoft.Json.Tests/Utilities/DynamicReflectionDelegateFactoryTests.cs @@ -50,6 +50,33 @@ namespace Newtonsoft.Json.Tests.Utilities [TestFixture] public class DynamicReflectionDelegateFactoryTests : TestFixtureBase { + [Test] + public void ConstructorWithInString() + { + ConstructorInfo constructor = TestReflectionUtils.GetConstructors(typeof(InTestClass)).Single(c => c.GetParameters().Count() == 1); + + var creator = DynamicReflectionDelegateFactory.Instance.CreateParameterizedConstructor(constructor); + + object[] args = new object[] { "Value" }; + InTestClass o = (InTestClass)creator(args); + Assert.IsNotNull(o); + Assert.AreEqual("Value", o.Value); + } + + [Test] + public void ConstructorWithInStringAndBool() + { + ConstructorInfo constructor = TestReflectionUtils.GetConstructors(typeof(InTestClass)).Single(c => c.GetParameters().Count() == 2); + + var creator = DynamicReflectionDelegateFactory.Instance.CreateParameterizedConstructor(constructor); + + object[] args = new object[] { "Value", true }; + InTestClass o = (InTestClass)creator(args); + Assert.IsNotNull(o); + Assert.AreEqual("Value", o.Value); + Assert.AreEqual(true, o.B1); + } + [Test] public void ConstructorWithRefString() { diff --git a/Src/Newtonsoft.Json.Tests/Utilities/ExpressionReflectionDelegateFactoryTests.cs b/Src/Newtonsoft.Json.Tests/Utilities/ExpressionReflectionDelegateFactoryTests.cs index 7d3cacc7..ef6e561d 100644 --- a/Src/Newtonsoft.Json.Tests/Utilities/ExpressionReflectionDelegateFactoryTests.cs +++ b/Src/Newtonsoft.Json.Tests/Utilities/ExpressionReflectionDelegateFactoryTests.cs @@ -47,6 +47,33 @@ namespace Newtonsoft.Json.Tests.Utilities [TestFixture] public class ExpressionReflectionDelegateFactoryTests : TestFixtureBase { + [Test] + public void ConstructorWithInString() + { + ConstructorInfo constructor = TestReflectionUtils.GetConstructors(typeof(InTestClass)).Single(c => c.GetParameters().Count() == 1); + + var creator = ExpressionReflectionDelegateFactory.Instance.CreateParameterizedConstructor(constructor); + + object[] args = new object[] { "Value" }; + InTestClass o = (InTestClass)creator(args); + Assert.IsNotNull(o); + Assert.AreEqual("Value", o.Value); + } + + [Test] + public void ConstructorWithInStringAndBool() + { + ConstructorInfo constructor = TestReflectionUtils.GetConstructors(typeof(InTestClass)).Single(c => c.GetParameters().Count() == 2); + + var creator = ExpressionReflectionDelegateFactory.Instance.CreateParameterizedConstructor(constructor); + + object[] args = new object[] { "Value", true }; + InTestClass o = (InTestClass)creator(args); + Assert.IsNotNull(o); + Assert.AreEqual("Value", o.Value); + Assert.AreEqual(true, o.B1); + } + [Test] public void ConstructorWithRefString() { diff --git a/Src/Newtonsoft.Json.Tests/Utilities/LateboundReflectionDelegateFactoryTests.cs b/Src/Newtonsoft.Json.Tests/Utilities/LateboundReflectionDelegateFactoryTests.cs index cedde63d..ab2689ab 100644 --- a/Src/Newtonsoft.Json.Tests/Utilities/LateboundReflectionDelegateFactoryTests.cs +++ b/Src/Newtonsoft.Json.Tests/Utilities/LateboundReflectionDelegateFactoryTests.cs @@ -44,9 +44,53 @@ namespace Newtonsoft.Json.Tests.Utilities } } + public class InTestClass + { + public string Value { get; } + public bool B1 { get; } + + public InTestClass(in string value) + { + Value = value; + } + + public InTestClass(in string value, in bool b1) + : this(in value) + { + B1 = b1; + } + } + [TestFixture] public class LateboundReflectionDelegateFactoryTests : TestFixtureBase { + [Test] + public void ConstructorWithInString() + { + ConstructorInfo constructor = TestReflectionUtils.GetConstructors(typeof(InTestClass)).Single(c => c.GetParameters().Count() == 1); + + var creator = LateBoundReflectionDelegateFactory.Instance.CreateParameterizedConstructor(constructor); + + object[] args = new object[] { "Value" }; + InTestClass o = (InTestClass)creator(args); + Assert.IsNotNull(o); + Assert.AreEqual("Value", o.Value); + } + + [Test] + public void ConstructorWithInStringAndBool() + { + ConstructorInfo constructor = TestReflectionUtils.GetConstructors(typeof(InTestClass)).Single(c => c.GetParameters().Count() == 2); + + var creator = LateBoundReflectionDelegateFactory.Instance.CreateParameterizedConstructor(constructor); + + object[] args = new object[] { "Value", true }; + InTestClass o = (InTestClass)creator(args); + Assert.IsNotNull(o); + Assert.AreEqual("Value", o.Value); + Assert.AreEqual(true, o.B1); + } + [Test] public void ConstructorWithRefString() { diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs index de0c07d9..ba0ef8b6 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs @@ -1156,12 +1156,14 @@ namespace Newtonsoft.Json.Serialization /// A for the given type. protected virtual JsonContract CreateContract(Type objectType) { - if (IsJsonPrimitiveType(objectType)) + Type t = ReflectionUtils.EnsureNotByRefType(objectType); + + if (IsJsonPrimitiveType(t)) { return CreatePrimitiveContract(objectType); } - Type t = ReflectionUtils.EnsureNotNullableType(objectType); + t = ReflectionUtils.EnsureNotNullableType(t); JsonContainerAttribute containerAttribute = JsonTypeReflector.GetCachedAttribute(t); if (containerAttribute is JsonObjectAttribute) diff --git a/Src/Newtonsoft.Json/Serialization/JsonContract.cs b/Src/Newtonsoft.Json/Serialization/JsonContract.cs index ddac8cc2..c19d5d2a 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonContract.cs @@ -238,7 +238,12 @@ namespace Newtonsoft.Json.Serialization UnderlyingType = underlyingType; + // resolve ByRef types + // typically comes from in and ref parameters on methods/ctors + underlyingType = ReflectionUtils.EnsureNotByRefType(underlyingType); + IsNullable = ReflectionUtils.IsNullable(underlyingType); + NonNullableUnderlyingType = (IsNullable && ReflectionUtils.IsNullableType(underlyingType)) ? Nullable.GetUnderlyingType(underlyingType) : underlyingType; CreatedType = NonNullableUnderlyingType; diff --git a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs index c9139c37..77b9aa99 100644 --- a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs @@ -275,6 +275,13 @@ namespace Newtonsoft.Json.Utilities : t; } + public static Type EnsureNotByRefType(Type t) + { + return (t.IsByRef && t.HasElementType) + ? t.GetElementType() + : t; + } + public static bool IsGenericDefinition(Type type, Type genericInterfaceDefinition) { if (!type.IsGenericType())