/*** error reporting ***/
-static __thread char *json_error_msg = NULL;
-
-static void json_set_error(const json_lex *lex, const char *msg)
+static void json_set_error(json_error_t *error, const json_lex *lex,
+ const char *msg)
{
- free(json_error_msg);
+ if(!error)
+ return;
+
if(*lex->start)
- asprintf(&json_error_msg, "%s near '%.*s' on line %d", msg,
- (int)(lex->input - lex->start), lex->start, lex->line);
+ {
+ int n = (int)(lex->input - lex->start);
+ error->line = lex->line;
+ snprintf(error->text, JSON_ERROR_TEXT_LENGTH,
+ "%s near '%.*s'", msg, n, lex->start);
+ }
else
- asprintf(&json_error_msg, "%s near end of file", msg);
-}
-
-const char *json_get_error(void)
-{
- if(!json_error_msg)
- json_error_msg = strdup("success");
- return json_error_msg;
+ {
+ snprintf(error->text, JSON_ERROR_TEXT_LENGTH,
+ "%s near end of file", msg);
+ }
}
/*** parser ***/
-static json_t *json_parse(json_lex *lex);
+static json_t *json_parse(json_lex *lex, json_error_t *error);
-static json_t *json_parse_object(json_lex *lex)
+static json_t *json_parse_object(json_lex *lex, json_error_t *error)
{
json_t *object = json_object();
if(!object)
json_t *value;
if(lex->token != JSON_TOKEN_STRING) {
- json_set_error(lex, "string expected");
+ json_set_error(error, lex, "string expected");
goto error;
}
json_lex_scan(lex);
if(lex->token != ':') {
- json_set_error(lex, "':' expected");
+ json_set_error(error, lex, "':' expected");
goto error;
}
json_lex_scan(lex);
- value = json_parse(lex);
+ value = json_parse(lex, error);
if(!value)
goto error;
}
if(lex->token != '}') {
- json_set_error(lex, "'}' expected");
+ json_set_error(error, lex, "'}' expected");
goto error;
}
return NULL;
}
-static json_t *json_parse_array(json_lex *lex)
+static json_t *json_parse_array(json_lex *lex, json_error_t *error)
{
json_t *array = json_array();
if(!array)
json_lex_scan(lex);
if(lex->token != ']') {
while(1) {
- json_t *elem = json_parse(lex);
+ json_t *elem = json_parse(lex, error);
if(!elem)
goto error;
}
if(lex->token != ']') {
- json_set_error(lex, "']' expected");
+ json_set_error(error, lex, "']' expected");
goto error;
}
return NULL;
}
-static json_t *json_parse(json_lex *lex)
+static json_t *json_parse(json_lex *lex, json_error_t *error)
{
json_t *json;
break;
case '{':
- json = json_parse_object(lex);
+ json = json_parse_object(lex, error);
break;
case '[':
- json = json_parse_array(lex);
+ json = json_parse_array(lex, error);
break;
case JSON_TOKEN_INVALID:
- json_set_error(lex, "invalid token");
+ json_set_error(error, lex, "invalid token");
return NULL;
default:
- json_set_error(lex, "unexpected token");
+ json_set_error(error, lex, "unexpected token");
return NULL;
}
return json;
}
-json_t *json_loads(const char *string)
+json_t *json_loads(const char *string, json_error_t *error)
{
json_lex lex;
json_t *result = NULL;
return NULL;
if(lex.token != '[' && lex.token != '{') {
- json_set_error(&lex, "'[' or '{' expected");
+ json_set_error(error, &lex, "'[' or '{' expected");
goto out;
}
- result = json_parse(&lex);
+ result = json_parse(&lex, error);
if(!result)
goto out;
if(lex.token != JSON_TOKEN_EOF) {
- json_set_error(&lex, "end of file expected");
+ json_set_error(error, &lex, "end of file expected");
json_decref(result);
result = NULL;
}