Make json_error_t opaque

All decoding functions now accept a json_error_t** parameter and set
it to point to a heap-allocated json_error_t structure if an error
occurs. The contents of json_error_t are no longer exposed directly, a
few functions to do it have been added instead. If an error occurs,
the user must free the json_error_t value.

This makes it possible to enhance the error reporting facilities in
the future without breaking ABI compatibility with older versions.

This is a backwards incompatible change.
This commit is contained in:
Petri Lehtinen
2010-10-14 20:57:55 +03:00
parent 781bda1404
commit 23dd078c8d
6 changed files with 144 additions and 98 deletions

View File

@@ -168,18 +168,19 @@ json_t *json_copy(json_t *value);
json_t *json_deep_copy(json_t *value);
/* error reporting */
typedef struct json_error_t json_error_t;
const char *json_error_msg(const json_error_t *error);
int json_error_line(const json_error_t *error);
/* loading, printing */
#define JSON_ERROR_TEXT_LENGTH 160
typedef struct {
char text[JSON_ERROR_TEXT_LENGTH];
int line;
} json_error_t;
json_t *json_loads(const char *input, size_t flags, json_error_t *error);
json_t *json_loadf(FILE *input, size_t flags, json_error_t *error);
json_t *json_load_file(const char *path, size_t flags, json_error_t *error);
json_t *json_loads(const char *input, size_t flags, json_error_t **error);
json_t *json_loadf(FILE *input, size_t flags, json_error_t **error);
json_t *json_load_file(const char *path, size_t flags, json_error_t **error);
#define JSON_INDENT(n) (n & 0x1F)
#define JSON_COMPACT 0x20

View File

@@ -60,53 +60,71 @@ typedef struct {
/*** error reporting ***/
static void error_init(json_error_t *error)
#define JSON_ERROR_MSG_LENGTH 160
struct json_error_t {
char msg[JSON_ERROR_MSG_LENGTH];
int line;
};
const char *json_error_msg(const json_error_t *error)
{
if(error)
{
error->text[0] = '\0';
error->line = -1;
}
return error ? error->msg : NULL;
}
static void error_set(json_error_t *error, const lex_t *lex,
int json_error_line(const json_error_t *error)
{
return error ? error->line : -1;
}
static void error_init(json_error_t **error)
{
if(error)
*error = NULL;
}
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 text[JSON_ERROR_MSG_LENGTH];
if(!error || error->text[0] != '\0') {
/* error already set */
if(!error || *error) {
/* error not given or already set */
return;
}
*error = malloc(sizeof(json_error_t));
if(!(*error))
return;
va_start(ap, msg);
vsnprintf(text, JSON_ERROR_TEXT_LENGTH, msg, ap);
vsnprintf(text, JSON_ERROR_MSG_LENGTH, msg, ap);
va_end(ap);
if(lex)
{
const char *saved_text = strbuffer_value(&lex->saved_text);
error->line = lex->line;
(*error)->line = lex->line;
if(saved_text && saved_text[0])
{
if(lex->saved_text.length <= 20) {
snprintf(error->text, JSON_ERROR_TEXT_LENGTH,
snprintf((*error)->msg, JSON_ERROR_MSG_LENGTH,
"%s near '%s'", text, saved_text);
}
else
snprintf(error->text, JSON_ERROR_TEXT_LENGTH, "%s", text);
snprintf((*error)->msg, JSON_ERROR_MSG_LENGTH, "%s", text);
}
else
{
snprintf(error->text, JSON_ERROR_TEXT_LENGTH,
snprintf((*error)->msg, JSON_ERROR_MSG_LENGTH,
"%s near end of file", text);
}
}
else
{
error->line = -1;
snprintf(error->text, JSON_ERROR_TEXT_LENGTH, "%s", text);
(*error)->line = -1;
snprintf((*error)->msg, JSON_ERROR_MSG_LENGTH, "%s", text);
}
}
@@ -124,7 +142,7 @@ stream_init(stream_t *stream, get_func get, eof_func eof, void *data)
stream->buffer_pos = 0;
}
static char stream_get(stream_t *stream, json_error_t *error)
static char stream_get(stream_t *stream, json_error_t **error)
{
char c;
@@ -182,7 +200,7 @@ static void stream_unget(stream_t *stream, char c)
}
static int lex_get(lex_t *lex, json_error_t *error)
static int lex_get(lex_t *lex, json_error_t **error)
{
return stream_get(&lex->stream, error);
}
@@ -197,7 +215,7 @@ static void lex_save(lex_t *lex, char c)
strbuffer_append_byte(&lex->saved_text, c);
}
static int lex_get_save(lex_t *lex, json_error_t *error)
static int lex_get_save(lex_t *lex, json_error_t **error)
{
char c = stream_get(&lex->stream, error);
lex_save(lex, c);
@@ -245,7 +263,7 @@ static int32_t decode_unicode_escape(const char *str)
return value;
}
static void lex_scan_string(lex_t *lex, json_error_t *error)
static void lex_scan_string(lex_t *lex, json_error_t **error)
{
char c;
const char *p;
@@ -407,7 +425,7 @@ out:
#define json_strtoint strtol
#endif
static int lex_scan_number(lex_t *lex, char c, json_error_t *error)
static int lex_scan_number(lex_t *lex, char c, json_error_t **error)
{
const char *saved_text;
char *end;
@@ -504,7 +522,7 @@ out:
return -1;
}
static int lex_scan(lex_t *lex, json_error_t *error)
static int lex_scan(lex_t *lex, json_error_t **error)
{
char c;
@@ -610,9 +628,9 @@ static void lex_close(lex_t *lex)
/*** parser ***/
static json_t *parse_value(lex_t *lex, json_error_t *error);
static json_t *parse_value(lex_t *lex, json_error_t **error);
static json_t *parse_object(lex_t *lex, json_error_t *error)
static json_t *parse_object(lex_t *lex, json_error_t **error)
{
json_t *object = json_object();
if(!object)
@@ -677,7 +695,7 @@ error:
return NULL;
}
static json_t *parse_array(lex_t *lex, json_error_t *error)
static json_t *parse_array(lex_t *lex, json_error_t **error)
{
json_t *array = json_array();
if(!array)
@@ -717,7 +735,7 @@ error:
return NULL;
}
static json_t *parse_value(lex_t *lex, json_error_t *error)
static json_t *parse_value(lex_t *lex, json_error_t **error)
{
json_t *json;
@@ -772,7 +790,7 @@ static json_t *parse_value(lex_t *lex, json_error_t *error)
return json;
}
static json_t *parse_json(lex_t *lex, json_error_t *error)
static json_t *parse_json(lex_t *lex, json_error_t **error)
{
error_init(error);
@@ -811,7 +829,7 @@ static int string_eof(void *data)
return (stream->data[stream->pos] == '\0');
}
json_t *json_loads(const char *string, size_t flags, json_error_t *error)
json_t *json_loads(const char *string, size_t flags, json_error_t **error)
{
lex_t lex;
json_t *result;
@@ -838,7 +856,7 @@ out:
return result;
}
json_t *json_loadf(FILE *input, size_t flags, json_error_t *error)
json_t *json_loadf(FILE *input, size_t flags, json_error_t **error)
{
lex_t lex;
json_t *result;
@@ -863,7 +881,7 @@ out:
return result;
}
json_t *json_load_file(const char *path, size_t flags, json_error_t *error)
json_t *json_load_file(const char *path, size_t flags, json_error_t **error)
{
json_t *result;
FILE *fp;