cereal icon indicating copy to clipboard operation
cereal copied to clipboard

Issue with closing braces in JSON using std::stringstream

Open jonah-gourlay44 opened this issue 11 months ago • 1 comments

It seems like the only way to get a final closing brace in the output JSON when using std::stringstream with cereal::JSONOutputArchive is to destroy the archive before using the string. Is this intentional? Making a call to cereal::JSONOutputArchive::finishNode causes an access violation on destruction of the archive.

Example:

int main()
{
    std::stringstream ss;
    auto* o_ar = new cereal::JSONOutputArchive(ss);
    
    ArchiveObject m;
    /* initialization */
    
    (*o_ar)(CEREAL_NVP(m));
    
    std::cout << ss.str() << std::endl; // Won't have final closing bracket '}' in output

    delete o_ar;    

    std::cout << ss.str() << std::endl; // Now final closing bracket '}' appears in output

    return 0;
}

This makes reusing an archive impossible, because I will have to destroy it each time I want to serialize a new object.

Even if archive reusability goes against the design intention here, I still have to wait for the archive to go out of scope or destroy it manually in order for the std::stringstream to store valid JSON. I've added a call to check if itsNodeStack is empty in the JSONOutputArchive destructor for now so I can make a call to finishNode before the archive is destroyed.

For context, I am developing an RPC application in which I will need the complete serialized string before the archive is destroyed.

jonah-gourlay44 avatar Mar 02 '24 02:03 jonah-gourlay44