Skip to content

Commit 8c4466d

Browse files
committed
CSHARP-4648: Standardize handling of decimal vs Decimal128.
1 parent 3ba8dc3 commit 8c4466d

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

src/MongoDB.Bson/Serialization/Serializers/ObjectSerializer.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,6 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati
303303
bsonWriter.WriteDateTime(bsonDateTime.MillisecondsSinceEpoch);
304304
return;
305305

306-
case TypeCode.Decimal:
307-
bsonWriter.WriteDecimal128((decimal)value);
308-
return;
309-
310306
case TypeCode.Double:
311307
bsonWriter.WriteDouble((double)value);
312308
return;
@@ -571,6 +567,11 @@ private static class DefaultFrameworkAllowedTypes
571567

572568
private static bool AllowedTypesImplementation(Type type)
573569
{
570+
if (typeof(BsonValue).IsAssignableFrom(type))
571+
{
572+
return true;
573+
}
574+
574575
return type.IsConstructedGenericType ? IsAllowedGenericType(type) : IsAllowedType(type);
575576

576577
static bool IsAllowedType(Type type) =>

tests/MongoDB.Bson.Tests/Serialization/Serializers/ObjectSerializerTests.cs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,23 @@ public void TestBoolean()
102102
Assert.True(bson.SequenceEqual(rehydrated.ToBson()));
103103
}
104104

105+
[Fact]
106+
public void TestBsonDecimal128()
107+
{
108+
var value = (BsonDecimal128)1.5M;
109+
var c = new C { Obj = value };
110+
111+
var json = c.ToJson();
112+
json.Should().Be("{ \"Obj\" : { \"_t\" : \"MongoDB.Bson.BsonDecimal128, MongoDB.Bson\", \"_v\" : NumberDecimal(\"1.5\") } }");
113+
114+
var bson = c.ToBson();
115+
var rehydrated = BsonSerializer.Deserialize<C>(bson);
116+
rehydrated.Obj.Should().BeOfType<BsonDecimal128>();
117+
rehydrated.Obj.Should().Be(value);
118+
119+
rehydrated.ToBson().Should().Equal(bson);
120+
}
121+
105122
[Fact]
106123
public void TestDateTime()
107124
{
@@ -118,27 +135,35 @@ public void TestDateTime()
118135
[Fact]
119136
public void TestDecimal()
120137
{
121-
var c = new C { Obj = 1.5M };
138+
var value = 1.5M;
139+
var c = new C { Obj = value };
140+
122141
var json = c.ToJson();
123-
var expected = "{ 'Obj' : NumberDecimal('1.5') }".Replace("'", "\"");
124-
Assert.Equal(expected, json);
142+
json.Should().Be("{ \"Obj\" : { \"_t\" : \"System.Decimal\", \"_v\" : \"1.5\" } }");
125143

126144
var bson = c.ToBson();
127145
var rehydrated = BsonSerializer.Deserialize<C>(bson);
128-
Assert.True(bson.SequenceEqual(rehydrated.ToBson()));
146+
rehydrated.Obj.Should().BeOfType<decimal>();
147+
rehydrated.Obj.Should().Be(value);
148+
149+
rehydrated.ToBson().Should().Equal(bson);
129150
}
130151

131152
[Fact]
132153
public void TestDecimal128()
133154
{
134-
var c = new C { Obj = (Decimal128)1.5M };
155+
var value = (Decimal128)1.5M;
156+
var c = new C { Obj = value };
157+
135158
var json = c.ToJson();
136-
var expected = "{ 'Obj' : NumberDecimal('1.5') }".Replace("'", "\"");
137-
Assert.Equal(expected, json);
159+
json.Should().Be("{ \"Obj\" : NumberDecimal(\"1.5\") }");
138160

139161
var bson = c.ToBson();
140162
var rehydrated = BsonSerializer.Deserialize<C>(bson);
141-
Assert.True(bson.SequenceEqual(rehydrated.ToBson()));
163+
rehydrated.Obj.Should().BeOfType<Decimal128>();
164+
rehydrated.Obj.Should().Be(value);
165+
166+
rehydrated.ToBson().Should().Equal(bson);
142167
}
143168

144169
[Fact]

0 commit comments

Comments
 (0)