Skip to content

Commit 2e6b906

Browse files
CSHARP-3207: Support parsing uuid as extended JSON representation for subtype 4 binary
1 parent 23a1f8c commit 2e6b906

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/MongoDB.Bson/IO/JsonReader.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,7 @@ private BsonType ParseExtendedJson()
13571357
case "$symbol": _currentValue = ParseSymbolExtendedJson(); return BsonType.Symbol;
13581358
case "$timestamp": _currentValue = ParseTimestampExtendedJson(); return BsonType.Timestamp;
13591359
case "$undefined": _currentValue = ParseUndefinedExtendedJson(); return BsonType.Undefined;
1360+
case "$uuid": _currentValue = ParseUuidExtendedJson(); return BsonType.Binary;
13601361
}
13611362
}
13621363
ReturnToBookmark(bookmark);
@@ -2077,6 +2078,28 @@ private BsonValue ParseUndefinedExtendedJson()
20772078
return BsonMaxKey.Value;
20782079
}
20792080

2081+
private BsonValue ParseUuidExtendedJson()
2082+
{
2083+
VerifyToken(":");
2084+
var uuidToken = PopToken();
2085+
if (uuidToken.Type != JsonTokenType.String)
2086+
{
2087+
var message = string.Format("JSON reader expected a string but found '{0}'.", uuidToken.Lexeme);
2088+
throw new FormatException(message);
2089+
}
2090+
VerifyToken("}");
2091+
2092+
var hexString = uuidToken.StringValue.Replace("-", "");
2093+
var bytes = BsonUtils.ParseHexString(hexString);
2094+
if (bytes.Length != 16)
2095+
{
2096+
var message = string.Format("Invalid $uuid string: '{0}'.", hexString);
2097+
throw new FormatException(message);
2098+
}
2099+
2100+
return new BsonBinaryData(bytes, BsonBinarySubType.UuidStandard);
2101+
}
2102+
20802103
private BsonValue ParseUUIDConstructor(string uuidConstructorName)
20812104
{
20822105
VerifyToken("(");

tests/MongoDB.Bson.Tests/Specifications/bson-corpus/tests/binary.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
"canonical_bson": "1D000000057800100000000473FFD26444B34C6990E8E7D1DFC035D400",
4040
"canonical_extjson": "{\"x\" : { \"$binary\" : {\"base64\" : \"c//SZESzTGmQ6OfR38A11A==\", \"subType\" : \"04\"}}}"
4141
},
42+
{
43+
"description": "subtype 0x04 UUID",
44+
"canonical_bson": "1D000000057800100000000473FFD26444B34C6990E8E7D1DFC035D400",
45+
"canonical_extjson": "{\"x\" : { \"$binary\" : {\"base64\" : \"c//SZESzTGmQ6OfR38A11A==\", \"subType\" : \"04\"}}}",
46+
"degenerate_extjson": "{\"x\" : { \"$uuid\" : \"73ffd264-44b3-4c69-90e8-e7d1dfc035d4\"}}"
47+
},
4248
{
4349
"description": "subtype 0x05",
4450
"canonical_bson": "1D000000057800100000000573FFD26444B34C6990E8E7D1DFC035D400",
@@ -81,5 +87,15 @@
8187
"description": "subtype 0x02 length negative one",
8288
"bson": "130000000578000600000002FFFFFFFFFFFF00"
8389
}
90+
],
91+
"parseErrors": [
92+
{
93+
"description": "$uuid wrong type",
94+
"string": "{\"x\" : { \"$uuid\" : { \"data\" : \"73ffd264-44b3-4c69-90e8-e7d1dfc035d4\"}}}"
95+
},
96+
{
97+
"description": "$uuid invalid value",
98+
"string": "{\"x\" : { \"$uuid\" : \"73ffd264-44b3-90e8-e7d1dfc035d4\"}}"
99+
}
84100
]
85101
}

0 commit comments

Comments
 (0)