Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RUST-1110 bson::to_vec fail to serialize any Serialize #357

Open
gkorland opened this issue May 3, 2022 · 2 comments
Open

RUST-1110 bson::to_vec fail to serialize any Serialize #357

gkorland opened this issue May 3, 2022 · 2 comments
Assignees
Labels
tracked-in-jira Ticket filed in Mongo's Jira system

Comments

@gkorland
Copy link

gkorland commented May 3, 2022

When trying to serialize Serialize object using bson::to_vec it gets to update_element_type which returns an error if the top level object is not ElementType::EmbeddedDocument

if matches!(t, ElementType::EmbeddedDocument) {

Is there a reason for this limitation? I tested the code without this check and it works.

@gkorland
Copy link
Author

gkorland commented May 4, 2022

I think I found the issue the problem is that I'm trying to serialize a Vec<Value> which gets me to this piece of code:

    fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
        self.update_element_type(ElementType::Array)?;
        DocumentSerializer::start(&mut *self)
    }

Which just can't work.... Since ElementType::Array doesn't match ElementType::EmbeddedDocument

@bajanam bajanam removed the triage label May 5, 2022
@patrickfreed
Copy link
Contributor

In BSON, the only supported top-level type is a document, unlike JSON (for example) which supports any of its types at the top level (see https://bsonspec.org/spec.html and https://www.json.org/json-en.html). For this reason, bson::to_vec raises an error if the value produced is not a document, since it wouldn't be valid BSON.

We do plan to eventually introduce a bson::to_raw_bson method, which would output a RawBson value and could successfully serialize values that don't serialize to documents at the top level. You can follow RUST-1110 to track progress on that. We currently haven't prioritized that work though, since it would require a decent amount of refactoring in the serializer and represents a somewhat niche use case. If you don't mind me asking, what is your use case for serializing the Vec<Value> at the top level?

@patrickfreed patrickfreed added the tracked-in-jira Ticket filed in Mongo's Jira system label May 10, 2022
@bajanam bajanam changed the title bson::to_vec fail to serialize any Serialize RUST-1110 bson::to_vec fail to serialize any Serialize May 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tracked-in-jira Ticket filed in Mongo's Jira system
Projects
None yet
Development

No branches or pull requests

4 participants