#include "json.h"

/* Parses a json-c object from a file. */
void
json_object_init(json_object** self, const char* path, char* buffer, size_t size)
{
	file_read(path, (void*)buffer, size, "r");

	*self = json_tokener_parse(buffer);
}

/* Attempts to parse a s32 from a json object, given a key. */
void
json_object_s32_parse(json_object** self, const char* key, s32* value)
{
	json_object* object;

	json_object_object_get_ex(*self, key, &object);

	*value = json_object_get_int(object);
}

/* Attempts to parse a string from a json object, given a key. */
void
json_object_string_parse(json_object** self, const char* key, char* value, size_t size)
{
	json_object* object;

	json_object_object_get_ex(*self, key, &object);

	/* don't be retarded with this */
	memcpy(value, json_object_get_string(object), size);
}


/* Attempts to parse a f64 from a json object, given a key. */
void
json_object_f64_parse(json_object** self, const char* key, f64* value)
{
	json_object* object;
	
	json_object_object_get_ex(*self, key, &object);

	*value = json_object_get_double(object);
}
