jansson icon indicating copy to clipboard operation
jansson copied to clipboard

json_error_t source setting

Open Villemoes opened this issue 8 years ago • 0 comments

When passing a FILE* or fd to json_loadf/json_loadfd, it would be nice if one could prevent jansson from overriding the jerr->source with "" (or which is special cased). One can sort-of manually fix it up afterwards before checking the return value from json_loadf/json_loadfd, but it's awkward, especially since jsonp_error_set_source that nicely handles an overly long string is not public.

Suggestion: teach the json_load* function a flag that says "don't override jerr->source", and make jsonp_error_set_source public.

This is also a problem internally: json_load_file sets the source appropriately, but unless the fopen() outright fails, all information about the origin is lost (json_load_file just delegates to json_loadf). For example

	json_t *json;
	json_error_t jerr;

	json = json_load_file(argv[1], 0, &jerr);
	if (!json) {
		fprintf(stderr, "json error: %s:%d:%d: %s\n",
			jerr.source, jerr.line, jerr.column,
			jerr.text);
	}

gives output such as

json error: <stream>:2:0: string or '}' expected near end of file
json error: y:-1:-1: unable to open y: No such file or directory

Villemoes avatar Jun 13 '17 13:06 Villemoes