-Fixed reading some types from KeyValuePairConverter incorrectly
This commit is contained in:
@@ -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<KeyValuePair<string, string>> values = new List<KeyValuePair<string, string>>
|
||||||
|
{
|
||||||
|
new KeyValuePair<string, string>("123", "2017-05-19T11:00:59")
|
||||||
|
};
|
||||||
|
|
||||||
|
string json = JsonConvert.SerializeObject(values, Formatting.Indented);
|
||||||
|
|
||||||
|
IList<KeyValuePair<string, string>> v1 = JsonConvert.DeserializeObject<IList<KeyValuePair<string, string>>>(json);
|
||||||
|
|
||||||
|
Assert.AreEqual("123", v1[0].Key);
|
||||||
|
Assert.AreEqual("2017-05-19T11:00:59", v1[0].Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -100,19 +100,23 @@ namespace Newtonsoft.Json.Converters
|
|||||||
: objectType;
|
: objectType;
|
||||||
|
|
||||||
ReflectionObject reflectionObject = ReflectionObjectPerType.Get(t);
|
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)
|
while (reader.TokenType == JsonToken.PropertyName)
|
||||||
{
|
{
|
||||||
string propertyName = reader.Value.ToString();
|
string propertyName = reader.Value.ToString();
|
||||||
if (string.Equals(propertyName, KeyName, StringComparison.OrdinalIgnoreCase))
|
if (string.Equals(propertyName, KeyName, StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
reader.ReadAndAssert();
|
reader.ReadForTypeAndAssert(keyContract, false);
|
||||||
key = serializer.Deserialize(reader, reflectionObject.GetType(KeyName));
|
|
||||||
|
key = serializer.Deserialize(reader, keyContract.UnderlyingType);
|
||||||
}
|
}
|
||||||
else if (string.Equals(propertyName, ValueName, StringComparison.OrdinalIgnoreCase))
|
else if (string.Equals(propertyName, ValueName, StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
reader.ReadAndAssert();
|
reader.ReadForTypeAndAssert(valueContract, false);
|
||||||
value = serializer.Deserialize(reader, reflectionObject.GetType(ValueName));
|
|
||||||
|
value = serializer.Deserialize(reader, valueContract.UnderlyingType);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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()
|
internal bool ReadAndMoveToContent()
|
||||||
{
|
{
|
||||||
return Read() && MoveToContent();
|
return Read() && MoveToContent();
|
||||||
|
|||||||
@@ -146,7 +146,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
{
|
{
|
||||||
JsonConverter converter = GetConverter(contract, null, null, null);
|
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)
|
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);
|
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.");
|
throw JsonSerializationException.Create(reader, "Unexpected end when deserializing object.");
|
||||||
}
|
}
|
||||||
@@ -1461,7 +1461,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (ReadForType(reader, collectionItemContract, collectionItemConverter != null))
|
if (reader.ReadForType(collectionItemContract, collectionItemConverter != null))
|
||||||
{
|
{
|
||||||
switch (reader.TokenType)
|
switch (reader.TokenType)
|
||||||
{
|
{
|
||||||
@@ -1616,7 +1616,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (ReadForType(reader, contract.ItemContract, collectionItemConverter != null))
|
if (reader.ReadForType(contract.ItemContract, collectionItemConverter != null))
|
||||||
{
|
{
|
||||||
switch (reader.TokenType)
|
switch (reader.TokenType)
|
||||||
{
|
{
|
||||||
@@ -2162,7 +2162,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
|
|
||||||
JsonConverter propertyConverter = GetConverter(property.PropertyContract, property.MemberConverter, contract, containerProperty);
|
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));
|
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;
|
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)
|
public object CreateNewObject(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMember, JsonProperty containerProperty, string id, out bool createdFromNonDefaultCreator)
|
||||||
{
|
{
|
||||||
object newObject = null;
|
object newObject = null;
|
||||||
@@ -2390,7 +2342,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
|
|
||||||
JsonConverter propertyConverter = GetConverter(property.PropertyContract, property.MemberConverter, contract, member);
|
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));
|
throw JsonSerializationException.Create(reader, "Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user