YasserAsmi / jvar

JS inspired Variants and JSON parsing for C++
MIT License
24 stars 11 forks source link

Parsing failure for negative values in arrays. #23

Closed pnaulls closed 8 years ago

pnaulls commented 8 years ago

I found that if value in an array is negative (other than the first value), it won't parse.

See:

include "jvar.h"

using namespace jvar;

int main(int argc, char* argv) { const char test = "{\"angle\":[171.8,20,2,3,-96.3,20.6]}";

Variant array;
array.parseJson(test);

puts(array.toString().c_str());

}

Gives: Json parsing failed: Parser error: Invalid value ',-' at line 1

Not knowing the full workings of state engine the parser, I propose this fix:

--- src/str.cpp 
+++ src/str.cpp 
@@ -581,7 +581,8 @@

                     case PuncTok:
                     {
-                        if (!mSinglePunc && (lastc != ':') && charPunc(c))
+                        bool arrayNegative = (lastc == ',') && (c == '-');
+                        if (!mSinglePunc && (lastc != ':') && charPunc(c) && !arrayNegative)
                         {
                             append(c);
                         }
YasserAsmi commented 8 years ago

Thanks for finding the issue. This is the part of the lower level parser that has given me some grief. At one point I had made a decision to cluster punctuation marks.. This made it easy to implement things like += >> and other operators... but it has caused issues.. I had added a single mode for this reason, it turns out that some combinations like ,- are never right. So this has been fixed.