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

Catch short documents in JSON input #677

Merged
merged 1 commit into from
May 5, 2021
Merged
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
13 changes: 8 additions & 5 deletions include/cereal/archives/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,16 +483,16 @@ namespace cereal
Iterator() : itsIndex( 0 ), itsType(Null_) {}

Iterator(MemberIterator begin, MemberIterator end) :
itsMemberItBegin(begin), itsMemberItEnd(end), itsIndex(0), itsType(Member)
itsMemberItBegin(begin), itsMemberItEnd(end), itsIndex(0), itsSize(std::distance(begin, end)), itsType(Member)
{
if( std::distance( begin, end ) == 0 )
if( itsSize == 0 )
itsType = Null_;
}

Iterator(ValueIterator begin, ValueIterator end) :
itsValueItBegin(begin), itsIndex(0), itsType(Value)
itsValueItBegin(begin), itsIndex(0), itsSize(std::distance(begin, end)), itsType(Value)
{
if( std::distance( begin, end ) == 0 )
if( itsSize == 0 )
itsType = Null_;
}

Expand All @@ -506,6 +506,9 @@ namespace cereal
//! Get the value of the current node
GenericValue const & value()
{
if( itsIndex >= itsSize )
throw cereal::Exception("No more objects in input");

switch(itsType)
{
case Value : return itsValueItBegin[itsIndex];
Expand Down Expand Up @@ -546,7 +549,7 @@ namespace cereal
private:
MemberIterator itsMemberItBegin, itsMemberItEnd; //!< The member iterator (object)
ValueIterator itsValueItBegin; //!< The value iterator (array)
size_t itsIndex; //!< The current index of this iterator
size_t itsIndex, itsSize; //!< The current index of this iterator
enum Type {Value, Member, Null_} itsType; //!< Whether this holds values (array) or members (objects) or nothing
};

Expand Down