.. type:: json_error_t
- This data structure is used to return information on decoding
- errors from the decoding functions. Its definition is repeated
- here::
+ This opaque structure is used to return information on errors from
+ the decoding functions. See below for more discussion on error
+ reporting.
- #define JSON_ERROR_TEXT_LENGTH 160
+The following functions perform the JSON decoding:
- typedef struct {
- char text[JSON_ERROR_TEXT_LENGTH];
- int line;
- } json_error_t;
+.. function:: json_t *json_loads(const char *input, size_t flags, json_error_t **error)
- *line* is the line number on which the error occurred, or -1 if
- this information is not available. *text* contains the error
- message (in UTF-8), or an empty string if a message is not
- available.
+ .. refcounting:: new
- The normal usef of :type:`json_error_t` is to allocate it normally
- on the stack, and pass a pointer to a decoding function. Example::
+ Decodes the JSON string *input* and returns the array or object it
+ contains, or *NULL* on error. If *error* is non-*NULL*, it's used
+ to return error information. See below for more discussion on error
+ reporting. *flags* is currently unused, and should be set to 0.
- int main() {
- json_t *json;
- json_error_t error;
+.. function:: json_t *json_loadf(FILE *input, size_t flags, json_error_t **error)
- json = json_load_file("/path/to/file.json", 0, &error);
- if(!json) {
- /* the error variable contains error information */
- }
- ...
- }
+ .. refcounting:: new
- Also note that if the decoding succeeded (``json != NULL`` in the
- above example), the contents of ``error`` are unspecified.
+ Decodes the JSON text in stream *input* and returns the array or
+ object it contains, or *NULL* on error. If *error* is non-*NULL*,
+ it's used to return error information. See below for more
+ discussion on error reporting. *flags* is currently unused, and
+ should be set to 0.
- All decoding functions also accept *NULL* as the
- :type:`json_error_t` pointer, in which case no error information
- is returned to the caller.
+.. function:: json_t *json_load_file(const char *path, size_t flags, json_error_t **error)
-The following functions perform the actual JSON decoding.
+ .. refcounting:: new
-.. function:: json_t *json_loads(const char *input, size_t flags, json_error_t *error)
+ Decodes the JSON text in file *path* and returns the array or
+ object it contains, or *NULL* on error. If *error* is non-*NULL*,
+ it's used to return error information. See below for more
+ discussion on error reporting. *flags* is currently unused, and
+ should be set to 0.
+
+
+The :type:`json_error_t` parameter, that all decoding function accept
+as their last parameter, is used to return information on decoding
+errors to the caller. It is used by having a ``json_error_t *``
+variable and passing a pointer to this variable to a decoding
+function. Example::
+
+ int main() {
+ json_t *json;
+ json_error_t *error;
+
+ json = json_load_file("/path/to/file.json", 0, &error);
+ if(!json) {
+ /* the error variable contains error information */
+ fprintf(stderr, "Decoding error occured on line %d: %s\n", json_error_line(error), json_error_msg(error));
+ free(error);
+ }
+
+ /* ... */
+ }
- .. refcounting:: new
+Note that **the caller must free the error structure** after use if a
+decoding error occurs. If decoding is succesfully finished, *error* is
+simply set to *NULL* by the decoding function.
- 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. *flags* is currently unused, and should be set
- to 0.
+All decoding functions also accept *NULL* as the :type:`json_error_t`
+pointer, in which case no error information is returned to the caller.
+Example::
-.. function:: json_t *json_loadf(FILE *input, size_t flags, json_error_t *error)
+ int main() {
+ json_t *json;
- .. refcounting:: new
+ json = json_load_file("/path/to/file.json", 0, NULL);
+ if(!json) {
+ /* A decoding error occured but no error information is available */
+ }
- 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. *flags* is currently unused, and should
- be set to 0.
+ /* ... */
+ }
-.. function:: json_t *json_load_file(const char *path, size_t flags, json_error_t *error)
+:type:`json_error_t` is totally opaque and must be queried using the
+following functions:
- .. refcounting:: new
+.. function:: const char *json_error_msg(const json_error_t *error)
- 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. *flags* is currently unused, and should
- be set to 0.
+ Return a pointer to an UTF-8 encoded string that describes the
+ error in human-readable text, or *NULL* if *error* is *NULL*.
+
+.. function:: int json_error_line(const json_error_t *error)
+
+ Return the line numer on which the error occurred, or -1 if this
+ information is not available or if *error* is *NULL*.
Equality
json_t *json_deep_copy(json_t *value);
-/* loading, printing */
+/* error reporting */
-#define JSON_ERROR_TEXT_LENGTH 160
+typedef struct json_error_t json_error_t;
-typedef struct {
- char text[JSON_ERROR_TEXT_LENGTH];
- int line;
-} json_error_t;
+const char *json_error_msg(const json_error_t *error);
+int json_error_line(const json_error_t *error);
+
+
+/* loading, printing */
-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
/*** 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)
+{
+ return error ? error->msg : NULL;
+}
+
+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->text[0] = '\0';
- error->line = -1;
- }
+ *error = NULL;
}
-static void error_set(json_error_t *error, const lex_t *lex,
+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);
}
}
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;
}
-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);
}
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);
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;
#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;
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;
/*** 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)
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)
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;
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);
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;
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;
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;
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]);
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;
}
json_dumps
json_dumpf
json_dump_file
+json_error_line
+json_error_msg
json_loads
json_loadf
json_load_file
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;
}