Skip to content

Commit

Permalink
Fix UInt64 maxvalue #389
Browse files Browse the repository at this point in the history
  • Loading branch information
mbdavid committed Dec 25, 2016
1 parent 9bd2ae4 commit e0059d6
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions LiteDB.Tests/LiteDB.Tests.NetStandard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<Compile Include="Engine\MultiKeyTest.cs" />
<Compile Include="Engine\ObjectIdTest.cs" />
<Compile Include="Concurrency\PerformanceTest.cs" />
<Compile Include="Mapper\MaxValueTest.cs" />
<Compile Include="Mapper\PolymorphicTest.cs" />
<Compile Include="Engine\QueryTest.cs" />
<Compile Include="Engine\EngineTest.cs" />
Expand Down
1 change: 1 addition & 0 deletions LiteDB.Tests/LiteDB.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<Compile Include="Engine\ShrinkTest.cs" />
<Compile Include="Engine\DropCollectionTest.cs" />
<Compile Include="Mapper\MapperExceptionTest.cs" />
<Compile Include="Mapper\MaxValueTest.cs" />
<Compile Include="Mapper\MapperTest.cs" />
<Compile Include="Engine\EncryptedTest.cs" />
<Compile Include="Engine\MemoryStreamTest.cs" />
Expand Down
42 changes: 42 additions & 0 deletions LiteDB.Tests/Mapper/MaxValueTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Drawing;

namespace LiteDB.Tests
{
public class MyMaxValueClass
{
public int Id { get; set; }

public UInt64 Min { get; set; }
public UInt64 Max { get; set; }
}

[TestClass]
public class MaxValueTest
{
[TestMethod]
public void MaxValue_Test()
{
var c1 = new MyMaxValueClass
{
Min = UInt64.MinValue,
Max = UInt64.MaxValue
};

var doc = BsonMapper.Global.ToDocument(c1);
var json = JsonSerializer.Serialize(doc, true);
var bson = BsonSerializer.Serialize(doc);

var ndoc = BsonSerializer.Deserialize(bson);
var c2 = BsonMapper.Global.ToObject<MyMaxValueClass>(ndoc);

Assert.AreEqual(c1.Min, c2.Min);
Assert.AreEqual(c1.Max, c2.Max);
}
}
}
7 changes: 6 additions & 1 deletion LiteDB/Mapper/BsonMapper.Deserialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ internal T Deserialize<T>(BsonValue value)
typeof(Int16),
typeof(UInt16),
typeof(UInt32),
typeof(UInt64),
typeof(Single),
typeof(Char),
typeof(Byte),
Expand Down Expand Up @@ -112,6 +111,12 @@ internal object Deserialize(Type type, BsonValue value)
return Convert.ChangeType(value.RawValue, type);
}

// special cast to UInt64 to Int64
else if (type == typeof(UInt64))
{
return unchecked((UInt64)((Int64)value.RawValue));
}

// enum value is an int
else if (type.GetTypeInfo().IsEnum)
{
Expand Down
11 changes: 9 additions & 2 deletions LiteDB/Mapper/BsonMapper.Serialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,18 @@ internal BsonValue Serialize(Type type, object obj, int depth)
{
return new BsonValue(Convert.ToInt32(obj));
}
else if (obj is UInt32 || obj is UInt64)
else if (obj is UInt32)
{
return new BsonValue(Convert.ToInt64(obj));
}
else if (obj is Single || obj is Decimal)
else if (obj is UInt64)
{
var ulng = ((UInt64)obj);
var lng = unchecked((Int64)ulng);

return new BsonValue(lng);
}
else if (obj is Single)
{
return new BsonValue(Convert.ToDouble(obj));
}
Expand Down

0 comments on commit e0059d6

Please sign in to comment.