-Changed JObject to enumerate over KeyValuePair<string, JToken> rather than JToken. Breaking change.

This commit is contained in:
JamesNK
2009-04-27 08:49:08 +00:00
parent 6e205383c7
commit 52f8657106
5 changed files with 68 additions and 16 deletions
@@ -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++;
}
}
}
}
@@ -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++;
}
}
}
}
@@ -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<string, JToken> pair in o)
{
Assert.AreEqual("PropertyNameValue" + i, pair.Key);
Assert.AreEqual(i, (int)pair.Value);
i++;
}
}
}
}
+5 -9
View File
@@ -387,21 +387,17 @@ namespace Newtonsoft.Json.Linq
#endregion
#region IEnumerable<KeyValuePair<string,JToken>> Members
internal override int GetDeepHashCode()
{
return ContentsHashCode();
}
IEnumerator<KeyValuePair<string, JToken>> IEnumerable<KeyValuePair<string,JToken>>.GetEnumerator()
public IEnumerator<KeyValuePair<string, JToken>> GetEnumerator()
{
foreach (JProperty property in Properties())
{
yield return new KeyValuePair<string, JToken>(property.Name, property.Value);
}
}
#endregion
internal override int GetDeepHashCode()
{
return ContentsHashCode();
}
}
}
+1 -7
View File
@@ -738,13 +738,7 @@ namespace Newtonsoft.Json.Linq
}
#endregion
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
/// </returns>
public IEnumerator GetEnumerator()
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<JToken>)this).GetEnumerator();
}