From b2109bb54e10ae0fe796c86983ec454934135b5f Mon Sep 17 00:00:00 2001 From: grantfar Date: Sun, 13 Oct 2019 19:29:13 -0400 Subject: [PATCH 1/2] Fixed issue 104 and added test. Changed method to use ToString instead of casting --- src/LitJson/JsonMapper.cs | 2 +- test/JsonMapperTest.cs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/LitJson/JsonMapper.cs b/src/LitJson/JsonMapper.cs index 5a0a436..a54a8eb 100644 --- a/src/LitJson/JsonMapper.cs +++ b/src/LitJson/JsonMapper.cs @@ -782,7 +782,7 @@ private static void WriteValue (object obj, JsonWriter writer, if (obj is IDictionary) { writer.WriteObjectStart (); foreach (DictionaryEntry entry in (IDictionary) obj) { - writer.WritePropertyName ((string) entry.Key); + writer.WritePropertyName (entry.Key.ToString()); WriteValue (entry.Value, writer, writer_is_private, depth + 1); } diff --git a/test/JsonMapperTest.cs b/test/JsonMapperTest.cs index 17829a9..a980cfa 100644 --- a/test/JsonMapperTest.cs +++ b/test/JsonMapperTest.cs @@ -306,6 +306,22 @@ public void ExportEnumsTest () Assert.AreEqual ("{\"FavouritePlanet\":1,\"Band\":9}", json); } + [Test] + public void ExportEnumDictionaryTest() + { + Dictionary planets = new Dictionary(); + + 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 () { From 10431c5e589be99279dc55a8b949706f0b1a8dc5 Mon Sep 17 00:00:00 2001 From: grantfar Date: Mon, 14 Oct 2019 13:16:31 -0400 Subject: [PATCH 2/2] Made jsonmapper for dictionary use InvariantCulture --- src/LitJson/JsonMapper.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/LitJson/JsonMapper.cs b/src/LitJson/JsonMapper.cs index a54a8eb..5cd9268 100644 --- a/src/LitJson/JsonMapper.cs +++ b/src/LitJson/JsonMapper.cs @@ -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 (entry.Key.ToString()); + 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); }