2014-08-04 21:00:03 +04:00
|
|
|
/*
|
2017-05-13 09:50:39 +03:00
|
|
|
* Copyright 2017, Andrew Lindesay <apl@lindesay.co.nz>
|
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.
|
|
|
|
*/
|
2017-05-15 15:20:53 +03:00
|
|
|
|
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
#include "Json.h"
|
2014-08-04 21:00:03 +04:00
|
|
|
|
2017-02-09 05:31:16 +03:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2017-05-13 09:50:39 +03:00
|
|
|
#include <ctype.h>
|
2017-02-09 05:31:16 +03:00
|
|
|
#include <cerrno>
|
2014-08-04 21:00:03 +04:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
#include <AutoDeleter.h>
|
|
|
|
#include <DataIO.h>
|
2014-08-04 21:00:03 +04:00
|
|
|
#include <UnicodeChar.h>
|
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
#include "JsonEventListener.h"
|
|
|
|
#include "JsonMessageWriter.h"
|
|
|
|
|
2014-08-04 21:00:03 +04:00
|
|
|
|
|
|
|
// #pragma mark - Public methods
|
|
|
|
|
|
|
|
namespace BPrivate {
|
|
|
|
|
2014-08-31 01:24:40 +04:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
static bool
|
2017-05-15 15:20:53 +03:00
|
|
|
b_jsonparse_is_hex(char c)
|
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
return isdigit(c)
|
|
|
|
|| (c > 0x41 && c <= 0x46)
|
|
|
|
|| (c > 0x61 && c <= 0x66);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool
|
2017-05-15 15:20:53 +03:00
|
|
|
b_jsonparse_all_hex(const char* c)
|
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
if (!b_jsonparse_is_hex(c[i]))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*! This class carries state around the parsing process. */
|
|
|
|
|
|
|
|
class JsonParseContext {
|
2014-08-31 01:24:40 +04:00
|
|
|
public:
|
2017-05-13 09:50:39 +03:00
|
|
|
JsonParseContext(BDataIO* data, BJsonEventListener* listener)
|
2014-08-31 01:24:40 +04:00
|
|
|
:
|
2017-05-13 09:50:39 +03:00
|
|
|
fListener(listener),
|
|
|
|
fData(data),
|
|
|
|
fLineNumber(1), // 1 is the first line
|
|
|
|
fPushbackChar(0),
|
|
|
|
fHasPushbackChar(false)
|
2014-08-31 01:24:40 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-05-15 15:20:53 +03:00
|
|
|
|
|
|
|
BJsonEventListener* Listener() const
|
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
return fListener;
|
2014-08-31 01:24:40 +04:00
|
|
|
}
|
|
|
|
|
2017-05-15 15:20:53 +03:00
|
|
|
|
|
|
|
BDataIO* Data() const
|
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
return fData;
|
2014-08-31 01:24:40 +04:00
|
|
|
}
|
2017-02-09 05:31:16 +03:00
|
|
|
|
2017-05-15 15:20:53 +03:00
|
|
|
|
|
|
|
int LineNumber() const
|
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
return fLineNumber;
|
|
|
|
}
|
|
|
|
|
2017-05-15 15:20:53 +03:00
|
|
|
|
|
|
|
void IncrementLineNumber()
|
|
|
|
{
|
|
|
|
fLineNumber++;
|
2017-05-13 09:50:39 +03:00
|
|
|
}
|
|
|
|
|
2017-05-15 15:20:53 +03:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
// TODO; there is considerable opportunity for performance improvements
|
|
|
|
// here by buffering the input and then feeding it into the parse
|
|
|
|
// algorithm character by character.
|
|
|
|
|
2017-05-15 15:20:53 +03:00
|
|
|
status_t NextChar(char* buffer)
|
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
|
|
|
|
if (fHasPushbackChar) {
|
|
|
|
buffer[0] = fPushbackChar;
|
|
|
|
fHasPushbackChar = false;
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Data()->ReadExactly(buffer, 1);
|
|
|
|
}
|
|
|
|
|
2017-05-15 15:20:53 +03:00
|
|
|
|
|
|
|
void PushbackChar(char c)
|
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
fPushbackChar = c;
|
|
|
|
fHasPushbackChar = true;
|
2014-08-31 01:24:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-05-13 09:50:39 +03:00
|
|
|
BJsonEventListener* fListener;
|
|
|
|
BDataIO* fData;
|
|
|
|
uint32 fLineNumber;
|
|
|
|
char fPushbackChar;
|
|
|
|
bool fHasPushbackChar;
|
2014-08-31 01:24:40 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-08-04 21:00:03 +04:00
|
|
|
status_t
|
2017-05-13 09:50:39 +03:00
|
|
|
BJson::Parse(const BString& JSON, BMessage& message)
|
2014-08-04 21:00:03 +04:00
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
return Parse(JSON.String(), message);
|
2014-08-04 21:00:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
2018-03-19 23:42:22 +03:00
|
|
|
BJson::Parse(const char* JSON, size_t length, BMessage& message)
|
2014-08-31 01:24:40 +04:00
|
|
|
{
|
2018-03-19 23:42:22 +03:00
|
|
|
BMemoryIO* input = new BMemoryIO(JSON, length);
|
2017-05-13 09:50:39 +03:00
|
|
|
ObjectDeleter<BMemoryIO> inputDeleter(input);
|
|
|
|
BJsonMessageWriter* writer = new BJsonMessageWriter(message);
|
|
|
|
ObjectDeleter<BJsonMessageWriter> writerDeleter(writer);
|
2014-08-31 01:24:40 +04:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
Parse(input, writer);
|
|
|
|
status_t result = writer->ErrorStatus();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2014-08-31 01:24:40 +04:00
|
|
|
|
|
|
|
|
2018-03-19 23:42:22 +03:00
|
|
|
status_t
|
|
|
|
BJson::Parse(const char* JSON, BMessage& message)
|
|
|
|
{
|
|
|
|
return Parse(JSON, strlen(JSON), message);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
/*! The data is read as a stream of JSON data. As the JSON is read, events are
|
|
|
|
raised such as;
|
|
|
|
- string
|
|
|
|
- number
|
|
|
|
- true
|
|
|
|
- array start
|
|
|
|
- object end
|
|
|
|
Each event is sent to the listener to process as required.
|
|
|
|
*/
|
2014-08-31 01:24:40 +04:00
|
|
|
|
|
|
|
void
|
2017-05-13 09:50:39 +03:00
|
|
|
BJson::Parse(BDataIO* data, BJsonEventListener* listener)
|
2014-08-04 21:00:03 +04:00
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
JsonParseContext context(data, listener);
|
|
|
|
ParseAny(context);
|
|
|
|
listener->Complete();
|
|
|
|
}
|
2014-08-04 21:00:03 +04:00
|
|
|
|
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
// #pragma mark - Specific parse logic.
|
2014-08-04 21:00:03 +04:00
|
|
|
|
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
bool
|
|
|
|
BJson::NextChar(JsonParseContext& jsonParseContext, char* c)
|
|
|
|
{
|
|
|
|
status_t result = jsonParseContext.NextChar(c);
|
2017-02-09 05:31:16 +03:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
switch (result) {
|
|
|
|
case B_OK:
|
|
|
|
return true;
|
2017-02-09 05:31:16 +03:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
case B_PARTIAL_READ:
|
2017-05-15 15:20:53 +03:00
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
jsonParseContext.Listener()->HandleError(B_BAD_DATA,
|
|
|
|
jsonParseContext.LineNumber(), "unexpected end of input");
|
|
|
|
return false;
|
2017-05-15 15:20:53 +03:00
|
|
|
}
|
2014-08-04 21:00:03 +04:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
default:
|
2017-05-15 15:20:53 +03:00
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
jsonParseContext.Listener()->HandleError(result, -1,
|
|
|
|
"io related read error");
|
|
|
|
return false;
|
2017-05-15 15:20:53 +03:00
|
|
|
}
|
2017-05-13 09:50:39 +03:00
|
|
|
}
|
|
|
|
}
|
2014-08-04 21:00:03 +04:00
|
|
|
|
2017-02-09 05:31:16 +03:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
bool
|
|
|
|
BJson::NextNonWhitespaceChar(JsonParseContext& jsonParseContext, char* c)
|
|
|
|
{
|
|
|
|
while (true) {
|
|
|
|
if (!NextChar(jsonParseContext, c))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (*c) {
|
|
|
|
case 0x0a: // newline
|
|
|
|
case 0x0d: // cr
|
|
|
|
jsonParseContext.IncrementLineNumber();
|
|
|
|
case ' ': // space
|
|
|
|
// swallow whitespace as it is not syntactically
|
|
|
|
// significant.
|
|
|
|
break;
|
2014-08-04 21:00:03 +04:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
default:
|
|
|
|
return true;
|
2014-08-04 21:00:03 +04:00
|
|
|
}
|
2017-05-13 09:50:39 +03:00
|
|
|
}
|
|
|
|
}
|
2014-08-04 21:00:03 +04:00
|
|
|
|
2017-02-09 05:31:16 +03:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
bool
|
|
|
|
BJson::ParseAny(JsonParseContext& jsonParseContext)
|
|
|
|
{
|
|
|
|
char c;
|
2014-08-04 21:00:03 +04:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
if (!NextNonWhitespaceChar(jsonParseContext, &c))
|
|
|
|
return false;
|
2014-08-04 21:00:03 +04:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
switch (c) {
|
|
|
|
case 'f': // [f]alse
|
|
|
|
return ParseExpectedVerbatimStringAndRaiseEvent(
|
|
|
|
jsonParseContext, "alse", 4, 'f', B_JSON_FALSE);
|
2017-02-09 05:31:16 +03:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
case 't': // [t]rue
|
|
|
|
return ParseExpectedVerbatimStringAndRaiseEvent(
|
|
|
|
jsonParseContext, "rue", 3, 't', B_JSON_TRUE);
|
2014-08-04 21:00:03 +04:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
case 'n': // [n]ull
|
|
|
|
return ParseExpectedVerbatimStringAndRaiseEvent(
|
|
|
|
jsonParseContext, "ull", 3, 'n', B_JSON_NULL);
|
2014-08-04 21:00:03 +04:00
|
|
|
|
|
|
|
case '"':
|
2017-05-13 09:50:39 +03:00
|
|
|
return ParseString(jsonParseContext, B_JSON_STRING);
|
2014-08-04 21:00:03 +04:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
case '{':
|
|
|
|
return ParseObject(jsonParseContext);
|
|
|
|
|
|
|
|
case '[':
|
|
|
|
return ParseArray(jsonParseContext);
|
2014-08-04 21:00:03 +04:00
|
|
|
|
|
|
|
case '+':
|
|
|
|
case '-':
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
2017-05-13 09:50:39 +03:00
|
|
|
jsonParseContext.PushbackChar(c); // keeps the parse simple
|
|
|
|
return ParseNumber(jsonParseContext);
|
|
|
|
|
|
|
|
default:
|
2014-08-04 21:00:03 +04:00
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
BString errorMessage;
|
|
|
|
if (c >= 0x20 && c < 0x7f) {
|
|
|
|
errorMessage.SetToFormat("unexpected character [%" B_PRIu8 "]"
|
|
|
|
" (%c) when parsing element", static_cast<uint8>(c), c);
|
|
|
|
} else {
|
|
|
|
errorMessage.SetToFormat("unexpected character [%" B_PRIu8 "]"
|
|
|
|
" when parsing element", (uint8) c);
|
2014-08-31 01:24:40 +04:00
|
|
|
}
|
2017-05-13 09:50:39 +03:00
|
|
|
jsonParseContext.Listener()->HandleError(B_BAD_DATA,
|
|
|
|
jsonParseContext.LineNumber(), errorMessage.String());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-02-09 05:31:16 +03:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-04 21:00:03 +04:00
|
|
|
|
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
/*! This method captures an object name, a separator ':' and then any value. */
|
2014-08-04 21:00:03 +04:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
bool
|
|
|
|
BJson::ParseObjectNameValuePair(JsonParseContext& jsonParseContext)
|
|
|
|
{
|
|
|
|
bool didParseName = false;
|
|
|
|
char c;
|
2017-02-09 05:31:16 +03:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
while (true) {
|
|
|
|
if (!NextNonWhitespaceChar(jsonParseContext, &c))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case '\"': // name of the object
|
|
|
|
{
|
|
|
|
if (!didParseName) {
|
2017-05-15 15:20:53 +03:00
|
|
|
if (!ParseString(jsonParseContext, B_JSON_OBJECT_NAME))
|
2017-05-13 09:50:39 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
didParseName = true;
|
|
|
|
} else {
|
|
|
|
jsonParseContext.Listener()->HandleError(B_BAD_DATA,
|
|
|
|
jsonParseContext.LineNumber(), "unexpected"
|
|
|
|
" [\"] character when parsing object name-"
|
|
|
|
" value separator");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ':': // separator
|
|
|
|
{
|
|
|
|
if (didParseName) {
|
|
|
|
if (!ParseAny(jsonParseContext))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
jsonParseContext.Listener()->HandleError(B_BAD_DATA,
|
|
|
|
jsonParseContext.LineNumber(), "unexpected"
|
|
|
|
" [:] character when parsing object name-"
|
|
|
|
" value pair");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
BString errorMessage;
|
|
|
|
errorMessage.SetToFormat(
|
|
|
|
"unexpected character [%c] when parsing object"
|
|
|
|
" name-value pair",
|
|
|
|
c);
|
|
|
|
jsonParseContext.Listener()->HandleError(B_BAD_DATA,
|
|
|
|
jsonParseContext.LineNumber(), errorMessage.String());
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-04 21:00:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
bool
|
|
|
|
BJson::ParseObject(JsonParseContext& jsonParseContext)
|
2014-08-04 21:00:03 +04:00
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
if (!jsonParseContext.Listener()->Handle(
|
2017-05-15 15:20:53 +03:00
|
|
|
BJsonEvent(B_JSON_OBJECT_START))) {
|
2017-05-13 09:50:39 +03:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-04 21:00:03 +04:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
char c;
|
|
|
|
bool firstItem = true;
|
2014-08-04 21:00:03 +04:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
while (true) {
|
|
|
|
if (!NextNonWhitespaceChar(jsonParseContext, &c))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case '}': // terminate the object
|
2017-05-15 15:20:53 +03:00
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
if (!jsonParseContext.Listener()->Handle(
|
2017-05-15 15:20:53 +03:00
|
|
|
BJsonEvent(B_JSON_OBJECT_END))) {
|
2017-05-13 09:50:39 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2017-05-15 15:20:53 +03:00
|
|
|
}
|
2017-05-13 09:50:39 +03:00
|
|
|
|
|
|
|
case ',': // next value.
|
2017-05-15 15:20:53 +03:00
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
if (firstItem) {
|
|
|
|
jsonParseContext.Listener()->HandleError(B_BAD_DATA,
|
|
|
|
jsonParseContext.LineNumber(), "unexpected"
|
|
|
|
" item separator when parsing start of"
|
|
|
|
" object");
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-04 21:00:03 +04:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
if (!ParseObjectNameValuePair(jsonParseContext))
|
|
|
|
return false;
|
2014-08-04 21:00:03 +04:00
|
|
|
break;
|
2017-05-15 15:20:53 +03:00
|
|
|
}
|
2014-08-04 21:00:03 +04:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
default:
|
2014-08-04 21:00:03 +04:00
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
if (firstItem) {
|
|
|
|
jsonParseContext.PushbackChar(c);
|
|
|
|
if (!ParseObjectNameValuePair(jsonParseContext))
|
|
|
|
return false;
|
|
|
|
firstItem = false;
|
|
|
|
} else {
|
|
|
|
jsonParseContext.Listener()->HandleError(B_BAD_DATA,
|
|
|
|
jsonParseContext.LineNumber(), "expected"
|
2017-08-02 00:19:27 +03:00
|
|
|
" separator when parsing an object");
|
2017-05-13 09:50:39 +03:00
|
|
|
}
|
2014-08-04 21:00:03 +04:00
|
|
|
}
|
2017-05-13 09:50:39 +03:00
|
|
|
}
|
|
|
|
}
|
2014-08-04 21:00:03 +04:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
BJson::ParseArray(JsonParseContext& jsonParseContext)
|
|
|
|
{
|
|
|
|
if (!jsonParseContext.Listener()->Handle(
|
2017-05-15 15:20:53 +03:00
|
|
|
BJsonEvent(B_JSON_ARRAY_START))) {
|
2017-05-13 09:50:39 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char c;
|
|
|
|
bool firstItem = true;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (!NextNonWhitespaceChar(jsonParseContext, &c))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case ']': // terminate the array
|
2017-05-15 15:20:53 +03:00
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
if (!jsonParseContext.Listener()->Handle(
|
2017-05-15 15:20:53 +03:00
|
|
|
BJsonEvent(B_JSON_ARRAY_END))) {
|
2017-05-13 09:50:39 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2017-05-15 15:20:53 +03:00
|
|
|
}
|
2017-05-13 09:50:39 +03:00
|
|
|
|
|
|
|
case ',': // next value.
|
2017-05-15 15:20:53 +03:00
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
if (firstItem) {
|
|
|
|
jsonParseContext.Listener()->HandleError(B_BAD_DATA,
|
|
|
|
jsonParseContext.LineNumber(), "unexpected"
|
|
|
|
" item separator when parsing start of"
|
|
|
|
" array");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ParseAny(jsonParseContext))
|
|
|
|
return false;
|
2014-08-04 21:00:03 +04:00
|
|
|
break;
|
2017-05-15 15:20:53 +03:00
|
|
|
}
|
2017-05-13 09:50:39 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
if (firstItem) {
|
|
|
|
jsonParseContext.PushbackChar(c);
|
|
|
|
if (!ParseAny(jsonParseContext))
|
|
|
|
return false;
|
|
|
|
firstItem = false;
|
|
|
|
} else {
|
|
|
|
jsonParseContext.Listener()->HandleError(B_BAD_DATA,
|
|
|
|
jsonParseContext.LineNumber(), "expected"
|
2017-08-02 00:19:27 +03:00
|
|
|
" separator when parsing an array");
|
2017-05-13 09:50:39 +03:00
|
|
|
}
|
2014-08-04 21:00:03 +04:00
|
|
|
}
|
2017-05-13 09:50:39 +03:00
|
|
|
}
|
2014-08-04 21:00:03 +04:00
|
|
|
}
|
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
return true;
|
2014-08-04 21:00:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
bool
|
|
|
|
BJson::ParseEscapeUnicodeSequence(JsonParseContext& jsonParseContext,
|
|
|
|
BString& stringResult)
|
2014-08-04 21:00:03 +04:00
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
char buffer[5];
|
|
|
|
buffer[4] = 0;
|
2014-08-04 21:00:03 +04:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
if (!NextChar(jsonParseContext, &buffer[0])
|
|
|
|
|| !NextChar(jsonParseContext, &buffer[1])
|
|
|
|
|| !NextChar(jsonParseContext, &buffer[2])
|
|
|
|
|| !NextChar(jsonParseContext, &buffer[3])) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-04 21:00:03 +04:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
if (!b_jsonparse_all_hex(buffer)) {
|
2017-05-15 15:20:53 +03:00
|
|
|
BString errorMessage;
|
|
|
|
errorMessage.SetToFormat(
|
|
|
|
"malformed unicode sequence [%s] in string parsing",
|
|
|
|
buffer);
|
|
|
|
jsonParseContext.Listener()->HandleError(B_BAD_DATA,
|
|
|
|
jsonParseContext.LineNumber(), errorMessage.String());
|
|
|
|
return false;
|
2017-05-13 09:50:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint intValue;
|
|
|
|
|
|
|
|
if (sscanf(buffer, "%4x", &intValue) != 1) {
|
2017-05-15 15:20:53 +03:00
|
|
|
BString errorMessage;
|
|
|
|
errorMessage.SetToFormat(
|
|
|
|
"unable to process unicode sequence [%s] in string "
|
|
|
|
" parsing", buffer);
|
|
|
|
jsonParseContext.Listener()->HandleError(B_BAD_DATA,
|
|
|
|
jsonParseContext.LineNumber(), errorMessage.String());
|
|
|
|
return false;
|
2017-05-13 09:50:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
char character[7];
|
|
|
|
char* ptr = character;
|
|
|
|
BUnicodeChar::ToUTF8(intValue, &ptr);
|
|
|
|
int32 sequenceLength = ptr - character;
|
|
|
|
stringResult.Append(character, sequenceLength);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
BJson::ParseStringEscapeSequence(JsonParseContext& jsonParseContext,
|
|
|
|
BString& stringResult)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
|
|
|
|
if (!NextChar(jsonParseContext, &c))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'n':
|
|
|
|
stringResult += "\n";
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
stringResult += "\r";
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
stringResult += "\b";
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
stringResult += "\f";
|
2014-08-04 21:00:03 +04:00
|
|
|
break;
|
2017-05-13 09:50:39 +03:00
|
|
|
case '\\':
|
|
|
|
stringResult += "\\";
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
stringResult += "/";
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
stringResult += "\t";
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
stringResult += "\"";
|
|
|
|
break;
|
|
|
|
case 'u':
|
2017-05-15 15:20:53 +03:00
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
// unicode escape sequence.
|
|
|
|
if (!ParseEscapeUnicodeSequence(jsonParseContext,
|
2017-05-15 15:20:53 +03:00
|
|
|
stringResult)) {
|
2017-05-13 09:50:39 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2017-05-15 15:20:53 +03:00
|
|
|
}
|
2017-05-13 09:50:39 +03:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
BString errorMessage;
|
|
|
|
errorMessage.SetToFormat(
|
|
|
|
"unexpected escaped character [%c] in string parsing",
|
|
|
|
c);
|
|
|
|
jsonParseContext.Listener()->HandleError(B_BAD_DATA,
|
|
|
|
jsonParseContext.LineNumber(), errorMessage.String());
|
|
|
|
return false;
|
2014-08-04 21:00:03 +04:00
|
|
|
}
|
|
|
|
}
|
2017-02-09 05:31:16 +03:00
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
return true;
|
2014-08-04 21:00:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
2017-05-13 09:50:39 +03:00
|
|
|
BJson::ParseString(JsonParseContext& jsonParseContext,
|
|
|
|
json_event_type eventType)
|
2014-08-04 21:00:03 +04:00
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
char c;
|
|
|
|
BString stringResult;
|
|
|
|
|
|
|
|
while(true) {
|
|
|
|
if (!NextChar(jsonParseContext, &c))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case '"':
|
2017-05-15 15:20:53 +03:00
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
// terminates the string assembled so far.
|
|
|
|
jsonParseContext.Listener()->Handle(
|
|
|
|
BJsonEvent(eventType, stringResult.String()));
|
|
|
|
return true;
|
2017-05-15 15:20:53 +03:00
|
|
|
}
|
2017-05-13 09:50:39 +03:00
|
|
|
|
|
|
|
case '\\':
|
2017-05-15 15:20:53 +03:00
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
if (!ParseStringEscapeSequence(jsonParseContext,
|
|
|
|
stringResult)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2017-05-15 15:20:53 +03:00
|
|
|
}
|
2017-05-13 09:50:39 +03:00
|
|
|
|
|
|
|
default:
|
2017-05-15 15:20:53 +03:00
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
uint8 uc = static_cast<uint8>(c);
|
|
|
|
|
|
|
|
if(uc < 0x20) { // control characters are not allowed
|
|
|
|
BString errorMessage;
|
|
|
|
errorMessage.SetToFormat("illegal control character"
|
|
|
|
" [%" B_PRIu8 "] when parsing a string", uc);
|
|
|
|
jsonParseContext.Listener()->HandleError(B_BAD_DATA,
|
|
|
|
jsonParseContext.LineNumber(),
|
|
|
|
errorMessage.String());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
stringResult.Append(&c, 1);
|
|
|
|
break;
|
2017-05-15 15:20:53 +03:00
|
|
|
}
|
2017-05-13 09:50:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
BJson::ParseExpectedVerbatimStringAndRaiseEvent(
|
|
|
|
JsonParseContext& jsonParseContext, const char* expectedString,
|
|
|
|
size_t expectedStringLength, char leadingChar,
|
|
|
|
json_event_type jsonEventType)
|
|
|
|
{
|
|
|
|
if (ParseExpectedVerbatimString(jsonParseContext, expectedString,
|
2017-05-15 15:20:53 +03:00
|
|
|
expectedStringLength, leadingChar)) {
|
2017-05-13 09:50:39 +03:00
|
|
|
if (!jsonParseContext.Listener()->Handle(BJsonEvent(jsonEventType)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! This will make sure that the constant string is available at the input. */
|
|
|
|
|
|
|
|
bool
|
|
|
|
BJson::ParseExpectedVerbatimString(JsonParseContext& jsonParseContext,
|
|
|
|
const char* expectedString, size_t expectedStringLength, char leadingChar)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
size_t offset = 0;
|
|
|
|
|
|
|
|
while (offset < expectedStringLength) {
|
|
|
|
if (!NextChar(jsonParseContext, &c))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (c != expectedString[offset]) {
|
|
|
|
BString errorMessage;
|
|
|
|
errorMessage.SetToFormat("malformed json primative literal; "
|
|
|
|
"expected [%c%s], but got [%c] at position %" B_PRIdSSIZE,
|
|
|
|
leadingChar, expectedString, c, offset);
|
|
|
|
jsonParseContext.Listener()->HandleError(B_BAD_DATA,
|
|
|
|
jsonParseContext.LineNumber(), errorMessage.String());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*! This function checks to see that the supplied string is a well formed
|
|
|
|
JSON number. It does this from a string rather than a stream for
|
|
|
|
convenience. This is not anticipated to impact performance because
|
|
|
|
the string values are short.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool
|
|
|
|
BJson::IsValidNumber(BString& number)
|
|
|
|
{
|
|
|
|
int32 offset = 0;
|
|
|
|
int32 len = number.Length();
|
|
|
|
|
|
|
|
if (offset < len && number[offset] == '-')
|
|
|
|
offset++;
|
|
|
|
|
|
|
|
if (offset >= len)
|
2014-08-04 21:00:03 +04:00
|
|
|
return false;
|
2017-05-13 09:50:39 +03:00
|
|
|
|
|
|
|
if (isdigit(number[offset]) && number[offset] != '0') {
|
|
|
|
while (offset < len && isdigit(number[offset]))
|
|
|
|
offset++;
|
|
|
|
} else {
|
|
|
|
if (number[offset] == '0')
|
|
|
|
offset++;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset < len && number[offset] == '.') {
|
|
|
|
offset++;
|
|
|
|
|
|
|
|
if (offset >= len)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while (offset < len && isdigit(number[offset]))
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset < len && (number[offset] == 'E' || number[offset] == 'e')) {
|
|
|
|
offset++;
|
|
|
|
|
|
|
|
if(offset < len && (number[offset] == '+' || number[offset] == '-'))
|
|
|
|
offset++;
|
|
|
|
|
|
|
|
if (offset >= len)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while (offset < len && isdigit(number[offset]))
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset == len;
|
2014-08-04 21:00:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-13 09:50:39 +03:00
|
|
|
/*! Note that this method hits the 'NextChar' method on the context directly
|
|
|
|
and handles any end-of-file state itself because it is feasible that the
|
|
|
|
entire JSON payload is a number and because (unlike other structures, the
|
|
|
|
number can take the end-of-file to signify the end of the number.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool
|
|
|
|
BJson::ParseNumber(JsonParseContext& jsonParseContext)
|
|
|
|
{
|
|
|
|
BString value;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
char c;
|
|
|
|
status_t result = jsonParseContext.NextChar(&c);
|
|
|
|
|
|
|
|
switch (result) {
|
|
|
|
case B_OK:
|
2017-05-15 15:20:53 +03:00
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
if (isdigit(c)) {
|
|
|
|
value += c;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL != strchr("+-eE.", c)) {
|
|
|
|
value += c;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonParseContext.PushbackChar(c);
|
|
|
|
// intentional fall through
|
2017-05-15 15:20:53 +03:00
|
|
|
}
|
2017-05-13 09:50:39 +03:00
|
|
|
case B_PARTIAL_READ:
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
if (!IsValidNumber(value)) {
|
|
|
|
jsonParseContext.Listener()->HandleError(B_BAD_DATA,
|
|
|
|
jsonParseContext.LineNumber(), "malformed number");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonParseContext.Listener()->Handle(BJsonEvent(B_JSON_NUMBER,
|
|
|
|
value.String()));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
default:
|
2017-05-15 15:20:53 +03:00
|
|
|
{
|
2017-05-13 09:50:39 +03:00
|
|
|
jsonParseContext.Listener()->HandleError(result, -1,
|
|
|
|
"io related read error");
|
|
|
|
return false;
|
2017-05-15 15:20:53 +03:00
|
|
|
}
|
2017-05-13 09:50:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-04 21:00:03 +04:00
|
|
|
|
2018-03-19 23:42:22 +03:00
|
|
|
} // namespace BPrivate
|