Fix bug deserializing enum values, added tests
ln.build - build0.waldrennach.l--n.de build job pending
Details
ln.build - build0.waldrennach.l--n.de build job pending
Details
parent
5d4852404f
commit
3beaaeec62
|
@ -113,6 +113,16 @@ namespace ln.json.tests
|
||||||
TestPrimitiveMapping((uint)0x80);
|
TestPrimitiveMapping((uint)0x80);
|
||||||
TestPrimitiveMapping((long)0x80);
|
TestPrimitiveMapping((long)0x80);
|
||||||
TestPrimitiveMapping((ulong)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)
|
void TestPrimitiveMapping(object primitiveValue)
|
||||||
|
@ -122,8 +132,18 @@ namespace ln.json.tests
|
||||||
JSONMapper.DefaultMapper.Deserialize(jsonPrimitive, primitiveType, out object primitiveValue2);
|
JSONMapper.DefaultMapper.Deserialize(jsonPrimitive, primitiveType, out object primitiveValue2);
|
||||||
Assert.AreEqual(primitiveValue, primitiveValue2);
|
Assert.AreEqual(primitiveValue, primitiveValue2);
|
||||||
Assert.AreEqual(primitiveType, primitiveValue2.GetType());
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
<Version>1.0.2</Version>
|
<Version>1.0.3</Version>
|
||||||
<Authors>Harald Wolff-Thobaben</Authors>
|
<Authors>Harald Wolff-Thobaben</Authors>
|
||||||
<Company>l--n.de</Company>
|
<Company>l--n.de</Company>
|
||||||
<Product>ln.json</Product>
|
<Product>ln.json</Product>
|
||||||
|
|
|
@ -200,14 +200,13 @@ namespace ln.json.mapping
|
||||||
|
|
||||||
if (nativeType.IsEnum)
|
if (nativeType.IsEnum)
|
||||||
{
|
{
|
||||||
if (nativeType.GetCustomAttribute<FlagsAttribute>() != null)
|
if (json is JSONNumber jsonNumber)
|
||||||
{
|
o = Enum.ToObject(nativeType, jsonNumber.ToNative());
|
||||||
o = Enum.ToObject(nativeType, (json as JSONNumber).AsInt);
|
else if (json is JSONString jsonString)
|
||||||
}
|
o = Enum.Parse(nativeType, jsonString.Value);
|
||||||
else
|
else
|
||||||
{
|
throw new NotSupportedException(String.Format("Mapping from {0} to {1} is not supported", json.GetType().Name, nativeType.Name));
|
||||||
o = Enum.Parse(nativeType, (json as JSONString).Value);
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue