Fixed more primitive mappings and added tests

master
Harald Wolff 2020-12-12 21:59:53 +01:00
parent cf0cd55be8
commit 5d4852404f
2 changed files with 20 additions and 8 deletions

View File

@ -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());
}
}

View File

@ -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(