Skip to content

Commit

Permalink
Merge branch 'grantfar-Issue104' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
devlead committed Oct 21, 2019
2 parents 2d20227 + 10431c5 commit b528c9d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/LitJson/JsonMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -779,10 +779,13 @@ private static void WriteValue (object obj, JsonWriter writer,
return;
}

if (obj is IDictionary) {
if (obj is IDictionary dictionary) {
writer.WriteObjectStart ();
foreach (DictionaryEntry entry in (IDictionary) obj) {
writer.WritePropertyName ((string) entry.Key);
foreach (DictionaryEntry entry in dictionary) {
var propertyName = entry.Key is string key ?
key
: Convert.ToString(entry.Key, CultureInfo.InvariantCulture);
writer.WritePropertyName (propertyName);
WriteValue (entry.Value, writer, writer_is_private,
depth + 1);
}
Expand Down
16 changes: 16 additions & 0 deletions test/JsonMapperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,22 @@ public void ExportEnumsTest ()
Assert.AreEqual ("{\"FavouritePlanet\":1,\"Band\":9}", json);
}

[Test]
public void ExportEnumDictionaryTest()
{
Dictionary<Planets, int> planets = new Dictionary<Planets, int>();

planets.Add(Planets.Jupiter, 5);
planets.Add(Planets.Saturn, 6);
planets.Add(Planets.Uranus, 7);
planets.Add(Planets.Neptune, 8);
planets.Add(Planets.Pluto, 9);

string json = JsonMapper.ToJson(planets);

Assert.AreEqual("{\"Jupiter\":5,\"Saturn\":6,\"Uranus\":7,\"Neptune\":8,\"Pluto\":9}", json);
}

[Test]
public void ExportObjectTest ()
{
Expand Down

0 comments on commit b528c9d

Please sign in to comment.