JSON grain output skips curly brace closing

I am using Cereal C ++ v1.1.1 and is similar to the example given in the documentation, I am trying to do the following:

#include <sstream>
#include <iostream>
#include <cereal/archives/json.hpp>

int main() {
  std::ostringstream os;
  cereal::JSONOutputArchive archive(os);
  int x = 12;
  std::cout << os.str(); // JUST FOR DEMONSTRATION!


I expect the following:



but the closing curly brace is missing. Any idea what's missing in the code?


the addition archive.finishNode()

seems to solve the problem. But I would say that this is not a solution. According to the documentation operator()

, the operator call serializes the input parameters, why should I add finishNode



source to share

1 answer

I had the same issue and found the solution in a comment on the issue filed on Cereal GitHub: https://github.com/USCiLab/cereal/issues/101

The documentation states: "The archives are meant to be used in RAII in a manner and are guaranteed to flush their contents only for destruction ..." ( http://uscilab.github.io/cereal/quickstart.html ).

Your problem is that you are trying to print the contents of the stringstream before the archive is destroyed. At this point, the archive has no idea if you want to write more data to it in the future, so it refrains from streaming the closing parenthesis. You need to make sure that the archive's destructor has been called before printing the line.

Try the following:

int main()
  std::stringstream ss;
    cereal::JSONOutputArchive archive( ss );
    SomeData myData;
    archive( myData );
  std::cout << ss.str() << std::endl;

  return 0;


See the documentation for more information.



All Articles