2014-08-04 21:00:03 +04:00
|
|
|
/*
|
2017-02-09 05:31:16 +03:00
|
|
|
* Copyright 2014-2017, Augustin Cavalier (waddlesplash)
|
2014-08-31 01:24:40 +04:00
|
|
|
* Copyright 2014, Stephan Aßmus <superstippi@gmx.de>
|
2014-08-04 21:00:03 +04:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <Json.h>
|
|
|
|
|
2017-02-09 05:31:16 +03:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cerrno>
|
2014-08-04 21:00:03 +04:00
|
|
|
|
|
|
|
#include <MessageBuilder.h>
|
|
|
|
#include <UnicodeChar.h>
|
|
|
|
|
|
|
|
|
|
|
|
// #pragma mark - Public methods
|
|
|
|
|
|
|
|
namespace BPrivate {
|
|
|
|
|
2014-08-31 01:24:40 +04:00
|
|
|
|
|
|
|
class ParseException {
|
|
|
|
public:
|
|
|
|
ParseException(int32 position, BString error)
|
|
|
|
:
|
|
|
|
fPosition(position),
|
|
|
|
fError(error),
|
|
|
|
fReturnCode(B_BAD_DATA)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ParseException(int32 position, status_t returnCode)
|
|
|
|
:
|
|
|
|
fPosition(position),
|
|
|
|
fError(""),
|
|
|
|
fReturnCode(returnCode)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintToStream() const {
|
|
|
|
const char* error;
|
|
|
|
if (fError.Length() > 0)
|
|
|
|
error = fError.String();
|
|
|
|
else
|
|
|
|
error = strerror(fReturnCode);
|
2014-09-01 15:12:59 +04:00
|
|
|
printf("Parse error at %" B_PRIi32 ": %s\n", fPosition, error);
|
2014-08-31 01:24:40 +04:00
|
|
|
}
|
2017-02-09 05:31:16 +03:00
|
|
|
|
2014-08-31 01:24:40 +04:00
|
|
|
status_t ReturnCode() const
|
|
|
|
{
|
|
|
|
return fReturnCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int32 fPosition;
|
|
|
|
BString fError;
|
|
|
|
status_t fReturnCode;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-08-04 21:00:03 +04:00
|
|
|
status_t
|
2017-02-09 21:12:49 +03:00
|
|
|
BJson::Parse(const char* JSON, BMessage& message)
|
2014-08-04 21:00:03 +04:00
|
|
|
{
|
|
|
|
BString temp(JSON);
|
2017-02-09 21:12:49 +03:00
|
|
|
return Parse(temp, message);
|
2014-08-04 21:00:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
2017-02-09 21:12:49 +03:00
|
|
|
BJson::Parse(const BString& JSON, BMessage& message)
|
2014-08-31 01:24:40 +04:00
|
|
|
{
|
|
|
|
try {
|
2017-02-09 21:12:49 +03:00
|
|
|
_Parse(JSON, message);
|
2014-08-31 01:24:40 +04:00
|
|
|
return B_OK;
|
|
|
|
} catch (ParseException e) {
|
|
|
|
e.PrintToStream();
|
|
|
|
return e.ReturnCode();
|
|
|
|
}
|
|
|
|
return B_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// #pragma mark - Private methods
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2017-02-09 21:12:49 +03:00
|
|
|
BJson::_Parse(const BString& JSON, BMessage& message)
|
2014-08-04 21:00:03 +04:00
|
|
|
{
|
|
|
|
BMessageBuilder builder(message);
|
|
|
|
int32 pos = 0;
|
2017-02-09 05:31:16 +03:00
|
|
|
const int32 length = JSON.Length();
|
|
|
|
bool hadRootNode = false;
|
|
|
|
|
2014-08-04 21:00:03 +04:00
|
|
|
/* Locals used by the parser. */
|
|
|
|
// Keeps track of the hierarchy (e.g. "{[{{") that has
|
|
|
|
// been read in. Allows the parser to verify that openbraces
|
|
|
|
// match up to closebraces and so on and so forth.
|
|
|
|
BString hierarchy("");
|
|
|
|
// Stores the key that was just read by the string parser,
|
|
|
|
// in the case that we are parsing a map.
|
|
|
|
BString key("");
|
2017-02-09 05:31:16 +03:00
|
|
|
|
2014-08-31 01:28:55 +04:00
|
|
|
// TODO: Check builder return codes and throw exception, or
|
|
|
|
// change builder implementation/interface to throw exceptions
|
|
|
|
// instead of returning errors.
|
|
|
|
// TODO: Elimitate more duplicated code, for example by moving
|
|
|
|
// more code into _ParseConstant().
|
2017-02-09 05:31:16 +03:00
|
|
|
|
2014-08-04 21:00:03 +04:00
|
|
|
while (pos < length) {
|
|
|
|
switch (JSON[pos]) {
|
|
|
|
case '{':
|
|
|
|
hierarchy += "{";
|
2017-02-09 05:31:16 +03:00
|
|
|
|
2014-08-04 21:00:03 +04:00
|
|
|
if (hierarchy != "{") {
|
|
|
|
if (builder.What() == JSON_TYPE_ARRAY)
|
|
|
|
builder.PushObject(builder.CountNames());
|
|
|
|
else {
|
|
|
|
builder.PushObject(key.String());
|
|
|
|
key = "";
|
|
|
|
}
|
2017-02-09 05:31:16 +03:00
|
|
|
} else if (hadRootNode == true) {
|
|
|
|
throw ParseException(pos,
|
|
|
|
"Got '{' with empty hierarchy but already had a root node");
|
|
|
|
} else
|
|
|
|
hadRootNode = true;
|
2014-08-04 21:00:03 +04:00
|
|
|
|
|
|
|
builder.SetWhat(JSON_TYPE_MAP);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '}':
|
2017-02-09 05:31:16 +03:00
|
|
|
if (key.Length() > 0)
|
|
|
|
throw ParseException(pos, "Got closebrace but still have a key");
|
2014-08-31 01:24:40 +04:00
|
|
|
if (hierarchy.EndsWith("{") && hierarchy.Length() != 1) {
|
2014-08-04 21:00:03 +04:00
|
|
|
hierarchy.Truncate(hierarchy.Length() - 1);
|
|
|
|
builder.PopObject();
|
2017-02-09 05:31:16 +03:00
|
|
|
} else if (hierarchy.EndsWith("{") && hierarchy.Length() == 1) {
|
|
|
|
hierarchy.Truncate(hierarchy.Length() - 1);
|
|
|
|
break; // Should be the end of the data.
|
|
|
|
} else
|
2014-08-31 01:24:40 +04:00
|
|
|
throw ParseException(pos, "Unmatched closebrace }");
|
2014-08-04 21:00:03 +04:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '[':
|
|
|
|
hierarchy += "[";
|
2017-02-09 05:31:16 +03:00
|
|
|
|
|
|
|
if (hierarchy != "[") {
|
|
|
|
if (builder.What() == JSON_TYPE_ARRAY)
|
|
|
|
builder.PushObject(builder.CountNames());
|
|
|
|
else {
|
|
|
|
builder.PushObject(key.String());
|
|
|
|
key = "";
|
|
|
|
}
|
|
|
|
} else if (hadRootNode == true) {
|
|
|
|
throw ParseException(pos,
|
|
|
|
"Got '[' with empty hierarchy but already had a root node");
|
|
|
|
} else
|
|
|
|
hadRootNode = true;
|
2014-08-04 21:00:03 +04:00
|
|
|
|
|
|
|
builder.SetWhat(JSON_TYPE_ARRAY);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ']':
|
2017-02-09 05:31:16 +03:00
|
|
|
if (hierarchy.EndsWith("[") && hierarchy.Length() != 1) {
|
2014-08-04 21:00:03 +04:00
|
|
|
hierarchy.Truncate(hierarchy.Length() - 1);
|
|
|
|
builder.PopObject();
|
2017-02-09 05:31:16 +03:00
|
|
|
} else if (hierarchy.EndsWith("[") && hierarchy.Length() == 1) {
|
|
|
|
hierarchy.Truncate(hierarchy.Length() - 1);
|
|
|
|
break; // Should be the end of the data.
|
|
|
|
} else
|
|
|
|
throw ParseException(pos, "Unmatched closebracket ]");
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2014-08-04 21:00:03 +04:00
|
|
|
|
2017-02-09 05:31:16 +03:00
|
|
|
case ':':
|
|
|
|
if (hierarchy.Length() == 0)
|
|
|
|
throw ParseException(pos, "Expected EOF, got ':'");
|
|
|
|
if (builder.What() != JSON_TYPE_MAP || key.Length() == 0) {
|
|
|
|
throw ParseException(pos, "Unexpected ':'");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ',':
|
|
|
|
if (builder.What() == JSON_TYPE_MAP && key.Length() != 0) {
|
|
|
|
throw ParseException(pos, "Unexpected ',' expected ':'");
|
|
|
|
}
|
|
|
|
if (hierarchy.Length() == 0)
|
|
|
|
throw ParseException(pos, "Expected EOF, got ','");
|
2014-08-04 21:00:03 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
{
|
2017-02-09 05:31:16 +03:00
|
|
|
if (hierarchy.Length() == 0)
|
|
|
|
throw ParseException(pos, "Expected EOF, got 't'");
|
2014-08-31 01:24:40 +04:00
|
|
|
if (builder.What() != JSON_TYPE_ARRAY && key.Length() == 0) {
|
|
|
|
throw ParseException(pos,
|
|
|
|
"'true' cannot be a key, it can only be a value");
|
|
|
|
}
|
2017-02-09 05:31:16 +03:00
|
|
|
|
2014-08-31 01:24:40 +04:00
|
|
|
if (_ParseConstant(JSON, pos, "true")) {
|
2014-08-04 21:00:03 +04:00
|
|
|
if (builder.What() == JSON_TYPE_ARRAY)
|
2014-08-24 23:29:49 +04:00
|
|
|
key.SetToFormat("%" B_PRIu32, builder.CountNames());
|
2014-08-04 21:00:03 +04:00
|
|
|
builder.AddBool(key.String(), true);
|
|
|
|
key = "";
|
|
|
|
} else
|
2014-08-31 01:24:40 +04:00
|
|
|
throw ParseException(pos, "Unexpected 't'");
|
2014-08-04 21:00:03 +04:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
{
|
2017-02-09 05:31:16 +03:00
|
|
|
if (hierarchy.Length() == 0)
|
|
|
|
throw ParseException(pos, "Expected EOF, got 'f'");
|
2014-08-31 01:24:40 +04:00
|
|
|
if (builder.What() != JSON_TYPE_ARRAY && key.Length() == 0) {
|
|
|
|
throw ParseException(pos,
|
|
|
|
"'false' cannot be a key, it can only be a value");
|
|
|
|
}
|
2017-02-09 05:31:16 +03:00
|
|
|
|
2014-08-31 01:24:40 +04:00
|
|
|
if (_ParseConstant(JSON, pos, "false")) {
|
2014-08-04 21:00:03 +04:00
|
|
|
if (builder.What() == JSON_TYPE_ARRAY)
|
2014-08-24 23:29:49 +04:00
|
|
|
key.SetToFormat("%" B_PRIu32, builder.CountNames());
|
2014-08-04 21:00:03 +04:00
|
|
|
builder.AddBool(key.String(), false);
|
|
|
|
key = "";
|
|
|
|
} else
|
2014-08-31 01:24:40 +04:00
|
|
|
throw ParseException(pos, "Unexpected 'f'");
|
2014-08-04 21:00:03 +04:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
{
|
2017-02-09 05:31:16 +03:00
|
|
|
if (hierarchy.Length() == 0)
|
|
|
|
throw ParseException(pos, "Expected EOF, got 'n'");
|
2014-08-31 01:24:40 +04:00
|
|
|
if (builder.What() != JSON_TYPE_ARRAY && key.Length() == 0) {
|
|
|
|
throw ParseException(pos,
|
|
|
|
"'null' cannot be a key, it can only be a value");
|
|
|
|
}
|
2017-02-09 05:31:16 +03:00
|
|
|
|
2014-08-31 01:24:40 +04:00
|
|
|
if (_ParseConstant(JSON, pos, "null")) {
|
2014-08-04 21:00:03 +04:00
|
|
|
if (builder.What() == JSON_TYPE_ARRAY)
|
2014-08-24 23:29:49 +04:00
|
|
|
key.SetToFormat("%" B_PRIu32, builder.CountNames());
|
2014-08-04 21:00:03 +04:00
|
|
|
builder.AddPointer(key.String(), (void*)NULL);
|
|
|
|
key = "";
|
|
|
|
} else
|
2014-08-31 01:24:40 +04:00
|
|
|
throw ParseException(pos, "Unexpected 'n'");
|
2014-08-04 21:00:03 +04:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case '"':
|
2017-02-09 05:31:16 +03:00
|
|
|
if (hierarchy.Length() == 0)
|
|
|
|
throw ParseException(pos, "Expected EOF, got '\"'");
|
2014-08-04 21:00:03 +04:00
|
|
|
if (builder.What() != JSON_TYPE_ARRAY && key.Length() == 0)
|
2014-08-31 01:24:40 +04:00
|
|
|
key = _ParseString(JSON, pos);
|
2014-08-04 21:00:03 +04:00
|
|
|
else if (builder.What() != JSON_TYPE_ARRAY && key.Length() > 0) {
|
2014-08-31 01:24:40 +04:00
|
|
|
builder.AddString(key, _ParseString(JSON, pos));
|
2014-08-04 21:00:03 +04:00
|
|
|
key = "";
|
|
|
|
} else if (builder.What() == JSON_TYPE_ARRAY) {
|
|
|
|
key << builder.CountNames();
|
2014-08-31 01:24:40 +04:00
|
|
|
builder.AddString(key, _ParseString(JSON, pos));
|
2014-08-04 21:00:03 +04:00
|
|
|
key = "";
|
|
|
|
} else
|
2014-08-31 01:24:40 +04:00
|
|
|
throw ParseException(pos, "Internal error at encountering \"");
|
2014-08-04 21:00:03 +04:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '+':
|
|
|
|
case '-':
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
{
|
2017-02-09 05:31:16 +03:00
|
|
|
if (hierarchy.Length() == 0)
|
|
|
|
throw ParseException(pos, "Expected EOF, got number");
|
2014-08-31 01:24:40 +04:00
|
|
|
if (builder.What() != JSON_TYPE_ARRAY && key.Length() == 0) {
|
|
|
|
throw ParseException(pos,
|
|
|
|
"Numbers cannot be keys, they can only be values");
|
|
|
|
}
|
2017-02-09 05:31:16 +03:00
|
|
|
|
2014-08-04 21:00:03 +04:00
|
|
|
if (builder.What() == JSON_TYPE_ARRAY)
|
|
|
|
key << builder.CountNames();
|
|
|
|
|
2014-08-31 01:24:40 +04:00
|
|
|
double number = _ParseNumber(JSON, pos);
|
2014-08-04 21:00:03 +04:00
|
|
|
builder.AddDouble(key.String(), number);
|
|
|
|
|
|
|
|
key = "";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-02-09 05:31:16 +03:00
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
// Whitespace; ignore.
|
2014-08-04 21:00:03 +04:00
|
|
|
break;
|
2017-02-09 05:31:16 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
throw ParseException(pos, "Unexpected character");
|
2014-08-04 21:00:03 +04:00
|
|
|
}
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
|
2017-02-09 05:31:16 +03:00
|
|
|
if (hierarchy.Length() != 0)
|
|
|
|
throw ParseException(pos, "Unexpected EOF");
|
2014-08-04 21:00:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BString
|
2017-02-09 21:12:49 +03:00
|
|
|
BJson::_ParseString(const BString& JSON, int32& pos)
|
2014-08-04 21:00:03 +04:00
|
|
|
{
|
|
|
|
if (JSON[pos] != '"') // Verify we're at the start of a string.
|
|
|
|
return BString("");
|
|
|
|
pos++;
|
2017-02-09 05:31:16 +03:00
|
|
|
|
2014-08-04 21:00:03 +04:00
|
|
|
BString str;
|
2017-02-09 05:31:16 +03:00
|
|
|
while (JSON[pos] != '"' && pos < JSON.Length()) {
|
2014-08-04 21:00:03 +04:00
|
|
|
if (JSON[pos] == '\\') {
|
|
|
|
pos++;
|
|
|
|
switch (JSON[pos]) {
|
|
|
|
case 'b':
|
|
|
|
str += "\b";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
str += "\f";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
str += "\n";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
str += "\r";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
str += "\t";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'u': // 4-byte hexadecimal Unicode char (e.g. "\uffff")
|
|
|
|
{
|
|
|
|
uint intValue;
|
|
|
|
BString substr;
|
|
|
|
JSON.CopyInto(substr, pos + 1, 4);
|
|
|
|
if (sscanf(substr.String(), "%4x", &intValue) != 1)
|
|
|
|
return str;
|
|
|
|
// We probably hit the end of the string.
|
|
|
|
// This probably should be counted as an error,
|
|
|
|
// but for now let's soft-fail instead of hard-fail.
|
|
|
|
|
|
|
|
char character[20];
|
|
|
|
char* ptr = character;
|
|
|
|
BUnicodeChar::ToUTF8(intValue, &ptr);
|
|
|
|
str.AppendChars(character, 1);
|
|
|
|
pos += 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
str += JSON[pos];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
str += JSON[pos];
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double
|
2017-02-09 21:12:49 +03:00
|
|
|
BJson::_ParseNumber(const BString& JSON, int32& pos)
|
2014-08-04 21:00:03 +04:00
|
|
|
{
|
|
|
|
BString value;
|
2017-02-09 05:31:16 +03:00
|
|
|
bool isDouble = false;
|
2014-08-04 21:00:03 +04:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
switch (JSON[pos]) {
|
|
|
|
case '+':
|
|
|
|
case '-':
|
|
|
|
case 'e':
|
|
|
|
case 'E':
|
2017-02-09 05:31:16 +03:00
|
|
|
case '.':
|
|
|
|
isDouble = true;
|
|
|
|
// fall through
|
2014-08-04 21:00:03 +04:00
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
value += JSON[pos];
|
|
|
|
pos++;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// We've reached the end of the number, so decrement the
|
|
|
|
// "pos" value so that the parser picks up on the next char.
|
|
|
|
pos--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-02-09 05:31:16 +03:00
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
double ret = 0;
|
|
|
|
if (isDouble)
|
|
|
|
ret = strtod(value.String(), NULL);
|
|
|
|
else
|
|
|
|
ret = strtoll(value.String(), NULL, 10);
|
|
|
|
if (errno != 0)
|
|
|
|
throw ParseException(pos, "Invalid number!");
|
|
|
|
return ret;
|
2014-08-04 21:00:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
2017-02-09 21:12:49 +03:00
|
|
|
BJson::_ParseConstant(const BString& JSON, int32& pos, const char* constant)
|
2014-08-04 21:00:03 +04:00
|
|
|
{
|
|
|
|
BString value;
|
|
|
|
JSON.CopyInto(value, pos, strlen(constant));
|
|
|
|
if (value == constant) {
|
2014-08-31 01:25:54 +04:00
|
|
|
// Increase pos by the remainder of the constant, pos will be
|
|
|
|
// increased for the first letter in the main parse loop.
|
|
|
|
pos += strlen(constant) - 1;
|
2014-08-04 21:00:03 +04:00
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace BPrivate
|
|
|
|
|