156 lines
3.9 KiB
C
156 lines
3.9 KiB
C
/**
|
|
rjp
|
|
Copyright (C) 2018 rexy712
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef RJP_H
|
|
#define RJP_H
|
|
|
|
#include <stddef.h> //size_t
|
|
|
|
#ifdef __cplusplus
|
|
extern "C"{
|
|
#endif
|
|
|
|
//type of data
|
|
typedef enum RJP_type{
|
|
json_object = 0,
|
|
json_string,
|
|
json_integer,
|
|
json_dfloat,
|
|
json_boolean,
|
|
json_array,
|
|
json_null
|
|
}RJP_type;
|
|
|
|
//keep track of string length
|
|
typedef struct RJP_string{
|
|
char* value;
|
|
size_t length;
|
|
}RJP_string;
|
|
|
|
//keep a linked list of all members of a given object
|
|
struct RJP_object_member;
|
|
typedef struct RJP_object{
|
|
struct RJP_object_member* members;
|
|
struct RJP_object_member* last;
|
|
size_t num_members;
|
|
}RJP_object;
|
|
|
|
//keep array plus length
|
|
struct RJP_array_element;
|
|
typedef struct RJP_array{
|
|
struct RJP_array_element* elements;
|
|
struct RJP_array_element* last;
|
|
size_t num_elements;
|
|
}RJP_array;
|
|
|
|
//hold any json data type
|
|
typedef struct RJP_value{
|
|
union{
|
|
long integer;
|
|
double dfloat;
|
|
char boolean;
|
|
struct RJP_object object;
|
|
struct RJP_string string;
|
|
struct RJP_array array;
|
|
};
|
|
struct RJP_value* parent;
|
|
enum RJP_type type;
|
|
}RJP_value;
|
|
|
|
typedef struct RJP_array_element{
|
|
struct RJP_value value;
|
|
struct RJP_array_element* next;
|
|
}RJP_array_element;
|
|
|
|
//A member of an object
|
|
typedef struct RJP_object_member{
|
|
struct RJP_value value;
|
|
struct RJP_string name;
|
|
struct RJP_object_member* next;
|
|
}RJP_object_member;
|
|
|
|
//read in json and convert to easy to work with format
|
|
RJP_value* rjp_parse(const char* str);
|
|
|
|
//deallocate a json handle
|
|
void rjp_free_json(char* json);
|
|
void rjp_free_value(RJP_value* root);
|
|
|
|
//initialize an empty json handle
|
|
RJP_value* rjp_init_json(void);
|
|
|
|
void* rjp_alloc(size_t nbytes);
|
|
void* rjp_calloc(size_t num, size_t nbytes);
|
|
void rjp_free(void* dest);
|
|
|
|
//add a member to a json object
|
|
RJP_value* rjp_add_member(RJP_value* dest, const char* key, size_t keylen, RJP_value value);
|
|
RJP_value* rjp_add_member_no_alloc(RJP_value* dest, char* key, size_t keylen, RJP_value value);
|
|
//add an element to a json array
|
|
RJP_value* rjp_add_element(RJP_value* dest, RJP_value value);
|
|
|
|
//set existing value
|
|
void rjp_set_value(RJP_value* dest, RJP_value value);
|
|
|
|
//initialize a RJP_value to the requested type and value
|
|
RJP_value rjp_integer(long i);
|
|
RJP_value rjp_boolean(char b);
|
|
RJP_value rjp_dfloat(double d);
|
|
RJP_value rjp_string(char* c, size_t len);
|
|
RJP_value rjp_null(void);
|
|
RJP_value rjp_object(void);
|
|
RJP_value rjp_array(void);
|
|
|
|
//access members/elements
|
|
RJP_value* rjp_get_member(RJP_value* object);
|
|
RJP_value* rjp_next_member(RJP_value* member);
|
|
RJP_value* rjp_get_element(RJP_value* array);
|
|
RJP_value* rjp_next_element(RJP_value* element);
|
|
|
|
//member metadata
|
|
char* rjp_member_name(RJP_value* member);
|
|
size_t rjp_member_name_length(RJP_value* member);
|
|
|
|
//value metadata
|
|
RJP_value* rjp_value_parent(RJP_value* element);
|
|
RJP_type rjp_value_type(RJP_value* value);
|
|
|
|
//value accessors
|
|
double rjp_value_dfloat(RJP_value* value);
|
|
int rjp_value_integer(RJP_value* value);
|
|
char rjp_value_boolean(RJP_value* value);
|
|
char* rjp_value_string(RJP_value* value);
|
|
size_t rjp_value_string_length(RJP_value* value);
|
|
|
|
|
|
//copy input string and add json escape sequences
|
|
size_t rjp_escape_strcpy(char* dest, const char* src);
|
|
|
|
//length of string including escape sequences
|
|
size_t rjp_escape_strlen(const char* str);
|
|
|
|
size_t rjp_dump_object(RJP_value* root, char* dest);
|
|
size_t rjp_dump_array(RJP_value* arr, char* dest);
|
|
char* rjp_to_json(RJP_value* root);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif
|