34 if( fseek(file, 0, SEEK_END) ){
53 file = fopen(filename,
"r");
66 printf(
"loaded file %s with a size of %li characters\n",filename,size);
68 buffer = (
char *)malloc(
sizeof(
char)*(size + 1));
72 sj_set_error(
"sj_load: failed to allocate character buffer for json file");
76 memset(buffer,0,
sizeof(
char)*(size+1));
78 if ((read = fread(buffer,
sizeof(
char), size, file)) != size)
80 printf(
"expected to read %li characters, but read %li instead\n",size,read);
84 printf(
"read %li characters of %li available",read,size);
86 printf(
"file contents:\n%s\n",buffer);
102 sj_set_error(
"sj_new: failed to allocate space for new json object");
105 memset(json,0,
sizeof(
SJson));
111 if (!json)
return NULL;
112 if (!json->
copy)
return NULL;
113 return json->
copy(json);
130 file = fopen(filename,
"w");
136 fputs(string->
text,file);
146 printf(
"%s\n",output->
text);
158 if (!json)
return NULL;
177 if (!json)
return NULL;
SJString * sj_string_new_float(float f)
SJson * sj_null_copy(SJson *json)
SJString * sj_value_to_json_string(SJson *json)
convert the json value into a json string
this is the abstract container structure for all json data This structure may be an object...
SJson * sj_new()
allocate a new empty json object
int sj_string_as_integer(SJString *string, int *output)
get the integer value if the string is a numer
int sj_string_as_float(SJString *string, float *output)
get the float value if the string is a numer
int sj_is_number(SJson *json)
check if the json is a number
int sj_get_bool_value(SJson *json, short int *b)
get the json value as a bool Can be used to check for existence
void sj_echo(SJson *json)
print the contents of the json file to stdout
SJString * sj_string_new_bool(int i)
void sj_null_free(SJson *json)
SJson * sj_new_float(float f)
make a new json value that is a float
SJson * sj_parse_buffer(char *string, unsigned long length)
given a string, create a JSON object from it
int sj_is_object(SJson *json)
check if the json is an object
SJson * sj_null_new()
make a new json value that is NULL
long get_file_Size(FILE *file)
int sj_is_array(SJson *json)
check if the json is an array
SJson * sj_load(const char *filename)
loads and parses a json file
void(* json_free)(struct SJson_S *json)
int sj_is_string(SJson *json)
check if the json is a string
SJson * sj_string_to_value(SJString *string)
make a jason value object out of a string
void sj_set_error(char *er)
used internally to se the error message
a basic structure that keeps track of a string and its length Automatically grows to accomodate longe...
SJString * sj_string_new_integer(int i)
int sj_get_float_value(SJson *json, float *f)
get the json value as a float Can be used to check for existence
int sj_string_as_bool(SJString *string, short int *output)
get the bool value if the string is a bool
SJson * sj_new_bool(int b)
make a new json value that is a bool
int sj_get_integer_value(SJson *json, int *i)
get the json value as an integer Can be used to check for existence
const char * sj_get_string_value(SJson *json)
get the JSON value as a string
SJson * sj_new_str(char *str)
make a new json value that is a string
SJson * sj_new_int(int i)
make a new json value that is an integer
void sj_string_free(SJString *string)
free an SJString
int sj_is_null(SJson *json)
check if the json is NULL
SJson * sj_copy(SJson *json)
make a duplicate of a json structure.
void sj_free(SJson *json)
frees a previously loaded json struct
struct SJson_S *(* copy)(struct SJson_S *json)
void sj_save(SJson *json, char *filename)
write a json value as a formatted json string to file
int sj_is_bool(SJson *json)
check if the json is a bool
SJString * sj_string_new_text(char *s)
make a new string with the characters from array s
SJString * sj_null_to_json_string(SJson *json)
SJString *(* get_string)(struct SJson_S *json)