Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dd3bfb49e9 | ||
|
|
af9b0b837e | ||
|
|
cd5ddd950b | ||
|
|
d68bea08f4 | ||
|
|
1445d668a5 | ||
|
|
6f4a55a766 | ||
|
|
b27031876b | ||
|
|
566952de5e |
@@ -4,7 +4,7 @@ include(GNUInstallDirs)
|
||||
cmake_minimum_required(VERSION 3.0.2)
|
||||
project(rjp)
|
||||
set(rjp_VERSION_MAJOR 0)
|
||||
set(rjp_VERSION_MINOR 2)
|
||||
set(rjp_VERSION_MINOR 4)
|
||||
set(INCLUDE_PATH ${CMAKE_SOURCE_DIR}/include)
|
||||
configure_file(
|
||||
"${INCLUDE_PATH}/config.h.in"
|
||||
@@ -12,8 +12,13 @@ configure_file(
|
||||
)
|
||||
include_directories("${INCLUDE_PATH}")
|
||||
|
||||
option(ENABLE_DIAGNOSTICS "Print diagnostic messages when parsing json to help identify issues" ON)
|
||||
|
||||
add_library (rjp STATIC src/input.c src/output.c src/strings.c)
|
||||
set_target_properties(rjp PROPERTIES PUBLIC_HEADER ${INCLUDE_PATH}/rjp.h)
|
||||
if(ENABLE_DIAGNOSTICS)
|
||||
target_compile_definitions(rjp PRIVATE RJP_DIAGNOSTICS)
|
||||
endif()
|
||||
|
||||
install(TARGETS rjp
|
||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
|
||||
135
include/rjp.h
135
include/rjp.h
@@ -26,106 +26,123 @@ extern "C"{
|
||||
#endif
|
||||
|
||||
//type of data
|
||||
enum JSON_type{
|
||||
json_object,
|
||||
typedef enum RJP_type{
|
||||
json_object = 0,
|
||||
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
|
||||
};
|
||||
}RJP_type;
|
||||
|
||||
//keep track of string length
|
||||
struct JSON_string{
|
||||
typedef struct RJP_string{
|
||||
char* value;
|
||||
size_t length;
|
||||
};
|
||||
}RJP_string;
|
||||
|
||||
//keep a linked list of all members of a given object
|
||||
struct JSON_object{
|
||||
struct JSON_object_member* members;
|
||||
struct JSON_object_member* last;
|
||||
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 JSON_array{
|
||||
struct JSON_array_element* elements;
|
||||
struct JSON_array_element* last;
|
||||
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
|
||||
struct JSON_value{
|
||||
typedef struct RJP_value{
|
||||
union{
|
||||
long integer;
|
||||
double dfloat;
|
||||
char boolean;
|
||||
struct JSON_object object;
|
||||
struct JSON_string string;
|
||||
struct JSON_array array;
|
||||
struct RJP_object object;
|
||||
struct RJP_string string;
|
||||
struct RJP_array array;
|
||||
};
|
||||
struct JSON_value* parent;
|
||||
enum JSON_type type;
|
||||
};
|
||||
struct RJP_value* parent;
|
||||
enum RJP_type type;
|
||||
}RJP_value;
|
||||
|
||||
struct JSON_array_element{
|
||||
struct JSON_value value;
|
||||
struct JSON_array_element* next;
|
||||
};
|
||||
typedef struct RJP_array_element{
|
||||
struct RJP_value value;
|
||||
struct RJP_array_element* next;
|
||||
}RJP_array_element;
|
||||
|
||||
//A member of an object
|
||||
struct JSON_object_member{
|
||||
struct JSON_string name;
|
||||
struct JSON_value value;
|
||||
struct JSON_object_member* next;
|
||||
};
|
||||
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
|
||||
struct JSON_value* rjp_parse(const char* str);
|
||||
RJP_value* rjp_parse(const char* str);
|
||||
|
||||
//deallocate a json handle
|
||||
void rjp_free_json(char* json);
|
||||
void rjp_free(struct JSON_value* root);
|
||||
void rjp_free_value(RJP_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
|
||||
RJP_value* rjp_init_json(void);
|
||||
|
||||
|
||||
//initialize an empty json handle (outputting to string would result in '{}')
|
||||
struct JSON_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
|
||||
struct JSON_value* rjp_add_member(struct JSON_value* dest, int alloc_key, char* key, size_t keylen, struct JSON_value value);
|
||||
RJP_value* rjp_add_member(RJP_value* dest, int alloc_key, char* key, size_t keylen, RJP_value value);
|
||||
//add an element to a json array
|
||||
struct JSON_value* rjp_add_element(struct JSON_value* dest, struct JSON_value value);
|
||||
RJP_value* rjp_add_element(RJP_value* dest, RJP_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);
|
||||
//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(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);
|
||||
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
|
||||
}
|
||||
|
||||
@@ -3,11 +3,12 @@
|
||||
|
||||
#include "rjp.h"
|
||||
|
||||
char* _rjp__parse_string(struct JSON_value* root, const char* str, int* len, int* row, int* column);
|
||||
size_t _rjp__object_strlen(struct JSON_value* root);
|
||||
void _rjp__add_member(struct JSON_object* j, char* str, size_t len);
|
||||
void _rjp__add_member_no_alloc(struct JSON_object* j, char* str, size_t len);
|
||||
void _rjp__add_element(struct JSON_array* j);
|
||||
size_t _rjp__object_strlen(struct JSON_value* root);
|
||||
char* _rjp__parse_string(RJP_value* root, const char* str, int* len, int* row, int* column);
|
||||
size_t _rjp__object_strlen(RJP_value* root);
|
||||
void _rjp__add_member(RJP_object* j, char* str, size_t len);
|
||||
void _rjp__add_member_no_alloc(RJP_object* j, char* str, size_t len);
|
||||
void _rjp__add_element(RJP_array* j);
|
||||
size_t _rjp__object_strlen(RJP_value* root);
|
||||
size_t _rjp__value_strlen(RJP_value* root);
|
||||
|
||||
#endif
|
||||
|
||||
253
src/input.c
253
src/input.c
@@ -31,135 +31,132 @@
|
||||
#define MAYBE_UNUSED
|
||||
#endif
|
||||
|
||||
struct JSON_state{
|
||||
#ifdef RJP_DIAGNOSTICS
|
||||
#define DIAG_PRINT(...) fprintf(__VA_ARGS__)
|
||||
#else
|
||||
#define DIAG_PRINT(...)
|
||||
#endif
|
||||
|
||||
typedef struct RJP_state{
|
||||
int in_array;
|
||||
int search_target;
|
||||
};
|
||||
}RJP_state;
|
||||
|
||||
//types of searches in the text
|
||||
enum json_search_target{
|
||||
typedef enum json_search_target{
|
||||
json_key,
|
||||
json_colon,
|
||||
json_comma,
|
||||
json_value
|
||||
};
|
||||
json_value,
|
||||
json_none
|
||||
}json_search_target;
|
||||
|
||||
//Determine if the character is valid whitespace
|
||||
static int _rjp__is_whitespace(char c){
|
||||
return c == ' ' || c == '\n' || c == '\r' || c == '\t';
|
||||
}
|
||||
//add an element to an array
|
||||
void _rjp__add_element(struct JSON_array* j){
|
||||
void _rjp__add_element(RJP_array* j){
|
||||
++j->num_elements;
|
||||
if(!j->elements){
|
||||
j->elements = calloc(1, sizeof(struct JSON_array_element));
|
||||
j->elements = rjp_calloc(1, sizeof(RJP_array_element));
|
||||
j->last = j->elements;
|
||||
}else{
|
||||
j->last->next = calloc(1, sizeof(struct JSON_array_element));
|
||||
j->last->next = rjp_calloc(1, sizeof(RJP_array_element));
|
||||
j->last = j->last->next;
|
||||
}
|
||||
}
|
||||
//create member of the object as a linked list member and assign a name with name allocation
|
||||
void _rjp__add_member(struct JSON_object* j, char* str, size_t len){
|
||||
void _rjp__add_member(RJP_object* j, char* str, size_t len){
|
||||
++j->num_members;
|
||||
if(!j->members){
|
||||
j->members = calloc(1, sizeof(struct JSON_object_member));
|
||||
j->members = rjp_calloc(1, sizeof(RJP_object_member));
|
||||
j->last = j->members;
|
||||
}else{
|
||||
j->last->next = calloc(1, sizeof(struct JSON_object_member));
|
||||
j->last->next = rjp_calloc(1, sizeof(RJP_object_member));
|
||||
j->last = j->last->next;
|
||||
}
|
||||
j->last->name.value = malloc(len + 1);
|
||||
j->last->name.value = rjp_alloc(len + 1);
|
||||
strncpy(j->last->name.value, str, len);
|
||||
j->last->name.value[len] = 0;
|
||||
j->last->name.length = len;
|
||||
}
|
||||
void _rjp__add_member_no_alloc(struct JSON_object* j, char* str, size_t len){
|
||||
void _rjp__add_member_no_alloc(RJP_object* j, char* str, size_t len){
|
||||
++j->num_members;
|
||||
if(!j->members){
|
||||
j->members = calloc(1, sizeof(struct JSON_object_member));
|
||||
j->members = rjp_calloc(1, sizeof(RJP_object_member));
|
||||
j->last = j->members;
|
||||
}else{
|
||||
j->last->next = calloc(1, sizeof(struct JSON_object_member));
|
||||
j->last->next = rjp_calloc(1, sizeof(RJP_object_member));
|
||||
j->last = j->last->next;
|
||||
}
|
||||
j->last->name.value = str;
|
||||
j->last->name.length = len;
|
||||
}
|
||||
|
||||
static struct JSON_value* _rjp__add_element_to_array(struct JSON_value* curr, struct JSON_state* state, struct JSON_value* element){
|
||||
struct JSON_value* tmp = &curr->object.members->value;
|
||||
static RJP_value* _rjp__add_element_to_array(RJP_value* curr, RJP_state* state, RJP_value* element){
|
||||
RJP_value* tmp = &curr->object.members->value;
|
||||
for(int i = state->in_array-1;i > 0;--i, tmp = &tmp->array.last->value);
|
||||
_rjp__add_element(&tmp->array);
|
||||
tmp->array.last->value = *element;
|
||||
return &tmp->array.last->value;
|
||||
}
|
||||
|
||||
//Assign object characteristics to previously allocated JSON_value
|
||||
static struct JSON_value* _rjp__add_object(struct JSON_value* curr, struct JSON_state* state){
|
||||
static RJP_value* _rjp__add_value(RJP_value* curr, RJP_value* new_val, RJP_state* state){
|
||||
if(!curr){
|
||||
curr = calloc(1, sizeof(struct JSON_value));
|
||||
curr->type = json_object;
|
||||
curr = rjp_calloc(1, sizeof(RJP_value));
|
||||
*curr = *new_val;
|
||||
return curr;
|
||||
}
|
||||
struct JSON_value new_object = {.type = json_object, .integer = 0, .parent = curr};
|
||||
if(curr->type == json_array){
|
||||
_rjp__add_element(&curr->array);
|
||||
curr->array.last->value = *new_val;
|
||||
return &curr->array.last->value;
|
||||
}
|
||||
if(state->in_array)
|
||||
return _rjp__add_element_to_array(curr, state, &new_object);
|
||||
curr->object.last->value = new_object;
|
||||
return _rjp__add_element_to_array(curr, state, new_val);
|
||||
curr->object.last->value = *new_val;
|
||||
return &curr->object.last->value;
|
||||
}
|
||||
//Assign array characteristics to previously allocated JSON_value
|
||||
static struct JSON_value* _rjp__add_array(struct JSON_value* curr, struct JSON_state* state){
|
||||
struct JSON_value new_array = {.type = json_array, .array = {.num_elements = 0, .elements = NULL}, .parent = curr};
|
||||
if(state->in_array)
|
||||
return _rjp__add_element_to_array(curr, state, &new_array);
|
||||
curr->object.last->value = new_array;
|
||||
return &curr->object.last->value;
|
||||
//Assign object characteristics to previously allocated RJP_value
|
||||
static RJP_value* _rjp__add_object(RJP_value* curr, RJP_state* state){
|
||||
RJP_value new_object = {.type = json_object, .integer = 0, .parent = curr};
|
||||
return _rjp__add_value(curr, &new_object, state);
|
||||
}
|
||||
//Assign string characteristics to previously allocated JSON_value with no string allocation
|
||||
static struct JSON_value* _rjp__add_string_no_alloc(struct JSON_value* curr, char* str, int len, struct JSON_state* state){
|
||||
struct JSON_value new_string = {.type = json_string, .string = {.value = str, .length = len}, .parent = curr};
|
||||
if(state->in_array)
|
||||
return _rjp__add_element_to_array(curr, state, &new_string);
|
||||
curr->object.last->value = new_string;
|
||||
return &curr->object.last->value;
|
||||
//Assign array characteristics to previously allocated RJP_value
|
||||
static RJP_value* _rjp__add_array(RJP_value* curr, RJP_state* state){
|
||||
RJP_value new_array = {.type = json_array, .array = {.num_elements = 0, .elements = NULL}, .parent = curr};
|
||||
return _rjp__add_value(curr, &new_array, state);
|
||||
}
|
||||
//Assign double characteristics to previously allocated JSON_value
|
||||
static struct JSON_value* _rjp__add_dfloat(struct JSON_value* curr, double value, struct JSON_state* state){
|
||||
struct JSON_value new_double = {.type = json_dfloat, .dfloat = value, .parent = curr};
|
||||
if(state->in_array)
|
||||
return _rjp__add_element_to_array(curr, state, &new_double);
|
||||
curr->object.last->value = new_double;
|
||||
return &curr->object.last->value;
|
||||
//Assign string characteristics to previously allocated RJP_value with no string allocation
|
||||
static RJP_value* _rjp__add_string_no_alloc(RJP_value* curr, char* str, int len, RJP_state* state){
|
||||
RJP_value new_string = {.type = json_string, .string = {.value = str, .length = len}, .parent = curr};
|
||||
return _rjp__add_value(curr, &new_string, state);
|
||||
}
|
||||
//Assign integer characteristics to previously allocated JSON_value
|
||||
static struct JSON_value* _rjp__add_integer(struct JSON_value* curr, long value, struct JSON_state* state){
|
||||
struct JSON_value new_integer = {.type = json_integer, .integer = value, .parent = curr};
|
||||
if(state->in_array)
|
||||
return _rjp__add_element_to_array(curr, state, &new_integer);
|
||||
curr->object.last->value = new_integer;
|
||||
return &curr->object.last->value;
|
||||
//Assign double characteristics to previously allocated RJP_value
|
||||
static RJP_value* _rjp__add_dfloat(RJP_value* curr, double value, RJP_state* state){
|
||||
RJP_value new_double = {.type = json_dfloat, .dfloat = value, .parent = curr};
|
||||
return _rjp__add_value(curr, &new_double, state);
|
||||
}
|
||||
static struct JSON_value* _rjp__add_boolean(struct JSON_value* curr, int value, struct JSON_state* state){
|
||||
struct JSON_value new_boolean = {.type = json_boolean, .boolean = value, .parent = curr};
|
||||
if(state->in_array)
|
||||
return _rjp__add_element_to_array(curr, state, &new_boolean);
|
||||
curr->object.last->value = new_boolean;
|
||||
return &curr->object.last->value;
|
||||
//Assign integer characteristics to previously allocated RJP_value
|
||||
static RJP_value* _rjp__add_integer(RJP_value* curr, long value, RJP_state* state){
|
||||
RJP_value new_integer = {.type = json_integer, .integer = value, .parent = curr};
|
||||
return _rjp__add_value(curr, &new_integer, state);
|
||||
}
|
||||
static struct JSON_value* _rjp__add_null(struct JSON_value* curr, struct JSON_state* state){
|
||||
struct JSON_value new_null = {.type = json_null, .integer = 0, .parent = curr};
|
||||
if(state->in_array)
|
||||
return _rjp__add_element_to_array(curr, state, &new_null);
|
||||
curr->object.last->value = new_null;
|
||||
return &curr->object.last->value;
|
||||
static RJP_value* _rjp__add_boolean(RJP_value* curr, int value, RJP_state* state){
|
||||
RJP_value new_boolean = {.type = json_boolean, .boolean = value, .parent = curr};
|
||||
return _rjp__add_value(curr, &new_boolean, state);
|
||||
}
|
||||
static RJP_value* _rjp__add_null(RJP_value* curr, RJP_state* state){
|
||||
RJP_value new_null = {.type = json_null, .integer = 0, .parent = curr};
|
||||
return _rjp__add_value(curr, &new_null, state);
|
||||
}
|
||||
|
||||
static void _rjp__free_object_recurse(struct JSON_value* root);
|
||||
static void _rjp__free_object_recurse(RJP_value* root);
|
||||
|
||||
static void _rjp__free_array(struct JSON_value* root){
|
||||
struct JSON_array_element* arr = root->array.elements;
|
||||
for(struct JSON_array_element* i = arr;i != NULL;i = arr){
|
||||
static void _rjp__free_array(RJP_value* root){
|
||||
RJP_array_element* arr = root->array.elements;
|
||||
for(RJP_array_element* i = arr;i != NULL;i = arr){
|
||||
arr = arr->next;
|
||||
if(i->value.type == json_object){
|
||||
_rjp__free_object_recurse(&i->value);
|
||||
@@ -172,9 +169,9 @@ static void _rjp__free_array(struct JSON_value* root){
|
||||
}
|
||||
}
|
||||
//Recursively free JSON objects
|
||||
static void _rjp__free_object_recurse(struct JSON_value* root){
|
||||
struct JSON_object_member* next;
|
||||
for(struct JSON_object_member* m = root->object.members;m;m = next){
|
||||
static void _rjp__free_object_recurse(RJP_value* root){
|
||||
RJP_object_member* next;
|
||||
for(RJP_object_member* m = root->object.members;m;m = next){
|
||||
next = m->next;
|
||||
if(m->value.type == json_object)
|
||||
_rjp__free_object_recurse(&m->value);
|
||||
@@ -187,36 +184,41 @@ static void _rjp__free_object_recurse(struct JSON_value* root){
|
||||
free(m);
|
||||
}
|
||||
}
|
||||
void rjp_free_json(char* json){
|
||||
void rjp_free_value_json(char* json){
|
||||
free(json);
|
||||
}
|
||||
//Same as recurse but also frees root node
|
||||
void rjp_free(struct JSON_value* root){
|
||||
void rjp_free_value(RJP_value* root){
|
||||
if(!root)
|
||||
return;
|
||||
_rjp__free_object_recurse(root);
|
||||
|
||||
if((root->type) == json_object)
|
||||
_rjp__free_object_recurse(root);
|
||||
else if((root->type) == json_array)
|
||||
_rjp__free_array(root);
|
||||
|
||||
free(root);
|
||||
}
|
||||
|
||||
MAYBE_UNUSED static int _rjp__is_array_empty(struct JSON_value* curr){
|
||||
MAYBE_UNUSED static int _rjp__is_array_empty(RJP_value* curr){
|
||||
if(curr->object.members->value.type != json_array)
|
||||
return 0;
|
||||
return curr->object.members->value.array.num_elements == 0;
|
||||
}
|
||||
|
||||
#define syntax_error(msg, row, column)\
|
||||
do{fprintf(stderr, "Syntax error! %s (%i:%i)\n", msg, row, column);rjp_free(root);return NULL;}while(0)
|
||||
do{DIAG_PRINT(stderr, "Syntax error! %s (%i:%i)\n", msg, row, column);rjp_free_value(root);return NULL;}while(0)
|
||||
|
||||
#define MAX_DEPTH 16
|
||||
struct JSON_value* rjp_parse(const char* str){
|
||||
struct JSON_value* root = 0;
|
||||
struct JSON_value* curr = 0;
|
||||
RJP_value* rjp_parse(const char* str){
|
||||
RJP_value* root = 0;
|
||||
RJP_value* curr = 0;
|
||||
int row = 1, column = 0;
|
||||
int in_line_comment = 0;
|
||||
int in_block_comment = 0;
|
||||
|
||||
//keep track of where we are in a given subobject
|
||||
struct JSON_state state_stack[MAX_DEPTH] = {0},*top = state_stack;
|
||||
RJP_state state_stack[MAX_DEPTH] = {0},*top = state_stack;
|
||||
|
||||
//initially search for the root object
|
||||
top->search_target = json_value;
|
||||
@@ -325,14 +327,21 @@ struct JSON_value* rjp_parse(const char* str){
|
||||
top->search_target = json_key;
|
||||
}
|
||||
}
|
||||
//something outside of any object
|
||||
/* //something outside of any object
|
||||
else if(!curr){
|
||||
syntax_error("Unexpected character outside of object definition!", row, column);
|
||||
}
|
||||
//arrays
|
||||
*/ //arrays
|
||||
else if(c == '['){
|
||||
_rjp__add_array(curr, top);
|
||||
++top->in_array;
|
||||
if(!root){
|
||||
root = _rjp__add_array(NULL, top);
|
||||
curr = root;
|
||||
++top->in_array;
|
||||
|
||||
}else{
|
||||
_rjp__add_array(curr, top);
|
||||
++top->in_array;
|
||||
}
|
||||
}
|
||||
else if(c == ']' && top->in_array){ //empty array
|
||||
--top->in_array;
|
||||
@@ -345,7 +354,7 @@ struct JSON_value* rjp_parse(const char* str){
|
||||
char* new_string = _rjp__parse_string(root, str, &vallen, &row, &column);
|
||||
if(!new_string){
|
||||
if(vallen == 0){
|
||||
new_string = calloc(1, 1);
|
||||
new_string = rjp_calloc(1, 1);
|
||||
}else{
|
||||
return NULL;
|
||||
}
|
||||
@@ -356,6 +365,10 @@ struct JSON_value* rjp_parse(const char* str){
|
||||
}
|
||||
//numbers
|
||||
else if((c >= '0' && c <= '9') || c == '-'){
|
||||
if(!curr)
|
||||
top->search_target = json_none;
|
||||
else
|
||||
top->search_target = json_comma;
|
||||
int numlen;
|
||||
int floating = 0; //is an int or a double
|
||||
for(numlen = 1;*(str+numlen) >= '0' && *(str+numlen) <= '9';++numlen);
|
||||
@@ -367,39 +380,63 @@ struct JSON_value* rjp_parse(const char* str){
|
||||
}
|
||||
floating = 1;
|
||||
}
|
||||
if(*(str+numlen) == '\0'){ //hit EOF early
|
||||
if(*(str+numlen) == '\0' && curr){ //hit EOF early
|
||||
syntax_error("Unexpected EOF before end of object!", row, column);
|
||||
}
|
||||
if(c == '-' && numlen == 1){ //only have a '-' with no numbers
|
||||
syntax_error("Missing numerals ofter '-' sign!", row, column);
|
||||
}
|
||||
if(floating){
|
||||
_rjp__add_dfloat(curr, strtod(str, NULL), top);
|
||||
if(!root){
|
||||
root = curr = _rjp__add_dfloat(NULL, strtod(str, NULL), top);
|
||||
}else{
|
||||
_rjp__add_dfloat(curr, strtod(str, NULL), top);
|
||||
}
|
||||
}else{
|
||||
_rjp__add_integer(curr, strtol(str, NULL, 10), top);
|
||||
if(!root){
|
||||
root = curr = _rjp__add_integer(NULL, strtol(str, NULL, 10), top);
|
||||
}else{
|
||||
_rjp__add_integer(curr, strtol(str, NULL, 10), top);
|
||||
}
|
||||
}
|
||||
str += (numlen-1);
|
||||
column += numlen;
|
||||
top->search_target = json_comma;
|
||||
}
|
||||
//booleans and null
|
||||
else if(!strncmp(str, "true", 4)){
|
||||
_rjp__add_boolean(curr, 1, top);
|
||||
str += 3;
|
||||
top->search_target = json_comma;
|
||||
if(!curr){
|
||||
top->search_target = json_none;
|
||||
root = curr = _rjp__add_boolean(curr, 1, top);
|
||||
}else{
|
||||
top->search_target = json_comma;
|
||||
_rjp__add_boolean(curr, 1, top);
|
||||
}
|
||||
str += 3;column += 3;
|
||||
}else if(!strncmp(str, "false", 5)){
|
||||
_rjp__add_boolean(curr, 0, top);
|
||||
str += 4;
|
||||
top->search_target = json_comma;
|
||||
if(!curr){
|
||||
top->search_target = json_none;
|
||||
root = curr = _rjp__add_boolean(curr, 0, top);
|
||||
}else{
|
||||
top->search_target = json_comma;
|
||||
_rjp__add_boolean(curr, 0, top);
|
||||
}
|
||||
str += 4;column += 4;
|
||||
}else if(!strncmp(str, "null", 4)){
|
||||
_rjp__add_null(curr, top);
|
||||
str += 3;
|
||||
top->search_target = json_comma;
|
||||
if(!curr){
|
||||
top->search_target = json_none;
|
||||
root = curr = _rjp__add_null(curr, top);
|
||||
}else{
|
||||
top->search_target = json_comma;
|
||||
_rjp__add_null(curr, top);
|
||||
}
|
||||
str += 3;column += 3;
|
||||
}
|
||||
//unrecognized character
|
||||
else if(!_rjp__is_whitespace(c)){
|
||||
syntax_error("Unexpected character!", row, column);
|
||||
}
|
||||
}else if(top->search_target == json_none && !_rjp__is_whitespace(c)){
|
||||
syntax_error("Unexpected character!", row, column);
|
||||
}
|
||||
}
|
||||
return root;
|
||||
@@ -407,22 +444,4 @@ struct JSON_value* rjp_parse(const char* str){
|
||||
|
||||
#undef syntax_error
|
||||
|
||||
//TODO
|
||||
struct JSON_object_member* rjp_get_members(struct JSON_object* j){
|
||||
return j->members;
|
||||
}
|
||||
struct JSON_object_member* rjp_next_member(struct JSON_object_member* j){
|
||||
return j->next;
|
||||
}
|
||||
struct JSON_object* rjp_member_parent(struct JSON_object_member* j){
|
||||
return &j->value.parent->object;
|
||||
}
|
||||
struct JSON_value* rjp_get_member_value(struct JSON_object_member* j){
|
||||
return &j->value;
|
||||
}
|
||||
enum JSON_type rjp_get_member_type(struct JSON_object_member* j){
|
||||
return j->value.type;
|
||||
}
|
||||
enum JSON_type rjp_get_value_type(struct JSON_value* j){
|
||||
return j->type;
|
||||
}
|
||||
|
||||
|
||||
140
src/output.c
140
src/output.c
@@ -5,13 +5,21 @@
|
||||
#include <stdlib.h> //malloc, calloc, free
|
||||
#include <stdio.h> //sprintf
|
||||
|
||||
struct JSON_value* rjp_init_json(void){
|
||||
struct JSON_value* ret = calloc(1, sizeof(struct JSON_value));
|
||||
ret->type = json_object;
|
||||
RJP_value* rjp_init_json(void){
|
||||
RJP_value* ret = rjp_calloc(1, sizeof(RJP_value));
|
||||
return ret;
|
||||
}
|
||||
struct JSON_value* rjp_add_member(struct JSON_value* dest, int alloc_key, char* key, size_t keylen, struct JSON_value value){
|
||||
if(!keylen && alloc_key)
|
||||
void* rjp_alloc(size_t nbytes){
|
||||
return malloc(nbytes);
|
||||
}
|
||||
void* rjp_calloc(size_t num, size_t nbytes){
|
||||
return calloc(num, nbytes);
|
||||
}
|
||||
void rjp_free(void* data){
|
||||
free(data);
|
||||
}
|
||||
RJP_value* rjp_add_member(RJP_value* dest, int alloc_key, char* key, size_t keylen, RJP_value value){
|
||||
if(!keylen)
|
||||
keylen = strlen(key);
|
||||
if(alloc_key)
|
||||
_rjp__add_member(&dest->object, key, keylen);
|
||||
@@ -21,36 +29,81 @@ struct JSON_value* rjp_add_member(struct JSON_value* dest, int alloc_key, char*
|
||||
dest->object.last->value.parent = dest;
|
||||
return &dest->object.last->value;
|
||||
}
|
||||
struct JSON_value* rjp_add_element(struct JSON_value* dest, struct JSON_value value){
|
||||
RJP_value* rjp_add_element(RJP_value* dest, RJP_value value){
|
||||
_rjp__add_element(&dest->array);
|
||||
dest->array.last->value = value;
|
||||
dest->array.last->value.parent = dest;
|
||||
return &dest->array.last->value;
|
||||
}
|
||||
struct JSON_value rjp_integer(long i){
|
||||
return (struct JSON_value){.integer = i, .type = json_integer};
|
||||
RJP_value rjp_integer(long i){
|
||||
return (RJP_value){.integer = i, .type = json_integer};
|
||||
}
|
||||
struct JSON_value rjp_boolean(char b){
|
||||
return (struct JSON_value){.boolean = b, .type = json_boolean};
|
||||
RJP_value rjp_boolean(char b){
|
||||
return (RJP_value){.boolean = b, .type = json_boolean};
|
||||
}
|
||||
struct JSON_value rjp_dfloat(double d){
|
||||
return (struct JSON_value){.dfloat = d, .type = json_dfloat};
|
||||
RJP_value rjp_dfloat(double d){
|
||||
return (RJP_value){.dfloat = d, .type = json_dfloat};
|
||||
}
|
||||
struct JSON_value rjp_string(char* c, size_t len){
|
||||
return (struct JSON_value){.string = {.value = c, .length = len}, .type = json_string};
|
||||
RJP_value rjp_string(char* c, size_t len){
|
||||
return (RJP_value){.string = {.value = c, .length = len}, .type = json_string};
|
||||
}
|
||||
struct JSON_value rjp_null(void){
|
||||
return (struct JSON_value){.integer = 0, .type = json_null};
|
||||
RJP_value rjp_null(void){
|
||||
return (RJP_value){.integer = 0, .type = json_null};
|
||||
}
|
||||
struct JSON_value rjp_object(void){
|
||||
return (struct JSON_value){.object = {0}, .type = json_object};
|
||||
RJP_value rjp_object(void){
|
||||
return (RJP_value){.object = {0}, .type = json_object};
|
||||
}
|
||||
struct JSON_value rjp_array(void){
|
||||
return (struct JSON_value){.array = {0}, .type = json_array};
|
||||
RJP_value rjp_array(void){
|
||||
return (RJP_value){.array = {0}, .type = json_array};
|
||||
}
|
||||
|
||||
|
||||
static size_t _rjp__write_value(char* dest, struct JSON_value* val){
|
||||
|
||||
RJP_value* rjp_get_member(RJP_value* object){
|
||||
return &object->object.members->value;
|
||||
}
|
||||
RJP_value* rjp_next_member(RJP_value* member){
|
||||
//polymorphism
|
||||
return (RJP_value*)(((RJP_object_member*)member)->next);
|
||||
}
|
||||
RJP_value* rjp_get_element(RJP_value* array){
|
||||
return &array->array.elements->value;
|
||||
}
|
||||
RJP_value* rjp_next_element(RJP_value* element){
|
||||
return (RJP_value*)(((RJP_array_element*)element)->next);
|
||||
}
|
||||
|
||||
char* rjp_member_name(RJP_value* member){
|
||||
return ((RJP_object_member*)member)->name.value;
|
||||
}
|
||||
size_t rjp_member_name_length(RJP_value* member){
|
||||
return ((RJP_object_member*)member)->name.length;
|
||||
}
|
||||
|
||||
RJP_value* rjp_value_parent(RJP_value* value){
|
||||
return value->parent;
|
||||
}
|
||||
RJP_type rjp_value_type(RJP_value* value){
|
||||
return value->type;
|
||||
}
|
||||
double rjp_value_dfloat(RJP_value* value){
|
||||
return value->dfloat;
|
||||
}
|
||||
int rjp_value_integer(RJP_value* value){
|
||||
return value->integer;
|
||||
}
|
||||
char rjp_value_boolean(RJP_value* value){
|
||||
return value->boolean;
|
||||
}
|
||||
char* rjp_value_string(RJP_value* value){
|
||||
return value->string.value;
|
||||
}
|
||||
size_t rjp_value_string_length(RJP_value* value){
|
||||
return value->string.length;
|
||||
}
|
||||
|
||||
|
||||
static size_t _rjp__write_value(char* dest, RJP_value* val){
|
||||
size_t ret;
|
||||
switch(val->type){
|
||||
case json_integer:
|
||||
@@ -69,14 +122,10 @@ static size_t _rjp__write_value(char* dest, struct JSON_value* val){
|
||||
ret = sprintf(dest, "\"%s\"", val->string.value);
|
||||
break;
|
||||
case json_array:
|
||||
ret = sprintf(dest, "[");
|
||||
ret += rjp_dump_array(val, dest+ret);
|
||||
ret += sprintf(dest+ret, "]");
|
||||
ret = rjp_dump_array(val, dest);
|
||||
break;
|
||||
case json_object:
|
||||
ret = sprintf(dest, "{");
|
||||
ret += rjp_dump_object(val, dest+ret);
|
||||
ret += sprintf(dest+ret, "}");
|
||||
ret = rjp_dump_object(val, dest);
|
||||
break;
|
||||
default:
|
||||
ret = 0;
|
||||
@@ -85,10 +134,11 @@ static size_t _rjp__write_value(char* dest, struct JSON_value* val){
|
||||
return ret;
|
||||
}
|
||||
|
||||
size_t rjp_dump_array(struct JSON_value* arr, char* dest){
|
||||
struct JSON_array* array = &arr->array;
|
||||
struct JSON_array_element* element_list = array->elements;
|
||||
size_t pos = 0;
|
||||
size_t rjp_dump_array(RJP_value* arr, char* dest){
|
||||
RJP_array* array = &arr->array;
|
||||
RJP_array_element* element_list = array->elements;
|
||||
size_t pos = 1;
|
||||
sprintf(dest, "[");
|
||||
for(;element_list;element_list = element_list->next){
|
||||
pos += _rjp__write_value(dest+pos, &element_list->value);
|
||||
|
||||
@@ -97,13 +147,16 @@ size_t rjp_dump_array(struct JSON_value* arr, char* dest){
|
||||
else
|
||||
break;
|
||||
}
|
||||
pos += sprintf(dest+pos, "]");
|
||||
return pos;
|
||||
}
|
||||
|
||||
size_t rjp_dump_object(struct JSON_value* root, char* dest){
|
||||
struct JSON_object* root_obj = &root->object;
|
||||
struct JSON_object_member* member_list = root_obj->members;
|
||||
size_t pos = 0;
|
||||
size_t rjp_dump_object(RJP_value* root, char* dest){
|
||||
RJP_object* root_obj = &root->object;
|
||||
RJP_object_member* member_list = root_obj->members;
|
||||
size_t pos = 1;
|
||||
sprintf(dest, "{");
|
||||
|
||||
for(;member_list;member_list = member_list->next){
|
||||
pos += sprintf(dest+pos, "\"%s\":", member_list->name.value);
|
||||
pos += _rjp__write_value(dest+pos, &member_list->value);
|
||||
@@ -113,18 +166,21 @@ size_t rjp_dump_object(struct JSON_value* root, char* dest){
|
||||
else
|
||||
break;
|
||||
}
|
||||
pos += sprintf(dest+pos, "}");
|
||||
return pos;
|
||||
}
|
||||
|
||||
char* rjp_to_json(struct JSON_value* root){
|
||||
size_t len = _rjp__object_strlen(root);
|
||||
char* tmp = malloc(len + 1);
|
||||
char* rjp_to_json(RJP_value* root){
|
||||
if(!root)
|
||||
return NULL;
|
||||
size_t len = _rjp__value_strlen(root);
|
||||
if(!len)
|
||||
return NULL;
|
||||
char* tmp = rjp_alloc(len + 1);
|
||||
tmp[len] = 0;
|
||||
size_t pos = 1;
|
||||
size_t pos = 0;
|
||||
|
||||
sprintf(tmp, "{");
|
||||
pos += rjp_dump_object(root, tmp+pos);
|
||||
sprintf(tmp+pos, "}");
|
||||
_rjp__write_value(tmp, root);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <stdlib.h> //malloc, free
|
||||
|
||||
//Convert escape sequences in strings
|
||||
char* _rjp__parse_string(struct JSON_value* root, const char* str, int* len, int* row, int* column){
|
||||
char* _rjp__parse_string(RJP_value* root, const char* str, int* len, int* row, int* column){
|
||||
char* new_string;
|
||||
++(*column); //account for starting quotation mark
|
||||
for(*len = 0;*(str+*len) != '"';++(*len), ++(*column)){
|
||||
@@ -15,7 +15,7 @@ char* _rjp__parse_string(struct JSON_value* root, const char* str, int* len, int
|
||||
}else if(*(str+*len) == '\0'){
|
||||
*len = 1;
|
||||
fprintf(stderr, "Syntax error! %s (%i:%i)\n", "Unexpected EOF in string!", *row, *column);
|
||||
rjp_free(root);
|
||||
rjp_free_value(root);
|
||||
return NULL;
|
||||
}else if(*(str+*len) == '\n'){
|
||||
++(*row);
|
||||
@@ -25,7 +25,7 @@ char* _rjp__parse_string(struct JSON_value* root, const char* str, int* len, int
|
||||
if(*len == 0){
|
||||
return NULL;
|
||||
}
|
||||
new_string = malloc(*len + 1);
|
||||
new_string = rjp_alloc(*len + 1);
|
||||
new_string[*len] = 0;
|
||||
for(int i = 0;*str != '"';++i,++str){
|
||||
if(*str == '\\'){
|
||||
@@ -100,6 +100,7 @@ size_t rjp_escape_strcpy(char* dest, const char* src){
|
||||
break;
|
||||
};
|
||||
}
|
||||
dest[j] = 0;
|
||||
return j;
|
||||
}
|
||||
size_t rjp_escape_strlen(const char* str){
|
||||
@@ -121,10 +122,10 @@ size_t rjp_escape_strlen(const char* str){
|
||||
}
|
||||
return count;
|
||||
}
|
||||
size_t _rjp__array_strlen(struct JSON_value* arr){
|
||||
size_t _rjp__array_strlen(RJP_value* arr){
|
||||
size_t count = 2; //[]
|
||||
struct JSON_array* array = &arr->array;
|
||||
struct JSON_array_element* element_list = array->elements;
|
||||
RJP_array* array = &arr->array;
|
||||
RJP_array_element* element_list = array->elements;
|
||||
for(;element_list;element_list = element_list->next){
|
||||
switch(element_list->value.type){
|
||||
case json_integer:
|
||||
@@ -157,35 +158,15 @@ size_t _rjp__array_strlen(struct JSON_value* arr){
|
||||
return count;
|
||||
}
|
||||
|
||||
size_t _rjp__object_strlen(struct JSON_value* root){
|
||||
size_t _rjp__object_strlen(RJP_value* root){
|
||||
size_t count = 2; //{}
|
||||
struct JSON_object* root_obj = &root->object;
|
||||
struct JSON_object_member* member_list = root_obj->members;
|
||||
RJP_object* root_obj = &root->object;
|
||||
RJP_object_member* member_list = root_obj->members;
|
||||
for(;member_list;member_list = member_list->next){
|
||||
if(member_list->name.length == 0 || member_list->name.value[0] == 0)
|
||||
return 0;
|
||||
count += member_list->name.length + 3; //"":
|
||||
switch(member_list->value.type){
|
||||
case json_integer:
|
||||
count += snprintf(NULL, 0, "%li", member_list->value.integer);
|
||||
break;
|
||||
case json_dfloat:
|
||||
count += snprintf(NULL, 0, "%lf", member_list->value.dfloat);
|
||||
break;
|
||||
case json_boolean:
|
||||
count += member_list->value.boolean ? 4 : 5; //true, false
|
||||
break;
|
||||
case json_null:
|
||||
count += 4; //null
|
||||
break;
|
||||
case json_string:
|
||||
count += member_list->value.string.length;
|
||||
break;
|
||||
case json_array:
|
||||
count += _rjp__array_strlen(&member_list->value);
|
||||
break;
|
||||
case json_object:
|
||||
count += _rjp__object_strlen(&member_list->value);
|
||||
break;
|
||||
};
|
||||
count += _rjp__value_strlen(&member_list->value);
|
||||
if(member_list->next)
|
||||
++count; //,
|
||||
else
|
||||
@@ -194,3 +175,23 @@ size_t _rjp__object_strlen(struct JSON_value* root){
|
||||
return count;
|
||||
}
|
||||
|
||||
size_t _rjp__value_strlen(RJP_value* root){
|
||||
switch(root->type){
|
||||
case json_integer:
|
||||
return snprintf(NULL, 0, "%li", root->integer);
|
||||
case json_dfloat:
|
||||
return snprintf(NULL, 0, "%lf", root->dfloat);
|
||||
case json_boolean:
|
||||
return root->boolean ? 4 : 5; //true, false
|
||||
case json_null:
|
||||
return 4;
|
||||
case json_string:
|
||||
return root->string.length;
|
||||
case json_array:
|
||||
return _rjp__array_strlen(root);
|
||||
case json_object:
|
||||
return _rjp__object_strlen(root);
|
||||
default:
|
||||
return 0;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user