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

Commit 00a5972

Browse files
committed
Implement JsonObject.GetEnumerator() so returns same value as indexer
1 parent 0fb69a0 commit 00a5972

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/ServiceStack.Text/JsonObject.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Globalization;
45
using System.IO;
@@ -88,6 +89,16 @@ public class JsonObject : Dictionary<string, string>
8889
set => base[key] = value;
8990
}
9091

92+
public Enumerator GetEnumerator()
93+
{
94+
var to = new Dictionary<string, string>();
95+
foreach (var key in Keys)
96+
{
97+
to[key] = this[key];
98+
}
99+
return to.GetEnumerator();
100+
}
101+
91102
public static JsonObject Parse(string json)
92103
{
93104
return JsonSerializer.DeserializeFromString<JsonObject>(json);

tests/ServiceStack.Text.Tests/JsonObjectTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,5 +474,20 @@ public void Does_serialize_ObjectDictionaryWrapper_line_breaks()
474474
Assert.That(nested.ToJson(), Is.EqualTo("{\"Prop\":{\"a\":{\"text\":\"line\\nbreak\"}}}"));
475475
}
476476

477+
[Test]
478+
public void Enumerating_JsonObject_returns_same_unescaped_value_as_indexer()
479+
{
480+
var obj = JsonObject.Parse(@"{""a"":""b\\c""}");
481+
Assert.That(obj["a"], Is.EqualTo("b\\c"));
482+
483+
foreach (var entry in obj)
484+
{
485+
if (entry.Key == "a")
486+
{
487+
Assert.That(entry.Value, Is.EqualTo("b\\c"));
488+
}
489+
}
490+
}
491+
477492
}
478493
}

0 commit comments

Comments
 (0)