From e65e371e1d5ce94ad188cce7e483fcb1ede5cca2 Mon Sep 17 00:00:00 2001 From: JamesNK Date: Mon, 1 Aug 2011 05:42:05 +0000 Subject: [PATCH] -Changed $id and $ref properties to allow null --- .../Properties/AssemblyInfo.cs | 2 +- .../Serialization/JsonSerializerTest.cs | 23 ++++++++++++++++- .../Properties/AssemblyInfo.cs | 2 +- .../JsonSerializerInternalReader.cs | 25 +++++++++++++------ 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index 3e6d3276..44cd7496 100644 --- a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs @@ -41,5 +41,5 @@ using System.Runtime.InteropServices; // by using the '*' as shown below: [assembly: AssemblyVersion("4.0.2.0")] #if !PocketPC -[assembly: AssemblyFileVersion("4.0.2.13928")] +[assembly: AssemblyFileVersion("4.0.2.14001")] #endif diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index 3d03c081..3bc15e63 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -3272,7 +3272,7 @@ keyword such as type of business."" } [Test] - [ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "JSON reference $ref property must have a string value.")] + [ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "JSON reference $ref property must have a string or null value.")] public void SerializeRefBadType() { //Additional text found in JSON string after finishing deserializing object. @@ -3290,6 +3290,27 @@ keyword such as type of business."" JsonConvert.DeserializeObject>(json); } + [Test] + public void SerializeRefNull() + { + var reference = new Dictionary(); + reference.Add("$ref", null); + reference.Add("$id", null); + reference.Add("blah", "blah!"); + + var child = new Dictionary(); + child.Add("_id", 2); + child.Add("Name", "Isabell"); + child.Add("Father", reference); + + var json = JsonConvert.SerializeObject(child); + Dictionary result = JsonConvert.DeserializeObject>(json); + + Assert.AreEqual(3, result.Count); + Assert.AreEqual(1, ((JObject) result["Father"]).Count); + Assert.AreEqual("blah!", (string)((JObject)result["Father"])["blah"]); + } + public class ConstructorCompexIgnoredProperty { [JsonIgnore] diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index 3ea5defe..85d5dfa4 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -110,7 +110,7 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("4.0.2.0")] #if !PocketPC -[assembly: AssemblyFileVersion("4.0.2.13928")] +[assembly: AssemblyFileVersion("4.0.2.14001")] #endif [assembly: CLSCompliant(true)] diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index ce80f092..9d085674 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -77,7 +77,7 @@ namespace Newtonsoft.Json.Serialization if (reader.TokenType == JsonToken.PropertyName && string.Equals(reader.Value.ToString(), JsonTypeReflector.IdPropertyName, StringComparison.Ordinal)) { CheckedRead(reader); - id = reader.Value.ToString(); + id = (reader.Value != null) ? reader.Value.ToString() : null; CheckedRead(reader); } @@ -307,16 +307,24 @@ namespace Newtonsoft.Json.Serialization if (string.Equals(propertyName, JsonTypeReflector.RefPropertyName, StringComparison.Ordinal)) { CheckedRead(reader); - if (reader.TokenType != JsonToken.String) - throw new JsonSerializationException("JSON reference {0} property must have a string value.".FormatWith(CultureInfo.InvariantCulture, JsonTypeReflector.RefPropertyName)); + if (reader.TokenType != JsonToken.String && reader.TokenType != JsonToken.Null) + throw new JsonSerializationException("JSON reference {0} property must have a string or null value.".FormatWith(CultureInfo.InvariantCulture, JsonTypeReflector.RefPropertyName)); - string reference = reader.Value.ToString(); + string reference = (reader.Value != null) ? reader.Value.ToString() : null; CheckedRead(reader); - if (reader.TokenType == JsonToken.PropertyName) - throw new JsonSerializationException("Additional content found in JSON reference object. A JSON reference object should only have a {0} property.".FormatWith(CultureInfo.InvariantCulture, JsonTypeReflector.RefPropertyName)); - return Serializer.ReferenceResolver.ResolveReference(this, reference); + if (reference != null) + { + if (reader.TokenType == JsonToken.PropertyName) + throw new JsonSerializationException("Additional content found in JSON reference object. A JSON reference object should only have a {0} property.".FormatWith(CultureInfo.InvariantCulture, JsonTypeReflector.RefPropertyName)); + + return Serializer.ReferenceResolver.ResolveReference(this, reference); + } + else + { + specialProperty = true; + } } else if (string.Equals(propertyName, JsonTypeReflector.TypePropertyName, StringComparison.Ordinal)) { @@ -356,7 +364,8 @@ namespace Newtonsoft.Json.Serialization { CheckedRead(reader); - id = reader.Value.ToString(); + id = (reader.Value != null) ? reader.Value.ToString() : null; + CheckedRead(reader); specialProperty = true; }