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

@@ -31,7 +31,7 @@ int main(int argc, char *argv[])
size_t flags = 0;
json_t *json;
json_error_t error;
json_error_t *error;
if(argc != 1) {
fprintf(stderr, "usage: %s\n", argv[0]);
@@ -61,7 +61,10 @@ int main(int argc, char *argv[])
json = json_loadf(stdin, 0, &error);
if(!json) {
fprintf(stderr, "%d\n%s\n", error.line, error.text);
fprintf(stderr, "%d\n%s\n",
json_error_line(error),
json_error_msg(error));
free(error);
return 1;
}

View File

@@ -47,6 +47,8 @@ json_object_iter_set_new
json_dumps
json_dumpf
json_dump_file
json_error_line
json_error_msg
json_loads
json_loadf
json_load_file

View File

@@ -12,13 +12,16 @@
int main()
{
json_t *json;
json_error_t error;
json_error_t *error;
json = json_load_file("/path/to/nonexistent/file.json", 0, &error);
if(error.line != -1)
if(json)
fail("json_load didn't return an error!");
if(json_error_line(error) != -1)
fail("json_load_file returned an invalid line number");
if(strcmp(error.text, "unable to open /path/to/nonexistent/file.json: No such file or directory") != 0)
if(strcmp(json_error_msg(error), "unable to open /path/to/nonexistent/file.json: No such file or directory") != 0)
fail("json_load_file returned an invalid error message");
free(error);
return 0;
}