Skip to content

JSON Functions

Chris Feger edited this page Jun 6, 2020 · 2 revisions
struct JSON { void* dummy };

This is a utility structure, used internally. You cannot do anything with it.

struct JSON* json_new();

Returns a new JSON structure for usage with the below functions.

  • Will be returned in the invalid state
  • Must be freed with json_delete
void json_parse(struct JSON* out, char* data);

Parses the string passed in, setting it to the JSON provided.

  • Overwrites the JSON that was originally in the structure
void json_delete(struct JSON* freed);

This is analogous to free(freed). It must be called for all JSON objects created by json_new.

int json_is_valid(struct JSON* check);

Returns 1 if the JSON is not in the invalid state.

int json_is_int(struct JSON* check);

Returns 1 if this JSON represents an integer, and zero otherwise.

int json_is_bool(struct JSON* check);

Returns 1 if this JSON represents a boolean, and zero otherwise.

int json_is_string(struct JSON* check);

Returns 1 if this JSON represents a string, and zero otherwise.

int json_is_array(struct JSON* check);

Returns 1 if this JSON represents an array, and zero otherwise.

int json_is_object(struct JSON* check);

Returns 1 if this JSON represents an object, and zero otherwise.

int json_get_int(struct JSON* get);

Retrieves an integer from this JSON.

  • Only safe for JSONs that are confirmed to represent an integer.
int json_get_bool(struct JSON* get);

Retrieves a boolean from this JSON, returned as 1 for true and 0 for false.

  • Only safe for JSONs that are confirmed to represent a boolean.
const char* json_get_string(struct JSON* get);

Retrieves a string from this JSON.

  • This string must be released from memory via free
  • Only safe for JSONs that are confirmed to represent a string.
int json_array_size(struct JSON* get);

Gets the array size of a JSON array.

  • Only safe for JSONs that are confirmed to represent an array
struct JSON* json_array_element(struct JSON* array, int index);

Returns a JSON representing the element at index, starting at zero.

  • This SHOULD NOT be freed by json_delete.
  • This WILL be invalid when the root JSON is freed by json_delete
  • Only safe for JSONs that are confirmed to represent an array.
int json_object_contains(struct JSON* get, const char* elemName);

Returns 1 if the JSON contains a value with the key elemName.

  • Only safe for JSONs that are confirmed to represent an object
struct JSON* json_object_element(struct JSON* object, const char* elemName);

Returns a JSON representing the value with the key elemName.

  • This SHOULD NOT be freed by json_delete.
  • This WILL be invalid when the root JSON is freed by json_delete
  • Only safe for JSONs that are confirmed to represent an object