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. JSON extension
Forum Updated to NodeBB v4.3 + New Features

JSON extension

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 494 Views
  • 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.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by
    #1

    Hi,
    is it normal / safe practice to save/load json files without extension (.json) ?

    QFile jsonFile("file");
       if(jsonFile.open(QFile::ReadOnly)){
    
          QJsonDocument jrampe(QJsonDocument::fromJson(jsonFile.readAll()));
          QJsonArray toolArr = jrampe.array();
    ..
    

    Thanks

    JonBJ 1 Reply Last reply
    0
    • ODБOïO ODБOï

      @JonB said in JSON extension:

      What do you mean?

      sorry i forgot to specify : i want to save the file without any extension.

      please let me explain : its just for convenience,
      my user will enter informations, then i will construct objects with his input and save in json format. I want him to enter the file name also but not the extension!

      so i do :

      void saveFile(QString filename){
       
        QFile jsonFile(fileName+".json")
      }
      

      Then the user can load his saved files.

      And for loading the file, i show a listView with fileNames for my user
      and i have to write this to hide extension

      function noExtension(fileName){
           return fileName.substring(0,fileName.lastIndexOf("."))
       }
      

      because its usless to show the extension to my user..
      this is why i wanted to simply save the file with no extension.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #4

      @LeLev
      If you wish/prefer to save the file without .json extension, that's OK, there is no law against it! In itself it certainly won't interfere with your ability to read it back in and parse as JSON.

      ODБOïO 1 Reply Last reply
      3
      • ODБOïO ODБOï

        Hi,
        is it normal / safe practice to save/load json files without extension (.json) ?

        QFile jsonFile("file");
           if(jsonFile.open(QFile::ReadOnly)){
        
              QJsonDocument jrampe(QJsonDocument::fromJson(jsonFile.readAll()));
              QJsonArray toolArr = jrampe.array();
        ..
        

        Thanks

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #2

        @LeLev
        What do you mean? It's up to you what extension you put on a file. .json would be a reasonable choice, but it's not compulsory. (It certainly won't matter to Qt/QFile.) Since here you are reading an existing file, you don't have choice, it's whatever name it was saved under.

        Whether it has an extension or not --- e.g. if you try to read a non- .json and it turns out it is not a JSON doc in the first place --- you may get failure. https://doc.qt.io/qt-5/qjsondocument.html#fromJson

        Returns a valid (non-null) QJsonDocument if the parsing succeeds. If it fails, the returned document will be null, and the optional error variable will contain further details about the error.

        You need to test for this (in all cases).

        Personally I do save JSON docs as .json, so I know what's going on.

        ODБOïO 1 Reply Last reply
        2
        • JonBJ JonB

          @LeLev
          What do you mean? It's up to you what extension you put on a file. .json would be a reasonable choice, but it's not compulsory. (It certainly won't matter to Qt/QFile.) Since here you are reading an existing file, you don't have choice, it's whatever name it was saved under.

          Whether it has an extension or not --- e.g. if you try to read a non- .json and it turns out it is not a JSON doc in the first place --- you may get failure. https://doc.qt.io/qt-5/qjsondocument.html#fromJson

          Returns a valid (non-null) QJsonDocument if the parsing succeeds. If it fails, the returned document will be null, and the optional error variable will contain further details about the error.

          You need to test for this (in all cases).

          Personally I do save JSON docs as .json, so I know what's going on.

          ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by ODБOï
          #3

          @JonB said in JSON extension:

          What do you mean?

          sorry i forgot to specify : i want to save the file without any extension.

          please let me explain : its just for convenience,
          my user will enter informations, then i will construct objects with his input and save in json format. I want him to enter the file name also but not the extension!

          so i do :

          void saveFile(QString filename){
           
            QFile jsonFile(fileName+".json")
          }
          

          Then the user can load his saved files.

          And for loading the file, i show a listView with fileNames for my user
          and i have to write this to hide extension

          function noExtension(fileName){
               return fileName.substring(0,fileName.lastIndexOf("."))
           }
          

          because its usless to show the extension to my user..
          this is why i wanted to simply save the file with no extension.

          JonBJ 1 Reply Last reply
          0
          • ODБOïO ODБOï

            @JonB said in JSON extension:

            What do you mean?

            sorry i forgot to specify : i want to save the file without any extension.

            please let me explain : its just for convenience,
            my user will enter informations, then i will construct objects with his input and save in json format. I want him to enter the file name also but not the extension!

            so i do :

            void saveFile(QString filename){
             
              QFile jsonFile(fileName+".json")
            }
            

            Then the user can load his saved files.

            And for loading the file, i show a listView with fileNames for my user
            and i have to write this to hide extension

            function noExtension(fileName){
                 return fileName.substring(0,fileName.lastIndexOf("."))
             }
            

            because its usless to show the extension to my user..
            this is why i wanted to simply save the file with no extension.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

            @LeLev
            If you wish/prefer to save the file without .json extension, that's OK, there is no law against it! In itself it certainly won't interfere with your ability to read it back in and parse as JSON.

            ODБOïO 1 Reply Last reply
            3
            • JonBJ JonB

              @LeLev
              If you wish/prefer to save the file without .json extension, that's OK, there is no law against it! In itself it certainly won't interfere with your ability to read it back in and parse as JSON.

              ODБOïO Offline
              ODБOïO Offline
              ODБOï
              wrote on last edited by
              #5

              ok thank you @JonB

              1 Reply Last reply
              1

              • Login

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