Skip to content

Interoperability with libbson #2

Open
@lucianoiam

Description

@lucianoiam

Round-trip encoding/decoding with js-bson works great, but I am having a rough time trying to exchange data with a client written in C using libbson from the MongoDB C driver.

According to the spec the first 4 bytes in a BSON document must be interpreted as an int32 with the total number of bytes comprising the document.

js-bson writes this header by taking the serialized string length:

return readAsInt32LE( bson.length + 5 ) + bson + '\x00';

The string's length property value is not necessarily equal to the number of bytes comprising the string:

console.log('\80\00'.length)
>> 3

console.log('\00\00'.length)
>> 2

When string length == string byte count libbson correctly deserializes documents, otherwise deserialization fails due to a validation check.

Does this make sense?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions