w0rp/dson

bug with float handling

Opened this issue · 3 comments

Hi,

I've found a little bug with float handling.

Try :

auto objet = jsonObject();
objet["valeur"] = 1.001;
writeln(objet); // shows 1.001
auto objet2 = parseJSON(toJSON(object));
writeln(objet2); // shows 1.1

this could be corrected with something like :

JSON parseNumber() {
        enum byte NEGATIVE = 1;
        enum byte EXP_NEGATIVE = 2;

        long integer   = 0;
        long remainder = 0;
        short exponent = 0;
        byte signInfo  = 0;
        int tailleReste = -1;

        // Accumulate digits reading left-to-right in a number.
        void parseDigits(T)(ref T accum) {
            while (!empty()) {
                switch(front()) {
                case '0': .. case '9':
                    accum = cast(T) (accum * 10 + (moveFront() - '0'));
                    tailleReste++;
                    if (accum < 0) {
                        throw complaint("overflow error!");
                    }
                break;
                default:
                    return;
                }
            }
        }

        ...

        if (remainder != 0) {
                // Add in the remainder.
                if (whole==0) {
                   tailleReste++;
                }
                whole += remainder / (10.0 ^^ tailleReste);
        }
w0rp commented

I have fixed the float parsing now. I used a solution similar to the one you suggested.

w0rp commented

Also, there's a test case which is similar to your example.

thanks.