#ifndef C8_JSON_H
#define C8_JSON_H

#include <stdbool.h>

/* Just enough JSON for the widgets' data sources: parse a document into a
 * tree and look things up in it. */

enum json_type {
	JSON_NULL,
	JSON_BOOL,
	JSON_NUMBER,
	JSON_STRING,
	JSON_ARRAY,
	JSON_OBJECT,
};

struct json {
	enum json_type type;
	char *key; // member name, inside an object
	bool boolean;
	double number;
	char *string;
	struct json *child, *next; // arrays and objects
};

struct json *json_parse(const char *text); // NULL on a syntax error
void json_free(struct json *json);
struct json *json_get(const struct json *object, const char *key);
struct json *json_at(const struct json *array, int index);
int json_length(const struct json *array);
/* Lookups that tolerate NULL and wrong types; numbers in strings count */
const char *json_str(const struct json *json, const char *fallback);
double json_num(const struct json *json, double fallback);
bool json_bool(const struct json *json);
/* Back to text (compact) */
char *json_dump(const struct json *json);

#endif
