Skip to content

Cannot serialize and immediatly deserialize json to/from bson #2892

@eike-fokken

Description

@eike-fokken

What is the issue you have?

When compiling the following code with the latest development branch

#include <iostream>
#include <nlohmann/json.hpp>

using json = nlohmann::json;

int main() {

  json input;
  input["test"] = "toast";

  std::vector<uint8_t> output = json::to_ubjson(input);
  json j = json::from_bson(output);

  std::cout << j << std::endl;

  return 0;
}

I get an error like

libc++abi: terminating with uncaught exception of type nlohmann::detail::parse_error: [json.exception.parse_error.110] parse error at byte 17: syntax error while parsing BSON cstring: unexpected end of input

This code simply combines the examples from json::to_ubson and json::from_ubson.
Therefore I guess one of the following:

  1. I made a mistake
  2. The examples in these two functions should be updated
  3. There is a bug in the library.

What is the expected behavior?

I think, this should work and write out

{
"test:"toast"
}

Which compiler and operating system are you using?

  • Compiler: both clang++ 11 and g++ 10
  • Operating system: Debian

Which version of the library did you use?

  • latest release version 3.9.1
  • other release - please state the version: ___
  • the develop branch

In case it is 1. from above, please let me know. If it is one of the others, I'm happy to help debug this, if that's useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions