diff --git a/README.md b/README.md index 49198a30..113b304c 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![NuGet version (Newtonsoft.Json)](https://img.shields.io/nuget/v/Newtonsoft.Json.svg?style=flat-square)](https://www.nuget.org/packages/Newtonsoft.Json/) [![Build status](https://ci.appveyor.com/api/projects/status/h1py293didjv3428/branch/master?svg=true)](https://ci.appveyor.com/project/JamesNewtonKing/newtonsoft-json) - + - [Homepage](http://www.newtonsoft.com/json) - [Documentation](http://www.newtonsoft.com/json/help) - [NuGet Package](https://www.nuget.org/packages/Newtonsoft.Json) diff --git a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs index ce5e4e95..04a1de1d 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs @@ -293,6 +293,27 @@ namespace Newtonsoft.Json.Tests.Linq Assert.AreEqual(false, contains); } + [Test] + public void Contains() + { + JObject o = new JObject(); + o.Add("PropertyNameValue", new JValue(1)); + Assert.AreEqual(1, o.Children().Count()); + + bool contains = o.ContainsKey("PropertyNameValue"); + Assert.AreEqual(true, contains); + + contains = o.ContainsKey("does not exist"); + Assert.AreEqual(false, contains); + + ExceptionAssert.Throws(() => + { + contains = o.ContainsKey(null); + Assert.AreEqual(false, contains); + }, @"Value cannot be null. +Parameter name: propertyName"); + } + [Test] public void GenericDictionaryContains() { diff --git a/Src/Newtonsoft.Json/Linq/JObject.cs b/Src/Newtonsoft.Json/Linq/JObject.cs index 5d9081f9..d1092bbc 100644 --- a/Src/Newtonsoft.Json/Linq/JObject.cs +++ b/Src/Newtonsoft.Json/Linq/JObject.cs @@ -541,9 +541,16 @@ namespace Newtonsoft.Json.Linq Add(new JProperty(propertyName, value)); } - bool IDictionary.ContainsKey(string key) + /// + /// Determines whether the JSON object has the specified property name. + /// + /// Name of the property. + /// true if the JSON object has the specified property name; otherwise, false. + public bool ContainsKey(string propertyName) { - return _properties.Contains(key); + ValidationUtils.ArgumentNotNull(propertyName, nameof(propertyName)); + + return _properties.Contains(propertyName); } ICollection IDictionary.Keys => _properties.Keys;