223 lines
7.1 KiB
C
223 lines
7.1 KiB
C
/**
|
|
rjp
|
|
Copyright (C) 2018-2019 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
|
|
|
|
#if defined(__GNUC__) || defined(__clang__)
|
|
#define DEPRECATED(str) __attribute__((deprecated(str)))
|
|
#else
|
|
#define DEPRECATED(str)
|
|
#endif
|
|
|
|
#ifndef RJP_int
|
|
#include <stdint.h>
|
|
#define RJP_int int64_t
|
|
#endif
|
|
#ifndef RJP_float
|
|
#include <stdint.h>
|
|
#define RJP_float double
|
|
#endif
|
|
|
|
//used with rjp_to_json
|
|
#define RJP_FORMAT_NONE 0
|
|
#define RJP_FORMAT_PRETTY 1
|
|
|
|
//type of data
|
|
DEPRECATED("Use RJP_data_type instead")
|
|
typedef enum RJP_type{
|
|
json_object = 0,
|
|
json_string,
|
|
json_integer,
|
|
json_dfloat,
|
|
json_boolean,
|
|
json_array,
|
|
json_null
|
|
}RJP_type;
|
|
|
|
typedef enum RJP_data_type{
|
|
rjp_json_object = 0,
|
|
rjp_json_string,
|
|
rjp_json_integer,
|
|
rjp_json_dfloat,
|
|
rjp_json_boolean,
|
|
rjp_json_array,
|
|
rjp_json_null
|
|
}RJP_data_type;
|
|
|
|
|
|
//Hold a C string and the length excluding NULL terminator
|
|
typedef struct RJP_string{
|
|
char* value;
|
|
size_t length;
|
|
}RJP_string;
|
|
|
|
//Forward declarations
|
|
struct RJP_object_member;
|
|
struct RJP_array_element;
|
|
|
|
//Represents a json object
|
|
typedef struct RJP_object{
|
|
struct RJP_object_member* members; //linked list of members
|
|
struct RJP_object_member* last; //final member of linked list
|
|
size_t num_members;
|
|
}RJP_object;
|
|
|
|
//Represents a json array
|
|
typedef struct RJP_array{
|
|
struct RJP_array_element* elements; //linked list of elements
|
|
struct RJP_array_element* last; //final member of linked list
|
|
size_t num_elements;
|
|
}RJP_array;
|
|
|
|
//Represents json data
|
|
//hold any json data type
|
|
typedef struct RJP_value{
|
|
union{
|
|
RJP_int integer;
|
|
RJP_float dfloat;
|
|
char boolean;
|
|
struct RJP_object object;
|
|
struct RJP_string string;
|
|
struct RJP_array array;
|
|
};
|
|
struct RJP_value* parent; //pointer to parent (either an array or object or NULL)
|
|
enum RJP_data_type type; //flag to determine active member of union
|
|
}RJP_value;
|
|
|
|
//Result of an rjp search operation
|
|
typedef struct RJP_search_res{
|
|
RJP_value* value; //pointer to match
|
|
size_t searchindex; //index in the search list
|
|
size_t objindex; //index in the searched object
|
|
}RJP_search_res;
|
|
|
|
//Convert C string consisting of json data into RJP's format
|
|
RJP_value* rjp_parse(const char* str);
|
|
//RJP_value* rjp_parse_chunked(const char* str, RJP_value* prev_chunk);
|
|
|
|
//Initialize a root RJP_value to NULL
|
|
RJP_value* rjp_init_json(void);
|
|
//Initialize a root RJP_value to copy of value
|
|
RJP_value* rjp_init_json_as(RJP_value value);
|
|
//Free an RJP_value and all members/elements
|
|
void rjp_free_value(RJP_value* root);
|
|
//Deep copy RJP_value
|
|
void rjp_copy_value(RJP_value* dest, const RJP_value* src);
|
|
|
|
|
|
//Allocate heap space for rjp to use. Use if the memory is to be freed by rjp
|
|
void* rjp_alloc(size_t nbytes);
|
|
//Allocate heap space for rjp and initialize to 0.
|
|
void* rjp_calloc(size_t num, size_t nbytes);
|
|
//Resize heap allocated space for rjp
|
|
void* rjp_realloc(void* ptr, size_t nbytes);
|
|
//Free memory allocated by rjp_alloc or rjp_calloc
|
|
void rjp_free(void* dest);
|
|
|
|
//add a member to a json object, allocating a copy of the key
|
|
RJP_value* rjp_add_member(RJP_value* dest, const char* key, size_t keylen, RJP_value value);
|
|
//add a member to a json object without allocation. key must be allocated using rjp_alloc/rjp_calloc
|
|
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 object member's key
|
|
void rjp_set_key(RJP_value* dest, const char* key, size_t keylen);
|
|
//set existing object member's key without allocation. key must be allocated using rjp_alloc/rjp_calloc
|
|
void rjp_set_key_no_alloc(RJP_value* dest, char* key, size_t keylen);
|
|
|
|
//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(RJP_int i);
|
|
RJP_value rjp_boolean(char b);
|
|
RJP_value rjp_dfloat(RJP_float d);
|
|
RJP_value rjp_string(char* c, size_t len);
|
|
RJP_value rjp_string_copy(const char* c);
|
|
RJP_value rjp_null(void);
|
|
RJP_value rjp_object(void);
|
|
RJP_value rjp_array(void);
|
|
|
|
//Access first member of a json object
|
|
RJP_value* rjp_get_member(const RJP_value* object);
|
|
//Access next member of a json object given the previous member
|
|
RJP_value* rjp_next_member(const RJP_value* member);
|
|
//Return number of members in the object
|
|
size_t rjp_num_members(const RJP_value* object);
|
|
//Return the object member's key name
|
|
char* rjp_member_name(const RJP_value* member);
|
|
//Return the object member's key name length excluding null terminator
|
|
size_t rjp_member_name_length(const RJP_value* member);
|
|
//Search for an object member with given key
|
|
RJP_search_res rjp_search_member(const RJP_value* object, const char* search, size_t skip);
|
|
//Search for first occurance of multiple keys. Returns first occurance of each.
|
|
//Assumes dest is large enough to hold maximum num items.
|
|
size_t rjp_search_members(const RJP_value* object, size_t num, const char* const* searches, RJP_search_res* dest, size_t skip);
|
|
//Search for multiple occurances of multiple keys. Returns pointer to list of matches.
|
|
//Returned pointer must be freed using rjp_free
|
|
RJP_search_res* rjp_search_members_multi(const RJP_value* object, size_t num, const char* const* searches, size_t* rsize, size_t skip);
|
|
//Access first element of a json array
|
|
RJP_value* rjp_get_element(const RJP_value* array);
|
|
//Access next element of a json array given the previous element
|
|
RJP_value* rjp_next_element(const RJP_value* element);
|
|
//Return number of elements in the array
|
|
size_t rjp_num_elements(const RJP_value* array);
|
|
|
|
|
|
//Return handle to parent of given value
|
|
RJP_value* rjp_value_parent(const RJP_value* element);
|
|
//Return type of value
|
|
RJP_data_type rjp_value_type(const RJP_value* value);
|
|
|
|
//value accessors
|
|
RJP_float rjp_value_dfloat(const RJP_value* value);
|
|
RJP_int rjp_value_integer(const RJP_value* value);
|
|
char rjp_value_boolean(const RJP_value* value);
|
|
char* rjp_value_string(RJP_value* value);
|
|
const char* rjp_value_cstring(const RJP_value* value);
|
|
size_t rjp_value_string_length(const 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);
|
|
|
|
//Convert RJP_object to json string
|
|
size_t rjp_dump_object(const RJP_value* root, char* dest);
|
|
//Convert RJP_array to json string
|
|
size_t rjp_dump_array(const RJP_value* arr, char* dest);
|
|
//Convert root rjp value into json string
|
|
char* rjp_to_json(const RJP_value* root, int pretty);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
#undef DEPRECATED
|
|
|
|
#endif
|