Skip to content

Commit b528c9d

Browse files
committed
Merge branch 'grantfar-Issue104' into develop
2 parents 2d20227 + 10431c5 commit b528c9d

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

src/LitJson/JsonMapper.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -779,10 +779,13 @@ private static void WriteValue (object obj, JsonWriter writer,
779779
return;
780780
}
781781

782-
if (obj is IDictionary) {
782+
if (obj is IDictionary dictionary) {
783783
writer.WriteObjectStart ();
784-
foreach (DictionaryEntry entry in (IDictionary) obj) {
785-
writer.WritePropertyName ((string) entry.Key);
784+
foreach (DictionaryEntry entry in dictionary) {
785+
var propertyName = entry.Key is string key ?
786+
key
787+
: Convert.ToString(entry.Key, CultureInfo.InvariantCulture);
788+
writer.WritePropertyName (propertyName);
786789
WriteValue (entry.Value, writer, writer_is_private,
787790
depth + 1);
788791
}

test/JsonMapperTest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,22 @@ public void ExportEnumsTest ()
306306
Assert.AreEqual ("{\"FavouritePlanet\":1,\"Band\":9}", json);
307307
}
308308

309+
[Test]
310+
public void ExportEnumDictionaryTest()
311+
{
312+
Dictionary<Planets, int> planets = new Dictionary<Planets, int>();
313+
314+
planets.Add(Planets.Jupiter, 5);
315+
planets.Add(Planets.Saturn, 6);
316+
planets.Add(Planets.Uranus, 7);
317+
planets.Add(Planets.Neptune, 8);
318+
planets.Add(Planets.Pluto, 9);
319+
320+
string json = JsonMapper.ToJson(planets);
321+
322+
Assert.AreEqual("{\"Jupiter\":5,\"Saturn\":6,\"Uranus\":7,\"Neptune\":8,\"Pluto\":9}", json);
323+
}
324+
309325
[Test]
310326
public void ExportObjectTest ()
311327
{

0 commit comments

Comments
 (0)