-Added support for serializing const fields
-Fixed tests
This commit is contained in:
@@ -57,4 +57,4 @@ using System.Runtime.InteropServices;
|
|||||||
// You can specify all the values or you can default the Build and Revision Numbers
|
// You can specify all the values or you can default the Build and Revision Numbers
|
||||||
// by using the '*' as shown below:
|
// by using the '*' as shown below:
|
||||||
[assembly: AssemblyVersion("7.0.0.0")]
|
[assembly: AssemblyVersion("7.0.0.0")]
|
||||||
[assembly: AssemblyFileVersion("7.0.1.18023")]
|
[assembly: AssemblyFileVersion("7.0.1.18111")]
|
||||||
|
|||||||
@@ -76,4 +76,4 @@ using System.Security;
|
|||||||
// by using the '*' as shown below:
|
// by using the '*' as shown below:
|
||||||
|
|
||||||
[assembly: AssemblyVersion("7.0.0.0")]
|
[assembly: AssemblyVersion("7.0.0.0")]
|
||||||
[assembly: AssemblyFileVersion("7.0.1.18023")]
|
[assembly: AssemblyFileVersion("7.0.1.18111")]
|
||||||
|
|||||||
@@ -723,5 +723,26 @@ namespace Newtonsoft.Json.Tests.Serialization
|
|||||||
Assert.AreEqual(1, (int)c.ExtensionData0["first"]);
|
Assert.AreEqual(1, (int)c.ExtensionData0["first"]);
|
||||||
Assert.AreEqual(2, (int)c.ExtensionData0["second"]);
|
Assert.AreEqual(2, (int)c.ExtensionData0["second"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class Test
|
||||||
|
{
|
||||||
|
[JsonProperty("LastActivityDate")]
|
||||||
|
public DateTime? LastActivityDate { get; set; }
|
||||||
|
|
||||||
|
[JsonExtensionData]
|
||||||
|
public Dictionary<string, object> CustomFields { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeserializeNullableProperty()
|
||||||
|
{
|
||||||
|
string json = @"{ ""LastActivityDate"":null, ""CustomField1"":""Testing"" }";
|
||||||
|
|
||||||
|
var c = JsonConvert.DeserializeObject<Test>(json);
|
||||||
|
|
||||||
|
Assert.AreEqual(null, c.LastActivityDate);
|
||||||
|
Assert.AreEqual(1, c.CustomFields.Count);
|
||||||
|
Assert.AreEqual("Testing", (string)c.CustomFields["CustomField1"]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -25,6 +25,7 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
|
using System.Collections.Specialized;
|
||||||
#if !(NET35 || NET20 || PORTABLE || ASPNETCORE50 || PORTABLE40)
|
#if !(NET35 || NET20 || PORTABLE || ASPNETCORE50 || PORTABLE40)
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
#endif
|
#endif
|
||||||
@@ -57,6 +58,19 @@ namespace Newtonsoft.Json.Tests.Serialization
|
|||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class JsonSerializerCollectionsTests : TestFixtureBase
|
public class JsonSerializerCollectionsTests : TestFixtureBase
|
||||||
{
|
{
|
||||||
|
public class NameValueCollectionTestClass
|
||||||
|
{
|
||||||
|
public NameValueCollection Collection { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeserializeNameValueCollection()
|
||||||
|
{
|
||||||
|
ExceptionAssert.Throws<JsonSerializationException>(
|
||||||
|
() => JsonConvert.DeserializeObject<NameValueCollectionTestClass>("{Collection:[]}"),
|
||||||
|
"Cannot create and populate list type System.Collections.Specialized.NameValueCollection. Path 'Collection', line 1, position 13.");
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void MultiDObjectArray()
|
public void MultiDObjectArray()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8472,6 +8472,27 @@ Path '', line 1, position 1.");
|
|||||||
Assert.AreEqual(typeof(DateTimeOffset), v.Value.GetType());
|
Assert.AreEqual(typeof(DateTimeOffset), v.Value.GetType());
|
||||||
Assert.AreEqual(dt, (DateTimeOffset)v.Value);
|
Assert.AreEqual(dt, (DateTimeOffset)v.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[DataContract]
|
||||||
|
public class ConstantTestClass
|
||||||
|
{
|
||||||
|
[DataMember]
|
||||||
|
public const char MY_CONSTANT = '.';
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeserializeConstantProperty()
|
||||||
|
{
|
||||||
|
ConstantTestClass c1 = new ConstantTestClass();
|
||||||
|
|
||||||
|
string json = JsonConvert.SerializeObject(c1, Formatting.Indented);
|
||||||
|
|
||||||
|
StringAssert.AreEqual(@"{
|
||||||
|
""MY_CONSTANT"": "".""
|
||||||
|
}", json);
|
||||||
|
|
||||||
|
JsonConvert.DeserializeObject<ConstantTestClass>(json);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
@@ -8610,15 +8631,15 @@ Path '', line 1, position 1.");
|
|||||||
""AlternateViews"": []
|
""AlternateViews"": []
|
||||||
}";
|
}";
|
||||||
|
|
||||||
System.Net.Mail.MailMessage mailMessage = JsonConvert.DeserializeObject<System.Net.Mail.MailMessage>(
|
ExceptionAssert.Throws<JsonSerializationException>(() =>
|
||||||
JsonMessage,
|
{
|
||||||
new MailAddressReadConverter(),
|
JsonConvert.DeserializeObject<System.Net.Mail.MailMessage>(
|
||||||
new AttachmentReadConverter(),
|
JsonMessage,
|
||||||
new EncodingReadConverter());
|
new MailAddressReadConverter(),
|
||||||
|
new AttachmentReadConverter(),
|
||||||
Assert.AreEqual("Family tree", mailMessage.Subject);
|
new EncodingReadConverter());
|
||||||
Assert.AreEqual("Luke Skywalker", mailMessage.To[0].DisplayName);
|
},
|
||||||
Assert.AreEqual("skywalker family tree.jpg", mailMessage.Attachments[0].ContentDisposition.FileName);
|
"Cannot populate list type System.Net.Mime.HeaderCollection. Path 'Headers', line 26, position 15.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MailAddressReadConverter : JsonConverter
|
public class MailAddressReadConverter : JsonConverter
|
||||||
|
|||||||
@@ -57,6 +57,7 @@
|
|||||||
<CodeAnalysisRules>
|
<CodeAnalysisRules>
|
||||||
</CodeAnalysisRules>
|
</CodeAnalysisRules>
|
||||||
<CodeAnalysisRuleSet>Newtonsoft.Json.ruleset</CodeAnalysisRuleSet>
|
<CodeAnalysisRuleSet>Newtonsoft.Json.ruleset</CodeAnalysisRuleSet>
|
||||||
|
<UseVSHostingProcess>true</UseVSHostingProcess>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||||
<DebugType>pdbonly</DebugType>
|
<DebugType>pdbonly</DebugType>
|
||||||
|
|||||||
@@ -92,5 +92,5 @@ using System.Security;
|
|||||||
// by using the '*' as shown below:
|
// by using the '*' as shown below:
|
||||||
|
|
||||||
[assembly: AssemblyVersion("7.0.0.0")]
|
[assembly: AssemblyVersion("7.0.0.0")]
|
||||||
[assembly: AssemblyFileVersion("7.0.1.18023")]
|
[assembly: AssemblyFileVersion("7.0.1.18111")]
|
||||||
[assembly: CLSCompliant(true)]
|
[assembly: CLSCompliant(true)]
|
||||||
|
|||||||
@@ -814,12 +814,8 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// can't populate something like an IEnumerable
|
|
||||||
if (!arrayContract.CanDeserialize)
|
if (!arrayContract.CanDeserialize)
|
||||||
{
|
throw JsonSerializationException.Create(reader, "Cannot populate list type {0}.".FormatWith(CultureInfo.InvariantCulture, contract.CreatedType));
|
||||||
reader.Skip();
|
|
||||||
return existingValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
value = PopulateList((arrayContract.ShouldCreateWrapper) ? arrayContract.CreateWrapper(existingValue) : (IList)existingValue, reader, arrayContract, member, id);
|
value = PopulateList((arrayContract.ShouldCreateWrapper) ? arrayContract.CreateWrapper(existingValue) : (IList)existingValue, reader, arrayContract, member, id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -252,6 +252,13 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
|
|
||||||
public override Func<T, object> CreateGet<T>(FieldInfo fieldInfo)
|
public override Func<T, object> CreateGet<T>(FieldInfo fieldInfo)
|
||||||
{
|
{
|
||||||
|
if (fieldInfo.IsLiteral)
|
||||||
|
{
|
||||||
|
object constantValue = fieldInfo.GetValue(null);
|
||||||
|
Func<T, object> getter = o => constantValue;
|
||||||
|
return getter;
|
||||||
|
}
|
||||||
|
|
||||||
DynamicMethod dynamicMethod = CreateDynamicMethod("Get" + fieldInfo.Name, typeof(T), new[] { typeof(object) }, fieldInfo.DeclaringType);
|
DynamicMethod dynamicMethod = CreateDynamicMethod("Get" + fieldInfo.Name, typeof(T), new[] { typeof(object) }, fieldInfo.DeclaringType);
|
||||||
ILGenerator generator = dynamicMethod.GetILGenerator();
|
ILGenerator generator = dynamicMethod.GetILGenerator();
|
||||||
|
|
||||||
|
|||||||
@@ -567,11 +567,13 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
case MemberTypes.Field:
|
case MemberTypes.Field:
|
||||||
FieldInfo fieldInfo = (FieldInfo)member;
|
FieldInfo fieldInfo = (FieldInfo)member;
|
||||||
|
|
||||||
|
if (fieldInfo.IsLiteral)
|
||||||
|
return false;
|
||||||
if (fieldInfo.IsInitOnly && !canSetReadOnly)
|
if (fieldInfo.IsInitOnly && !canSetReadOnly)
|
||||||
return false;
|
return false;
|
||||||
if (nonPublic)
|
if (nonPublic)
|
||||||
return true;
|
return true;
|
||||||
else if (fieldInfo.IsPublic)
|
if (fieldInfo.IsPublic)
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
case MemberTypes.Property:
|
case MemberTypes.Property:
|
||||||
@@ -691,8 +693,16 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
if (provider is Type)
|
if (provider is Type)
|
||||||
{
|
{
|
||||||
Type t = (Type)provider;
|
Type t = (Type)provider;
|
||||||
object[] attributes = (attributeType != null) ? t.GetCustomAttributes(attributeType, inherit) : t.GetCustomAttributes(inherit);
|
object[] a = (attributeType != null) ? t.GetCustomAttributes(attributeType, inherit) : t.GetCustomAttributes(inherit);
|
||||||
return attributes.Cast<Attribute>().ToArray();
|
Attribute[] attributes = a.Cast<Attribute>().ToArray();
|
||||||
|
|
||||||
|
#if (NET20 || NET35)
|
||||||
|
// ye olde .NET GetCustomAttributes doesn't respect the inherit argument
|
||||||
|
if (inherit && t.BaseType != null)
|
||||||
|
attributes = attributes.Union(GetAttributes(t.BaseType, attributeType, inherit)).ToArray();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return attributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (provider is Assembly)
|
if (provider is Assembly)
|
||||||
|
|||||||
Reference in New Issue
Block a user