diff --git a/Src/Newtonsoft.Json.Tests/Issues/Issue1322.cs b/Src/Newtonsoft.Json.Tests/Issues/Issue1322.cs new file mode 100644 index 00000000..ba6a6f31 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Issues/Issue1322.cs @@ -0,0 +1,61 @@ +#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.Generic; +using System.Linq; +using System.Text; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +#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 Issue1322 : TestFixtureBase + { + [Test] + public void Test() + { + IList> values = new List> + { + new KeyValuePair("123", "2017-05-19T11:00:59") + }; + + string json = JsonConvert.SerializeObject(values, Formatting.Indented); + + IList> v1 = JsonConvert.DeserializeObject>>(json); + + Assert.AreEqual("123", v1[0].Key); + Assert.AreEqual("2017-05-19T11:00:59", v1[0].Value); + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Converters/KeyValuePairConverter.cs b/Src/Newtonsoft.Json/Converters/KeyValuePairConverter.cs index 71f5fbb0..2d419703 100644 --- a/Src/Newtonsoft.Json/Converters/KeyValuePairConverter.cs +++ b/Src/Newtonsoft.Json/Converters/KeyValuePairConverter.cs @@ -100,19 +100,23 @@ namespace Newtonsoft.Json.Converters : objectType; ReflectionObject reflectionObject = ReflectionObjectPerType.Get(t); + JsonContract keyContract = serializer.ContractResolver.ResolveContract(reflectionObject.GetType(KeyName)); + JsonContract valueContract = serializer.ContractResolver.ResolveContract(reflectionObject.GetType(ValueName)); while (reader.TokenType == JsonToken.PropertyName) { string propertyName = reader.Value.ToString(); if (string.Equals(propertyName, KeyName, StringComparison.OrdinalIgnoreCase)) { - reader.ReadAndAssert(); - key = serializer.Deserialize(reader, reflectionObject.GetType(KeyName)); + reader.ReadForTypeAndAssert(keyContract, false); + + key = serializer.Deserialize(reader, keyContract.UnderlyingType); } else if (string.Equals(propertyName, ValueName, StringComparison.OrdinalIgnoreCase)) { - reader.ReadAndAssert(); - value = serializer.Deserialize(reader, reflectionObject.GetType(ValueName)); + reader.ReadForTypeAndAssert(valueContract, false); + + value = serializer.Deserialize(reader, valueContract.UnderlyingType); } else { diff --git a/Src/Newtonsoft.Json/JsonReader.cs b/Src/Newtonsoft.Json/JsonReader.cs index 2f771e87..95f08757 100644 --- a/Src/Newtonsoft.Json/JsonReader.cs +++ b/Src/Newtonsoft.Json/JsonReader.cs @@ -1164,6 +1164,62 @@ namespace Newtonsoft.Json } } + internal void ReadForTypeAndAssert(JsonContract contract, bool hasConverter) + { + if (!ReadForType(contract, hasConverter)) + { + throw JsonSerializationException.Create(this, "Unexpected end when reading JSON."); + } + } + + internal bool ReadForType(JsonContract contract, bool hasConverter) + { + // don't read properties with converters as a specific value + // the value might be a string which will then get converted which will error if read as date for example + if (hasConverter) + { + return Read(); + } + + ReadType t = (contract != null) ? contract.InternalReadType : ReadType.Read; + + switch (t) + { + case ReadType.Read: + return ReadAndMoveToContent(); + case ReadType.ReadAsInt32: + ReadAsInt32(); + break; + case ReadType.ReadAsDecimal: + ReadAsDecimal(); + break; + case ReadType.ReadAsDouble: + ReadAsDouble(); + break; + case ReadType.ReadAsBytes: + ReadAsBytes(); + break; + case ReadType.ReadAsBoolean: + ReadAsBoolean(); + break; + case ReadType.ReadAsString: + ReadAsString(); + break; + case ReadType.ReadAsDateTime: + ReadAsDateTime(); + break; +#if HAVE_DATE_TIME_OFFSET + case ReadType.ReadAsDateTimeOffset: + ReadAsDateTimeOffset(); + break; +#endif + default: + throw new ArgumentOutOfRangeException(); + } + + return (TokenType != JsonToken.None); + } + internal bool ReadAndMoveToContent() { return Read() && MoveToContent(); diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index 9cb8f7d2..5d5d1ca0 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -146,7 +146,7 @@ namespace Newtonsoft.Json.Serialization { JsonConverter converter = GetConverter(contract, null, null, null); - if (reader.TokenType == JsonToken.None && !ReadForType(reader, contract, converter != null)) + if (reader.TokenType == JsonToken.None && !reader.ReadForType(contract, converter != null)) { if (contract != null && !contract.IsNullable) { @@ -1385,7 +1385,7 @@ namespace Newtonsoft.Json.Serialization throw JsonSerializationException.Create(reader, "Could not convert string '{0}' to dictionary key type '{1}'. Create a TypeConverter to convert from the string to the key type object.".FormatWith(CultureInfo.InvariantCulture, reader.Value, contract.DictionaryKeyType), ex); } - if (!ReadForType(reader, contract.ItemContract, dictionaryValueConverter != null)) + if (!reader.ReadForType(contract.ItemContract, dictionaryValueConverter != null)) { throw JsonSerializationException.Create(reader, "Unexpected end when deserializing object."); } @@ -1461,7 +1461,7 @@ namespace Newtonsoft.Json.Serialization { try { - if (ReadForType(reader, collectionItemContract, collectionItemConverter != null)) + if (reader.ReadForType(collectionItemContract, collectionItemConverter != null)) { switch (reader.TokenType) { @@ -1616,7 +1616,7 @@ namespace Newtonsoft.Json.Serialization { try { - if (ReadForType(reader, contract.ItemContract, collectionItemConverter != null)) + if (reader.ReadForType(contract.ItemContract, collectionItemConverter != null)) { switch (reader.TokenType) { @@ -2162,7 +2162,7 @@ namespace Newtonsoft.Json.Serialization JsonConverter propertyConverter = GetConverter(property.PropertyContract, property.MemberConverter, contract, containerProperty); - if (!ReadForType(reader, property.PropertyContract, propertyConverter != null)) + if (!reader.ReadForType(property.PropertyContract, propertyConverter != null)) { throw JsonSerializationException.Create(reader, "Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName)); } @@ -2223,54 +2223,6 @@ namespace Newtonsoft.Json.Serialization return propertyValues; } - private bool ReadForType(JsonReader reader, JsonContract contract, bool hasConverter) - { - // don't read properties with converters as a specific value - // the value might be a string which will then get converted which will error if read as date for example - if (hasConverter) - { - return reader.Read(); - } - - ReadType t = (contract != null) ? contract.InternalReadType : ReadType.Read; - - switch (t) - { - case ReadType.Read: - return reader.ReadAndMoveToContent(); - case ReadType.ReadAsInt32: - reader.ReadAsInt32(); - break; - case ReadType.ReadAsDecimal: - reader.ReadAsDecimal(); - break; - case ReadType.ReadAsDouble: - reader.ReadAsDouble(); - break; - case ReadType.ReadAsBytes: - reader.ReadAsBytes(); - break; - case ReadType.ReadAsBoolean: - reader.ReadAsBoolean(); - break; - case ReadType.ReadAsString: - reader.ReadAsString(); - break; - case ReadType.ReadAsDateTime: - reader.ReadAsDateTime(); - break; -#if HAVE_DATE_TIME_OFFSET - case ReadType.ReadAsDateTimeOffset: - reader.ReadAsDateTimeOffset(); - break; -#endif - default: - throw new ArgumentOutOfRangeException(); - } - - return (reader.TokenType != JsonToken.None); - } - public object CreateNewObject(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMember, JsonProperty containerProperty, string id, out bool createdFromNonDefaultCreator) { object newObject = null; @@ -2390,7 +2342,7 @@ namespace Newtonsoft.Json.Serialization JsonConverter propertyConverter = GetConverter(property.PropertyContract, property.MemberConverter, contract, member); - if (!ReadForType(reader, property.PropertyContract, propertyConverter != null)) + if (!reader.ReadForType(property.PropertyContract, propertyConverter != null)) { throw JsonSerializationException.Create(reader, "Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName)); }