Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Few more fixes and improvements #19

Merged
merged 6 commits into from Dec 16, 2019

Make sure every JSON_ERROR return has set error message

Also add missing JSON_ERROR return.
  • Loading branch information
boris-kolpackov committed Dec 15, 2019
commit 1500ca73f2ed44ed8a6129fd1fa164bd7e326874
@@ -145,9 +145,13 @@ static void init(json_stream *json)
static enum json_type
is_match(json_stream *json, const char *pattern, enum json_type type)
{
int c;
for (const char *p = pattern; *p; p++)
if (*p != json->source.get(&json->source))
if (*p != (c = json->source.get(&json->source)))
{
json_error(json, "expected '%c' instead of byte '%c'", *p, c);
return JSON_ERROR;
}
return type;
}

@@ -523,15 +527,17 @@ is_digit(int c)
static int
read_digits(json_stream *json)
{
int c;
unsigned nread = 0;
while (is_digit(json->source.peek(&json->source))) {
while (is_digit(c = json->source.peek(&json->source))) {
if (pushchar(json, json->source.get(&json->source)) != 0)
return -1;

nread++;
}

if (nread == 0) {
json_error(json, "expected digit instead of byte '%c'", c);
return -1;
}

@@ -549,6 +555,7 @@ read_number(json_stream *json, int c)
return read_number(json, c);
} else {
json_error(json, "unexpected byte, '%c'", c);
return JSON_ERROR;
}
} else if (strchr("123456789", c) != NULL) {
c = json->source.peek(&json->source);
@@ -692,7 +699,13 @@ enum json_type json_next(json_stream *json)
}
} while (json_isspace(c));

/* TODO: this feels a bit too loose: with the current logic we treat
01 as two values which feels wrong. But on the other hand,
{...}{...} or [...][...] do not feel unreasonable. Maybe we should
require some form of separation in the streaming case (either
whitespace or }/])? */
if (!(json->flags & JSON_FLAG_STREAMING) && c != EOF) {
json_error(json, "expected EOF instead of byte '%c'", c);
return JSON_ERROR;
}

@@ -726,7 +739,8 @@ enum json_type json_next(json_stream *json)
/* No property value pairs yet. */
enum json_type value = read_value(json, c);
if (value != JSON_STRING) {
json_error(json, "%s", "expected property name or '}'");
if (value != JSON_ERROR)
json_error(json, "%s", "expected property name or '}'");
return JSON_ERROR;
} else {
json->stack[json->stack_top].count++;
@@ -742,7 +756,8 @@ enum json_type json_next(json_stream *json)
} else {
enum json_type value = read_value(json, next(json));
if (value != JSON_STRING) {
json_error(json, "%s", "expected property name");
if (value != JSON_ERROR)
json_error(json, "%s", "expected property name");
return JSON_ERROR;
} else {
json->stack[json->stack_top].count++;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.