diff --git a/ln.json.tests/JSONTests.cs b/ln.json.tests/JSONTests.cs index f7b5c71..27916f4 100644 --- a/ln.json.tests/JSONTests.cs +++ b/ln.json.tests/JSONTests.cs @@ -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); + } + + + } } \ No newline at end of file diff --git a/ln.json/ln.json.csproj b/ln.json/ln.json.csproj index 2480517..262fbd5 100644 --- a/ln.json/ln.json.csproj +++ b/ln.json/ln.json.csproj @@ -2,7 +2,7 @@ netcoreapp3.1 - 1.0.2 + 1.0.3 Harald Wolff-Thobaben l--n.de ln.json diff --git a/ln.json/mapping/JSONMapper.cs b/ln.json/mapping/JSONMapper.cs index 7c6e28f..5aff1d7 100644 --- a/ln.json/mapping/JSONMapper.cs +++ b/ln.json/mapping/JSONMapper.cs @@ -200,14 +200,13 @@ namespace ln.json.mapping if (nativeType.IsEnum) { - if (nativeType.GetCustomAttribute() != 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; }