JSON Suggestion


#1

JSON parseNumber. added parsing of hex of the form 0x<digits>. might not be standard, but useful to me?

static Result parseNumber (String::CharPointerType& t, var& result, const bool isNegative)
    {
        String::CharPointerType oldT (t);

        int64 intValue = t.getAndAdvance() - '0';
        jassert (intValue >= 0 && intValue < 10);

        bool hex = false;
        if (!intValue)
        {
            // look for 0xHEX
            if (*t == 'x')
            {
                t.getAndAdvance(); // skip 'x'
                hex = true;
            }
        }

        for (;;)
        {
            String::CharPointerType previousChar (t);
            const juce_wchar c = t.getAndAdvance();

            if (hex)
            {
                // parse hex
                int d = -1;
                if (c >= '0' && c <= '9')
                    d = c - '0';
                else if (c >= 'a' && c <= 'f')
                    d = c - 'a' + 10;
                else if (c >= 'A' && c <= 'F')
                    d = c - 'A' + 10;                    
                
                if (d >= 0)
                {
                    intValue = (intValue<<4) + d;
                    continue;
                }
            }
            else
            {
                const int digit = ((int) c) - '0';
                if (isPositiveAndBelow (digit, 10))
                {
                    intValue = intValue * 10 + digit;
                    continue;
                }

                if (c == 'e' || c == 'E' || c == '.')
                {
                    t = oldT;
                    const double asDouble = CharacterFunctions::readDoubleValue (t);
                    result = isNegative ? -asDouble : asDouble;
                    return Result::ok();
                }
            }

            if (CharacterFunctions::isWhitespace (c)
                 || c == ',' || c == '}' || c == ']' || c == 0)
            {
                t = previousChar;
                break;
            }

            return createFail ("Syntax error in number", &oldT);
        }

        const int64 correctedValue = isNegative ? -intValue : intValue;

        if ((intValue >> 31) != 0)
            result = correctedValue;
        else
            result = (int) correctedValue;

        return Result::ok();
    }

 


#2

Yeah.. Should I add non-standard features like that? I really don't know in this case.. Wouldn't do any harm, but feels wrong on some level..


#3

Just add a default bool "strictMode" parameter set to "true" and reject such code when it's set.

As long as you don't generate such text, it should be ok.