void JSON::parse() {
assert(start != NULL, "Need something to parse");
if (start == NULL) {
_valid = false;
error(INTERNAL_ERROR, "JSON parser was called with a string that was NULL.");
} else {
_valid = parse_json_value();
}
}
bool JSON::valid() { return _valid;
}
bool JSON::parse_json_value() {
int c;
c = skip_to_token();
if (c == -1) { return false;
}
// Must start with object or array
if (level == 0) {
switch (c) { case'{':
if (parse_json_object() == false) { return false;
}
c = skip_to_token();
if (c > 0) {
mark_pos();
error(SYNTAX_ERROR, "Only one top level object/array is allowed."); return false;
} else if (c < 0) { return false;
} returntrue;
case'[':
if (parse_json_array() == false) { return false;
}
c = skip_to_token();
if (c > 0) {
mark_pos();
error(SYNTAX_ERROR, "Only one top level object/array is allowed."); return false;
} else if (c < 0) { return false;
} returntrue;
case0:
error(SYNTAX_ERROR, "EOS was encountered before any json declarations"); return false;
default:
error(SYNTAX_ERROR, "Json must start with an object or an array."); return false;
}
} else { // level > 0 switch (c) { case'{': return parse_json_object();
case0:
error(SYNTAX_ERROR, "EOS was encountered when expecting a json value."); return false;
default:
error(SYNTAX_ERROR, "Could not parse as a json value (did you forget to quote your strings?)."); return false;
}
}
}
// Should only be called when we actually have the start of an object // Otherwise it is an internal error bool JSON::parse_json_object() {
NOT_PRODUCT(const char* prev_pos);
int c;
mark_pos(); // Check that we are not called in error
if (expect_any("{", "object start", INTERNAL_ERROR) <= 0) { return false;
}
if (!callback(JSON_OBJECT_BEGIN, NULL, level++)) { return false;
}
for (;;) {
mark_pos();
c = skip_to_token();
if (c == 0) {
error(SYNTAX_ERROR, "EOS when expecting an object key or object end"); return false;
} else if (c < 0) { return false;
} else if (c == '}') { // We got here from either empty object "{}" or ending comma "{a:1,}"
next(); break;
}
c = skip_to_token();
mark_pos();
if (expect_any(",}", "value separator or object end") <= 0) { return false;
}
if (c == '}') { break;
}
}
assert(c == '}', "array parsing ended without object end token ('}')"); return callback(JSON_OBJECT_END, NULL, --level);
}
// Should only be called when we actually have the start of an array // Otherwise it is an internal error bool JSON::parse_json_array() {
NOT_PRODUCT(const char* prev_pos);
int c;
mark_pos(); // Check that we are not called in error
if (expect_any("[", "array start character", INTERNAL_ERROR) <= 0) { return false;
}
if (!callback(JSON_ARRAY_BEGIN, NULL, level++)) { return false;
}
for (;;) {
mark_pos();
c = skip_to_token();
if (c == 0) {
error(SYNTAX_ERROR, "EOS when expecting a json value or array end"); return false;
} else if (c < 0) { return false;
} else if (c == ']') { // We got here from either empty array "[]" or ending comma "[1,]"
next(); break;
}
end = strchr(pos, '"'); // TODO: escapes
if (end == NULL) {
error(SYNTAX_ERROR, "String started here never ended. Expected \'\"\' before EOS."); return false;
}
// TODO: hotspot equivalents? staticbool is_alpha(u_char c) { return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
} staticbool is_numeric(u_char c) { return (c >= '0' && c <= '9');
} staticbool is_alnum(u_char c) { return is_alpha(c) || is_numeric(c);
} staticbool is_word(u_char c) { return c == '_' || is_alnum(c);
}
// Allow object keys to be without quotation, // but then restrict to ([a-zA-Z0-9_])+ bool JSON::parse_json_key() { const char* begin;
JSON_VAL v;
u_char c;
mark_pos();
c = peek();
if (c == '"') { return parse_json_string(true);
}
begin = pos;
c = peek();
if (c == 0) {
error(SYNTAX_ERROR, "Got EOS when expecting an object key."); return false;
} else if (is_word(c) == false) {
error(SYNTAX_ERROR, "Expected an object key, which can be a double-quoted (\") string or a simple string (only alphanumeric characters and underscore, separated by whitespace) that doesn't need to be quoted."); return false;
}
for (;;) {
c = peek(); // Allow the key to be delimited by control characters and the object key-value separator ':'
if (c <= ' ' || c == ':') { break;
} else if (is_word(c) == false) {
error(SYNTAX_ERROR, "Object key need to be quoted, or consist entirely of alphanumeric characters and underscores."); return false;
}
next();
}
bool JSON::parse_json_number() { double double_value;
int tokens, read;
JSON_VAL v;
mark_pos();
// Parsing number - for simplicity ints are limited to 2**53 // sscanf as a double and check if part is 0.
tokens = sscanf(pos, "%lf%n", &double_value, &read);
assert(tokens <= 1, "scanf implementation that counts as a token, parsing json numbers will always fail");
if (tokens == 1) {
assert(read > 0, "sanity");
if (floor(double_value) == double_value) { // No exponent - treat as an int
v.int_value = (int)double_value;
if (!callback(JSON_NUMBER_INT, &v, level)) { return false;
}
} else {
v.double_value = double_value;
if (!callback(JSON_NUMBER_FLOAT, &v, level)) { return false;
}
}
skip(read); returntrue;
}
error(SYNTAX_ERROR, "Couldn't parse json number (note that exponents are not supported)."); return false;
}
bool JSON::parse_json_symbol(const char* name, JSON_TYPE symbol) {
if (expect_string(name, "maybe you forgot to quote your strings?") == false) {
mark_pos(); return false;
} return callback(symbol, NULL, level);
}
c = peek();
for (j = i; c != 0 && j > 0; j--) {
c = next();
} return i - j;
}
/* *Skipwhitespaceandcomments. *Returnsthefirsttokenafterwhitespace/commentswithoutconsumingit *Returns0ifEOSisencountered. *Returns-1ifthereisanerror
*/
int JSON::skip_to_token() {
for (;;) {
int c = peek(0);
if (c == '/') {
u_char c2 = peek(1);
if (c2 == '/') {
c = skip_line_comment();
} else if (c2 == '*') {
c = skip_block_comment();
if (c < 0) { return -1;
}
} // Fall through to keep checking if there // are more whitespace / comments to skip
}
if (c == 0 || c > ' ') { return c;
}
next();
} return0;
}
/* *Skipto,andreturnthewantedcharwithoutconsumingit *Returns0ifEOSisencountered.
*/
u_char JSON::skip_to(u_char want) { // We want the bookkeeping done in next(). // Otherwise strchr could have been used.
u_char c;
for(;;) {
c = peek();
if (c == 0 || c == want) { return c;
}
next();
}
}
// Check that we are not called in error.
if (peek() != '/' || peek(1) != '*') { // Let expect handle EOS.
expect_string("/*", "block comment start", INTERNAL_ERROR); return0;
}
current = pos;
for (;;) {
current = strchrnul_(current, '*');
if (current[0] == 0 || current[1] == 0) { // Advance error marker to start of block comment
mark_pos();
error(SYNTAX_ERROR, "Block comment started here never ended. Expected \"*/\" before EOS."); return -1;
}
if (current[1] == '/') {
pos = current;
if (expect_string("*/", "block comment end", INTERNAL_ERROR) == false) { return -1;
} // Found block comment end return peek();
}
current++;
}
}
const char* JSON::strerror(JSON_ERROR e) { switch (e) { case SYNTAX_ERROR: return"Syntax error"; case INTERNAL_ERROR: return"Internal error"; case KEY_ERROR: return"Key error"; case VALUE_ERROR: return"Value error"; default:
ShouldNotReachHere(); return"Unknown error";
}
}
void JSON::error(JSON_ERROR e, const char* format, ...) {
_valid = false;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.