135 lines
3.5 KiB
C
135 lines
3.5 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
|
|
enum JSON_type{
|
|
json_object,
|
|
json_string,
|
|
json_integer,
|
|
json_dfloat,
|
|
json_boolean,
|
|
json_array,
|
|
json_null
|
|
};
|
|
|
|
enum JSON_flags{
|
|
json_flag_string = 1, //in a string
|
|
json_flag_comment = 2, //in a comment block
|
|
json_flag_search_name = 4, //looking for key/value pair
|
|
json_flag_escape = 16 //escaped character in a string
|
|
};
|
|
|
|
//keep track of string length
|
|
struct JSON_string{
|
|
char* value;
|
|
size_t length;
|
|
};
|
|
|
|
//keep a linked list of all members of a given object
|
|
struct JSON_object{
|
|
struct JSON_object_member* members;
|
|
struct JSON_object_member* last;
|
|
size_t num_members;
|
|
};
|
|
|
|
//keep array plus length
|
|
struct JSON_array{
|
|
struct JSON_array_element* elements;
|
|
struct JSON_array_element* last;
|
|
size_t num_elements;
|
|
};
|
|
|
|
//hold any json data type
|
|
struct JSON_value{
|
|
union{
|
|
long integer;
|
|
double dfloat;
|
|
char boolean;
|
|
struct JSON_object object;
|
|
struct JSON_string string;
|
|
struct JSON_array array;
|
|
};
|
|
struct JSON_value* parent;
|
|
enum JSON_type type;
|
|
};
|
|
|
|
struct JSON_array_element{
|
|
struct JSON_value value;
|
|
struct JSON_array_element* next;
|
|
};
|
|
|
|
//A member of an object
|
|
struct JSON_object_member{
|
|
struct JSON_string name;
|
|
struct JSON_value value;
|
|
struct JSON_object_member* next;
|
|
};
|
|
|
|
//read in json and convert to easy to work with format
|
|
struct JSON_value* rjp_parse(const char* str);
|
|
|
|
//deallocate a json handle
|
|
void rjp_free_json(char* json);
|
|
void rjp_free(struct JSON_value* root);
|
|
|
|
struct JSON_object_member* rjp_get_members(struct JSON_object* j);
|
|
struct JSON_object_member* rjp_next_member(struct JSON_object_member* j);
|
|
struct JSON_object* rjp_member_parent(struct JSON_object_member* j);
|
|
struct JSON_value* rjp_get_member_value(struct JSON_object_member* j);
|
|
enum JSON_type rjp_get_member_type(struct JSON_object_member* j);
|
|
enum JSON_type rjp_get_value_type(struct JSON_value* j);
|
|
|
|
|
|
//initialize an empty json handle (outputting to string would result in '{}')
|
|
struct JSON_value* rjp_init_json(void);
|
|
|
|
//add a member to a json object
|
|
struct JSON_value* rjp_add_member(struct JSON_object* dest, int alloc_key, char* key, size_t keylen, struct JSON_value value);
|
|
//add an element to a json array
|
|
struct JSON_value* rjp_add_element(struct JSON_array* dest, struct JSON_value value);
|
|
|
|
struct JSON_value rjp_integer(long i);
|
|
struct JSON_value rjp_boolean(char b);
|
|
struct JSON_value rjp_dfloat(double d);
|
|
struct JSON_value rjp_string(char* c, size_t len);
|
|
struct JSON_value rjp_null(void);
|
|
struct JSON_value rjp_object(void);
|
|
struct JSON_value rjp_array(void);
|
|
|
|
size_t rjp_escape_strcpy(char* dest, const char* src);
|
|
size_t rjp_escape_strlen(const char* str);
|
|
|
|
size_t rjp_dump_object(struct JSON_value* root, char* dest);
|
|
size_t rjp_dump_array(struct JSON_value* arr, char* dest);
|
|
char* rjp_to_json(struct JSON_value* root);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif
|