From 11ce71ee8b72a0892cbfdfa22d5343e0dffa7fa8 Mon Sep 17 00:00:00 2001 From: JamesNK Date: Sat, 11 Sep 2010 22:56:13 +0000 Subject: [PATCH] -Added INotifyCollectionChanged to JContainer in .NET 4.0 build -Changed .NET 3.5 and .NET 4.0 builds to target client frameworks -Fixed JSON Schema not raising an error for additional properties when the schema has no properties --- .../Linq/JObjectTests.cs | 5 ++-- .../Schema/ExtensionsTests.cs | 20 ++++++++++++++ Src/Newtonsoft.Json/JsonValidatingReader.cs | 11 +++----- Src/Newtonsoft.Json/Linq/JContainer.cs | 27 +++++++++++++------ Src/Newtonsoft.Json/Linq/JObject.cs | 9 ++++--- .../Newtonsoft.Json.Net35.csproj | 3 +-- Src/Newtonsoft.Json/Newtonsoft.Json.csproj | 3 +-- .../Properties/AssemblyInfo.cs | 5 ++++ 8 files changed, 57 insertions(+), 26 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs index dc1264e2..73e433b8 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs @@ -1305,9 +1305,10 @@ Parameter name: arrayIndex")] Assert.AreEqual(index, 0); Assert.AreEqual(2, (int)o["Test1"]); } -#else +#endif +#if SILVERLIGHT || !(NET20 || NET35) [Test] - public void ListChanged() + public void CollectionChanged() { JProperty p1 = new JProperty("Test1", 1); JProperty p2 = new JProperty("Test2", "Two"); diff --git a/Src/Newtonsoft.Json.Tests/Schema/ExtensionsTests.cs b/Src/Newtonsoft.Json.Tests/Schema/ExtensionsTests.cs index d2538de9..92e52e70 100644 --- a/Src/Newtonsoft.Json.Tests/Schema/ExtensionsTests.cs +++ b/Src/Newtonsoft.Json.Tests/Schema/ExtensionsTests.cs @@ -197,5 +197,25 @@ namespace Newtonsoft.Json.Tests.Schema GenerateSchemaAndSerializeFromType(DBNull.Value); GenerateSchemaAndSerializeFromType(new JsonPropertyWithHandlingValues()); } + + [Test] + public void UndefinedPropertyOnNoPropertySchema() + { + JsonSchema schema = JsonSchema.Parse(@"{ + ""description"": ""test"", + ""type"": ""object"", + ""additionalProperties"": false, + ""properties"": { + } +}"); + + JObject o = JObject.Parse("{'g':1}"); + + List errors = new List(); + o.Validate(schema, (sender, args) => errors.Add(args.Message)); + + Assert.AreEqual(1, errors.Count); + Assert.AreEqual("Property 'g' has not been defined and the schema does not allow additional properties. Line 1, position 5.", errors[0]); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/JsonValidatingReader.cs b/Src/Newtonsoft.Json/JsonValidatingReader.cs index 23a1cd4f..4a46a909 100644 --- a/Src/Newtonsoft.Json/JsonValidatingReader.cs +++ b/Src/Newtonsoft.Json/JsonValidatingReader.cs @@ -552,15 +552,10 @@ namespace Newtonsoft.Json if (_currentScope.RequiredProperties.ContainsKey(propertyName)) _currentScope.RequiredProperties[propertyName] = true; - if (schema.Properties != null && !schema.Properties.ContainsKey(propertyName)) - { - IList definedProperties = schema.Properties.Select(p => p.Key).ToList(); + bool propertyDefinied = (schema.Properties != null && schema.Properties.ContainsKey(propertyName)); - if (!schema.AllowAdditionalProperties && !definedProperties.Contains(propertyName)) - { - RaiseError("Property '{0}' has not been defined and the schema does not allow additional properties.".FormatWith(CultureInfo.InvariantCulture, propertyName), schema); - } - } + if (!propertyDefinied && !schema.AllowAdditionalProperties) + RaiseError("Property '{0}' has not been defined and the schema does not allow additional properties.".FormatWith(CultureInfo.InvariantCulture, propertyName), schema); _currentScope.CurrentPropertyName = propertyName; } diff --git a/Src/Newtonsoft.Json/Linq/JContainer.cs b/Src/Newtonsoft.Json/Linq/JContainer.cs index 52d2a788..b9704a3c 100644 --- a/Src/Newtonsoft.Json/Linq/JContainer.cs +++ b/Src/Newtonsoft.Json/Linq/JContainer.cs @@ -47,6 +47,9 @@ namespace Newtonsoft.Json.Linq , ITypedList, IBindingList #else , IList, INotifyCollectionChanged +#endif +#if !(SILVERLIGHT || NET20 || NET35) + , INotifyCollectionChanged #endif { #if !SILVERLIGHT @@ -59,7 +62,8 @@ namespace Newtonsoft.Json.Linq /// Occurs before an item is added to the collection. /// public event AddingNewEventHandler AddingNew; -#else +#endif +#if SILVERLIGHT || !(NET20 || NET35) /// /// Occurs when the items list of the collection has changed, or the collection is reset. /// @@ -135,7 +139,8 @@ namespace Newtonsoft.Json.Linq } } } -#else +#endif +#if SILVERLIGHT || !(NET20 || NET35) /// /// Raises the event. /// @@ -309,7 +314,8 @@ namespace Newtonsoft.Json.Linq #if !SILVERLIGHT if (ListChanged != null) OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded, IndexOfItem(item))); -#else +#endif +#if SILVERLIGHT || !(NET20 || NET35) if (CollectionChanged != null) OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, IndexOfItem(item))); #endif @@ -401,7 +407,8 @@ namespace Newtonsoft.Json.Linq #if !SILVERLIGHT OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted, index)); -#else +#endif +#if SILVERLIGHT || !(NET20 || NET35) OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, token, index)); #endif @@ -447,7 +454,8 @@ namespace Newtonsoft.Json.Linq #if !SILVERLIGHT OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted, itemIndex)); -#else +#endif +#if SILVERLIGHT || !(NET20 || NET35) OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, itemIndex)); #endif @@ -468,7 +476,8 @@ namespace Newtonsoft.Json.Linq #if !SILVERLIGHT OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, index)); -#else +#endif +#if SILVERLIGHT || !(NET20 || NET35) OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, item, token, index)); #endif } @@ -496,7 +505,8 @@ namespace Newtonsoft.Json.Linq #if !SILVERLIGHT OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1)); -#else +#endif +#if SILVERLIGHT || !(NET20 || NET35) OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); #endif } @@ -547,7 +557,8 @@ namespace Newtonsoft.Json.Linq #if !SILVERLIGHT OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, itemIndex)); -#else +#endif +#if SILVERLIGHT || !(NET20 || NET35) OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, replacement, existing, itemIndex)); #endif } diff --git a/Src/Newtonsoft.Json/Linq/JObject.cs b/Src/Newtonsoft.Json/Linq/JObject.cs index 957e1f6a..308f2db0 100644 --- a/Src/Newtonsoft.Json/Linq/JObject.cs +++ b/Src/Newtonsoft.Json/Linq/JObject.cs @@ -45,10 +45,10 @@ namespace Newtonsoft.Json.Linq /// Represents a JSON object. /// public class JObject : JContainer, IDictionary, INotifyPropertyChanged -#if !PocketPC && !SILVERLIGHT +#if !(PocketPC || SILVERLIGHT) , ICustomTypeDescriptor #endif -#if !PocketPC && !SILVERLIGHT && !NET20 +#if !(PocketPC || SILVERLIGHT || NET20) , INotifyPropertyChanging #endif { @@ -57,7 +57,7 @@ namespace Newtonsoft.Json.Linq /// public event PropertyChangedEventHandler PropertyChanged; -#if !PocketPC && !SILVERLIGHT && !NET20 +#if !(PocketPC || SILVERLIGHT || NET20) /// /// Occurs when a property value is changing. /// @@ -126,7 +126,8 @@ namespace Newtonsoft.Json.Linq OnPropertyChanged(childProperty.Name); #if !SILVERLIGHT OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, IndexOfItem(childProperty))); -#else +#endif +#if SILVERLIGHT || !(NET20 || NET35) OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, childProperty, childProperty, IndexOfItem(childProperty))); #endif } diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj index 5edb4bc1..585b88aa 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj @@ -42,8 +42,7 @@ 1.0.0.%2a false true - - + Client true diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.csproj index 153a3e87..7c6902d3 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.csproj @@ -42,8 +42,7 @@ 1.0.0.%2a false true - - + Client true diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index df6333de..8862465b 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -33,6 +33,9 @@ using System.Security; #elif NET20 [assembly: AssemblyTitle("Newtonsoft Json.NET .NET 2.0")] [assembly: AllowPartiallyTrustedCallers] +#elif NET35 +[assembly: AssemblyTitle("Newtonsoft Json.NET .NET 3.5")] +[assembly: AllowPartiallyTrustedCallers] #else [assembly: AssemblyTitle("Newtonsoft Json.NET")] [assembly: AllowPartiallyTrustedCallers] @@ -60,6 +63,8 @@ using System.Security; [assembly: InternalsVisibleTo("Newtonsoft.Json.Tests.Compact, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f561df277c6c0b497d629032b410cdcf286e537c054724f7ffa0164345f62b3e642029d7a80cc351918955328c4adc8a048823ef90b0cf38ea7db0d729caf2b633c3babe08b0310198c1081995c19029bc675193744eab9d7345b8a67258ec17d112cebdbbb2a281487dceeafb9d83aa930f32103fbe1d2911425bc5744002c7")] #elif NET20 [assembly: InternalsVisibleTo("Newtonsoft.Json.Tests.Net20, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f561df277c6c0b497d629032b410cdcf286e537c054724f7ffa0164345f62b3e642029d7a80cc351918955328c4adc8a048823ef90b0cf38ea7db0d729caf2b633c3babe08b0310198c1081995c19029bc675193744eab9d7345b8a67258ec17d112cebdbbb2a281487dceeafb9d83aa930f32103fbe1d2911425bc5744002c7")] +#elif NET35 +[assembly: InternalsVisibleTo("Newtonsoft.Json.Tests.Net35, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f561df277c6c0b497d629032b410cdcf286e537c054724f7ffa0164345f62b3e642029d7a80cc351918955328c4adc8a048823ef90b0cf38ea7db0d729caf2b633c3babe08b0310198c1081995c19029bc675193744eab9d7345b8a67258ec17d112cebdbbb2a281487dceeafb9d83aa930f32103fbe1d2911425bc5744002c7")] #else [assembly: InternalsVisibleTo("Newtonsoft.Json.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f561df277c6c0b497d629032b410cdcf286e537c054724f7ffa0164345f62b3e642029d7a80cc351918955328c4adc8a048823ef90b0cf38ea7db0d729caf2b633c3babe08b0310198c1081995c19029bc675193744eab9d7345b8a67258ec17d112cebdbbb2a281487dceeafb9d83aa930f32103fbe1d2911425bc5744002c7")] #endif