Skip to content

Commit 1db6945

Browse files
committed
CSHARP-2305: Support $numberInt when reading extended JSON.
1 parent 5ec4bba commit 1db6945

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/MongoDB.Bson/IO/JsonReader.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,7 @@ private BsonType ParseExtendedJson()
12181218
case "$maxkey": case "$maxKey": _currentValue = ParseMaxKeyExtendedJson(); return BsonType.MaxKey;
12191219
case "$minkey": case "$minKey": _currentValue = ParseMinKeyExtendedJson(); return BsonType.MinKey;
12201220
case "$numberDecimal": _currentValue = ParseNumberDecimalExtendedJson(); return BsonType.Decimal128;
1221+
case "$numberInt": _currentValue = ParseNumberIntExtendedJson(); return BsonType.Int32;
12211222
case "$numberLong": _currentValue = ParseNumberLongExtendedJson(); return BsonType.Int64;
12221223
case "$oid": _currentValue = ParseObjectIdExtendedJson(); return BsonType.ObjectId;
12231224
case "$regex": _currentValue = ParseRegularExpressionExtendedJson(); return BsonType.RegularExpression;
@@ -1548,6 +1549,26 @@ private BsonValue ParseNumberDecimalExtendedJson()
15481549
return (BsonDecimal128)value;
15491550
}
15501551

1552+
private BsonValue ParseNumberIntExtendedJson()
1553+
{
1554+
VerifyToken(":");
1555+
1556+
int value;
1557+
var valueToken = PopToken();
1558+
if (valueToken.Type == JsonTokenType.Int32)
1559+
{
1560+
value = valueToken.Int32Value;
1561+
}
1562+
else
1563+
{
1564+
var message = string.Format("JSON reader expected an integer but found '{0}'.", valueToken.Lexeme);
1565+
throw new FormatException(message);
1566+
}
1567+
1568+
VerifyToken("}");
1569+
return (BsonInt32)value;
1570+
}
1571+
15511572
private BsonValue ParseNumberLongExtendedJson()
15521573
{
15531574
VerifyToken(":");

tests/MongoDB.Bson.Tests/IO/JsonReaderTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,21 @@ public void TestInt32()
525525
Assert.Equal(json, BsonSerializer.Deserialize<int>(json).ToJson());
526526
}
527527

528+
[Theory]
529+
[InlineData("{ $numberInt: 1 }", 1)]
530+
[InlineData("{ $numberInt: -2147483648 }", -2147483648)]
531+
[InlineData("{ $numberInt: 2147483647 }", 2147483647)]
532+
public void TestInt32ExtendedJson(string json, int expectedResult)
533+
{
534+
using (var reader = new JsonReader(json))
535+
{
536+
var result = reader.ReadInt32();
537+
538+
result.Should().Be(expectedResult);
539+
reader.IsAtEndOfFile().Should().BeTrue();
540+
}
541+
}
542+
528543
[Theory]
529544
[InlineData("Number(123)")]
530545
[InlineData("NumberInt(123)")]

0 commit comments

Comments
 (0)