diff --git a/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs index 5cfb2b46..fff3dc97 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs @@ -353,5 +353,18 @@ Parameter name: index")] ] }", result); } + + [Test] + public void Iterate() + { + JArray a = new JArray(1, 2, 3, 4, 5); + + int i = 1; + foreach (JToken token in a) + { + Assert.AreEqual(i, (int)token); + i++; + } + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Linq/JConstructorTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JConstructorTests.cs index 353691d0..dc5dedb6 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JConstructorTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JConstructorTests.cs @@ -31,5 +31,18 @@ namespace Newtonsoft.Json.Tests.Linq Assert.AreEqual(2, (int)constructor.Children().ElementAt(1)); Assert.AreEqual(3, (int)constructor.Children().ElementAt(2)); } + + [Test] + public void Iterate() + { + JConstructor c = new JConstructor("MrConstructor", 1, 2, 3, 4, 5); + + int i = 1; + foreach (JToken token in c) + { + Assert.AreEqual(i, (int)token); + i++; + } + } } } diff --git a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs index 1527d5ce..4834b89c 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs @@ -374,5 +374,41 @@ Parameter name: arrayIndex")] ""val2"": 1 }", json); } + + [Test] + public void Iterate() + { + JObject o = new JObject(); + o.Add("PropertyNameValue1", new JValue(1)); + o.Add("PropertyNameValue2", new JValue(2)); + + JToken t = o; + + int i = 1; + foreach (JProperty property in t) + { + Assert.AreEqual("PropertyNameValue" + i, property.Name); + Assert.AreEqual(i, (int)property.Value); + + i++; + } + } + + [Test] + public void KeyValuePairIterate() + { + JObject o = new JObject(); + o.Add("PropertyNameValue1", new JValue(1)); + o.Add("PropertyNameValue2", new JValue(2)); + + int i = 1; + foreach (KeyValuePair pair in o) + { + Assert.AreEqual("PropertyNameValue" + i, pair.Key); + Assert.AreEqual(i, (int)pair.Value); + + i++; + } + } } } diff --git a/Src/Newtonsoft.Json/Linq/JObject.cs b/Src/Newtonsoft.Json/Linq/JObject.cs index 11261c23..8d1c156f 100644 --- a/Src/Newtonsoft.Json/Linq/JObject.cs +++ b/Src/Newtonsoft.Json/Linq/JObject.cs @@ -387,21 +387,17 @@ namespace Newtonsoft.Json.Linq #endregion - #region IEnumerable> Members + internal override int GetDeepHashCode() + { + return ContentsHashCode(); + } - IEnumerator> IEnumerable>.GetEnumerator() + public IEnumerator> GetEnumerator() { foreach (JProperty property in Properties()) { yield return new KeyValuePair(property.Name, property.Value); } } - - #endregion - - internal override int GetDeepHashCode() - { - return ContentsHashCode(); - } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Linq/JToken.cs b/Src/Newtonsoft.Json/Linq/JToken.cs index 94aaf40e..08d223eb 100644 --- a/Src/Newtonsoft.Json/Linq/JToken.cs +++ b/Src/Newtonsoft.Json/Linq/JToken.cs @@ -738,13 +738,7 @@ namespace Newtonsoft.Json.Linq } #endregion - /// - /// Returns an enumerator that iterates through a collection. - /// - /// - /// An object that can be used to iterate through the collection. - /// - public IEnumerator GetEnumerator() + IEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable)this).GetEnumerator(); }