1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

JSON parsing fix.

This commit is contained in:
jules 2013-04-24 12:00:09 +01:00
parent d6076d680a
commit ed03b30c01

View file

@ -26,6 +26,21 @@
class JSONParser
{
public:
static Result parseObjectOrArray (String::CharPointerType t, var& result)
{
t = t.findEndOfWhitespace();
switch (t.getAndAdvance())
{
case 0: result = var::null; return Result::ok();
case '{': return parseObject (t, result);
case '[': return parseArray (t, result);
}
return createFail ("Expected '{' or '['", &t);
}
private:
static Result parseAny (String::CharPointerType& t, var& result)
{
t = t.findEndOfWhitespace();
@ -34,7 +49,7 @@ public:
switch (t2.getAndAdvance())
{
case '{': t = t2; return parseObject (t, result);
case '[': t = t2; return parseArray (t, result);
case '[': t = t2; return parseArray (t, result);
case '"': t = t2; return parseString (t, result);
case '-':
@ -84,7 +99,6 @@ public:
return createFail ("Syntax error", &t);
}
private:
static Result createFail (const char* const message, const String::CharPointerType* location = nullptr)
{
String m (message);
@ -483,8 +497,8 @@ private:
var JSON::parse (const String& text)
{
var result;
String::CharPointerType t (text.getCharPointer());
if (! JSONParser::parseAny (t, result))
if (! JSONParser::parseObjectOrArray (text.getCharPointer(), result))
result = var::null;
return result;
@ -502,8 +516,7 @@ var JSON::parse (const File& file)
Result JSON::parse (const String& text, var& result)
{
String::CharPointerType t (text.getCharPointer());
return JSONParser::parseAny (t, result);
return JSONParser::parseObjectOrArray (text.getCharPointer(), result);
}
String JSON::toString (const var& data, const bool allOnOneLine)
@ -606,12 +619,12 @@ public:
expect (JSON::parse (String::empty) == var::null);
expect (JSON::parse ("{}").isObject());
expect (JSON::parse ("[]").isArray());
expect (JSON::parse ("1234").isInt());
expect (JSON::parse ("12345678901234").isInt64());
expect (JSON::parse ("1.123e3").isDouble());
expect (JSON::parse ("-1234").isInt());
expect (JSON::parse ("-12345678901234").isInt64());
expect (JSON::parse ("-1.123e3").isDouble());
expect (JSON::parse ("[ 1234 ]")[0].isInt());
expect (JSON::parse ("[ 12345678901234 ]")[0].isInt64());
expect (JSON::parse ("[ 1.123e3 ]")[0].isDouble());
expect (JSON::parse ("[ -1234]")[0].isInt());
expect (JSON::parse ("[-12345678901234]")[0].isInt64());
expect (JSON::parse ("[-1.123e3]")[0].isDouble());
for (int i = 100; --i >= 0;)
{
@ -622,7 +635,7 @@ public:
const bool oneLine = r.nextBool();
String asString (JSON::toString (v, oneLine));
var parsed = JSON::parse (asString);
var parsed = JSON::parse ("[" + asString + "]")[0];
String parsedString (JSON::toString (parsed, oneLine));
expect (asString.isNotEmpty() && parsedString == asString);
}