diff --git a/src/parser.cpp b/src/parser.cpp index 0c63448..964e8c6 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -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(); @@ -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(); } @@ -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; } diff --git a/src/parser.h b/src/parser.h index c3132f5..241ab81 100644 --- a/src/parser.h +++ b/src/parser.h @@ -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