From a2d0fb8d2b4473bf71e5f283d20a780e53be67c5 Mon Sep 17 00:00:00 2001
From: Geovanni Perez <1775792+geoperez@users.noreply.github.com>
Date: Fri, 13 Sep 2019 09:56:53 -0500
Subject: [PATCH] [Fix] Issue when serializing an enum (#194)
---
src/Swan.Lite/Formatters/Json.Serializer.cs | 4 ++++
src/Swan.Lite/Formatters/Json.cs | 6 +++---
test/Swan.Test/JsonTest.Serialize.cs | 6 ++++++
test/Swan.Test/JsonTest.cs | 4 ++--
4 files changed, 15 insertions(+), 5 deletions(-)
diff --git a/src/Swan.Lite/Formatters/Json.Serializer.cs b/src/Swan.Lite/Formatters/Json.Serializer.cs
index 15d092885..fc87ea866 100644
--- a/src/Swan.Lite/Formatters/Json.Serializer.cs
+++ b/src/Swan.Lite/Formatters/Json.Serializer.cs
@@ -268,6 +268,10 @@ private string ResolveDictionary(IDictionary items, int depth)
private string ResolveObject(object target, int depth)
{
var targetType = target.GetType();
+
+ if (targetType.IsEnum)
+ return Enum.ToObject(targetType, target).ToString();
+
var fields = _options.GetProperties(targetType);
if (fields.Count == 0 && string.IsNullOrWhiteSpace(_options.TypeSpecifier))
diff --git a/src/Swan.Lite/Formatters/Json.cs b/src/Swan.Lite/Formatters/Json.cs
index 87287defd..20931ee91 100644
--- a/src/Swan.Lite/Formatters/Json.cs
+++ b/src/Swan.Lite/Formatters/Json.cs
@@ -170,7 +170,7 @@ public static string Serialize(
/// The object.
/// The options.
///
- /// A that represents the current object.
+ /// A that represents the current object.
///
public static string Serialize(object obj, SerializerOptions options) => Serializer.Serialize(obj, 0, options);
@@ -180,7 +180,7 @@ public static string Serialize(
/// The object.
/// if set to true it formats and indents the output.
/// The include names.
- /// A that represents the current object.
+ /// A that represents the current object.
///
/// The following example shows how to serialize a simple object including the specified properties.
///
@@ -211,7 +211,7 @@ public static string Serialize(
/// The object.
/// if set to true it formats and indents the output.
/// The exclude names.
- /// A that represents the current object.
+ /// A that represents the current object.
///
/// The following code shows how to serialize a simple object excluding the specified properties.
///
diff --git a/test/Swan.Test/JsonTest.Serialize.cs b/test/Swan.Test/JsonTest.Serialize.cs
index c190a2a12..359f5cef3 100644
--- a/test/Swan.Test/JsonTest.Serialize.cs
+++ b/test/Swan.Test/JsonTest.Serialize.cs
@@ -141,6 +141,12 @@ public void WithStructure_ReturnsStructureSerialized()
{
Assert.AreEqual("{\"Value\": 1,\"Name\": \"DefaultStruct\"}", Json.Serialize(DefaultStruct));
}
+
+ [Test]
+ public void WithObjEnum_ReturnsObjectSerialized()
+ {
+ Assert.AreEqual("{\"Id\": 0,\"MyEnum\": 0}", Json.Serialize(new ObjectEnum()));
+ }
}
[TestFixture]
diff --git a/test/Swan.Test/JsonTest.cs b/test/Swan.Test/JsonTest.cs
index 4acec6604..0a1c4d409 100644
--- a/test/Swan.Test/JsonTest.cs
+++ b/test/Swan.Test/JsonTest.cs
@@ -255,10 +255,10 @@ public void WithByteArrayProperty_ReturnValidObject()
[Test]
public void WithClassNoEmptyConstructor_ReturnClassDeserialized()
{
- var obj = Json.Deserialize("{ \"Id\": 0 }");
+ var obj = Json.Deserialize("{ \"Id\": 1 }");
Assert.IsNotNull(obj);
- Assert.AreEqual(0, obj.Id);
+ Assert.AreEqual(1, obj.Id);
}
}
}
\ No newline at end of file