GithubHelp home page GithubHelp logo

Comments (4)

danvratil avatar danvratil commented on July 24, 2024

From Qt doc:
"QNetworkReply is a sequential-access QIODevice, which means that once data is read from the object, it no longer kept by the device. It is therefore the application's responsibility to keep this data if it needs to."

You probably read the data somewhere else first, then try to read them again and pass it to QJson parser.

from qjson.

flavio avatar flavio commented on July 24, 2024

I checked the code. Everything works fine when the QNetworkReply is done.

For example:

void Downloader::replyFinished(QNetworkReply* reply)
{
  QJson::Parser parser;
  bool ok;

  QVariant v = parser.parse(reply, &ok);
  qDebug() << ok;
  qDebug() << v;
}

Works like a charm.

The issue happens with code like this one:

  QNetworkReply* reply = m_manager->get(QNetworkRequest(QUrl(url)));
  QJson::Parser parser;
  bool ok;

  QVariant v = parser.parse(reply, &ok);
  qDebug() << ok;
  qDebug() << v;

In this case ok will be set to true and v will be an empty variant. That happens because the QNetworkReply::atEnd() returns true inside of JsonScanner::yylex.

The only solution to this problem would be to provide a dedicated Parser::parse(QNetworkReply*, bool*) that somehow takes advantage of the readyRead() signal.

Hints/opinions?

from qjson.

danvratil avatar danvratil commented on July 24, 2024

The first approach is the correct one. You have to wait for reply to receive all data before accessing them.

In the second example, you send a GET request, which is AFAIK actually started when the method returns to event loop, so when you immediately pass it to QJson, it calls readAll() and receives an empty string. ok is set to truebecause empty string is valid and is correctly parsed into an empty object.

It's your responsibility to ensure that you pass a ready QIODevice to QJson::Parser.

from qjson.

flavio avatar flavio commented on July 24, 2024

I totally agree with you. I'm going to close this issue as a INVALID one.

BTW: this issue was filed by a user a long time ago on the old bug tracking system, but I never had time to look into it.

from qjson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.