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

Added JSONInputArchive constructor from an already parsed rapidjson::Document #653

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dev-plvlml
Copy link

@dev-plvlml dev-plvlml commented Aug 2, 2020

Hi,
And thanks for a great library!

I've got the following problem.

std::string jsonStr;
rapidjson::Document doc;
doc.Parse(jsonStr.data());

if (doc["type"].GetString() == "foo") {  // "type" field can't be renamed
    Foo foo{};
    {
        std::istringstream input{jsonStr};
        cereal::JSONInputArchive archive{input};  // creates a new Document and parses the string again!
        serialize(archive, foo);
    }
    applyStaticallyTypedCallback(foo);

} else if (doc["type"].GetString() == "bar") {
    Bar bar{};
    // etc.
}

I'm aware that some similar (de)serializing could be done with the built-in Polymorphic Types feature, but it uses its own internal fields like "polymorphic_name" and "polymorphic_id", whose names are hardcoded, and the code around them seems not so easy to intervene and customize.

I propose to allow constructing JSONInputArchive via move-from an already parsed rapidjson::Document to fix at least the performance issues of this case:

rapidjson::Document doc;

if (doc["type"].GetString() == "foo") {
    Foo foo{};
    {
        cereal::JSONInputArchive archive{std::move(doc)};  // no more parsing!
        serialize(archive, foo);
    }
    applyStaticallyTypedCallback(foo);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant