-Added IgnoreDataMemberAttribute support

This commit is contained in:
James Newton-King
2012-05-13 13:52:26 +12:00
parent fdabc13d8c
commit e47a0eab58
5 changed files with 130 additions and 12 deletions
@@ -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
@@ -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<Event>(json);
JsonConvert.DeserializeObject<TestObjects.Event>(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>
{
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<DateTime> 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
}
@@ -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)]
@@ -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<IgnoreDataMemberAttribute>(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
@@ -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
{