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. Qt Json number format changes automatically
Forum Updated to NodeBB v4.3 + New Features

Qt Json number format changes automatically

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 2.0k Views 2 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.
  • K Offline
    K Offline
    kyujin kim
    wrote on last edited by
    #1

    Hi all!!

    I use QJsonDocument and QJsonObject
    and... I have an issue.
    The issue is number format is automatically changed.
    like below.

    {
     "age" : 6000000,
     "name" : "John"
    }
    

    become

    {
     "age" : 6e+06,
     "name" : "John"
    }
    

    after pass through QJsonDocument and QJsonObject.

    ========================================================================================

    I have a JSON format data in the txt file.
    The data is like above.

    I import file via QJsonDocument to QJsonObject

    QFile file("input_file.txt");
    QJsonObject json_object = QJsonDocument::fromJson(file.readAll()).object();
    

    I change the name

    json_object["name"] = "Jon";
    

    And export JSON data to file

    std::ofstream output_file;
    output_file.open("output_file.txt", std::ofstream::out | std::ofstream::trunc);
    if(output_file.is_open())
    {
    	output_file << QJsonDocument(json_object).toJson().toStdString().c_str();
    	output_file .close();
    }
    

    After this process, json data changes
    from this

    {
     "age" : 6000000,
     "name" : "John"
    }
    

    to this

    {
     "age" : 6e+06,
     "name" : "Jon"
    }
    

    I didn't touch the age but format is changed!

    ========================================================================================

    After taking hard time, I found the reason.
    If I have 6 or more continuous zero digit from 0, it becomes e+06
    ex1) 6000000 -> 6e+06
    ex2) 190000000 -> 1.9e+08
    ex3) 60000001 -> 60000001

    Currently, I just put 1 at the end of digit.
    like 6000001 instead of 6000000.

    But I think there's way to fix the format.
    Is there anyone who knows about it?

    Thanks,

    ========================================================================================

    I can change manually, I know.
    But In my json file, I have a looooooooooooot of numbers...

    ========================================================================================

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

      Hi @kyujin-kim,

      That behaviour is a little unfortunate (though still technically valid, depending on how you look at it).

      What version of Qt are you using? It looks to me like this would no longer happen from Qt 5.10 onwards, thanks to this change.

      Cheers.

      K 1 Reply Last reply
      3
      • K kyujin kim

        Hi all!!

        I use QJsonDocument and QJsonObject
        and... I have an issue.
        The issue is number format is automatically changed.
        like below.

        {
         "age" : 6000000,
         "name" : "John"
        }
        

        become

        {
         "age" : 6e+06,
         "name" : "John"
        }
        

        after pass through QJsonDocument and QJsonObject.

        ========================================================================================

        I have a JSON format data in the txt file.
        The data is like above.

        I import file via QJsonDocument to QJsonObject

        QFile file("input_file.txt");
        QJsonObject json_object = QJsonDocument::fromJson(file.readAll()).object();
        

        I change the name

        json_object["name"] = "Jon";
        

        And export JSON data to file

        std::ofstream output_file;
        output_file.open("output_file.txt", std::ofstream::out | std::ofstream::trunc);
        if(output_file.is_open())
        {
        	output_file << QJsonDocument(json_object).toJson().toStdString().c_str();
        	output_file .close();
        }
        

        After this process, json data changes
        from this

        {
         "age" : 6000000,
         "name" : "John"
        }
        

        to this

        {
         "age" : 6e+06,
         "name" : "Jon"
        }
        

        I didn't touch the age but format is changed!

        ========================================================================================

        After taking hard time, I found the reason.
        If I have 6 or more continuous zero digit from 0, it becomes e+06
        ex1) 6000000 -> 6e+06
        ex2) 190000000 -> 1.9e+08
        ex3) 60000001 -> 60000001

        Currently, I just put 1 at the end of digit.
        like 6000001 instead of 6000000.

        But I think there's way to fix the format.
        Is there anyone who knows about it?

        Thanks,

        ========================================================================================

        I can change manually, I know.
        But In my json file, I have a looooooooooooot of numbers...

        ========================================================================================

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        @kyujin-kim said in Qt Json number format changes automatically:

        But I think there's way to fix the format.
        Is there anyone who knows about it?

        All JSON numbers are floating point, and there can be no distinction between 6000000 and 6e+06.
        Consequently, all JSON parsers should know what 6e+06 means, so why do you want the number to be specifically formatted as an integer?

        Read and abide by the Qt Code of Conduct

        K 2 Replies Last reply
        4
        • kshegunovK kshegunov

          @kyujin-kim said in Qt Json number format changes automatically:

          But I think there's way to fix the format.
          Is there anyone who knows about it?

          All JSON numbers are floating point, and there can be no distinction between 6000000 and 6e+06.
          Consequently, all JSON parsers should know what 6e+06 means, so why do you want the number to be specifically formatted as an integer?

          K Offline
          K Offline
          kyujin kim
          wrote on last edited by
          #4

          @kshegunov
          Thank you for your answer!
          I check the receiving part, and found that they get json value by unsigned long.
          I'll try to change receiving part to double.
          Thanks,

          1 Reply Last reply
          1
          • Paul ColbyP Paul Colby

            Hi @kyujin-kim,

            That behaviour is a little unfortunate (though still technically valid, depending on how you look at it).

            What version of Qt are you using? It looks to me like this would no longer happen from Qt 5.10 onwards, thanks to this change.

            Cheers.

            K Offline
            K Offline
            kyujin kim
            wrote on last edited by
            #5

            @Paul-Colby
            I'm using 5.8 now.
            Thank you for your answer!!

            aha_1980A 1 Reply Last reply
            1
            • K kyujin kim

              @Paul-Colby
              I'm using 5.8 now.
              Thank you for your answer!!

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @kyujin-kim Please mark this thread as solved if your problem is solved now.

              Thanks.

              Qt has to stay free or it will die.

              1 Reply Last reply
              1
              • kshegunovK kshegunov

                @kyujin-kim said in Qt Json number format changes automatically:

                But I think there's way to fix the format.
                Is there anyone who knows about it?

                All JSON numbers are floating point, and there can be no distinction between 6000000 and 6e+06.
                Consequently, all JSON parsers should know what 6e+06 means, so why do you want the number to be specifically formatted as an integer?

                K Offline
                K Offline
                kyujin kim
                wrote on last edited by
                #7

                @kshegunov
                I got the value as <unsigned long>.
                But after your comment, I get the value as <double> and cast by static_cast<unsigned long>.

                and

                It works!!!!!!!!

                Thanks.

                1 Reply Last reply
                0

                • Login

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