diff --git a/Src/Newtonsoft.Json.TestConsole/Program.cs b/Src/Newtonsoft.Json.TestConsole/Program.cs index 119b7677..708254fd 100644 --- a/Src/Newtonsoft.Json.TestConsole/Program.cs +++ b/Src/Newtonsoft.Json.TestConsole/Program.cs @@ -26,7 +26,8 @@ namespace Newtonsoft.Json.TestConsole //DeserializeLargeJson(); //WriteLargeJson(); - DeserializeJson(); + //DeserializeJson(); + ReadLargeJson(); Console.WriteLine(); Console.WriteLine("Finished"); @@ -60,7 +61,7 @@ namespace Newtonsoft.Json.TestConsole Stopwatch sw = new Stopwatch(); 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")) { @@ -74,5 +75,36 @@ namespace Newtonsoft.Json.TestConsole 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>(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>(jsonTextReader); + } + } + + sw.Stop(); + + Console.WriteLine("Finished. Total seconds: " + sw.Elapsed.TotalSeconds); + } } } diff --git a/Src/Newtonsoft.Json.TestConsole/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.TestConsole/Properties/AssemblyInfo.cs index c5bf884f..6cf1e17e 100644 --- a/Src/Newtonsoft.Json.TestConsole/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.TestConsole/Properties/AssemblyInfo.cs @@ -33,4 +33,4 @@ using System.Runtime.InteropServices; // 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")] diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index c5efbbcf..b476b164 100644 --- a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs @@ -74,4 +74,4 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("6.0.0.0")] -[assembly: AssemblyFileVersion("6.0.6.17820")] +[assembly: AssemblyFileVersion("6.0.6.17901")] diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index 6e9f4f5a..8da564ea 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -95,6 +95,61 @@ namespace Newtonsoft.Json.Tests.Serialization [TestFixture] 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("{'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(@"{ + ""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 { /// diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index 5c8bc83c..3fd3f822 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -91,5 +91,5 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("6.0.0.0")] -[assembly: AssemblyFileVersion("6.0.6.17820")] +[assembly: AssemblyFileVersion("6.0.6.17901")] [assembly: CLSCompliant(true)] diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index cff4cb27..058c0eb2 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -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) { 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 {