kstenerud/KSJSON

Loss of number precision

Opened this issue · 1 comments

I just came around a case where the NSJONSerializer behaves different than KSJON. Say, we have the following object:

(lldb) po d
{
    timestamp = "1457795057.445483";
    type = foreground;
}

Serializing with NSJSONSerialization gives me:

(lldb) po [[NSJSONSerialization dataWithJSONObject:d options:0 error:nil] LT_stringByConvertingToUTF8]
{"type":"foreground","timestamp":1457795057.445483}

whereas KSJSON gives me:

(lldb) po [KSJSON serializeObject:d error:nil]
{"type":"foreground","timestamp":1.4578e+09}

which ultimately leads to a loss of precision:

(lldb) po [KSJSON deserializeString:[KSJSON serializeObject:d error:nil] error:nil]
{
    timestamp = 1457800000;
    type = foreground;
}

Is this a bug in KSJSON or am I using it wrong?

Obviously the line snprintf(buff, sizeof(buff), "%g", value); (which is being used for all float number marshalling) is responsible here. What is the reasoning to not use %f here?