Add a flags parameter to all decoding functions for future needs

As of now, the parameter is unused, but may be needed in the future.
I'm adding it now so that in the future both API and ABI remain
backwards compatible as long as possible.

This is a backwards incompatible change.
This commit is contained in:
Petri Lehtinen 2010-08-13 22:19:20 +03:00
parent f8d0e01e46
commit bfac1972e2
9 changed files with 29 additions and 24 deletions

View File

@ -713,7 +713,7 @@ subset of).
json_t *json; json_t *json;
json_error_t error; json_error_t error;
json = json_load_file("/path/to/file.json", &error); json = json_load_file("/path/to/file.json", 0, &error);
if(!json) { if(!json) {
/* the error variable contains error information */ /* the error variable contains error information */
} }
@ -729,32 +729,35 @@ subset of).
The following functions perform the actual JSON decoding. The following functions perform the actual JSON decoding.
.. cfunction:: json_t *json_loads(const char *input, json_error_t *error) .. cfunction:: json_t *json_loads(const char *input, size_t flags, json_error_t *error)
.. refcounting:: new .. refcounting:: new
Decodes the JSON string *input* and returns the array or object it Decodes the JSON string *input* and returns the array or object it
contains, or *NULL* on error, in which case *error* is filled with contains, or *NULL* on error, in which case *error* is filled with
information about the error. See above for discussion on the information about the error. See above for discussion on the
*error* parameter. *error* parameter. *flags* is currently unused, and should be set
to 0.
.. cfunction:: json_t *json_loadf(FILE *input, json_error_t *error) .. cfunction:: json_t *json_loadf(FILE *input, size_t flags, json_error_t *error)
.. refcounting:: new .. refcounting:: new
Decodes the JSON text in stream *input* and returns the array or Decodes the JSON text in stream *input* and returns the array or
object it contains, or *NULL* on error, in which case *error* is object it contains, or *NULL* on error, in which case *error* is
filled with information about the error. See above for discussion filled with information about the error. See above for discussion
on the *error* parameter. on the *error* parameter. *flags* is currently unused, and should
be set to 0.
.. cfunction:: json_t *json_load_file(const char *path, json_error_t *error) .. cfunction:: json_t *json_load_file(const char *path, size_t flags, json_error_t *error)
.. refcounting:: new .. refcounting:: new
Decodes the JSON text in file *path* and returns the array or Decodes the JSON text in file *path* and returns the array or
object it contains, or *NULL* on error, in which case *error* is object it contains, or *NULL* on error, in which case *error* is
filled with information about the error. See above for discussion filled with information about the error. See above for discussion
on the *error* parameter. on the *error* parameter. *flags* is currently unused, and should
be set to 0.
Equality Equality

View File

@ -117,7 +117,7 @@ int main(int argc, char *argv[])
if(!text) if(!text)
return 1; return 1;
root = json_loads(text, &error); root = json_loads(text, 0, &error);
free(text); free(text);
if(!root) if(!root)

View File

@ -152,7 +152,7 @@ function.
Next we'll call :cfunc:`json_loads()` to decode the JSON text we got Next we'll call :cfunc:`json_loads()` to decode the JSON text we got
as a response:: as a response::
root = json_loads(text, &error); root = json_loads(text, 0, &error);
free(text); free(text);
if(!root) if(!root)

View File

@ -177,9 +177,9 @@ typedef struct {
int line; int line;
} json_error_t; } json_error_t;
json_t *json_loads(const char *input, json_error_t *error); json_t *json_loads(const char *input, size_t flags, json_error_t *error);
json_t *json_loadf(FILE *input, 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, 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_INDENT(n) (n & 0x1F)
#define JSON_COMPACT 0x20 #define JSON_COMPACT 0x20

View File

@ -811,10 +811,11 @@ static int string_eof(void *data)
return (stream->data[stream->pos] == '\0'); return (stream->data[stream->pos] == '\0');
} }
json_t *json_loads(const char *string, json_error_t *error) json_t *json_loads(const char *string, size_t flags, json_error_t *error)
{ {
lex_t lex; lex_t lex;
json_t *result; json_t *result;
(void)flags; /* unused */
string_data_t stream_data = { string_data_t stream_data = {
.data = string, .data = string,
@ -840,10 +841,11 @@ out:
return result; return result;
} }
json_t *json_loadf(FILE *input, json_error_t *error) json_t *json_loadf(FILE *input, size_t flags, json_error_t *error)
{ {
lex_t lex; lex_t lex;
json_t *result; json_t *result;
(void)flags; /* unused */
if(lex_init(&lex, (get_func)fgetc, (eof_func)feof, input)) if(lex_init(&lex, (get_func)fgetc, (eof_func)feof, input))
return NULL; return NULL;
@ -864,7 +866,7 @@ out:
return result; return result;
} }
json_t *json_load_file(const char *path, json_error_t *error) json_t *json_load_file(const char *path, size_t flags, json_error_t *error)
{ {
json_t *result; json_t *result;
FILE *fp; FILE *fp;
@ -879,7 +881,7 @@ json_t *json_load_file(const char *path, json_error_t *error)
return NULL; return NULL;
} }
result = json_loadf(fp, error); result = json_loadf(fp, flags, error);
fclose(fp); fclose(fp);
return result; return result;

View File

@ -59,7 +59,7 @@ int main(int argc, char *argv[])
if(getenv_int("JSON_SORT_KEYS")) if(getenv_int("JSON_SORT_KEYS"))
flags |= JSON_SORT_KEYS; flags |= JSON_SORT_KEYS;
json = json_loadf(stdin, &error); json = json_loadf(stdin, 0, &error);
if(!json) { if(!json) {
fprintf(stderr, "%d\n%s\n", error.line, error.text); fprintf(stderr, "%d\n%s\n", error.line, error.text);
return 1; return 1;

View File

@ -176,7 +176,7 @@ static void test_copy_array(void)
json_t *array, *copy; json_t *array, *copy;
size_t i; size_t i;
array = json_loads(json_array_text, NULL); array = json_loads(json_array_text, 0, NULL);
if(!array) if(!array)
fail("unable to parse an array"); fail("unable to parse an array");
@ -205,7 +205,7 @@ static void test_deep_copy_array(void)
json_t *array, *copy; json_t *array, *copy;
size_t i; size_t i;
array = json_loads(json_array_text, NULL); array = json_loads(json_array_text, 0, NULL);
if(!array) if(!array)
fail("unable to parse an array"); fail("unable to parse an array");
@ -235,7 +235,7 @@ static void test_copy_object(void)
json_t *object, *copy; json_t *object, *copy;
void *iter; void *iter;
object = json_loads(json_object_text, NULL); object = json_loads(json_object_text, 0, NULL);
if(!object) if(!object)
fail("unable to parse an object"); fail("unable to parse an object");
@ -275,7 +275,7 @@ static void test_deep_copy_object(void)
json_t *object, *copy; json_t *object, *copy;
void *iter; void *iter;
object = json_loads(json_object_text, NULL); object = json_loads(json_object_text, 0, NULL);
if(!object) if(!object)
fail("unable to parse an object"); fail("unable to parse an object");

View File

@ -167,8 +167,8 @@ static void test_equal_complex()
" \"array\": [\"foo\", false, null, 1.234]" " \"array\": [\"foo\", false, null, 1.234]"
"}"; "}";
value1 = json_loads(complex_json, NULL); value1 = json_loads(complex_json, 0, NULL);
value2 = json_loads(complex_json, NULL); value2 = json_loads(complex_json, 0, NULL);
if(!value1 || !value2) if(!value1 || !value2)
fail("unable to parse JSON"); fail("unable to parse JSON");
if(!json_equal(value1, value2)) if(!json_equal(value1, value2))

View File

@ -14,7 +14,7 @@ int main()
json_t *json; json_t *json;
json_error_t error; json_error_t error;
json = json_load_file("/path/to/nonexistent/file.json", &error); json = json_load_file("/path/to/nonexistent/file.json", 0, &error);
if(error.line != -1) if(error.line != -1)
fail("json_load_file returned an invalid line number"); 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(error.text, "unable to open /path/to/nonexistent/file.json: No such file or directory") != 0)