Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 99a556f

Browse files
committed
Also override JsonObject's IEnumerable<KeyValuePair<string, string>>
1 parent 00a5972 commit 99a556f

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/ServiceStack.Text/JsonObject.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static Dictionary<string, string> ToDictionary(this JsonObject jsonObject
7878
}
7979
}
8080

81-
public class JsonObject : Dictionary<string, string>
81+
public class JsonObject : Dictionary<string, string>, IEnumerable<KeyValuePair<string, string>>
8282
{
8383
/// <summary>
8484
/// Get JSON string value
@@ -89,7 +89,7 @@ public class JsonObject : Dictionary<string, string>
8989
set => base[key] = value;
9090
}
9191

92-
public Enumerator GetEnumerator()
92+
public new Enumerator GetEnumerator()
9393
{
9494
var to = new Dictionary<string, string>();
9595
foreach (var key in Keys)
@@ -99,6 +99,9 @@ public Enumerator GetEnumerator()
9999
return to.GetEnumerator();
100100
}
101101

102+
IEnumerator<KeyValuePair<string, string>> IEnumerable<KeyValuePair<string, string>>.GetEnumerator()
103+
=> GetEnumerator();
104+
102105
public static JsonObject Parse(string json)
103106
{
104107
return JsonSerializer.DeserializeFromString<JsonObject>(json);

tests/ServiceStack.Text.Tests/JsonObjectTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,34 @@ public void Enumerating_JsonObject_returns_same_unescaped_value_as_indexer()
487487
Assert.That(entry.Value, Is.EqualTo("b\\c"));
488488
}
489489
}
490+
491+
var asEnumerable = (IEnumerable<KeyValuePair<string, string>>)obj;
492+
foreach (var entry in asEnumerable)
493+
{
494+
if (entry.Key == "a")
495+
{
496+
Assert.That(entry.Value, Is.EqualTo("b\\c"));
497+
}
498+
}
499+
500+
var asIDict = (IDictionary<string, string>)obj;
501+
foreach (var entry in asIDict)
502+
{
503+
if (entry.Key == "a")
504+
{
505+
Assert.That(entry.Value, Is.EqualTo("b\\c"));
506+
}
507+
}
508+
509+
// Warning: can't override concrete Dictionary<string, string> enumerator
510+
// var asDict = (Dictionary<string, string>)obj;
511+
// foreach (var entry in asDict)
512+
// {
513+
// if (entry.Key == "a")
514+
// {
515+
// Assert.That(entry.Value, Is.EqualTo("b\\c"));
516+
// }
517+
// }
490518
}
491519

492520
}

0 commit comments

Comments
 (0)