From 5d4852404fb3ea59fc7ab8f48cb33b8c4c5ca5ee Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Sat, 12 Dec 2020 21:59:53 +0100 Subject: [PATCH] Fixed more primitive mappings and added tests --- ln.json.tests/JSONTests.cs | 22 +++++++++++++++++----- ln.json/mapping/JSONMapper.cs | 6 +++--- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/ln.json.tests/JSONTests.cs b/ln.json.tests/JSONTests.cs index 02ea47e..f7b5c71 100644 --- a/ln.json.tests/JSONTests.cs +++ b/ln.json.tests/JSONTests.cs @@ -106,11 +106,23 @@ namespace ln.json.tests [Test] public void TestMapper() { - byte b = 0x80; - Assert.IsTrue(JSONMapper.DefaultMapper.Serialize(b, out JSONValue jsonByte)); - JSONMapper.DefaultMapper.Deserialize(jsonByte, typeof(byte), out object b2); - Assert.AreEqual(b,b2); - Assert.AreEqual(b.GetType(), b2.GetType()); + TestPrimitiveMapping((byte)0x80); + TestPrimitiveMapping((short)0x80); + TestPrimitiveMapping((ushort)0x80); + TestPrimitiveMapping((int)0x80); + TestPrimitiveMapping((uint)0x80); + TestPrimitiveMapping((long)0x80); + TestPrimitiveMapping((ulong)0x80); + } + + void TestPrimitiveMapping(object primitiveValue) + { + Type primitiveType = primitiveValue.GetType(); + Assert.IsTrue(JSONMapper.DefaultMapper.Serialize(primitiveValue, out JSONValue jsonPrimitive)); + JSONMapper.DefaultMapper.Deserialize(jsonPrimitive, primitiveType, out object primitiveValue2); + Assert.AreEqual(primitiveValue, primitiveValue2); + Assert.AreEqual(primitiveType, primitiveValue2.GetType()); + } } diff --git a/ln.json/mapping/JSONMapper.cs b/ln.json/mapping/JSONMapper.cs index 0517c55..7c6e28f 100644 --- a/ln.json/mapping/JSONMapper.cs +++ b/ln.json/mapping/JSONMapper.cs @@ -264,7 +264,7 @@ namespace ln.json.mapping )); DefaultMapper.Add(new JSONMapping( typeof(short), - (JSONMapper arg1, object arg2) => new JSONNumber((int)arg2), + (JSONMapper arg1, object arg2) => new JSONNumber((int)(short)arg2), (JSONMapper arg1, JSONValue arg2) => Decimal.ToInt16(((JSONNumber)arg2).Decimal) )); DefaultMapper.Add(new JSONMapping( @@ -279,12 +279,12 @@ namespace ln.json.mapping )); DefaultMapper.Add(new JSONMapping( typeof(ushort), - (JSONMapper arg1, object arg2) => new JSONNumber((uint)arg2), + (JSONMapper arg1, object arg2) => new JSONNumber((uint)(ushort)arg2), (JSONMapper arg1, JSONValue arg2) => Decimal.ToUInt16(((JSONNumber)arg2).Decimal) )); DefaultMapper.Add(new JSONMapping( typeof(uint), - (JSONMapper arg1, object arg2) => new JSONNumber((uint)arg2), + (JSONMapper arg1, object arg2) => new JSONNumber((uint)(uint)arg2), (JSONMapper arg1, JSONValue arg2) => Decimal.ToUInt32(((JSONNumber)arg2).Decimal) )); DefaultMapper.Add(new JSONMapping(