Skip to content

Commit

Permalink
added parson wrapper-2
Browse files Browse the repository at this point in the history
Signed-off-by: Nishant Bansal <[email protected]>
  • Loading branch information
NishantBansal2003 committed Dec 4, 2024
1 parent 9666ed0 commit f87f1fc
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 137 deletions.
12 changes: 5 additions & 7 deletions include/grass/defs/gparson.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef GRASS_GPARSONDEFS_H
#define GRASS_GPARSONDEFS_H

#include <grass/parson.h>

/* *************************************************************** */
/* ***** WRAPPER FOR PARSON FUNCTIONS USED IN GRASS ************** */
/* *************************************************************** */
Expand All @@ -12,9 +14,6 @@ extern void G_json_free_serialized_string(char *);
/* JSON Value Creation and Freeing */
extern JSON_Value *G_json_value_init_object(void);
extern JSON_Value *G_json_value_init_array(void);
extern JSON_Value *G_json_value_init_string(const char *);
extern JSON_Value *G_json_value_init_number(double);
extern JSON_Value *G_json_value_init_boolean(int);
extern void G_json_value_free(JSON_Value *);

/* JSON Object Access Functions */
Expand All @@ -37,14 +36,13 @@ extern JSON_Status G_json_object_set_number(JSON_Object *, const char *,
double);
extern JSON_Status G_json_object_set_boolean(JSON_Object *, const char *, int);
extern JSON_Status G_json_object_set_null(JSON_Object *, const char *);
extern JSON_Status G_json_object_dotset_string(JSON_Object *, const char *,
const char *);
extern JSON_Status G_json_object_dotset_number(JSON_Object *, const char *,
double);

/* JSON Array Functions */
extern JSON_Array *G_json_array(const JSON_Value *);
extern JSON_Value *G_json_array_get_value(const JSON_Array *, size_t);
extern const char *G_json_array_get_string(const JSON_Array *, size_t);
extern double G_json_array_get_number(const JSON_Array *, size_t);
extern int G_json_array_get_boolean(const JSON_Array *, size_t);
extern JSON_Status G_json_array_append_value(JSON_Array *, JSON_Value *);
extern JSON_Status G_json_array_append_string(JSON_Array *, const char *);
extern JSON_Status G_json_array_append_number(JSON_Array *, double);
Expand Down
31 changes: 8 additions & 23 deletions lib/gparson/parson_grass_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,6 @@ JSON_Status G_json_object_set_null(JSON_Object *object, const char *name)
return json_object_set_null(object, name);
}

JSON_Status G_json_object_dotset_string(JSON_Object *object, const char *name,
const char *string)
{
return json_object_dotset_string(object, name, string);
}

JSON_Status G_json_object_dotset_number(JSON_Object *object, const char *name,
double number)
{
return json_object_dotset_number(object, name, number);
}

/* JSON Array Functions */
JSON_Status G_json_array_append_value(JSON_Array *array, JSON_Value *value)
{
Expand Down Expand Up @@ -165,25 +153,22 @@ JSON_Value *G_json_array_get_value(const JSON_Array *array, size_t index)
{
return json_array_get_value(array, index);
}

JSON_Value *G_json_value_init_array(void)
const char *G_json_array_get_string(const JSON_Array *array, size_t index)
{
return json_value_init_array();
return json_array_get_string(array, index);
}

JSON_Value *G_json_value_init_string(const char *string)
double G_json_array_get_number(const JSON_Array *array, size_t index)
{
return json_value_init_string(string);
return json_array_get_number(array, index);
}

JSON_Value *G_json_value_init_number(double number)
int G_json_array_get_boolean(const JSON_Array *array, size_t index)
{
return json_value_init_number(number);
return json_array_get_boolean(array, index);
}

JSON_Value *G_json_value_init_boolean(int boolean)
JSON_Value *G_json_value_init_array(void)
{
return json_value_init_boolean(boolean);
return json_value_init_array();
}

void G_json_value_free(JSON_Value *value)
Expand Down
194 changes: 87 additions & 107 deletions lib/gparson/test/test_parson_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ int test_gparson_wrapper()
{
int sum = 0;
JSON_Value *value = NULL;
JSON_Value *testValue = NULL;
JSON_Object *object = NULL;
JSON_Array *array = NULL;
char *serialized_string;

G_message("\t * testing json object initialization\n");
value = G_json_value_init_object();
Expand All @@ -71,35 +71,6 @@ int test_gparson_wrapper()
G_json_value_free(value);
}

// G_message("\t * testing json string initialization\n");
// value = G_json_value_init_string("test string");
// if (value == NULL ||
// strcmp(G_json_object_get_string(G_json_value_get_object(value),
// "test string"),
// "test string") != 0) {
// G_warning("G_json_value_init_string failed!");
// sum++;
// }
// G_json_value_free(value);

G_message("\t * testing json number initialization\n");
testValue = G_json_value_init_number(42.00);
if (testValue == NULL || json_value_get_type(testValue) != JSONNumber ||
json_value_get_number(testValue) != 42.00) {
G_warning("G_json_value_init_number failed!");
sum++;
}
G_json_value_free(testValue);

G_message("\t * testing json boolean initialization\n");
testValue = G_json_value_init_boolean(1);
if (testValue == NULL || json_value_get_type(testValue) != JSONBoolean ||
json_value_get_boolean(testValue) != 1) {
G_warning("G_json_value_init_boolean failed!");
sum++;
}
G_json_value_free(testValue);

G_message("\t * testing json object set and get functions\n");
value = G_json_value_init_object();
object = G_json_value_get_object(value);
Expand Down Expand Up @@ -174,87 +145,67 @@ int test_gparson_wrapper()
}
G_json_value_free(value);

// G_message("\t * testing json array append value\n");
// value = G_json_value_init_array();
// array = G_json_value_get_array(value);
// if (G_json_array_append_value(array, G_json_value_init_string("item")) !=
// JSONSuccess) {
// G_warning("G_json_array_append_value failed!");
// sum++;
// }
// if (strcmp(G_json_array_get_string(array, 0), "item") != 0) {
// G_warning("G_json_array_append_value or retrieval failed!");
// sum++;
// }
// G_json_value_free(value);

// G_message("\t * testing json array append number\n");
// value = G_json_value_init_array();
// array = G_json_value_get_array(value);
// if (G_json_array_append_number(array, 123.45) != JSONSuccess) {
// G_warning("G_json_array_append_number failed!");
// sum++;
// }
// if (G_json_array_get_number(array, 0) != 123.45) {
// G_warning("G_json_array_append_number or retrieval failed!");
// sum++;
// }
// G_json_value_free(value);
G_message("\t * testing json array append value\n");
value = G_json_value_init_array();
array = G_json_array(value);
if (G_json_array_append_value(array, G_json_value_init_object()) !=
JSONSuccess) {
G_warning("G_json_array_append_value failed!");
sum++;
}
G_json_value_free(value);

// G_message("\t * testing json array append boolean\n");
// value = G_json_value_init_array();
// array = G_json_value_get_array(value);
// if (G_json_array_append_boolean(array, 1) != JSONSuccess) {
// G_warning("G_json_array_append_boolean failed!");
// sum++;
// }
// if (G_json_array_get_boolean(array, 0) != 1) {
// G_warning("G_json_array_append_boolean or retrieval failed!");
// sum++;
// }
// G_json_value_free(value);
G_message("\t * testing json array append number\n");
value = G_json_value_init_array();
array = G_json_array(value);
if (G_json_array_append_number(array, 123.45) != JSONSuccess) {
G_warning("G_json_array_append_number failed!");
sum++;
}
if (G_json_array_get_number(array, 0) != 123.45) {
G_warning("G_json_array_append_number or retrieval failed!");
sum++;
}
G_json_value_free(value);

// G_message("\t * testing json array append null\n");
// value = G_json_value_init_array();
// array = G_json_value_get_array(value);
// if (G_json_array_append_null(array) != JSONSuccess) {
// G_warning("G_json_array_append_null failed!");
// sum++;
// }
// if (G_json_array_get_value(array, 0)->type != JSONNull) {
// G_warning("G_json_array_append_null or retrieval failed!");
// sum++;
// }
// G_json_value_free(value);
G_message("\t * testing json array append boolean\n");
value = G_json_value_init_array();
array = G_json_array(value);
if (G_json_array_append_boolean(array, 1) != JSONSuccess) {
G_warning("G_json_array_append_boolean failed!");
sum++;
}
if (G_json_array_get_boolean(array, 0) != 1) {
G_warning("G_json_array_append_boolean or retrieval failed!");
sum++;
}
G_json_value_free(value);

// G_message("\t * testing json object dotset number\n");
// value = G_json_value_init_object();
// object = G_json_value_get_object(value);
// if (G_json_object_dotset_number(object, "dot.number", 123.45) !=
// JSONSuccess) {
// G_warning("G_json_object_dotset_number failed!");
// sum++;
// }
// if (G_json_object_dotget_number(object, "dot.number") != 123.45) {
// G_warning("G_json_object_dotset_number or retrieval failed!");
// sum++;
// }
// G_json_value_free(value);
G_message("\t * testing json array append null\n");
value = G_json_value_init_array();
array = G_json_array(value);
if (G_json_array_append_null(array) != JSONSuccess) {
G_warning("G_json_array_append_null failed!");
sum++;
}
if (json_value_get_type(G_json_array_get_value(array, 0)) != JSONNull) {
G_warning("G_json_array_append_null or retrieval failed!");
sum++;
}
G_json_value_free(value);

// G_message("\t * testing json object dotset string\n");
// value = G_json_value_init_object();
// object = G_json_value_get_object(value);
// if (G_json_object_dotset_string(object, "nested.key", "nested_value") !=
// JSONSuccess) {
// G_warning("G_json_object_dotset_string failed!");
// sum++;
// }
// if (strcmp(G_json_object_get_string(object, "nested.key"),
// "nested_value") != 0) {
// G_warning("G_json_object_dotset_string or retrieval failed!");
// sum++;
// }
// G_json_value_free(value);
G_message("\t * testing json array append string\n");
value = G_json_value_init_array();
array = G_json_array(value);
if (G_json_array_append_string(array, "array_string") != JSONSuccess) {
G_warning("G_json_array_append_null failed!");
sum++;
}
if (strcmp(G_json_array_get_string(array, 0), "array_string") != 0) {
G_warning("G_json_array_append_null or retrieval failed!");
sum++;
}
G_json_value_free(value);

G_message("\t * testing json object set and get number functions\n");
value = G_json_value_init_object();
Expand All @@ -271,5 +222,34 @@ int test_gparson_wrapper()
}
G_json_value_free(value);

G_message("\t * testing json object set and get boolean functions\n");
value = G_json_value_init_object();
object = G_json_value_get_object(value);
if (G_json_object_set_boolean(object, "boolean_key", 1) != JSONSuccess) {
G_warning("G_json_object_set_boolean failed!");
sum++;
}
int booleanValue = G_json_object_get_boolean(object, "boolean_key");
if (booleanValue != 1) {
G_warning(
"G_json_object_set_boolean or G_json_object_get_boolean failed!");
sum++;
}
G_json_value_free(value);

G_message("\t * testing JSON serialization\n");
value = G_json_value_init_object();
object = G_json_value_get_object(value);
G_json_object_set_string(object, "key", "value");

serialized_string = G_json_serialize_to_string_pretty(value);
if (!serialized_string || strstr(serialized_string, "value") == NULL) {
G_warning("Error in JSON serialization");
sum++;
}

G_json_free_serialized_string(serialized_string);
G_json_value_free(value);

return sum;
}

0 comments on commit f87f1fc

Please sign in to comment.