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:
parent
f8d0e01e46
commit
bfac1972e2
@ -713,7 +713,7 @@ subset of).
|
||||
json_t *json;
|
||||
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) {
|
||||
/* the error variable contains error information */
|
||||
}
|
||||
@ -729,32 +729,35 @@ subset of).
|
||||
|
||||
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
|
||||
|
||||
Decodes the JSON string *input* and returns the array or object it
|
||||
contains, or *NULL* on error, in which case *error* is filled with
|
||||
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
|
||||
|
||||
Decodes the JSON text in stream *input* and returns the array or
|
||||
object it contains, or *NULL* on error, in which case *error* is
|
||||
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
|
||||
|
||||
Decodes the JSON text in file *path* and returns the array or
|
||||
object it contains, or *NULL* on error, in which case *error* is
|
||||
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
|
||||
|
@ -117,7 +117,7 @@ int main(int argc, char *argv[])
|
||||
if(!text)
|
||||
return 1;
|
||||
|
||||
root = json_loads(text, &error);
|
||||
root = json_loads(text, 0, &error);
|
||||
free(text);
|
||||
|
||||
if(!root)
|
||||
|
@ -152,7 +152,7 @@ function.
|
||||
Next we'll call :cfunc:`json_loads()` to decode the JSON text we got
|
||||
as a response::
|
||||
|
||||
root = json_loads(text, &error);
|
||||
root = json_loads(text, 0, &error);
|
||||
free(text);
|
||||
|
||||
if(!root)
|
||||
|
@ -177,9 +177,9 @@ typedef struct {
|
||||
int line;
|
||||
} json_error_t;
|
||||
|
||||
json_t *json_loads(const char *input, json_error_t *error);
|
||||
json_t *json_loadf(FILE *input, json_error_t *error);
|
||||
json_t *json_load_file(const char *path, 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
|
||||
|
10
src/load.c
10
src/load.c
@ -811,10 +811,11 @@ static int string_eof(void *data)
|
||||
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;
|
||||
json_t *result;
|
||||
(void)flags; /* unused */
|
||||
|
||||
string_data_t stream_data = {
|
||||
.data = string,
|
||||
@ -840,10 +841,11 @@ out:
|
||||
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;
|
||||
json_t *result;
|
||||
(void)flags; /* unused */
|
||||
|
||||
if(lex_init(&lex, (get_func)fgetc, (eof_func)feof, input))
|
||||
return NULL;
|
||||
@ -864,7 +866,7 @@ out:
|
||||
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;
|
||||
FILE *fp;
|
||||
@ -879,7 +881,7 @@ json_t *json_load_file(const char *path, json_error_t *error)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
result = json_loadf(fp, error);
|
||||
result = json_loadf(fp, flags, error);
|
||||
|
||||
fclose(fp);
|
||||
return result;
|
||||
|
@ -59,7 +59,7 @@ int main(int argc, char *argv[])
|
||||
if(getenv_int("JSON_SORT_KEYS"))
|
||||
flags |= JSON_SORT_KEYS;
|
||||
|
||||
json = json_loadf(stdin, &error);
|
||||
json = json_loadf(stdin, 0, &error);
|
||||
if(!json) {
|
||||
fprintf(stderr, "%d\n%s\n", error.line, error.text);
|
||||
return 1;
|
||||
|
@ -176,7 +176,7 @@ static void test_copy_array(void)
|
||||
json_t *array, *copy;
|
||||
size_t i;
|
||||
|
||||
array = json_loads(json_array_text, NULL);
|
||||
array = json_loads(json_array_text, 0, NULL);
|
||||
if(!array)
|
||||
fail("unable to parse an array");
|
||||
|
||||
@ -205,7 +205,7 @@ static void test_deep_copy_array(void)
|
||||
json_t *array, *copy;
|
||||
size_t i;
|
||||
|
||||
array = json_loads(json_array_text, NULL);
|
||||
array = json_loads(json_array_text, 0, NULL);
|
||||
if(!array)
|
||||
fail("unable to parse an array");
|
||||
|
||||
@ -235,7 +235,7 @@ static void test_copy_object(void)
|
||||
json_t *object, *copy;
|
||||
void *iter;
|
||||
|
||||
object = json_loads(json_object_text, NULL);
|
||||
object = json_loads(json_object_text, 0, NULL);
|
||||
if(!object)
|
||||
fail("unable to parse an object");
|
||||
|
||||
@ -275,7 +275,7 @@ static void test_deep_copy_object(void)
|
||||
json_t *object, *copy;
|
||||
void *iter;
|
||||
|
||||
object = json_loads(json_object_text, NULL);
|
||||
object = json_loads(json_object_text, 0, NULL);
|
||||
if(!object)
|
||||
fail("unable to parse an object");
|
||||
|
||||
|
@ -167,8 +167,8 @@ static void test_equal_complex()
|
||||
" \"array\": [\"foo\", false, null, 1.234]"
|
||||
"}";
|
||||
|
||||
value1 = json_loads(complex_json, NULL);
|
||||
value2 = json_loads(complex_json, NULL);
|
||||
value1 = json_loads(complex_json, 0, NULL);
|
||||
value2 = json_loads(complex_json, 0, NULL);
|
||||
if(!value1 || !value2)
|
||||
fail("unable to parse JSON");
|
||||
if(!json_equal(value1, value2))
|
||||
|
@ -14,7 +14,7 @@ int main()
|
||||
json_t *json;
|
||||
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)
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user