Skip to content
This repository was archived by the owner on Nov 17, 2025. It is now read-only.
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
25 changes: 23 additions & 2 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,22 @@ Parser::~Parser()
delete d;
}


QVariant Parser::parse (QIODevice* io, bool* ok)
{
if (!io->isOpen()) {
if (!io->open(QIODevice::ReadOnly)) {
if (ok != 0)
*ok = false;
qCritical ("Error opening device");
return QVariant();
}
}
return parse(io, true, ok);
}


QVariant Parser::parse (QIODevice* io,bool alwaysCloseDevice, bool* ok)
{
d->reset();

Expand All @@ -81,13 +96,16 @@ QVariant Parser::parse (QIODevice* io, bool* ok)
qCritical ("Error opening device");
return QVariant();
}
alwaysCloseDevice = true;
}

if (!io->isReadable()) {
if (ok != 0)
*ok = false;
qCritical ("Device is not readable");
io->close();
if(alwaysCloseDevice) {
io->close();
}
return QVariant();
}

Expand All @@ -110,7 +128,10 @@ QVariant Parser::parse (QIODevice* io, bool* ok)
if (ok != 0)
*ok = !d->m_error;

io->close();
if(alwaysCloseDevice) {
io->close();
}

return d->m_result;
}

Expand Down
9 changes: 9 additions & 0 deletions src/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ namespace QJson {
*/
QVariant parse(QIODevice* io, bool* ok = 0);

/**
* Read JSON string from the I/O Device and converts it to a QVariant object
* @param io Input output device
* @param alwaysCloseDevice if false, io is kept in the same open state, otherwise io is left closed
* @param ok if a conversion error occurs, *ok is set to false; otherwise *ok is set to true.
* @returns a QVariant object generated from the JSON string
*/
QVariant parse(QIODevice* io,bool alwaysCloseDevice, bool* ok = 0);

/**
* This is a method provided for convenience.
* @param jsonData data containing the JSON object representation
Expand Down