Qt Json number format changes automatically



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

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



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


  • Qt Champions 2017

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



  • @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,



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


  • Qt Champions 2017

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

    Thanks.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.