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
Servers for Qt installer are currently down

JSON extension

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 459 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.
  • O Offline
    O Offline
    ODБOï
    wrote on 31 Jan 2020, 10:25 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

    J 1 Reply Last reply 31 Jan 2020, 10:28
    0
    • O ODБOï
      31 Jan 2020, 10:47

      @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.

      J Offline
      J Offline
      JonB
      wrote on 31 Jan 2020, 10:49 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.

      O 1 Reply Last reply 31 Jan 2020, 10:50
      3
      • O ODБOï
        31 Jan 2020, 10:25

        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

        J Offline
        J Offline
        JonB
        wrote on 31 Jan 2020, 10:28 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.

        O 1 Reply Last reply 31 Jan 2020, 10:47
        2
        • J JonB
          31 Jan 2020, 10:28

          @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.

          O Offline
          O Offline
          ODБOï
          wrote on 31 Jan 2020, 10:47 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.

          J 1 Reply Last reply 31 Jan 2020, 10:49
          0
          • O ODБOï
            31 Jan 2020, 10:47

            @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.

            J Offline
            J Offline
            JonB
            wrote on 31 Jan 2020, 10:49 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.

            O 1 Reply Last reply 31 Jan 2020, 10:50
            3
            • J JonB
              31 Jan 2020, 10:49

              @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.

              O Offline
              O Offline
              ODБOï
              wrote on 31 Jan 2020, 10:50 last edited by
              #5

              ok thank you @JonB

              1 Reply Last reply
              1

              1/5

              31 Jan 2020, 10:25

              • Login

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