Skip to content

Return error on empty or white-space only string

Rene Kita requested to merge rkta/microjson:master into master

Before this change passing an empty or white-space only string to json_read_object would lead to a return value of 0. Therefore a second check was necessary, either before or after calling the function, to check if an actual parsable JSON text was received.

As both empty and white-space only strings are not valid JSON this check should happen inside the library.

When passing an end pointer this will be NULL in case of this error.

Here is a minimal example code that shows the behaviour:

#include "mjson.h"

struct json{
	_Bool value;
} json;

static const struct json_attr_t json_attrs[] = {
	{"key", t_boolean, .addr.boolean = &json.value},
	{NULL},
};

int main(void)
{
	json_enable_debug(1000, stdout);
	char *input[] = { "", " ", "\f", "\n", "\r", "\t", "\v" };
	int len = sizeof(input) / sizeof(input[0]);
	int r[len];
	for (int i = 0; i < len; i++){
		r[i] = json_read_object(input[i], json_attrs, NULL);
	}

	for (int i = 0; i < len; i++){
		printf("%d ", r[i]);
	}
	printf("\n");
}
Edited by Rene Kita

Merge request reports