Skip to content

Commit

Permalink
[Fix] Issue when serializing an enum (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
geoperez authored Sep 13, 2019
1 parent 71d3870 commit a2d0fb8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/Swan.Lite/Formatters/Json.Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions src/Swan.Lite/Formatters/Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public static string Serialize(
/// <param name="obj">The object.</param>
/// <param name="options">The options.</param>
/// <returns>
/// A <see cref="System.String" /> that represents the current object.
/// A <see cref="string" /> that represents the current object.
/// </returns>
public static string Serialize(object obj, SerializerOptions options) => Serializer.Serialize(obj, 0, options);

Expand All @@ -180,7 +180,7 @@ public static string Serialize(
/// <param name="obj">The object.</param>
/// <param name="format">if set to <c>true</c> it formats and indents the output.</param>
/// <param name="includeNames">The include names.</param>
/// <returns>A <see cref="System.String" /> that represents the current object.</returns>
/// <returns>A <see cref="string" /> that represents the current object.</returns>
/// <example>
/// The following example shows how to serialize a simple object including the specified properties.
/// <code>
Expand Down Expand Up @@ -211,7 +211,7 @@ public static string Serialize(
/// <param name="obj">The object.</param>
/// <param name="format">if set to <c>true</c> it formats and indents the output.</param>
/// <param name="excludeNames">The exclude names.</param>
/// <returns>A <see cref="System.String" /> that represents the current object.</returns>
/// <returns>A <see cref="string" /> that represents the current object.</returns>
/// <example>
/// The following code shows how to serialize a simple object excluding the specified properties.
/// <code>
Expand Down
6 changes: 6 additions & 0 deletions test/Swan.Test/JsonTest.Serialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions test/Swan.Test/JsonTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,10 @@ public void WithByteArrayProperty_ReturnValidObject()
[Test]
public void WithClassNoEmptyConstructor_ReturnClassDeserialized()
{
var obj = Json.Deserialize<ObjectNoEmptyCtor>("{ \"Id\": 0 }");
var obj = Json.Deserialize<ObjectNoEmptyCtor>("{ \"Id\": 1 }");

Assert.IsNotNull(obj);
Assert.AreEqual(0, obj.Id);
Assert.AreEqual(1, obj.Id);
}
}
}

0 comments on commit a2d0fb8

Please sign in to comment.