Skip to content

Commit

Permalink
lib: add grass JSON API (OSGeo#4801)
Browse files Browse the repository at this point in the history
  • Loading branch information
NishantBansal2003 authored and nilason committed Jan 16, 2025
1 parent 3807720 commit f17cce3
Show file tree
Hide file tree
Showing 10 changed files with 616 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ SUBDIRS = \
lidar \
raster3d \
raster3d/test \
external/parson/test \
gpde \
dspf \
symbol \
Expand Down
2 changes: 1 addition & 1 deletion lib/external/parson/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ include $(MODULE_TOPDIR)/include/Make/Lib.make
default: headers
$(MAKE) lib

headers: $(ARCH_INCDIR)/parson.h
headers: $(ARCH_INCDIR)/parson.h $(ARCH_INCDIR)/gjson.h

$(ARCH_INCDIR)/%.h: %.h
$(INSTALL_DATA) $< $@
155 changes: 155 additions & 0 deletions lib/external/parson/gjson.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*****************************************************************************
*
* MODULE: GRASS json output interface
*
* AUTHOR: Nishant Bansal ([email protected])
*
* PURPOSE: parson library function wrapper
* part of the gjson library
*
* COPYRIGHT: (C) 2024 by the GRASS Development Team
*
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
* for details.
*
*****************************************************************************/

#include "gjson.h"

/* *************************************************************** */
/* ***** WRAPPER FOR PARSON FUNCTIONS USED IN GRASS ************** */
/* *************************************************************** */

JSON_Value *G_json_value_init_object(void)
{
return json_value_init_object();
}

JSON_Value *G_json_value_init_array(void)
{
return json_value_init_array();
}

JSON_Object *G_json_value_get_object(const JSON_Value *value)
{
return json_value_get_object(value);
}

JSON_Object *G_json_object(const JSON_Value *value)
{
return json_object(value);
}
JSON_Object *G_json_object_get_object(const JSON_Object *object,
const char *name)
{
return json_object_get_object(object, name);
}
JSON_Array *G_json_object_get_array(const JSON_Object *object, const char *name)
{
return json_object_get_array(object, name);
}
JSON_Value *G_json_object_get_value(const JSON_Object *object, const char *name)
{
return json_object_get_value(object, name);
}
const char *G_json_object_get_string(const JSON_Object *object,
const char *name)
{
return json_object_get_string(object, name);
}
double G_json_object_get_number(const JSON_Object *object, const char *name)
{
return json_object_get_number(object, name);
}
int G_json_object_get_boolean(const JSON_Object *object, const char *name)
{
return json_object_get_boolean(object, name);
}
JSON_Value *G_json_object_get_wrapping_value(const JSON_Object *object)
{
return json_object_get_wrapping_value(object);
}
JSON_Status G_json_object_set_value(JSON_Object *object, const char *name,
JSON_Value *value)
{
return json_object_set_value(object, name, value);
}
JSON_Status G_json_object_set_string(JSON_Object *object, const char *name,
const char *string)
{
return json_object_set_string(object, name, string);
}
JSON_Status G_json_object_set_number(JSON_Object *object, const char *name,
double number)
{
return json_object_set_number(object, name, number);
}
JSON_Status G_json_object_set_boolean(JSON_Object *object, const char *name,
int boolean)
{
return json_object_set_boolean(object, name, boolean);
}
JSON_Status G_json_object_set_null(JSON_Object *object, const char *name)
{
return json_object_set_null(object, name);
}
JSON_Array *G_json_array(const JSON_Value *value)
{
return json_array(value);
}
JSON_Value *G_json_array_get_value(const JSON_Array *array, size_t index)
{
return json_array_get_value(array, index);
}
const char *G_json_array_get_string(const JSON_Array *array, size_t index)
{
return json_array_get_string(array, index);
}
double G_json_array_get_number(const JSON_Array *array, size_t index)
{
return json_array_get_number(array, index);
}
int G_json_array_get_boolean(const JSON_Array *array, size_t index)
{
return json_array_get_boolean(array, index);
}

JSON_Status G_json_array_append_value(JSON_Array *array, JSON_Value *value)
{
return json_array_append_value(array, value);
}

JSON_Status G_json_array_append_string(JSON_Array *array, const char *string)
{
return json_array_append_string(array, string);
}

JSON_Status G_json_array_append_number(JSON_Array *array, double number)
{
return json_array_append_number(array, number);
}

JSON_Status G_json_array_append_boolean(JSON_Array *array, int boolean)
{
return json_array_append_boolean(array, boolean);
}

JSON_Status G_json_array_append_null(JSON_Array *array)
{
return json_array_append_null(array);
}

char *G_json_serialize_to_string_pretty(const JSON_Value *value)
{
return json_serialize_to_string_pretty(value);
}

void G_json_free_serialized_string(char *string)
{
json_free_serialized_string(string);
}
void G_json_value_free(JSON_Value *value)
{
json_value_free(value);
}
48 changes: 48 additions & 0 deletions lib/external/parson/gjson.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef GRASS_GJSON_H
#define GRASS_GJSON_H

#include "parson.h"

/* *************************************************************** */
/* ***** WRAPPER FOR PARSON FUNCTIONS USED IN GRASS ************** */
/* *************************************************************** */

extern JSON_Value *G_json_value_init_object(void);
extern JSON_Value *G_json_value_init_array(void);

extern JSON_Object *G_json_value_get_object(const JSON_Value *);
extern JSON_Object *G_json_object(const JSON_Value *);
extern JSON_Object *G_json_object_get_object(const JSON_Object *, const char *);
extern JSON_Array *G_json_object_get_array(const JSON_Object *, const char *);
extern JSON_Value *G_json_object_get_value(const JSON_Object *, const char *);
extern const char *G_json_object_get_string(const JSON_Object *, const char *);
extern double G_json_object_get_number(const JSON_Object *, const char *);
extern int G_json_object_get_boolean(const JSON_Object *, const char *);
extern JSON_Value *G_json_object_get_wrapping_value(const JSON_Object *);

extern JSON_Status G_json_object_set_value(JSON_Object *, const char *,
JSON_Value *);
extern JSON_Status G_json_object_set_string(JSON_Object *, const char *,
const char *);
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_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);
extern JSON_Status G_json_array_append_boolean(JSON_Array *, int);
extern JSON_Status G_json_array_append_null(JSON_Array *);

extern char *G_json_serialize_to_string_pretty(const JSON_Value *);
extern void G_json_free_serialized_string(char *);
extern void G_json_value_free(JSON_Value *);

#endif /* GRASS_GJSON_H */
10 changes: 10 additions & 0 deletions lib/external/parson/test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
MODULE_TOPDIR = ../../../..

PGM=test.gjson.lib

LIBES = $(PARSONLIB) $(GISLIB)
DEPENDENCIES = $(PARSONDEP) $(GISDEP)

include $(MODULE_TOPDIR)/include/Make/Module.make

default: cmd
9 changes: 9 additions & 0 deletions lib/external/parson/test/test.gjson.lib.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h2>DESCRIPTION</h2>

<em>test.gjson.lib</em>
is a module dedicated for testing the gjson library.
This module is used by the testing framework to perform library tests.

<h2>AUTHOR</h2>

Nishant Bansal
31 changes: 31 additions & 0 deletions lib/external/parson/test/test_gjson_lib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*****************************************************************************
*
* MODULE: GRASS gjson Library
*
* AUTHOR: Nishant Bansal ([email protected])
*
* PURPOSE: Unit tests
*
* COPYRIGHT: (C) 2024 by the GRASS Development Team
*
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
* for details.
*
*****************************************************************************/

#ifndef _TEST_GJSON_LIB_H_
#define _TEST_GJSON_LIB_H_

#include <grass/gjson.h>

#define TEST_OBJECT_KEY "key"
#define TEST_OBJECT_VALUE "value"
#define TEST_ARRAY_STRING "array"
#define TEST_NUMBER 123.45
#define TEST_BOOLEAN 1

/* parson wrapper tests */
int unit_test_parson_wrapper(void);

#endif
78 changes: 78 additions & 0 deletions lib/external/parson/test/test_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/****************************************************************************
*
* MODULE: test.gjson.lib
*
* AUTHOR: Nishant Bansal ([email protected])
*
* PURPOSE: Unit tests for the gjson library
*
* COPYRIGHT: (C) 2024 by the GRASS Development Team
*
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with
* GRASS for details.
*
*****************************************************************************/

#include <stdlib.h>
#include <grass/gis.h>
#include <grass/glocale.h>
#include <grass/gjson.h>
#include "test_gjson_lib.h"

/*- Parameters and global variables -----------------------------------------*/
typedef struct {
struct Flag *testunit;
} paramType;

paramType param; /*Parameters */

/*- prototypes --------------------------------------------------------------*/
static void set_params(void); /*Fill the paramType structure */

/* ************************************************************************* */
/* Set up the arguments we are expecting ********************************** */

/* ************************************************************************* */
void set_params(void)
{
param.testunit = G_define_flag();
param.testunit->key = 'u';
param.testunit->description = "Run all unit tests";
}
/* ************************************************************************* */
/* ************************************************************************* */

/* ************************************************************************* */
int main(int argc, char *argv[])
{
struct GModule *module;
int returnstat = 0;

/* Initialize GRASS */
G_gisinit(argv[0]);

module = G_define_module();
G_add_keyword(_("gjson"));
G_add_keyword(_("unit test"));
module->description = _("Performs unit tests "
"for the gjson library");

/* Get parameters from user */
set_params();

if (G_parser(argc, argv))
exit(EXIT_FAILURE);

/*Run the unit tests */
if (param.testunit->answer) {
returnstat += unit_test_parson_wrapper();
}

if (returnstat != 0)
G_warning("Errors detected while testing the gjson lib");
else
G_message("\n-- gjson lib tests finished successfully --");

return (returnstat);
}
Loading

0 comments on commit f17cce3

Please sign in to comment.