Skip to content

Commit 2261422

Browse files
authored
CSHARP-4909: Add serialization support for new Numeric types (#1449)
1 parent b3a2de6 commit 2261422

File tree

6 files changed

+1009
-0
lines changed

6 files changed

+1009
-0
lines changed

src/MongoDB.Bson/IO/JsonConvert.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ public static double ToDouble(string value)
9090
return double.Parse(value, NumberFormatInfo.InvariantInfo);
9191
}
9292

93+
#if NET5_0_OR_GREATER
94+
/// <summary>
95+
/// Converts a string to a Half.
96+
/// </summary>
97+
/// <param name="value">The value.</param>
98+
/// <returns>A Half.</returns>
99+
public static Half ToHalf(string value)
100+
{
101+
return Half.Parse(value, NumberFormatInfo.InvariantInfo);
102+
}
103+
#endif
104+
93105
/// <summary>
94106
/// Converts a string to an Int16.
95107
/// </summary>
@@ -200,6 +212,18 @@ public static string ToString(float value)
200212
return value.ToString("R", NumberFormatInfo.InvariantInfo);
201213
}
202214

215+
#if NET5_0_OR_GREATER
216+
/// <summary>
217+
/// Converts a Half to a string.
218+
/// </summary>
219+
/// <param name="value">The value.</param>
220+
/// <returns>A string.</returns>
221+
public static string ToString(Half value)
222+
{
223+
return value.ToString("G5", NumberFormatInfo.InvariantInfo);
224+
}
225+
#endif
226+
203227
/// <summary>
204228
/// Converts an Int32 to a string.
205229
/// </summary>

src/MongoDB.Bson/ObjectModel/Decimal128.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,20 @@ public static explicit operator Decimal128(float value)
259259
return new Decimal128(value);
260260
}
261261

262+
#if NET5_0_OR_GREATER
263+
/// <summary>
264+
/// Performs an explicit conversion from <see cref="Half"/> to <see cref="Decimal128"/>.
265+
/// </summary>
266+
/// <param name="value">The value.</param>
267+
/// <returns>
268+
/// The result of the conversion.
269+
/// </returns>
270+
public static explicit operator Decimal128(Half value)
271+
{
272+
return new Decimal128(value);
273+
}
274+
#endif
275+
262276
/// <summary>
263277
/// Performs an implicit conversion from <see cref="System.Int32"/> to <see cref="Decimal128"/>.
264278
/// </summary>
@@ -371,6 +385,20 @@ public static explicit operator float(Decimal128 value)
371385
return Decimal128.ToSingle(value);
372386
}
373387

388+
#if NET5_0_OR_GREATER
389+
/// <summary>
390+
/// Performs an explicit conversion from <see cref="Decimal128"/> to <see cref="Half"/>.
391+
/// </summary>
392+
/// <param name="value">The value to convert.</param>
393+
/// <returns>
394+
/// The result of the conversion.
395+
/// </returns>
396+
public static explicit operator Half(Decimal128 value)
397+
{
398+
return Decimal128.ToHalf(value);
399+
}
400+
#endif
401+
374402
/// <summary>
375403
/// Performs an explicit conversion from <see cref="Decimal128"/> to <see cref="System.Int32"/>.
376404
/// </summary>
@@ -962,6 +990,40 @@ public static float ToSingle(Decimal128 d)
962990
}
963991
}
964992

993+
#if NET5_0_OR_GREATER
994+
/// <summary>
995+
/// Converts the value of the specified <see cref="Decimal128"/> to the equivalent <see cref="Half"/>.
996+
/// </summary>
997+
/// <param name="d">The number to convert.</param>
998+
/// <returns>A <see cref="Half"/> equivalent to <paramref name="d" />.</returns>
999+
public static Half ToHalf(Decimal128 d)
1000+
{
1001+
if (Flags.IsFirstForm(d._highBits))
1002+
{
1003+
// TODO: implement this more efficiently
1004+
var stringValue = d.ToString();
1005+
return Half.Parse(stringValue, CultureInfo.InvariantCulture);
1006+
}
1007+
1008+
if (Flags.IsSecondForm(d._highBits))
1009+
{
1010+
return (Half)0.0;
1011+
}
1012+
1013+
if (Flags.IsPositiveInfinity(d._highBits))
1014+
{
1015+
return Half.PositiveInfinity;
1016+
}
1017+
1018+
if (Flags.IsNegativeInfinity(d._highBits))
1019+
{
1020+
return Half.NegativeInfinity;
1021+
}
1022+
1023+
return Half.NaN;
1024+
}
1025+
#endif
1026+
9651027
/// <summary>
9661028
/// Converts the value of the specified <see cref="Decimal128"/> to the equivalent 16-bit unsigned integer.
9671029
/// </summary>
@@ -1441,6 +1503,21 @@ public Decimal128(float value)
14411503
_lowBits = decimal128Value.GetIEEELowBits();
14421504
}
14431505

1506+
#if NET5_0_OR_GREATER
1507+
/// <summary>
1508+
/// Initializes a new instance of the <see cref="Decimal128"/> struct.
1509+
/// </summary>
1510+
/// <param name="value">The value.</param>
1511+
public Decimal128(Half value)
1512+
{
1513+
// TODO: implement this more efficiently
1514+
var stringValue = JsonConvert.ToString(value);
1515+
var decimal128Value = Decimal128.Parse(stringValue);
1516+
_highBits = MapIEEEHighBitsToDecimal128HighBits(decimal128Value.GetIEEEHighBits());
1517+
_lowBits = decimal128Value.GetIEEELowBits();
1518+
}
1519+
#endif
1520+
14441521
/// <summary>
14451522
/// Initializes a new instance of the <see cref="Decimal128"/> struct.
14461523
/// </summary>

0 commit comments

Comments
 (0)