Simple JSON  0.1
A free simple json library for C
simple_json.h File Reference
Include dependency graph for simple_json.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void sj_free (SJson *sjs)
 frees a previously loaded json struct More...
 
SJsonsj_copy (SJson *json)
 make a duplicate of a json structure. More...
 
SJsonsj_load (const char *filename)
 loads and parses a json file More...
 
void sj_save (SJson *json, char *filename)
 write a json value as a formatted json string to file More...
 
SJsonsj_new_str (char *str)
 make a new json value that is a string More...
 
const char * sj_get_string_value (SJson *json)
 get the JSON value as a string More...
 
int sj_get_integer_value (SJson *json, int *i)
 get the json value as an integer Can be used to check for existence More...
 
int sj_get_float_value (SJson *json, float *f)
 get the json value as a float Can be used to check for existence More...
 
int sj_get_bool_value (SJson *json, short int *b)
 get the json value as a bool Can be used to check for existence More...
 
SJsonsj_new_int (int i)
 make a new json value that is an integer More...
 
SJsonsj_new_float (float f)
 make a new json value that is a float More...
 
SJsonsj_new_bool (int b)
 make a new json value that is a bool More...
 
SJsonsj_null_new ()
 make a new json value that is NULL More...
 
SJsonsj_object_new ()
 allocate a new empty json object More...
 
void sj_object_insert (SJson *object, char *key, SJson *value)
 insert data into a json object More...
 
SJsonsj_object_get_value (SJson *object, char *key)
 get the json value from an object given a key More...
 
SJsonsj_array_new ()
 allocate a new empty json array More...
 
void sj_array_append (SJson *array, SJson *value)
 append to a JSON array a new value More...
 
int sj_array_get_count (SJson *array)
 get the number of elements in the json array More...
 
SJsonsj_array_get_nth (SJson *array, int n)
 retrieve the nth element in the json array More...
 
void sj_echo (SJson *json)
 print the contents of the json file to stdout More...
 
int sj_is_array (SJson *json)
 check if the json is an array More...
 
int sj_is_object (SJson *json)
 check if the json is an object More...
 
int sj_is_string (SJson *json)
 check if the json is a string More...
 
int sj_is_number (SJson *json)
 check if the json is a number More...
 
int sj_is_bool (SJson *json)
 check if the json is a bool More...
 
int sj_is_null (SJson *json)
 check if the json is NULL More...
 

Function Documentation

void sj_array_append ( SJson array,
SJson value 
)

append to a JSON array a new value

Parameters
arraythe JSON array to append to
valuethe value to add to the string
Note
: this is a no-op if array is not an actual JSON array

Definition at line 72 of file simple_json_array.c.

int sj_array_get_count ( SJson array)

get the number of elements in the json array

Parameters
arraythe json array
Returns
0 if there is an error, the count otherwise

Definition at line 66 of file simple_json_array.c.

SJson* sj_array_get_nth ( SJson array,
int  n 
)

retrieve the nth element in the json array

Parameters
arraythe array to search through
nthe index of the element to get
Returns
NULL on error (check sj_get_error()) or the SJson value otherwise

Definition at line 79 of file simple_json_array.c.

SJson* sj_array_new ( )

allocate a new empty json array

Returns
NULL on memory allocation error, an empty SJson array otherwise

Definition at line 38 of file simple_json_array.c.

SJson* sj_copy ( SJson json)

make a duplicate of a json structure.

Note
the duplicate needs to be sj_free()'d separately
Parameters
jsonthe json to be duplicated
Returns
NULL on error, or a Duplicate json

Definition at line 109 of file simple_json.c.

void sj_echo ( SJson json)

print the contents of the json file to stdout

Parameters
jsonthe json struct to print

Definition at line 140 of file simple_json.c.

void sj_free ( SJson sjs)

frees a previously loaded json struct

Definition at line 116 of file simple_json.c.

int sj_get_bool_value ( SJson json,
short int *  b 
)

get the json value as a bool Can be used to check for existence

Parameters
jsonthe json value to get from
b[optional] place to write the output to
Returns
0 if the json did not contain a bool or if there was an error, 1 otherwise

Definition at line 202 of file simple_json.c.

int sj_get_float_value ( SJson json,
float *  f 
)

get the json value as a float Can be used to check for existence

Parameters
jsonthe json value to get from
f[optional] place to write the output to
Returns
0 if the json did not contain a float or if there was an error, 1 otherwise

Definition at line 196 of file simple_json.c.

int sj_get_integer_value ( SJson json,
int *  i 
)

get the json value as an integer Can be used to check for existence

Parameters
jsonthe json value to get from
i[optional] place to write the output to
Returns
0 if the json did not contain an integer or if there was an error, 1 otherwise

Definition at line 190 of file simple_json.c.

const char* sj_get_string_value ( SJson json)

get the JSON value as a string

Parameters
jsonthe json value to get the string from
Returns
NULL if the provided json was not a string or on error, the string otherwise
Note
: the returned value should not be altered or freed

Definition at line 184 of file simple_json.c.

int sj_is_array ( SJson json)

check if the json is an array

Parameters
jsonthe json to check
Returns
1 if it is, 0 if not

Definition at line 208 of file simple_json.c.

int sj_is_bool ( SJson json)

check if the json is a bool

Parameters
jsonthe json to check
Returns
1 if it is, 0 if not

Definition at line 236 of file simple_json.c.

int sj_is_null ( SJson json)

check if the json is NULL

Parameters
jsonthe json to check
Returns
1 if it is, 0 if not

Definition at line 243 of file simple_json.c.

int sj_is_number ( SJson json)

check if the json is a number

Parameters
jsonthe json to check
Returns
1 if it is, 0 if not

Definition at line 229 of file simple_json.c.

int sj_is_object ( SJson json)

check if the json is an object

Parameters
jsonthe json to check
Returns
1 if it is, 0 if not

Definition at line 215 of file simple_json.c.

int sj_is_string ( SJson json)

check if the json is a string

Parameters
jsonthe json to check
Returns
1 if it is, 0 if not

Definition at line 222 of file simple_json.c.

SJson* sj_load ( const char *  filename)

loads and parses a json file

Parameters
filenamethe file to parse
Returns
NULL on error or an SJS pointer

Definition at line 47 of file simple_json.c.

SJson* sj_new_bool ( int  b)

make a new json value that is a bool

Parameters
bthe boolean value (1 is true, 0 is false)
Returns
NULL on error or a new json that holds a bool value

Definition at line 24 of file simple_json.c.

SJson* sj_new_float ( float  f)

make a new json value that is a float

Parameters
fthe float value
Returns
NULL on error or a new json that holds a floating point value

Definition at line 19 of file simple_json.c.

SJson* sj_new_int ( int  i)

make a new json value that is an integer

Parameters
ithe integer value
Returns
NULL on error or a new json that holds an integer value

Definition at line 14 of file simple_json.c.

SJson* sj_new_str ( char *  str)

make a new json value that is a string

Parameters
strthe string to base the json value on
Returns
NULL on error or a new json that holds a string value

Definition at line 9 of file simple_json.c.

SJson* sj_null_new ( )

make a new json value that is NULL

Returns
NULL on error (Ironically) or a new json that holds a NULL value

Definition at line 173 of file simple_json.c.

SJson* sj_object_get_value ( SJson object,
char *  key 
)

get the json value from an object given a key

Parameters
objectthe hson object to get
keythe key to search by
Returns
NULL on error, or if no object provided or it is not of object or the key is not found, the value otherwise
Note
: the json returned is OWNED by the parent object do not free it.

Definition at line 116 of file simple_json_object.c.

void sj_object_insert ( SJson object,
char *  key,
SJson value 
)

insert data into a json object

Parameters
objectthe json object to insert into
keythe string to identify the object value with
valuethe value of the key
Note
: this is a no-op if object is not an actual JSON object

Definition at line 58 of file simple_json_object.c.

SJson* sj_object_new ( )

allocate a new empty json object

Returns
NULL on memory allocation error, an empty SJson object otherwise

Definition at line 87 of file simple_json_object.c.

void sj_save ( SJson json,
char *  filename 
)

write a json value as a formatted json string to file

Parameters
jsonthe struct to convert and write
filenamethe file to overwrite

Definition at line 123 of file simple_json.c.