Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ modules.order
Module.symvers
Mkfile.old
dkms.conf
sj_test
libs/libsj.*
output.json

5 changes: 3 additions & 2 deletions include/simple_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ SJson *sj_load(const char *filename);
* @brief write a json value as a formatted json string to file
* @param json the struct to convert and write
* @param filename the file to overwrite
* @param pretty bool to denote pretty output
*/
void sj_save(SJson *json,const char *filename);
void sj_save(SJson *json,const char *filename, int pretty);

/**
* @brief make a new json value that is a string
Expand Down Expand Up @@ -322,7 +323,7 @@ SJson *sj_array_nth(SJson *array,int n);
* @brief print the contents of the json file to stdout
* @param json the json struct to print
*/
void sj_echo(SJson *json);
void sj_echo(SJson *json, int pretty);

/**
* @brief check if the json is an array
Expand Down
2 changes: 1 addition & 1 deletion include/simple_json_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ SJString *sj_array_get_nth_as_string(SJson *array,int n);
* @param array the json array to convert
* @return a formatted json character array "[*,*,....]"
*/
SJString *sj_array_to_json_string(SJson *array);
SJString *sj_array_to_json_string(SJson *array, int pretty);

#endif
9 changes: 7 additions & 2 deletions include/simple_json_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ void sj_object_insert(SJson *object,const char *key,SJson *value);
* @param object the object to convert
* @return NULL on error, or a string encapsulated ith {}
*/
SJString *sj_object_to_json_string(SJson *object);

SJString *sj_object_to_json_string(SJson *object, int pretty);

/**
* @brief appends spaces for get_string(), nothing if 0
* @param object the string to append to
* @param int amount of tabs to append offset by 1
*/
void sj_pretty_append_spaces(SJString *object, int pretty);
#endif
6 changes: 3 additions & 3 deletions include/simple_json_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ typedef struct SJson_S
SJList *array; /**<an array or values or an array of pairs*/
SJString *string; /**<the string if this is a string type*/
}v; /**<union of possible values*/
SJString *(*get_string)(struct SJson_S *json); /**<pointer to the function to convert this into json string*/
SJString *(*get_string)(struct SJson_S *json, int pretty); /**<pointer to the function to convert this into json string*/
void (*json_free)(struct SJson_S *json); /**<pointer to the function to free this json*/
struct SJson_S *(*copy)(struct SJson_S *json); /**<pointer to the function to copy this json*/
}SJson;
Expand All @@ -51,13 +51,13 @@ SJson *sj_string_to_value(SJString *string);
* @param string the json string to conver
* @return NULL on error or the converted string
*/
SJString *sj_string_to_json_string(SJson *string);
SJString *sj_string_to_json_string(SJson *string, int pretty);

/**
* @brief convert the json value into a json string
* @param json the value to convert
* @return NULL on error or the json string
*/
SJString *sj_value_to_json_string(SJson *json);
SJString *sj_value_to_json_string(SJson *json, int pretty);

#endif
5 changes: 3 additions & 2 deletions libs/include/simple_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ SJson *sj_load(const char *filename);
* @brief write a json value as a formatted json string to file
* @param json the struct to convert and write
* @param filename the file to overwrite
* @param pretty bool to denote pretty output
*/
void sj_save(SJson *json,char *filename);
void sj_save(SJson *json,char *filename, int pretty);

/**
* @brief make a new json value that is a string
Expand Down Expand Up @@ -158,7 +159,7 @@ SJson *sj_array_get_nth(SJson *array,int n);
* @brief print the contents of the json file to stdout
* @param json the json struct to print
*/
void sj_echo(SJson *json);
void sj_echo(SJson *json, int pretty);

/**
* @brief check if the json is an array
Expand Down
36 changes: 35 additions & 1 deletion output.json
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
{"version":"0.1","name":"font list","fonts":[{"font":"fonts\/colony_wars.ttf","size":"8"},{"font":"fonts\/colony_wars.ttf","size":"16"},{"font":"fonts\/colony_wars.ttf","size":"24"},{"font":"fonts\/colony_wars.ttf","size":"32"},{"font":"fonts\/colony_wars.ttf","size":"40"}],"numbers":["0","0.7","1.4","2.1","2.8"],"NULL value":null}
{
"version":"0.1",
"name":"font list",
"fonts":[
{
"font":"fonts/colony_wars.ttf",
"size":"8"
},
{
"font":"fonts/colony_wars.ttf",
"size":"16"
},
{
"font":"fonts/colony_wars.ttf",
"size":"24"
},
{
"font":"fonts/colony_wars.ttf",
"size":"32"
},
{
"font":"fonts/colony_wars.ttf",
"size":"40"
}
],
"numbers":[
"0",
"0.7",
"1.4",
"2.1",
"2.8"
],
"NULL value":null

}
16 changes: 8 additions & 8 deletions src/simple_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ void sj_free(SJson *json)
if (json->json_free)json->json_free(json);
}

void sj_save(SJson *json,const char *filename)
void sj_save(SJson *json,const char *filename, int pretty)
{
FILE *file;
SJString *string;
if ((!json) || (!json->get_string))return;
string = json->get_string(json);
string = json->get_string(json,pretty);
if (!string)return;
file = fopen(filename,"w");
if (!file)
Expand All @@ -159,11 +159,11 @@ void sj_save(SJson *json,const char *filename)
fclose(file);
}

void sj_echo(SJson *json)
void sj_echo(SJson *json, int pretty)
{
SJString *output;
if ((!json) || (!json->get_string))return;
output = json->get_string(json);
output = json->get_string(json,pretty);
if (!output)return;
printf("%s\n",output->text);
sj_string_free(output);
Expand All @@ -175,16 +175,16 @@ void sj_null_free(SJson *json)
free(json);
}

SJString *sj_value_to_json_string(SJson *json)
SJString *sj_value_to_json_string(SJson *json, int pretty)
{
if (!json)return NULL;
if (!json->get_string)return NULL;
return json->get_string(json);
return json->get_string(json, pretty);
}

SJString *sj_null_to_json_string(SJson *json)
SJString *sj_null_to_json_string(SJson *json, int pretty)
{
return sj_string_new_text("null",1);
return sj_string_new_text("null",pretty);
}

SJson *sj_null_copy(SJson *json)
Expand Down
21 changes: 18 additions & 3 deletions src/simple_json_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,24 +137,39 @@ SJString *sj_array_get_nth_as_string(SJson *array,int n)
return item->v.string;
}

SJString *sj_array_to_json_string(SJson *array)
SJString *sj_array_to_json_string(SJson *array, int pretty)
{
SJString *string;
SJString *valuestring;
SJson *value;
int i, count;
if (!sj_array_check(array))return NULL;
pretty = pretty ? pretty + 1 : 0;
string = sj_string_new_text("[",0);
//for each
count = sj_list_get_count(array->v.array);
for (i = 0; i < count; i++)
{
value = sj_list_get_nth(array->v.array,i);
if (!value)continue;
valuestring = sj_value_to_json_string(value);

valuestring = sj_value_to_json_string(value,pretty);
if(value->sjtype != SJVT_Object && pretty)
{
sj_string_append(string,"\n");
}
sj_pretty_append_spaces(string,pretty);
sj_string_concat(string,valuestring);
sj_string_free(valuestring);
if (i +1 < count)sj_string_append(string,",");
if (i + 1 < count)
{
sj_string_append(string,",");
}
}
if(count > 0 && pretty)
{
sj_string_append(string,"\n");
sj_pretty_append_spaces(string,pretty - 1);
}
sj_string_append(string,"]");
return string;
Expand Down
41 changes: 37 additions & 4 deletions src/simple_json_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,27 +285,60 @@ const char *sj_object_get_value_as_string(SJson *object,const char *key)
return sj_string_get_text(value->v.string);
}

SJString *sj_object_to_json_string(SJson *object)
void sj_pretty_append_spaces(SJString *object, int pretty)
{
for(int i = 0; i < (pretty - 1); ++i)
{
sj_string_append(object, " ");
}
}


SJString *sj_object_to_json_string(SJson *object, int pretty)
{
SJString *string;
SJString *valuestring;
SJPair *pair;
int i, count;
string = sj_string_new_text("{",0);

string = sj_string_new_text("", 0);
if(pretty > 1)
{
sj_string_append(string,"\n");
sj_pretty_append_spaces(string,pretty);
}
pretty = pretty ? pretty + 1 : 0;
sj_string_append(string, "{");

//for each
count = sj_list_get_count(object->v.array);
for (i = 0; i < count; i++)
{
pair = sj_list_get_nth(object->v.array,i);
if (!pair)continue;
if(pretty)
{
sj_string_append(string,"\n");
sj_pretty_append_spaces(string,pretty);
}
sj_string_append(string,"\"");

sj_string_concat(string,pair->key);
sj_string_append(string,"\":");
valuestring = sj_value_to_json_string(pair->value);
valuestring = sj_value_to_json_string(pair->value,pretty);
sj_string_concat(string,valuestring);
sj_string_free(valuestring);
if (i +1 < count)sj_string_append(string,",");
if (i + 1 < count)
{
sj_string_append(string,",");
}
}
if(count > 0 && pretty)
{
sj_string_append(string,"\n");
sj_pretty_append_spaces(string,pretty - 1);
}

sj_string_append(string,"}");
return string;
}
Expand Down
22 changes: 19 additions & 3 deletions src/simple_json_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,19 +283,35 @@ SJson *sj_parse_buffer(char *string,unsigned long length)
{
SJson *json = NULL;
static jsParse parse;
char *firstBrace;
char *firstBracket;
if (!string)
{
sj_set_error("sj_parse_buffer: no string provided");
return NULL;
}
parse.buffer = string;
parse.position = strchr(string, '{');
if (parse.position == NULL)

firstBrace = strchr(string, '{');
firstBracket = strchr(string, '[');
if(firstBrace && firstBracket)
{
parse.position = MIN(firstBracket, firstBrace);
}
else if(firstBrace)
{
parse.position = firstBrace;
}
else if(firstBracket)
{
parse.position = firstBracket;
}
else
{
if (__SJ_DEBUG) sj_set_error("sj_parse_buffer: --=== invalid file ===--");
return NULL;
}
parse.end = &string[length -1];
json = sj_parse_object(&parse);
json = (*parse.position == '{') ? sj_parse_object(&parse): sj_parse_array(&parse);
return json;
}
2 changes: 1 addition & 1 deletion src/simple_json_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ void sj_string_append(SJString *string,const char *buffer)
string->size = size;
}

SJString *sj_string_to_json_string(SJson *string)
SJString *sj_string_to_json_string(SJson *string, int pretty)
{
SJString *json;
if (!string)return NULL;
Expand Down
8 changes: 4 additions & 4 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ int main(int argc, char *argv[])
{
printf("json failed to laod:\n%s\n",sj_get_error());
}
sj_echo(json);
sj_echo(json, 1);

printf("\nsaving json to file\n");
sj_save(json,"output.json");
sj_save(json,"output.json", 1);

printf("freeing json structure\n");
sj_free(json);
Expand All @@ -46,13 +46,13 @@ int main(int argc, char *argv[])


printf("json created:\n");
sj_echo(json);
sj_echo(json, 0);

printf("copying json\n");
sub = sj_copy(json);

printf("copied json:\n");
sj_echo(sub);
sj_echo(sub, 0);

sj_free(sub);

Expand Down