From 2b9941fa74000c01239206acdb21e3d8673a6f70 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Wed, 1 May 2013 19:33:39 +1200 Subject: [PATCH] -Fixed using JArray with ImpromptuInterface by changing some members from explicit to public -Fixed deserializing non-ISO date dictionary keys -Fixed values not being set when deserializing with DefaultValueHandling.IgnoreAndPopulate --- .../Properties/AssemblyInfo.cs | 2 +- .../DefaultValueHandlingTests.cs | 27 +++++++++++++++++++ .../Serialization/JsonSerializerTest.cs | 11 ++++++++ Src/Newtonsoft.Json/Linq/JArray.cs | 24 +++++++++++++++-- .../Properties/AssemblyInfo.cs | 2 +- .../JsonSerializerInternalReader.cs | 11 +++++--- 6 files changed, 70 insertions(+), 7 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index 8ea14963..3c9132e7 100644 --- a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs @@ -75,4 +75,4 @@ using System.Security; // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] -[assembly: AssemblyFileVersion("5.0.4.16030")] +[assembly: AssemblyFileVersion("5.0.4.16101")] diff --git a/Src/Newtonsoft.Json.Tests/Serialization/DefaultValueHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/DefaultValueHandlingTests.cs index e8497229..1a3c23d1 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/DefaultValueHandlingTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/DefaultValueHandlingTests.cs @@ -147,6 +147,33 @@ namespace Newtonsoft.Json.Tests.Serialization Assert.AreEqual("TestProperty1Value", c.TestProperty1); } + public class DefaultHandler + { + [DefaultValue(-1)] + public int field1; + + [DefaultValue("default")] + public string field2; + } + + [Test] + public void DeserializeIgnoreAndPopulate() + { + DefaultHandler c1 = JsonConvert.DeserializeObject("{}", new JsonSerializerSettings + { + DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate + }); + Assert.AreEqual(-1, c1.field1); + Assert.AreEqual("default", c1.field2); + + DefaultHandler c2 = JsonConvert.DeserializeObject("{'field1':-1,'field2':'default'}", new JsonSerializerSettings + { + DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate + }); + Assert.AreEqual(-1, c2.field1); + Assert.AreEqual("default", c2.field2); + } + [JsonObject] public class NetworkUser { diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index e85197e9..dfd1330d 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -8687,6 +8687,17 @@ Parameter name: value", Assert.AreEqual(2, list[1]); Assert.AreEqual(0, list[2]); } + + [Test] + public void DeserializeNonIsoDateDictionaryKey() + { + Dictionary d = JsonConvert.DeserializeObject>(@"{""04/28/2013 00:00:00"":""test""}"); + + Assert.AreEqual(1, d.Count); + + DateTime key = DateTime.Parse("04/28/2013 00:00:00", CultureInfo.InvariantCulture); + Assert.AreEqual("test", d[key]); + } } public enum Antworten diff --git a/Src/Newtonsoft.Json/Linq/JArray.cs b/Src/Newtonsoft.Json/Linq/JArray.cs index 76adf1a2..2b39791f 100644 --- a/Src/Newtonsoft.Json/Linq/JArray.cs +++ b/Src/Newtonsoft.Json/Linq/JArray.cs @@ -271,6 +271,17 @@ namespace Newtonsoft.Json.Linq RemoveItemAt(index); } + /// + /// Returns an enumerator that iterates through the collection. + /// + /// + /// A that can be used to iterate through the collection. + /// + public IEnumerator GetEnumerator() + { + return Children().GetEnumerator(); + } + #endregion #region ICollection Members @@ -306,12 +317,21 @@ namespace Newtonsoft.Json.Linq return ContainsItem(item); } - void ICollection.CopyTo(JToken[] array, int arrayIndex) + /// + /// Copies to. + /// + /// The array. + /// Index of the array. + public void CopyTo(JToken[] array, int arrayIndex) { CopyItemsTo(array, arrayIndex); } - bool ICollection.IsReadOnly + /// + /// Gets a value indicating whether the is read-only. + /// + /// true if the is read-only; otherwise, false. + public bool IsReadOnly { get { return false; } } diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index 62fd6f7a..9f5d6a57 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -92,6 +92,6 @@ using System.Security; // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] -[assembly: AssemblyFileVersion("5.0.4.16030")] +[assembly: AssemblyFileVersion("5.0.4.16101")] [assembly: CLSCompliant(true)] diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index 5377daaf..76f19463 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -808,6 +808,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ // test tokentype here because default value might not be convertable to actual type, e.g. default of "" for DateTime if (HasFlag(property.DefaultValueHandling.GetValueOrDefault(Serializer._defaultValueHandling), DefaultValueHandling.Ignore) + && !HasFlag(property.DefaultValueHandling.GetValueOrDefault(Serializer._defaultValueHandling), DefaultValueHandling.Populate) && JsonReader.IsPrimitiveToken(tokenType) && MiscellaneousUtils.ValueEquals(reader.Value, property.GetResolvedDefaultValue())) return true; @@ -853,7 +854,8 @@ To fix this error either change the JSON to a {1} or change the deserialized typ return false; if (HasFlag(property.DefaultValueHandling.GetValueOrDefault(Serializer._defaultValueHandling), DefaultValueHandling.Ignore) - && MiscellaneousUtils.ValueEquals(value, property.GetResolvedDefaultValue())) + && !HasFlag(property.DefaultValueHandling.GetValueOrDefault(Serializer._defaultValueHandling), DefaultValueHandling.Populate) + && MiscellaneousUtils.ValueEquals(value, property.GetResolvedDefaultValue())) return false; if (!property.Writable) @@ -975,15 +977,18 @@ To fix this error either change the JSON to a {1} or change the deserialized typ { try { + object dt; // this is for correctly reading ISO and MS formatted dictionary keys if ((keyTypeCode == PrimitiveTypeCode.DateTime || keyTypeCode == PrimitiveTypeCode.DateTimeNullable) - && DateTimeUtils.TryParseDateTime(keyValue.ToString(), DateParseHandling.DateTime, reader.DateTimeZoneHandling, out keyValue)) + && DateTimeUtils.TryParseDateTime(keyValue.ToString(), DateParseHandling.DateTime, reader.DateTimeZoneHandling, out dt)) { + keyValue = dt; } #if !NET20 else if ((keyTypeCode == PrimitiveTypeCode.DateTimeOffset || keyTypeCode == PrimitiveTypeCode.DateTimeOffsetNullable) - && DateTimeUtils.TryParseDateTime(keyValue.ToString(), DateParseHandling.DateTimeOffset, reader.DateTimeZoneHandling, out keyValue)) + && DateTimeUtils.TryParseDateTime(keyValue.ToString(), DateParseHandling.DateTimeOffset, reader.DateTimeZoneHandling, out dt)) { + keyValue = dt; } #endif else