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

Adding ability to detect a RapidJSON parse error in JSONInputArchive #658

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions include/cereal/archives/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,20 +432,37 @@ namespace cereal

//! Construct, reading from the provided stream
/*! @param stream The stream to read from */
JSONInputArchive(std::istream & stream) :
JSONInputArchive(std::istream & stream, bool shouldSuppressParseErrors = false) :
InputArchive<JSONInputArchive>(this),
itsNextName( nullptr ),
itsReadStream(stream)
itsReadStream(stream),
suppressParseError(shouldSuppressParseErrors),
hasRapidParseError(false)
{
itsDocument.ParseStream<>(itsReadStream);
if (itsDocument.IsArray())
itsIteratorStack.emplace_back(itsDocument.Begin(), itsDocument.End());
hasRapidParseError = itsDocument.HasParseError();
if (hasRapidParseError && suppressParseError)
{
// Do nothing if RapidJSON fails to parse the input stream and we want to suppress parse errors
// In this case, it's up to the client to check against hasParseError() or operator bool()
}
else
itsIteratorStack.emplace_back(itsDocument.MemberBegin(), itsDocument.MemberEnd());
{
if (itsDocument.IsArray())
itsIteratorStack.emplace_back(itsDocument.Begin(), itsDocument.End());
else
itsIteratorStack.emplace_back(itsDocument.MemberBegin(), itsDocument.MemberEnd());
}
}

~JSONInputArchive() CEREAL_NOEXCEPT = default;

//! Whether or not RapidJSON failed to parse the input stream
bool hasParseError() const { return hasRapidParseError; }

//! Whether or not RapidJSON failed to parse the input stream
operator bool() const { return !hasRapidParseError; }

//! Loads some binary data, encoded as a base64 string
/*! This will automatically start and finish a node to load the data, and can be called directly by
users.
Expand Down Expand Up @@ -733,6 +750,8 @@ namespace cereal
ReadStream itsReadStream; //!< Rapidjson write stream
std::vector<Iterator> itsIteratorStack; //!< 'Stack' of rapidJSON iterators
CEREAL_RAPIDJSON_NAMESPACE::Document itsDocument; //!< Rapidjson document
bool suppressParseError; //!< Whether this should attempt to emplace when RapidJSON parse fails
bool hasRapidParseError; //!< Whether RapidJSON failed to parse the input
};

// ######################################################################
Expand Down
16 changes: 16 additions & 0 deletions sandbox/sandbox_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,5 +442,21 @@ int main()
std::cout << (ull == std::numeric_limits<unsigned long long>::max()) << std::endl;
}

// test parse error
std::stringstream errstr;
{
std::cout << "---------------------" << std::endl;
cereal::JSONInputArchive ar(errstr, true);
std::cout << ar.hasParseError() << std::endl;
if (ar)
{
std::cout << "fail" << std::endl;
}
else
{
std::cout << "success" << std::endl;
}
}

return 0;
}