Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved unsigned values support in BsonValue #1227

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions LiteDB/Document/BsonValue.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
Expand Down Expand Up @@ -61,6 +61,14 @@ public BsonValue(Int64 value)
this.Type = BsonType.Int64;
this.RawValue = value;
}

public BsonValue(UInt32 value) : this((Int64)value)
{
}

public BsonValue(UInt64 value) : this((Double)value)
{
}

public BsonValue(Double value)
{
Expand Down Expand Up @@ -260,6 +268,17 @@ public long AsInt64
get { return this.IsNumber ? Convert.ToInt64(this.RawValue) : default(Int64); }
}

public uint AsUInt32
{
get { return this.IsNumber ? Convert.ToUInt32(this.RawValue) : default(UInt32); }
}

public ulong AsUInt64
{
get { return this.IsNumber ? Convert.ToUInt64(this.RawValue) : default(UInt64); }
}


public double AsDouble
{
get { return this.IsNumber ? Convert.ToDouble(this.RawValue) : default(Double); }
Expand Down Expand Up @@ -385,6 +404,18 @@ public static implicit operator BsonValue(Int32 value)
return new BsonValue(value);
}

// UInt32
public static implicit operator UInt32(BsonValue value)
{
return (UInt32)value.RawValue;
}

// UInt32
public static implicit operator BsonValue(UInt32 value)
{
return new BsonValue(value);
}

// Int64
public static implicit operator Int64(BsonValue value)
{
Expand Down Expand Up @@ -421,16 +452,16 @@ public static implicit operator BsonValue(Decimal value)
return new BsonValue(value);
}

// UInt64 (to avoid ambigous between Double-Decimal)
// UInt64 (to avoid ambiguous between Double-Decimal)
public static implicit operator UInt64(BsonValue value)
{
return (UInt64)value.RawValue;
}

// Decimal
// UInt64
public static implicit operator BsonValue(UInt64 value)
{
return new BsonValue((Double)value);
return new BsonValue(value);
}

// String
Expand Down