Skip to content

Commit

Permalink
Fixed bugs in the JSON parser
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed May 24, 2012
1 parent 3669939 commit ea60ad8
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions doomsday/engine/portable/src/json.cpp
Expand Up @@ -25,9 +25,10 @@
#include <QVarLengthArray>
#include <de/Log>
#include <de/Error>
#include <QDebug>

/**
* JSON parser. Not exposed outside this source file; use parseJSON() instead.
* Not exposed outside this source file; use parseJSON() instead.
*/
class JSONParser
{
Expand Down Expand Up @@ -78,7 +79,8 @@ class JSONParser

void error(const QString& message)
{
throw de::Error("JSONParser", de::String("Error at position %1: %2").arg(pos).arg(message));
throw de::Error("JSONParser", de::String("Error at position %1 (%2^%3): %4")
.arg(pos).arg(source.mid(pos - 4, 4)).arg(source.mid(pos, 4)).arg(message));
}

QVariant parse()
Expand Down Expand Up @@ -219,9 +221,9 @@ class JSONParser
if(c == '-')
{
str.append(c);
c = next();
c = nextNoSkip();
}
for(; c.isDigit(); c = next())
for(; c.isDigit(); c = nextNoSkip())
{
str.append(c);
}
Expand All @@ -230,8 +232,8 @@ class JSONParser
{
str.append(c);
hasDecimal = true;
c = next();
for(; c.isDigit(); c = next())
c = nextNoSkip();
for(; c.isDigit(); c = nextNoSkip())
{
str.append(c);
}
Expand All @@ -240,13 +242,13 @@ class JSONParser
{
// Exponent.
str.append(c);
c = next();
c = nextNoSkip();
if(c == '+' || c == '-')
{
str.append(c);
c = next();
c = nextNoSkip();
}
for(; c.isDigit(); c = next())
for(; c.isDigit(); c = nextNoSkip())
{
str.append(c);
}
Expand All @@ -270,16 +272,19 @@ class JSONParser
if(source.mid(pos, 4) == "true")
{
pos += 4;
skipWhite();
return QVariant(true);
}
else if(source.mid(pos, 5) == "false")
{
pos += 5;
skipWhite();
return QVariant(false);
}
else if(source.mid(pos, 4) == "null")
{
pos += 4;
skipWhite();
return QVariant(0);
}
else
Expand Down

0 comments on commit ea60ad8

Please sign in to comment.