Fix bug deserializing enum values, added tests
ln.build - build0.waldrennach.l--n.de build job pending Details

master 1.0.3
Harald Wolff 2020-12-15 11:30:47 +01:00
parent 5d4852404f
commit 3beaaeec62
3 changed files with 28 additions and 9 deletions

View File

@ -113,6 +113,16 @@ namespace ln.json.tests
TestPrimitiveMapping((uint)0x80);
TestPrimitiveMapping((long)0x80);
TestPrimitiveMapping((ulong)0x80);
TestDeserialization("4", testEnum3.C);
TestDeserialization("\"C\"", testEnum3.C);
TestDeserialization("4", testEnum2.C);
TestDeserialization("\"C\"", testEnum2.C);
TestDeserialization("7", testEnum3.E | testEnum3.A);
TestDeserialization("\"A,E\"", testEnum3.A | testEnum3.E);
}
void TestPrimitiveMapping(object primitiveValue)
@ -122,8 +132,18 @@ namespace ln.json.tests
JSONMapper.DefaultMapper.Deserialize(jsonPrimitive, primitiveType, out object primitiveValue2);
Assert.AreEqual(primitiveValue, primitiveValue2);
Assert.AreEqual(primitiveType, primitiveValue2.GetType());
}
void TestDeserialization(string jsonSource, object targetValue)
{
Type targetType = targetValue.GetType();
JSONValue json = JSONParser.Parse(jsonSource);
Assert.IsTrue(JSONMapper.DefaultMapper.Deserialize(json, targetType, out object value));
Assert.AreEqual(targetType, value.GetType());
Assert.AreEqual(targetValue, value);
}
}
}

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>1.0.2</Version>
<Version>1.0.3</Version>
<Authors>Harald Wolff-Thobaben</Authors>
<Company>l--n.de</Company>
<Product>ln.json</Product>

View File

@ -200,14 +200,13 @@ namespace ln.json.mapping
if (nativeType.IsEnum)
{
if (nativeType.GetCustomAttribute<FlagsAttribute>() != null)
{
o = Enum.ToObject(nativeType, (json as JSONNumber).AsInt);
}
if (json is JSONNumber jsonNumber)
o = Enum.ToObject(nativeType, jsonNumber.ToNative());
else if (json is JSONString jsonString)
o = Enum.Parse(nativeType, jsonString.Value);
else
{
o = Enum.Parse(nativeType, (json as JSONString).Value);
}
throw new NotSupportedException(String.Format("Mapping from {0} to {1} is not supported", json.GetType().Name, nativeType.Name));
return true;
}