Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Accessing json data from .qrc file using std::ifstream and nlohmann json
Forum Updated to NodeBB v4.3 + New Features

Accessing json data from .qrc file using std::ifstream and nlohmann json

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 582 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shreya_agrawal
    wrote on last edited by
    #1

    I am trying to parse a json file, placed in the .qrc directory of my project. When I use QFile and QJsonDocument, and QJsonObject; it works fine. But, when I try to use std::ifstream and nlohmann::json, the file is not parsed.
    Also, if::stream and nlohmann::json function parses the file if I give a hard coded path, instead of including the file in the .qrc folder of the project.
    I am confident about the format of my json file.
    So, is there any way to parse the json file, present in the .qrc directory using std::ifstream and nlohmann::json?

    This is how I am parsing the json data:

    std::ifstream file1(jsonFilePath1);
    jsonObject1 = nlohmann::json::parse(file1);
    

    This is the exception I am getting:
    terminate called after throwing an instance of 'nlohmann::json_abi_v3_11_3::detail::type_error'
    what(): [json.exception.type_error.304] cannot use at() with null

    1 Reply Last reply
    1
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      Qrc files are only accessible by Qt classes. How should ifstream know how to read a qrc file?
      You need to read the file using Qt classes and pass the content data to other classes.

      1 Reply Last reply
      2
      • Paul ColbyP Offline
        Paul ColbyP Offline
        Paul Colby
        wrote on last edited by
        #3

        Hi @shreya_agrawal,

        As @Bonnie said, only QFIle knows how to access files embedded via the Qt Resource System, so your options are:

        1. Use QFile::copy() to copy the file out of qrc, onto disk somewhere; or
        2. Use QFile to read the contents of the file, then pass the contents to nlohmann::json::parse() (there's an overload the accepts a std::string::iterator, for example); or
        3. If you really need to parse in a streaming mode, with nlohmann::json, then you'll need to use a QFile wrapper that implements std::istream; something like this: https://stackoverflow.com/questions/5204335/how-to-use-a-qfile-with-stdiostream#answers

        Cheers.

        S 1 Reply Last reply
        3
        • Paul ColbyP Paul Colby

          Hi @shreya_agrawal,

          As @Bonnie said, only QFIle knows how to access files embedded via the Qt Resource System, so your options are:

          1. Use QFile::copy() to copy the file out of qrc, onto disk somewhere; or
          2. Use QFile to read the contents of the file, then pass the contents to nlohmann::json::parse() (there's an overload the accepts a std::string::iterator, for example); or
          3. If you really need to parse in a streaming mode, with nlohmann::json, then you'll need to use a QFile wrapper that implements std::istream; something like this: https://stackoverflow.com/questions/5204335/how-to-use-a-qfile-with-stdiostream#answers

          Cheers.

          S Offline
          S Offline
          shreya_agrawal
          wrote on last edited by
          #4

          @Paul-Colby
          Thank you so much for such a detailed solution!
          I think I would go with option 3 in my case.

          1 Reply Last reply
          0
          • S shreya_agrawal has marked this topic as solved on

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved