/*** error reporting ***/
-static void error_init(json_error_t *error)
-{
- if(error)
- {
- error->text[0] = '\0';
- error->line = -1;
- }
-}
-
static void error_set(json_error_t *error, const lex_t *lex,
const char *msg, ...)
{
va_list ap;
- char text[JSON_ERROR_TEXT_LENGTH];
+ char msg_text[JSON_ERROR_TEXT_LENGTH];
- if(!error || error->text[0] != '\0') {
- /* error already set */
+ int line = -1, col = -1;
+ const char *result = msg_text;
+
+ if(!error)
return;
- }
va_start(ap, msg);
- vsnprintf(text, JSON_ERROR_TEXT_LENGTH, msg, ap);
+ vsnprintf(msg_text, JSON_ERROR_TEXT_LENGTH, msg, ap);
va_end(ap);
if(lex)
{
const char *saved_text = strbuffer_value(&lex->saved_text);
- error->line = lex->line;
+ char msg_with_context[JSON_ERROR_TEXT_LENGTH];
+
+ line = lex->line;
+
if(saved_text && saved_text[0])
{
if(lex->saved_text.length <= 20) {
- snprintf(error->text, JSON_ERROR_TEXT_LENGTH,
- "%s near '%s'", text, saved_text);
+ snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH,
+ "%s near '%s'", msg_text, saved_text);
+ result = msg_with_context;
}
- else
- snprintf(error->text, JSON_ERROR_TEXT_LENGTH, "%s", text);
}
else
{
- snprintf(error->text, JSON_ERROR_TEXT_LENGTH,
- "%s near end of file", text);
+ snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH,
+ "%s near end of file", msg_text);
+ result = msg_with_context;
}
}
- else
- {
- error->line = -1;
- snprintf(error->text, JSON_ERROR_TEXT_LENGTH, "%s", text);
- }
+
+ jsonp_error_set(error, line, col, "%s", result);
}
static json_t *parse_json(lex_t *lex, json_error_t *error)
{
- error_init(error);
-
lex_scan(lex, error);
if(lex->token != '[' && lex->token != '{') {
error_set(error, lex, "'[' or '{' expected");
if(lex_init(&lex, string_get, string_eof, (void *)&stream_data))
return NULL;
+ jsonp_error_init(error, "<string>");
+
result = parse_json(&lex, error);
if(!result)
goto out;
json_t *json_loadf(FILE *input, size_t flags, json_error_t *error)
{
lex_t lex;
+ const char *source;
json_t *result;
(void)flags; /* unused */
if(lex_init(&lex, (get_func)fgetc, (eof_func)feof, input))
return NULL;
+ if(input == stdin)
+ source = "<stdin>";
+ else
+ source = "<stream>";
+
+ jsonp_error_init(error, source);
+
result = parse_json(&lex, error);
if(!result)
goto out;
json_t *result;
FILE *fp;
- error_init(error);
+ jsonp_error_init(error, path);
fp = fopen(path, "r");
if(!fp)
#include <jansson.h>
#include "jansson_private.h"
-static void error_init(json_error_t *error)
-{
- if(error)
- {
- error->text[0] = '\0';
- error->line = -1;
- }
-}
-
-static void error_set(json_error_t *error, const int line, const char *msg, ...)
-{
- va_list ap;
-
- if(!error || error->text[0] != '\0') {
- /* error already set */
- return;
- }
-
- error->line = line;
-
- va_start(ap, msg);
- vsnprintf(error->text, JSON_ERROR_TEXT_LENGTH, msg, ap);
- va_end(ap);
-}
-
json_t *json_pack(json_error_t *error, const char *fmt, ...) {
int fmt_length = strlen(fmt);
va_list ap;
stack = calloc(fmt_length, sizeof(json_t *));
free_list = calloc(fmt_length, sizeof(json_t *));
- error_init(error);
+ jsonp_error_init(error, "");
if(!stack || !free_list)
goto out;
case ',': /* Element spacer */
if(!root)
{
- error_set(error, line,
+ jsonp_error_set(error, line, -1,
"Unexpected COMMA precedes root element!");
root = NULL;
goto out;
if(!cur)
{
- error_set(error, line,
+ jsonp_error_set(error, line, -1,
"Unexpected COMMA outside a list or object!");
root = NULL;
goto out;
if(key)
{
- error_set(error, line, "Expected KEY, got COMMA!");
+ jsonp_error_set(error, line, -1,
+ "Expected KEY, got COMMA!");
root = NULL;
goto out;
}
case ':': /* Key/value separator */
if(!key)
{
- error_set(error, line, "Got key/value separator without "
- "a key preceding it!");
+ jsonp_error_set(error, line, -1,
+ "Got key/value separator without "
+ "a key preceding it!");
root = NULL;
goto out;
}
if(!json_is_object(cur))
{
- error_set(error, line, "Got a key/value separator "
- "(':') outside an object!");
+ jsonp_error_set(error, line, -1,
+ "Got a key/value separator "
+ "(':') outside an object!");
root = NULL;
goto out;
}
if(key)
{
- error_set(error, line, "OBJECT or ARRAY ended with an "
+ jsonp_error_set(error, line, -1,
+ "OBJECT or ARRAY ended with an "
"incomplete key/value pair!");
root = NULL;
goto out;
if(depth <= 0)
{
- error_set(error, line,
+ jsonp_error_set(error, line, -1,
"Too many close-brackets '%c'", *fmt);
root = NULL;
goto out;
if(*fmt == ']' && !json_is_array(cur))
{
- error_set(error, line,
+ jsonp_error_set(error, line, -1,
"Stray close-array ']' character");
root = NULL;
goto out;
if(*fmt == '}' && !json_is_object(cur))
{
- error_set(error, line,
+ jsonp_error_set(error, line, -1,
"Stray close-object '}' character");
root = NULL;
goto out;
if(!s)
{
- error_set(error, line,
+ jsonp_error_set(error, line, -1,
"Refusing to handle a NULL string");
root = NULL;
goto out;
if(json_is_object(cur)) {
if(!key)
{
- error_set(error, line,
- "Expected key, got identifier '%c'!", *fmt);
+ jsonp_error_set(error, line, -1,
+ "Expected key, got identifier '%c'!", *fmt);
root = NULL;
goto out;
}
}
else
{
- error_set(error, line, "Can't figure out where to attach "
+ jsonp_error_set(error, line, -1,
+ "Can't figure out where to attach "
"'%c' object!", *fmt);
root = NULL;
goto out;
va_end(ap);
if(depth != 0) {
- error_set(error, line,
+ jsonp_error_set(error, line, -1,
"Missing object or array close-brackets in format string");
root = NULL;
goto out;
int fmt_length = strlen(fmt);
- error_init(error);
+ jsonp_error_init(error, "");
/* Allocation provisioned for worst case */
stack = calloc(fmt_length, sizeof(json_t *));
if(!stack)
{
- error_set(error, line, "Out of memory!");
+ jsonp_error_set(error, line, -1, "Out of memory!");
rv = -1;
goto out;
}
if(!cur)
{
- error_set(error, line,
+ jsonp_error_set(error, line, -1,
"Unexpected COMMA outside a list or object!");
rv = -1;
goto out;
if(key)
{
- error_set(error, line, "Expected KEY, got COMMA!");
+ jsonp_error_set(error, line, -1,
+ "Expected KEY, got COMMA!");
rv = -1;
goto out;
}
case ':': /* Key/value separator */
if(!json_is_object(cur) || !key)
{
- error_set(error, line, "Unexpected ':'");
+ jsonp_error_set(error, line, -1, "Unexpected ':'");
rv = -1;
goto out;
}
{
if(!key)
{
- error_set(error, line, "Objects can't be keys");
+ jsonp_error_set(error, line, -1,
+ "Objects can't be keys");
rv = -1;
goto out;
}
if(json_is_array(cur) && *fmt!=']')
{
- error_set(error, line, "Missing ']'");
+ jsonp_error_set(error, line, -1, "Missing ']'");
rv = -1;
goto out;
}
if(json_is_object(cur) && *fmt!='}')
{
- error_set(error, line, "Missing '}'");
+ jsonp_error_set(error, line, -1, "Missing '}'");
rv = -1;
goto out;
}
if(key)
{
- error_set(error, line, "Unexpected '%c'", *fmt);
+ jsonp_error_set(error, line, -1, "Unexpected '%c'", *fmt);
rv = -1;
goto out;
}
if(depth <= 0)
{
- error_set(error, line, "Unexpected '%c'", *fmt);
+ jsonp_error_set(error, line, -1, "Unexpected '%c'", *fmt);
rv = -1;
goto out;
}
{
if(!key)
{
- error_set(error, line,
+ jsonp_error_set(error, line, -1,
"Only strings may be used as keys!");
rv = -1;
goto out;
}
else
{
- error_set(error, line,
+ jsonp_error_set(error, line, -1,
"Unsure how to retrieve JSON object '%c'",
*fmt);
rv = -1;
case 's':
if(!json_is_string(obj))
{
- error_set(error, line,
+ jsonp_error_set(error, line, -1,
"Type mismatch! Object wasn't a string.");
rv = -2;
goto out;
case 'i':
if(!json_is_integer(obj))
{
- error_set(error, line,
+ jsonp_error_set(error, line, -1,
"Type mismatch! Object wasn't an integer.");
rv = -2;
goto out;
case 'b':
if(!json_is_boolean(obj))
{
- error_set(error, line,
+ jsonp_error_set(error, line, -1,
"Type mismatch! Object wasn't a boolean.");
rv = -2;
goto out;
case 'f':
if(!json_is_number(obj))
{
- error_set(error, line,
+ jsonp_error_set(error, line, -1,
"Type mismatch! Object wasn't a real.");
rv = -2;
goto out;
break;
default:
- error_set(error, line,
+ jsonp_error_set(error, line, -1,
"Unknown format character '%c'", *fmt);
rv = -1;
goto out;