-Fixed deserializing null value onto JObject or JArray proeprty

This commit is contained in:
James Newton-King
2014-11-01 10:10:29 +13:00
parent 70d3e9327d
commit 55e482c930
6 changed files with 101 additions and 6 deletions
+34 -2
View File
@@ -26,7 +26,8 @@ namespace Newtonsoft.Json.TestConsole
//DeserializeLargeJson(); //DeserializeLargeJson();
//WriteLargeJson(); //WriteLargeJson();
DeserializeJson(); //DeserializeJson();
ReadLargeJson();
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("Finished"); Console.WriteLine("Finished");
@@ -60,7 +61,7 @@ namespace Newtonsoft.Json.TestConsole
Stopwatch sw = new Stopwatch(); Stopwatch sw = new Stopwatch();
sw.Start(); sw.Start();
for (int i = 0; i < 100; i++) for (int i = 0; i < 10; i++)
{ {
using (StreamWriter file = System.IO.File.CreateText("largewrite.json")) using (StreamWriter file = System.IO.File.CreateText("largewrite.json"))
{ {
@@ -74,5 +75,36 @@ namespace Newtonsoft.Json.TestConsole
Console.WriteLine("Finished. Total seconds: " + sw.Elapsed.TotalSeconds); Console.WriteLine("Finished. Total seconds: " + sw.Elapsed.TotalSeconds);
} }
public static void ReadLargeJson()
{
using (var jsonFile = System.IO.File.OpenText("large.json"))
using (JsonTextReader jsonTextReader = new JsonTextReader(jsonFile))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Deserialize<IList<PerformanceTests.RootObject>>(jsonTextReader);
}
Console.WriteLine("Press any key to start deserialization");
Console.ReadKey();
Console.WriteLine("Deserializing...");
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 10; i++)
{
using (var jsonFile = System.IO.File.OpenText("large.json"))
using (JsonTextReader jsonTextReader = new JsonTextReader(jsonFile))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Deserialize<IList<PerformanceTests.RootObject>>(jsonTextReader);
}
}
sw.Stop();
Console.WriteLine("Finished. Total seconds: " + sw.Elapsed.TotalSeconds);
}
} }
} }
@@ -33,4 +33,4 @@ using System.Runtime.InteropServices;
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("6.0.0.0")] // [assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyVersion("6.0.0.0")] [assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyFileVersion("6.0.6.17820")] [assembly: AssemblyFileVersion("6.0.6.17901")]
@@ -74,4 +74,4 @@ using System.Security;
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("6.0.0.0")] [assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyFileVersion("6.0.6.17820")] [assembly: AssemblyFileVersion("6.0.6.17901")]
@@ -95,6 +95,61 @@ namespace Newtonsoft.Json.Tests.Serialization
[TestFixture] [TestFixture]
public class JsonSerializerTest : TestFixtureBase public class JsonSerializerTest : TestFixtureBase
{ {
#if !(NETFX_CORE || ASPNETCORE50 || NET20)
[MetadataType(typeof(CustomerValidation))]
public partial class CustomerWithMetadataType
{
public System.Guid UpdatedBy_Id { get; set; }
public class CustomerValidation
{
[JsonIgnore]
public System.Guid UpdatedBy_Id { get; set; }
}
}
[Test]
public void SerializeMetadataType()
{
CustomerWithMetadataType c = new CustomerWithMetadataType();
c.UpdatedBy_Id = Guid.NewGuid();
string json = JsonConvert.SerializeObject(c);
Assert.AreEqual("{}", json);
CustomerWithMetadataType c2 = JsonConvert.DeserializeObject<CustomerWithMetadataType>("{'UpdatedBy_Id':'F6E0666D-13C7-4745-B486-800812C8F6DE'}");
Assert.AreEqual(Guid.Empty, c2.UpdatedBy_Id);
}
#endif
public class NullTestClass
{
public JObject Value1 { get; set; }
public JValue Value2 { get; set; }
public JRaw Value3 { get; set; }
public JToken Value4 { get; set; }
public object Value5 { get; set; }
}
[Test]
public void DeserializeNullToJTokenProperty()
{
NullTestClass otc = JsonConvert.DeserializeObject<NullTestClass>(@"{
""Value1"": null,
""Value2"": null,
""Value3"": null,
""Value4"": null,
""Value5"": null
}");
Assert.IsNull(otc.Value1);
Assert.AreEqual(JTokenType.Null, otc.Value2.Type);
Assert.AreEqual(JTokenType.Raw, otc.Value3.Type);
Assert.AreEqual(JTokenType.Null, otc.Value4.Type);
Assert.IsNull(otc.Value5);
}
public class Link public class Link
{ {
/// <summary> /// <summary>
@@ -91,5 +91,5 @@ using System.Security;
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("6.0.0.0")] [assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyFileVersion("6.0.6.17820")] [assembly: AssemblyFileVersion("6.0.6.17901")]
[assembly: CLSCompliant(true)] [assembly: CLSCompliant(true)]
@@ -268,7 +268,15 @@ namespace Newtonsoft.Json.Serialization
private object CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, object existingValue) private object CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, object existingValue)
{ {
if (contract != null && contract.ContractType == JsonContractType.Linq) if (contract != null && contract.ContractType == JsonContractType.Linq)
return CreateJToken(reader, contract); {
JToken token = CreateJToken(reader, contract);
// test to make sure token is valid when null, e.g. can't set JValue.Null to JObject proeprty
if (token.Type == JTokenType.Null && !contract.UnderlyingType.IsAssignableFrom(typeof(JValue)))
return null;
return token;
}
do do
{ {