Skip to content

Commit 0eb71aa

Browse files
JamesKovacsDmitryLukyanov
authored andcommitted
CSHARP-4496: Add decimal support to ObjectSerializer. (#1024)
1 parent 0b1f14a commit 0eb71aa

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

src/MongoDB.Bson/IO/JsonReader.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,9 @@ public override void SkipValue()
804804
case BsonType.DateTime:
805805
ReadDateTime();
806806
break;
807+
case BsonType.Decimal128:
808+
ReadDecimal128();
809+
break;
807810
case BsonType.Document:
808811
ReadStartDocument();
809812
while (ReadBsonType() != BsonType.EndOfDocument)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati
272272
bsonWriter.WriteDateTime(bsonDateTime.MillisecondsSinceEpoch);
273273
return;
274274

275+
case TypeCode.Decimal:
276+
bsonWriter.WriteDecimal128((decimal)value);
277+
return;
278+
275279
case TypeCode.Double:
276280
bsonWriter.WriteDouble((double)value);
277281
return;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 FluentAssertions;
17+
using MongoDB.Bson.Serialization;
18+
using Xunit;
19+
20+
namespace MongoDB.Bson.Tests.Jira
21+
{
22+
23+
public class CSharp4496
24+
{
25+
[Fact]
26+
public void ObjectSerializer_should_deserialize_decimals_successfully()
27+
{
28+
const string json = "{ 'Object' : NumberDecimal('1.5') }";
29+
30+
dynamic rehydrated = BsonSerializer.Deserialize<object>(json);
31+
((decimal)rehydrated.Object).Should().Be(1.5m);
32+
}
33+
}
34+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@ public void TestDateTime()
111111
Assert.True(bson.SequenceEqual(rehydrated.ToBson()));
112112
}
113113

114+
[Fact]
115+
public void TestDecimal()
116+
{
117+
var c = new C { Obj = 1.5M };
118+
var json = c.ToJson();
119+
var expected = "{ 'Obj' : NumberDecimal('1.5') }".Replace("'", "\"");
120+
Assert.Equal(expected, json);
121+
122+
var bson = c.ToBson();
123+
var rehydrated = BsonSerializer.Deserialize<C>(bson);
124+
Assert.True(bson.SequenceEqual(rehydrated.ToBson()));
125+
}
126+
114127
[Fact]
115128
public void TestDecimal128()
116129
{

0 commit comments

Comments
 (0)