diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index 16bc7626..9fd3c176 100644 --- a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs @@ -72,5 +72,5 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] #if !PocketPC -[assembly: AssemblyFileVersion("4.5.5.14908")] +[assembly: AssemblyFileVersion("4.5.5.14913")] #endif diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index 99cdf3a9..c650990f 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -1976,7 +1976,7 @@ keyword such as type of business."" @"Unable to find a constructor to use for type Newtonsoft.Json.Tests.TestObjects.Event. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'sublocation', line 1, position 15.", () => { - Event e = JsonConvert.DeserializeObject(json); + JsonConvert.DeserializeObject(json); }); } @@ -6179,6 +6179,83 @@ Parameter name: value", return serializer.Deserialize(jsonReader, typeof(T)) as T; } #endif + + [Test] + public void PropertyItemConverter() + { + Event e = new Event + { + EventName = "Blackadder III", + Venue = "Gryphon Theatre", + Performances = new List + { + DateTime.Parse("8 Tue May 2012, 6:30pm"), + DateTime.Parse("9 Wed May 2012, 6:30pm"), + DateTime.Parse("10 Thu May 2012, 8:00pm") + } + }; + + string json = JsonConvert.SerializeObject(e, Formatting.Indented); + //{ + // "EventName": "Blackadder III", + // "Venue": "Gryphon Theatre", + // "Performances": [ + // new Date(1336458600000), + // new Date(1336545000000), + // new Date(1336636800000) + // ] + //} + + Assert.AreEqual(@"{ + ""EventName"": ""Blackadder III"", + ""Venue"": ""Gryphon Theatre"", + ""Performances"": [ + new Date( + 1336458600000 + ), + new Date( + 1336545000000 + ), + new Date( + 1336636800000 + ) + ] +}", json); + } + +#if !(NET20 || NET35) + [Test] + public void SerializeDataContractSerializationAttributes() + { + DataContractSerializationAttributesClass dataContract = new DataContractSerializationAttributesClass + { + NoAttribute = "Value!", + IgnoreDataMemberAttribute = "Value!", + DataMemberAttribute = "Value!", + IgnoreDataMemberAndDataMemberAttribute = "Value!" + }; + + string json = JsonConvert.SerializeObject(dataContract, Formatting.Indented); + Assert.AreEqual(@"{ + ""DataMemberAttribute"": ""Value!"", + ""IgnoreDataMemberAndDataMemberAttribute"": ""Value!"" +}", json); + + PocoDataContractSerializationAttributesClass poco = new PocoDataContractSerializationAttributesClass + { + NoAttribute = "Value!", + IgnoreDataMemberAttribute = "Value!", + DataMemberAttribute = "Value!", + IgnoreDataMemberAndDataMemberAttribute = "Value!" + }; + + json = JsonConvert.SerializeObject(poco, Formatting.Indented); + Assert.AreEqual(@"{ + ""NoAttribute"": ""Value!"", + ""DataMemberAttribute"": ""Value!"" +}", json); + } +#endif } #if !(SILVERLIGHT || NETFX_CORE || PORTABLE) @@ -6205,6 +6282,15 @@ Parameter name: value", } #endif + public class Event + { + public string EventName { get; set; } + public string Venue { get; set; } + + [JsonProperty(ItemConverterType = typeof(JavaScriptDateTimeConverter))] + public IList Performances { get; set; } + } + public class PropertyItemConverter { [JsonProperty(ItemConverterType = typeof(MetroStringConverter))] @@ -6453,4 +6539,31 @@ Parameter name: value", #endif } } + +#if !(NET20 || NET35) + [DataContract] + public class DataContractSerializationAttributesClass + { + public string NoAttribute { get; set; } + [IgnoreDataMember] + public string IgnoreDataMemberAttribute { get; set; } + [DataMember] + public string DataMemberAttribute { get; set; } + [IgnoreDataMember] + [DataMember] + public string IgnoreDataMemberAndDataMemberAttribute { get; set; } + } + + public class PocoDataContractSerializationAttributesClass + { + public string NoAttribute { get; set; } + [IgnoreDataMember] + public string IgnoreDataMemberAttribute { get; set; } + [DataMember] + public string DataMemberAttribute { get; set; } + [IgnoreDataMember] + [DataMember] + public string IgnoreDataMemberAndDataMemberAttribute { get; set; } + } +#endif } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index d118eab7..2f760e90 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -90,7 +90,7 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] #if !PocketPC -[assembly: AssemblyFileVersion("4.5.5.14908")] +[assembly: AssemblyFileVersion("4.5.5.14913")] #endif [assembly: CLSCompliant(true)] diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs index bb0db37a..10f2d2a2 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs @@ -946,16 +946,19 @@ namespace Newtonsoft.Json.Serialization property.PropertyName = ResolvePropertyName(mappedName); property.UnderlyingName = name; + bool hasMemberAttribute = false; if (propertyAttribute != null) { property._required = propertyAttribute._required; property.Order = propertyAttribute._order; + hasMemberAttribute = true; } #if !PocketPC && !NET20 else if (dataMemberAttribute != null) { property._required = (dataMemberAttribute.IsRequired) ? Required.AllowNull : Required.Default; property.Order = (dataMemberAttribute.Order != -1) ? (int?) dataMemberAttribute.Order : null; + hasMemberAttribute = true; } #endif @@ -968,19 +971,19 @@ namespace Newtonsoft.Json.Serialization if (memberSerialization != MemberSerialization.OptIn) { - // ignored if it has JsonIgnore or NonSerialized attributes - property.Ignored = hasJsonIgnoreAttribute; + bool hasIgnoreDataMemberAttribute = false; + +#if !(NET20 || NET35) + hasIgnoreDataMemberAttribute = (JsonTypeReflector.GetAttribute(attributeProvider) != null); +#endif + + // ignored if it has JsonIgnore or NonSerialized or IgnoreDataMember attributes + property.Ignored = (hasJsonIgnoreAttribute || hasIgnoreDataMemberAttribute); } else { // ignored if it has JsonIgnore/NonSerialized or does not have DataMember or JsonProperty attributes - property.Ignored = - hasJsonIgnoreAttribute - || (propertyAttribute == null -#if !PocketPC && !NET20 - && dataMemberAttribute == null -#endif - ); + property.Ignored = (hasJsonIgnoreAttribute || !hasMemberAttribute); } // resolve converter for property diff --git a/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs b/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs index 9bf50df2..59e6c836 100644 --- a/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs @@ -41,6 +41,7 @@ using IConvertible = Newtonsoft.Json.Utilities.Convertible; namespace Newtonsoft.Json.Utilities { +#if NETFX_CORE internal class Convertible { private object _underlyingValue; @@ -120,6 +121,7 @@ namespace Newtonsoft.Json.Utilities return Convert.ToUInt64(_underlyingValue, provider); } } +#endif internal static class ConvertUtils {