fix bug in JSONMapper triggered by serializing enums based on smaller types then int (e.g. ushort)
- added testcase for enum serializationmaster
parent
1c48245b90
commit
6160ea5065
|
@ -1,9 +1,31 @@
|
||||||
|
using System;
|
||||||
|
using ln.json.mapping;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
|
||||||
namespace ln.json.tests
|
namespace ln.json.tests
|
||||||
{
|
{
|
||||||
public class JSONTests
|
public class JSONTests
|
||||||
{
|
{
|
||||||
|
|
||||||
|
enum testEnum1 { A, B, C, D, E, F }
|
||||||
|
enum testEnum2 : ushort {
|
||||||
|
NONE = 0,
|
||||||
|
A = 1,
|
||||||
|
B = 2,
|
||||||
|
C = 4,
|
||||||
|
D = 5,
|
||||||
|
E = 6
|
||||||
|
}
|
||||||
|
[Flags]
|
||||||
|
enum testEnum3 : ushort {
|
||||||
|
NONE = 0,
|
||||||
|
A = 1,
|
||||||
|
B = 2,
|
||||||
|
C = 4,
|
||||||
|
D = 5,
|
||||||
|
E = 6
|
||||||
|
}
|
||||||
|
|
||||||
[SetUp]
|
[SetUp]
|
||||||
public void Setup()
|
public void Setup()
|
||||||
{
|
{
|
||||||
|
@ -42,5 +64,48 @@ namespace ln.json.tests
|
||||||
JSONParser.Parse("{\t}");
|
JSONParser.Parse("{\t}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestEnumMappings()
|
||||||
|
{
|
||||||
|
TestEnumSerialization(testEnum1.A);
|
||||||
|
TestEnumSerialization(testEnum1.B);
|
||||||
|
TestEnumSerialization(testEnum1.C);
|
||||||
|
TestEnumSerialization(testEnum1.D);
|
||||||
|
TestEnumSerialization(testEnum1.E);
|
||||||
|
TestEnumSerialization(testEnum1.F);
|
||||||
|
|
||||||
|
TestEnumSerialization(testEnum2.NONE);
|
||||||
|
TestEnumSerialization(testEnum2.A);
|
||||||
|
TestEnumSerialization(testEnum2.B);
|
||||||
|
TestEnumSerialization(testEnum2.C);
|
||||||
|
TestEnumSerialization(testEnum2.D);
|
||||||
|
TestEnumSerialization(testEnum2.E);
|
||||||
|
|
||||||
|
TestEnumSerialization(testEnum3.NONE);
|
||||||
|
TestEnumSerialization(testEnum3.A);
|
||||||
|
TestEnumSerialization(testEnum3.B);
|
||||||
|
TestEnumSerialization(testEnum3.C);
|
||||||
|
TestEnumSerialization(testEnum3.D);
|
||||||
|
TestEnumSerialization(testEnum3.E);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TestEnumSerialization(object enumValue)
|
||||||
|
{
|
||||||
|
Type enumType = enumValue.GetType();
|
||||||
|
|
||||||
|
Assert.IsTrue(JSONMapper.DefaultMapper.Serialize(enumValue, out JSONValue jsonEnum));
|
||||||
|
|
||||||
|
JSONMapper.DefaultMapper.Deserialize(jsonEnum, enumType, out object enunValueDeserialized);
|
||||||
|
|
||||||
|
Assert.AreEqual(enumType, enunValueDeserialized.GetType());
|
||||||
|
Assert.AreEqual(enumValue, enunValueDeserialized);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,7 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace ln.json.mapping
|
namespace ln.json.mapping
|
||||||
{
|
{
|
||||||
|
@ -157,7 +156,10 @@ namespace ln.json.mapping
|
||||||
{
|
{
|
||||||
if (type.GetCustomAttribute<FlagsAttribute>() != null)
|
if (type.GetCustomAttribute<FlagsAttribute>() != null)
|
||||||
{
|
{
|
||||||
json = new JSONNumber((int)o);
|
Type enumBaseType = type.GetEnumUnderlyingType();
|
||||||
|
o = Convert.ChangeType(o, enumBaseType);
|
||||||
|
ConstructorInfo constructor = typeof(JSONNumber).GetConstructor(new Type[]{ enumBaseType });
|
||||||
|
json = (JSONNumber)constructor.Invoke(new object[]{ o });
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue