/*
- * Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
+ * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
*
* Jansson is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
/*** 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];
+
+ int line = -1, col = -1;
+ const char *result = msg_text;
- if(!error || error->text[0] != '\0') {
- /* error already set */
+ 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);
}
- two \uXXXX escapes (length 12) forming an UTF-16 surrogate pair
are converted to 4 bytes
*/
- lex->value.string = malloc(lex->saved_text.length + 1);
+ lex->value.string = jsonp_malloc(lex->saved_text.length + 1);
if(!lex->value.string) {
/* this is not very nice, since TOKEN_INVALID is returned */
goto out;
return;
out:
- free(lex->value.string);
+ jsonp_free(lex->value.string);
}
#if JSON_INTEGER_IS_LONG_LONG
strbuffer_clear(&lex->saved_text);
if(lex->token == TOKEN_STRING) {
- free(lex->value.string);
+ jsonp_free(lex->value.string);
lex->value.string = NULL;
}
static void lex_close(lex_t *lex)
{
if(lex->token == TOKEN_STRING)
- free(lex->value.string);
+ jsonp_free(lex->value.string);
strbuffer_close(&lex->saved_text);
}
lex_scan(lex, error);
if(lex->token != ':') {
- free(key);
+ jsonp_free(key);
error_set(error, lex, "':' expected");
goto error;
}
lex_scan(lex, error);
value = parse_value(lex, error);
if(!value) {
- free(key);
+ jsonp_free(key);
goto error;
}
if(json_object_set_nocheck(object, key, value)) {
- free(key);
+ jsonp_free(key);
json_decref(value);
goto error;
}
json_decref(value);
- free(key);
+ jsonp_free(key);
lex_scan(lex, error);
if(lex->token != ',')
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");
{
lex_t lex;
json_t *result;
- (void)flags; /* unused */
-
string_data_t stream_data = {string, 0};
+ (void)flags; /* unused */
+
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)