Skip to content

Commit e9d6231

Browse files
committed
CSHARP-4566: Support widening numeric and nullable-numeric conversions.
1 parent 4ddebfb commit e9d6231

24 files changed

+2453
-168
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using MongoDB.Bson.Serialization.Options;
17+
18+
namespace MongoDB.Bson.Serialization
19+
{
20+
/// <summary>
21+
/// Implemented by serializers that serialize numeric values.
22+
/// </summary>
23+
public interface IBsonNumericSerializer
24+
{
25+
/// <summary>
26+
/// Gets the representation converter.
27+
/// </summary>
28+
RepresentationConverter Converter { get; }
29+
30+
/// <summary>
31+
/// Gets the representation.
32+
/// </summary>
33+
BsonType Representation { get; }
34+
}
35+
}

src/MongoDB.Bson/Serialization/Options/RepresentationConverter.cs

Lines changed: 160 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515

1616
using System;
17-
using MongoDB.Bson.Serialization.Attributes;
1817

1918
namespace MongoDB.Bson.Serialization.Options
2019
{
@@ -57,6 +56,70 @@ public bool AllowTruncation
5756
}
5857

5958
// public methods
59+
/// <summary>
60+
/// Converts an Int32 to a byte.
61+
/// </summary>
62+
/// <param name="value">An Int32.</param>
63+
/// <returns>A byte.</returns>
64+
public byte ToByte(int value)
65+
{
66+
if (value < byte.MinValue || value > byte.MaxValue)
67+
{
68+
if (!_allowOverflow) { throw new OverflowException(); }
69+
return unchecked((byte)value);
70+
}
71+
72+
return checked((byte)value);
73+
}
74+
75+
/// <summary>
76+
/// Converts an Int64 to a byte.
77+
/// </summary>
78+
/// <param name="value">An Int64.</param>
79+
/// <returns>A byte.</returns>
80+
public byte ToByte(long value)
81+
{
82+
if (value < byte.MinValue || value > byte.MaxValue)
83+
{
84+
if (!_allowOverflow) { throw new OverflowException(); }
85+
return unchecked((byte)value);
86+
}
87+
88+
return checked((byte)value);
89+
}
90+
91+
/// <summary>
92+
/// Converts an Int32 to a char.
93+
/// </summary>
94+
/// <param name="value">An Int32.</param>
95+
/// <returns>A char.</returns>
96+
public char ToChar(int value)
97+
{
98+
if (value < char.MinValue || value > char.MaxValue)
99+
{
100+
if (!_allowOverflow) { throw new OverflowException(); }
101+
return unchecked((char)value);
102+
}
103+
104+
return checked((char)value);
105+
}
106+
107+
/// <summary>
108+
/// Converts an Int64 to a char.
109+
/// </summary>
110+
/// <param name="value">An Int64.</param>
111+
/// <returns>A char.</returns>
112+
public char ToChar(long value)
113+
{
114+
if (value < char.MinValue || value > char.MaxValue)
115+
{
116+
if (!_allowOverflow) { throw new OverflowException(); }
117+
return unchecked((char)value);
118+
}
119+
120+
return checked((char)value);
121+
}
122+
60123
/// <summary>
61124
/// Converts a Decimal128 to a Decimal.
62125
/// </summary>
@@ -485,6 +548,26 @@ public short ToInt16(long value)
485548
return (short)value;
486549
}
487550

551+
/// <summary>
552+
/// Converts a byte to an Int32.
553+
/// </summary>
554+
/// <param name="value">A byte.</param>
555+
/// <returns>An Int32.</returns>
556+
public int ToInt32(byte value)
557+
{
558+
return (int)value;
559+
}
560+
561+
/// <summary>
562+
/// Converts a char to an Int32.
563+
/// </summary>
564+
/// <param name="value">A char.</param>
565+
/// <returns>An Int32.</returns>
566+
public int ToInt32(char value)
567+
{
568+
return (int)value;
569+
}
570+
488571
/// <summary>
489572
/// Converts a Decimal to an Int32.
490573
/// </summary>
@@ -598,6 +681,17 @@ public int ToInt32(long value)
598681
return (int)value;
599682
}
600683

684+
/// <summary>
685+
/// Converts an sbyte to an Int32.
686+
/// </summary>
687+
/// <param name="value">An sbyte.</param>
688+
/// <returns>An Int32.</returns>
689+
[CLSCompliant(false)]
690+
public int ToInt32(sbyte value)
691+
{
692+
return (int)value;
693+
}
694+
601695
/// <summary>
602696
/// Converts an Int16 to an Int32.
603697
/// </summary>
@@ -649,6 +743,26 @@ public int ToInt32(ushort value)
649743
return value;
650744
}
651745

746+
/// <summary>
747+
/// Converts a byte to an Int64.
748+
/// </summary>
749+
/// <param name="value">A byte.</param>
750+
/// <returns>An Int64.</returns>
751+
public long ToInt64(byte value)
752+
{
753+
return (long)value;
754+
}
755+
756+
/// <summary>
757+
/// Converts a char to an Int64.
758+
/// </summary>
759+
/// <param name="value">A char.</param>
760+
/// <returns>An Int64.</returns>
761+
public long ToInt64(char value)
762+
{
763+
return (long)value;
764+
}
765+
652766
/// <summary>
653767
/// Converts a Decimal to an Int64.
654768
/// </summary>
@@ -758,6 +872,17 @@ public long ToInt64(long value)
758872
return value;
759873
}
760874

875+
/// <summary>
876+
/// Converts an sbyte to an Int64.
877+
/// </summary>
878+
/// <param name="value">An sbyte.</param>
879+
/// <returns>An Int64.</returns>
880+
[CLSCompliant(false)]
881+
public long ToInt64(sbyte value)
882+
{
883+
return (long)value;
884+
}
885+
761886
/// <summary>
762887
/// Converts an Int16 to an Int64.
763888
/// </summary>
@@ -805,6 +930,40 @@ public long ToInt64(ushort value)
805930
return value;
806931
}
807932

933+
/// <summary>
934+
/// Converts an Int32 to an sbyte.
935+
/// </summary>
936+
/// <param name="value">An Int32.</param>
937+
/// <returns>An sbyte.</returns>
938+
[CLSCompliant(false)]
939+
public sbyte ToSByte(int value)
940+
{
941+
if (value < sbyte.MinValue || value > sbyte.MaxValue)
942+
{
943+
if (!_allowOverflow) { throw new OverflowException(); }
944+
return unchecked((sbyte)value);
945+
}
946+
947+
return checked((sbyte)value);
948+
}
949+
950+
/// <summary>
951+
/// Converts an Int64 to an sbyte.
952+
/// </summary>
953+
/// <param name="value">An Int64.</param>
954+
/// <returns>An sbyte.</returns>
955+
[CLSCompliant(false)]
956+
public sbyte ToSByte(long value)
957+
{
958+
if (value < sbyte.MinValue || value > sbyte.MaxValue)
959+
{
960+
if (!_allowOverflow) { throw new OverflowException(); }
961+
return unchecked((sbyte)value);
962+
}
963+
964+
return checked((sbyte)value);
965+
}
966+
808967
/// <summary>
809968
/// Converts a Decimal128 to a Single.
810969
/// </summary>

0 commit comments

Comments
 (0)