mongodbmongodb-.net-driver

C# BsonDocument.Parse and invalid JSON


Am I missing something? BsonDocument.Parse() appears to allow invalid JSON input where there is valid document before the invalid characters start?

For example I would consider invalid, but parses fine:

{
    "_id": ObjectId("5731080e61737e37f84c848d")
}
This is not a typo(

What happens is it will parse the initial document and ignore anything after the initial document.

Surely it should treat this as invalid?

This is likely because the parser simply parses the first document then stops, but I'd argue if anything else is in the token stream it should be considered a fail.


Solution

  • BsonDocument.Parse() appears to allow invalid JSON input where there is valid document before the invalid characters start?

    Yes, the JsonReader allows the reading of multiple top-level documents, in accordance with how many formats output a document per-line. Looks like the BsonDocument.Parse only read until the end of the first document, while the JsonReader knows there is more to read and waiting to read the next one (which will throw exception).

    Surely it should treat this as invalid?

    I've opened a ticket CSHARP-1676 for this case. Perhaps if there is an extra 'text' after the first document it should also throw FormatException. Feel free to watch/upvote the ticket to receive updates.

    This is based on mongocsharpdriver v2.2.x

    Update: The ticket has been resolved and targeted for release v2.2.5. See ticket for more information.