8 Commits
v0.1 ... v0.3

Author SHA1 Message Date
rexy712
6f4a55a766 removed excess 'struct's (typedefs) 2018-12-01 08:33:08 -08:00
rexy712
b27031876b added accessor interface 2018-12-01 08:21:06 -08:00
rexy712
566952de5e Fixed a potential segfault and added diagnostics option to cmake 2018-11-30 14:12:39 -08:00
rexy712
036c732ee8 changed member/element addition to be more convenient in most situations 2018-11-30 13:49:51 -08:00
rexy712
f1d8dbde97 Fixed a memory leak and removed main function (again) 2018-11-30 13:32:21 -08:00
rexy712
890876a490 cleaned up object/array output and version bump 2018-11-30 11:22:54 -08:00
rexy712
e70b0833e6 Working json output and split source into multiple files 2018-11-29 14:26:20 -08:00
rexy712
9eaa734fbb Rudimentary output to json 2018-11-25 16:19:14 -08:00
7 changed files with 892 additions and 541 deletions

View File

@@ -4,7 +4,7 @@ include(GNUInstallDirs)
cmake_minimum_required(VERSION 3.0.2)
project(rjp)
set(rjp_VERSION_MAJOR 0)
set(rjp_VERSION_MINOR 1)
set(rjp_VERSION_MINOR 2)
set(INCLUDE_PATH ${CMAKE_SOURCE_DIR}/include)
configure_file(
"${INCLUDE_PATH}/config.h.in"
@@ -12,8 +12,13 @@ configure_file(
)
include_directories("${INCLUDE_PATH}")
add_library (rjp STATIC src/rjp.c)
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}

View File

@@ -24,8 +24,9 @@
#ifdef __cplusplus
extern "C"{
#endif
//type of data
enum JSON_type{
typedef enum RJP_type{
json_object,
json_string,
json_integer,
@@ -33,98 +34,111 @@ enum JSON_type{
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{
enum JSON_type type;
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;
};
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(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);
void rjp_free_json(char* json);
void rjp_free(RJP_value* root);
//initialize an empty json handle (outputting to string would result in '{}')
struct JSON_value* rjp_init_json(void);
//initialize an empty json object
struct JSON_object* rjp_init_object(void);
//initialize an empty json array
struct JSON_array* rjp_init_array(void);
RJP_value* rjp_init_json(void);
//add a member to a json object
struct JSON_value* rjp_add_member(struct JSON_object* dest, const 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_array* dest, struct JSON_value* value);
RJP_value* rjp_add_element(RJP_value* dest, RJP_value value);
struct JSON_value* rjp_init_integer(long i);
struct JSON_value* rjp_init_dfloat(double d);
struct JSON_value* rjp_init_boolean(char b);
struct JSON_value* rjp_init_string(const char* str, size_t len);
//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);
/*struct JSON_value* root = rjp_init_json();
rjp_add_member(root, "intel_backlight", 15, rjp_init_integer(100));
rjp_free(root);
//{intel_backlight:100}
*/
#ifdef __cplusplus
}
#endif

13
include/rjp_internal.h Normal file
View File

@@ -0,0 +1,13 @@
#ifndef RJP_INTERNAL_H
#define RJP_INTERNAL_H
#include "rjp.h"
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);
#endif

416
src/input.c Normal file
View File

@@ -0,0 +1,416 @@
/**
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/>.
*/
//TODO: Scientific notation
//TODO: \e escape sequence in strings
#include "rjp.h"
#include "rjp_internal.h"
#include <string.h> //strncpy
#include <stdlib.h> //malloc, calloc, free
#include <stdio.h> //fprintf, stderr
#ifdef __GNUC__
#define MAYBE_UNUSED __attribute__((unused))
#else
#define MAYBE_UNUSED
#endif
#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
typedef enum json_search_target{
json_key,
json_colon,
json_comma,
json_value
}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(RJP_array* j){
++j->num_elements;
if(!j->elements){
j->elements = calloc(1, sizeof(RJP_array_element));
j->last = j->elements;
}else{
j->last->next = 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(RJP_object* j, char* str, size_t len){
++j->num_members;
if(!j->members){
j->members = calloc(1, sizeof(RJP_object_member));
j->last = j->members;
}else{
j->last->next = calloc(1, sizeof(RJP_object_member));
j->last = j->last->next;
}
j->last->name.value = malloc(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(RJP_object* j, char* str, size_t len){
++j->num_members;
if(!j->members){
j->members = calloc(1, sizeof(RJP_object_member));
j->last = j->members;
}else{
j->last->next = calloc(1, sizeof(RJP_object_member));
j->last = j->last->next;
}
j->last->name.value = str;
j->last->name.length = len;
}
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 RJP_value
static RJP_value* _rjp__add_object(RJP_value* curr, RJP_state* state){
if(!curr){
curr = calloc(1, sizeof(RJP_value));
curr->type = json_object;
return curr;
}
RJP_value new_object = {.type = json_object, .integer = 0, .parent = curr};
if(state->in_array)
return _rjp__add_element_to_array(curr, state, &new_object);
curr->object.last->value = new_object;
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};
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 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};
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 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};
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 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};
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;
}
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};
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;
}
static RJP_value* _rjp__add_null(RJP_value* curr, RJP_state* state){
RJP_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 void _rjp__free_object_recurse(RJP_value* root);
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);
}else if(i->value.type == json_array){
_rjp__free_array(&i->value);
}else if(i->value.type == json_string){
free(i->value.string.value);
}
free(i);
}
}
//Recursively free JSON objects
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);
else if(m->value.type == json_string)
free(m->value.string.value);
else if(m->value.type == json_array)
_rjp__free_array(&m->value);
if(m->name.value)
free(m->name.value);
free(m);
}
}
void rjp_free_json(char* json){
free(json);
}
//Same as recurse but also frees root node
void rjp_free(RJP_value* root){
if(!root)
return;
_rjp__free_object_recurse(root);
free(root);
}
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{DIAG_PRINT(stderr, "Syntax error! %s (%i:%i)\n", msg, row, column);rjp_free(root);return NULL;}while(0)
#define MAX_DEPTH 16
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
RJP_state state_stack[MAX_DEPTH] = {0},*top = state_stack;
//initially search for the root object
top->search_target = json_value;
top->in_array = 0;
for(;*str != '\0';++str){
char c = *str;
//keep track of position in input file
if(c == '\n'){
++row;
column = 0;
}else{
++column;
}
//Handle comments
if(in_line_comment){
if(c == '\n')
in_line_comment = 0;
}
else if(in_block_comment){
if(c == '*' && *(str+1) == '/'){
in_block_comment = 0;
++str;
}
}
else if(c == '/' && *(str+1) == '/'){
in_line_comment = 1;
++str;
}
else if(c == '/' && *(str+1) == '*'){
in_block_comment = 1;
++str;
}
else if(top->search_target == json_key){
//start of key
if(c == '"'){
if(curr == NULL)
syntax_error("Key found outside of object definition!", row, column);
int keylen;
char* new_string = _rjp__parse_string(root, ++str, &keylen, &row, &column);
if(!new_string){
if(!keylen)
syntax_error("Cannot have empty key name!", row, column);
return NULL;
}
_rjp__add_member_no_alloc(&curr->object, new_string, keylen);
str += keylen;
top->search_target = json_colon;
//end of this object (object is empty)
}else if(c == '}'){
curr = curr->parent;
if(top != state_stack)
--top;
//unrecognized character
}else if(!_rjp__is_whitespace(c)){
syntax_error("Unexpected character, expected '\"'!", row, column);
}
}
else if(top->search_target == json_colon){
//colon after a key
if(c == ':'){
top->search_target = json_value;
//unrecognized character
}else if(!_rjp__is_whitespace(c)){
syntax_error( "Unexpected character, expected ':'!", row, column);
}
}
else if(top->search_target == json_comma){
//comma separating keys in an object or values in an array
if(c == ','){
top->search_target = top->in_array ? json_value : json_key;
//end of object
}else if(c == '}'){
if(top->in_array){
syntax_error("Unexpected end of object within array!", row, column);
}
curr = curr->parent;
if(top != state_stack)
--top;
//end of array
}else if(c == ']' && top->in_array){
--top->in_array;
//unrecognized character
}else if(!_rjp__is_whitespace(c)){
syntax_error("Unexpected character, expected ','!", row, column);
}
}
else if(top->search_target == json_value){
//object
if(c == '{'){
if(!root){
root = _rjp__add_object(NULL, top);
curr = root;
top->search_target = json_key;
}else{
curr = _rjp__add_object(curr, top);
top->search_target = json_comma;
++top;
top->in_array = 0;
top->search_target = json_key;
}
}
//something outside of any object
else if(!curr){
syntax_error("Unexpected character outside of object definition!", row, column);
}
//arrays
else if(c == '['){
_rjp__add_array(curr, top);
++top->in_array;
}
else if(c == ']' && top->in_array){ //empty array
--top->in_array;
top->search_target = json_comma;
}
//strings
else if(c == '"'){
int vallen;
++str;
char* new_string = _rjp__parse_string(root, str, &vallen, &row, &column);
if(!new_string){
if(vallen == 0){
new_string = calloc(1, 1);
}else{
return NULL;
}
}
_rjp__add_string_no_alloc(curr, new_string, vallen, top);
str += vallen;
top->search_target = json_comma;
}
//numbers
else if((c >= '0' && c <= '9') || c == '-'){
int numlen;
int floating = 0; //is an int or a double
for(numlen = 1;*(str+numlen) >= '0' && *(str+numlen) <= '9';++numlen);
if(*(str+numlen) == '.'){ //if we have a decimal, make it a double and continue parsing as a number
int i = ++numlen;
for(;*(str+numlen) >= '0' && *(str+numlen) <= '9';++numlen);
if(i == numlen){ //no number after decimal
syntax_error("Missing numerals after decimal place!", row, column);
}
floating = 1;
}
if(*(str+numlen) == '\0'){ //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);
}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;
}else if(!strncmp(str, "false", 5)){
_rjp__add_boolean(curr, 0, top);
str += 4;
top->search_target = json_comma;
}else if(!strncmp(str, "null", 4)){
_rjp__add_null(curr, top);
str += 3;
top->search_target = json_comma;
}
//unrecognized character
else if(!_rjp__is_whitespace(c)){
syntax_error("Unexpected character!", row, column);
}
}
}
return root;
}
#undef syntax_error

179
src/output.c Normal file
View File

@@ -0,0 +1,179 @@
#include "rjp.h"
#include "rjp_internal.h"
#include <string.h> //strlen
#include <stdlib.h> //malloc, calloc, free
#include <stdio.h> //sprintf
RJP_value* rjp_init_json(void){
RJP_value* ret = calloc(1, sizeof(RJP_value));
ret->type = json_object;
return ret;
}
RJP_value* rjp_add_member(RJP_value* dest, int alloc_key, char* key, size_t keylen, RJP_value value){
if(!keylen && alloc_key)
keylen = strlen(key);
if(alloc_key)
_rjp__add_member(&dest->object, key, keylen);
else
_rjp__add_member_no_alloc(&dest->object, key, keylen);
dest->object.last->value = value;
dest->object.last->value.parent = dest;
return &dest->object.last->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;
}
RJP_value rjp_integer(long i){
return (RJP_value){.integer = i, .type = json_integer};
}
RJP_value rjp_boolean(char b){
return (RJP_value){.boolean = b, .type = json_boolean};
}
RJP_value rjp_dfloat(double d){
return (RJP_value){.dfloat = d, .type = json_dfloat};
}
RJP_value rjp_string(char* c, size_t len){
return (RJP_value){.string = {.value = c, .length = len}, .type = json_string};
}
RJP_value rjp_null(void){
return (RJP_value){.integer = 0, .type = json_null};
}
RJP_value rjp_object(void){
return (RJP_value){.object = {0}, .type = json_object};
}
RJP_value rjp_array(void){
return (RJP_value){.array = {0}, .type = json_array};
}
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:
ret = sprintf(dest, "%li", val->integer);
break;
case json_dfloat:
ret = sprintf(dest, "%lf", val->dfloat);
break;
case json_boolean:
ret = sprintf(dest, val->boolean ? "true" : "false");
break;
case json_null:
ret = sprintf(dest, "null");
break;
case json_string:;
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, "]");
break;
case json_object:
ret = sprintf(dest, "{");
ret += rjp_dump_object(val, dest+ret);
ret += sprintf(dest+ret, "}");
break;
default:
ret = 0;
break;
};
return ret;
}
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 = 0;
for(;element_list;element_list = element_list->next){
pos += _rjp__write_value(dest+pos, &element_list->value);
if(element_list->next)
pos += sprintf(dest+pos, ",");
else
break;
}
return pos;
}
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 = 0;
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);
if(member_list->next)
pos += sprintf(dest+pos, ",");
else
break;
}
return pos;
}
char* rjp_to_json(RJP_value* root){
if(!root)
return NULL;
size_t len = _rjp__object_strlen(root);
if(!len)
return NULL;
char* tmp = malloc(len + 1);
tmp[len] = 0;
size_t pos = 1;
sprintf(tmp, "{");
pos += rjp_dump_object(root, tmp+pos);
sprintf(tmp+pos, "}");
return tmp;
}

477
src/rjp.c
View File

@@ -1,477 +0,0 @@
/**
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/>.
*/
//TODO: Scientific notation
//TODO: \e escape sequence in strings
#include "rjp.h"
#include <string.h> //strncpy
#include <stdlib.h> //malloc, calloc
#include <stdio.h> //fprintf, stderr
#ifdef __GNUC__
#define MAYBE_UNUSED __attribute__((unused))
#else
#define MAYBE_UNUSED
#endif
struct JSON_state{
int in_array;
int search_target;
};
//types of searches in the text
enum json_search_target{
json_key,
json_colon,
json_comma,
json_value
};
//Determine if the character is valid whitespace
static int is_whitespace(char c){
return c == ' ' || c == '\n' || c == '\r' || c == '\t';
}
//allocate a member of the object as a linked list member and assign a name
MAYBE_UNUSED static void add_member(struct JSON_value* j, const char* str, size_t len){
++j->object.num_members;
struct JSON_object_member* prev = j->object.members;
j->object.members = calloc(1, sizeof(struct JSON_object_member));
j->object.members->name.value = malloc(len + 1);
j->object.members->name.length = len;
strncpy(j->object.members->name.value, str, len);
j->object.members->name.value[len] = 0;
j->object.members->next = prev;
}
//add an element to an array
static void add_element(struct JSON_value* j){
++j->array.num_elements;
if(!j->array.elements){
j->array.elements = calloc(1, sizeof(struct JSON_array_element));
j->array.last = j->array.elements;
}else{
j->array.last->next = calloc(1, sizeof(struct JSON_array_element));
j->array.last = j->array.last->next;
}
}
//create member of the object as a linked list member and assign a name with no name allocation
static void add_member_no_alloc(struct JSON_value* j, char* str, size_t len){
++j->object.num_members;
if(!j->object.members){
j->object.members = calloc(1, sizeof(struct JSON_object_member));
j->object.last = j->object.members;
}else{
j->object.last->next = calloc(1, sizeof(struct JSON_object_member));
j->object.last = j->object.last->next;
}
j->object.last->name.value = str;
j->object.last->name.length = len;
}
static struct JSON_value* add_element_to_array(struct JSON_value* curr, struct JSON_state* state, struct JSON_value* element){
struct JSON_value* tmp = &curr->object.members->value;
for(int i = state->in_array-1;i > 0;--i, tmp = &tmp->array.last->value);
add_element(tmp);
tmp->array.elements->value = *element;
return &tmp->array.elements->value;
}
//Assign object characteristics to previously allocated JSON_value
static struct JSON_value* add_object(struct JSON_value* curr, struct JSON_state* state){
if(!curr){
curr = calloc(1, sizeof(struct JSON_value));
curr->type = json_object;
return curr;
}
struct JSON_value new_object = {.type = json_object, .integer = 0, .parent = curr};
if(state->in_array)
return add_element_to_array(curr, state, &new_object);
curr->object.members->value = new_object;
return &curr->object.members->value;
}
//Assign array characteristics to previously allocated JSON_value
static struct JSON_value* 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 add_element_to_array(curr, state, &new_array);
curr->object.members->value = new_array;
return &curr->object.members->value;
}
//Assign string characteristics to previously allocated JSON_value with no string allocation
static struct JSON_value* 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 add_element_to_array(curr, state, &new_string);
curr->object.members->value = new_string;
return &curr->object.members->value;
}
//Assign double characteristics to previously allocated JSON_value
static struct JSON_value* add_double(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 add_element_to_array(curr, state, &new_double);
curr->object.members->value = new_double;
return &curr->object.members->value;
}
//Assign integer characteristics to previously allocated JSON_value
static struct JSON_value* 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 add_element_to_array(curr, state, &new_integer);
curr->object.members->value = new_integer;
return &curr->object.members->value;
}
static struct JSON_value* 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 add_element_to_array(curr, state, &new_boolean);
curr->object.members->value = new_boolean;
return &curr->object.members->value;
}
static struct JSON_value* 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 add_element_to_array(curr, state, &new_null);
curr->object.members->value = new_null;
return &curr->object.members->value;
}
static void free_json_recurse(struct JSON_value* root);
static void free_json_array(struct JSON_value* root){
struct JSON_array_element* arr = root->array.elements;
for(struct JSON_array_element* i = arr;i != NULL;i = arr){
arr = arr->next;
if(i->value.type == json_object){
free_json_recurse(&i->value);
}else if(i->value.type == json_array){
free_json_array(&i->value);
}else if(i->value.type == json_string){
free(i->value.string.value);
}
free(i);
}
}
//Recursively free JSON objects
static void free_json_recurse(struct JSON_value* root){
struct JSON_object_member* next;
for(struct JSON_object_member* m = root->object.members;m;m = next){
next = m->next;
if(m->value.type == json_object)
free_json_recurse(&m->value);
else if(m->value.type == json_string)
free(m->value.string.value);
else if(m->value.type == json_array)
free_json_array(&m->value);
if(m->name.value)
free(m->name.value);
free(m);
}
}
//Same as recurse but also frees root node
void rjp_free(struct JSON_value* root){
if(!root)
return;
free_json_recurse(root);
free(root);
}
MAYBE_UNUSED static int is_array_empty(struct JSON_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)
//Convert escape sequences in strings
static char* parse_string(struct JSON_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)){
if(*(str+*len) == '\\'){
++(*len);
++(*column);
}else if(*(str+*len) == '\0'){
*len = 1;
syntax_error("Unexpected EOF in string!", *row, *column);
}else if(*(str+*len) == '\n'){
++(*row);
*column = 0;
}
}
if(*len == 0){
return NULL;
}
new_string = malloc(*len + 1);
new_string[*len] = 0;
for(int i = 0;*str != '"';++i,++str){
if(*str == '\\'){
++str;
switch(*str){
case '"':
new_string[i] = '"';
break;
case 'n':
new_string[i] = '\n';
break;
case 'r':
new_string[i] = '\r';
break;
case 'b':
new_string[i] = '\b';
break;
case '\\':
new_string[i] = '\\';
break;
case 't':
new_string[i] = '\t';
break;
case 'f':
new_string[i] = '\f';
break;
default:
new_string[i] = *str;
break;
}
}else{
new_string[i] = *str;
}
}
return new_string;
}
#define MAX_DEPTH 16
struct JSON_value* rjp_parse(const char* str){
struct JSON_value* root = 0;
struct JSON_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;
//initially search for the root object
top->search_target = json_value;
top->in_array = 0;
for(;*str != '\0';++str){
char c = *str;
//keep track of position in input file
if(c == '\n'){
++row;
column = 0;
}else{
++column;
}
//Handle comments
if(in_line_comment){
if(c == '\n')
in_line_comment = 0;
}
else if(in_block_comment){
if(c == '*' && *(str+1) == '/'){
in_block_comment = 0;
++str;
}
}
else if(c == '/' && *(str+1) == '/'){
in_line_comment = 1;
++str;
}
else if(c == '/' && *(str+1) == '*'){
in_block_comment = 1;
++str;
}
else if(top->search_target == json_key){
//start of key
if(c == '"'){
if(curr == NULL)
syntax_error("Key found outside of object definition!", row, column);
int keylen;
char* new_string = parse_string(root, ++str, &keylen, &row, &column);
if(!new_string){
if(!keylen)
syntax_error("Cannot have empty key name!", row, column);
return NULL;
}
add_member_no_alloc(curr, new_string, keylen);
str += keylen;
top->search_target = json_colon;
//end of this object (object is empty)
}else if(c == '}'){
curr = curr->parent;
if(top != state_stack)
--top;
//unrecognized character
}else if(!is_whitespace(c)){
syntax_error("Unexpected character, expected '\"'!", row, column);
}
}
else if(top->search_target == json_colon){
//colon after a key
if(c == ':'){
top->search_target = json_value;
//unrecognized character
}else if(!is_whitespace(c)){
syntax_error( "Unexpected character, expected ':'!", row, column);
}
}
else if(top->search_target == json_comma){
//comma separating keys in an object or values in an array
if(c == ','){
top->search_target = top->in_array ? json_value : json_key;
//end of object
}else if(c == '}'){
if(top->in_array){
syntax_error("Unexpected end of object within array!", row, column);
}
curr = curr->parent;
if(top != state_stack)
--top;
//end of array
}else if(c == ']' && top->in_array){
--top->in_array;
//unrecognized character
}else if(!is_whitespace(c)){
syntax_error("Unexpected character, expected ','!", row, column);
}
}
else if(top->search_target == json_value){
//object
if(c == '{'){
if(!root){
root = add_object(NULL, top);
curr = root;
top->search_target = json_key;
}else{
curr = add_object(curr, top);
top->search_target = json_comma;
++top;
top->in_array = 0;
top->search_target = json_key;
}
}
//something outside of any object
else if(!curr){
syntax_error("Unexpected character outside of object definition!", row, column);
}
//arrays
else if(c == '['){
add_array(curr, top);
++top->in_array;
}
else if(c == ']' && top->in_array){ //empty array
--top->in_array;
top->search_target = json_comma;
}
//strings
else if(c == '"'){
int vallen;
++str;
char* new_string = parse_string(root, str, &vallen, &row, &column);
if(!new_string){
if(vallen == 0){
new_string = calloc(1, 1);
}else{
return NULL;
}
}
add_string_no_alloc(curr, new_string, vallen, top);
str += vallen;
top->search_target = json_comma;
}
//numbers
else if((c >= '0' && c <= '9') || c == '-'){
int numlen;
int floating = 0; //is an int or a double
for(numlen = 1;*(str+numlen) >= '0' && *(str+numlen) <= '9';++numlen);
if(*(str+numlen) == '.'){ //if we have a decimal, make it a double and continue parsing as a number
int i = ++numlen;
for(;*(str+numlen) >= '0' && *(str+numlen) <= '9';++numlen);
if(i == numlen){ //no number after decimal
syntax_error("Missing numerals after decimal place!", row, column);
}
floating = 1;
}
if(*(str+numlen) == '\0'){ //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){
add_double(curr, strtod(str, NULL), top);
}else{
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)){
add_boolean(curr, 1, top);
str += 3;
top->search_target = json_comma;
}else if(!strncmp(str, "false", 5)){
add_boolean(curr, 0, top);
str += 4;
top->search_target = json_comma;
}else if(!strncmp(str, "null", 4)){
add_null(curr, top);
str += 3;
top->search_target = json_comma;
}
//unrecognized character
else if(!is_whitespace(c)){
syntax_error("Unexpected character!", row, column);
}
}
}
return root;
}
#undef syntax_error
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;
}

201
src/strings.c Normal file
View File

@@ -0,0 +1,201 @@
#include "rjp.h"
#include "rjp_internal.h"
#include <stdio.h> //fprintf
#include <stdlib.h> //malloc, free
//Convert escape sequences in strings
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)){
if(*(str+*len) == '\\'){
++(*len);
++(*column);
}else if(*(str+*len) == '\0'){
*len = 1;
fprintf(stderr, "Syntax error! %s (%i:%i)\n", "Unexpected EOF in string!", *row, *column);
rjp_free(root);
return NULL;
}else if(*(str+*len) == '\n'){
++(*row);
*column = 0;
}
}
if(*len == 0){
return NULL;
}
new_string = malloc(*len + 1);
new_string[*len] = 0;
for(int i = 0;*str != '"';++i,++str){
if(*str == '\\'){
++str;
switch(*str){
case '"':
new_string[i] = '"';
break;
case 'n':
new_string[i] = '\n';
break;
case 'r':
new_string[i] = '\r';
break;
case 'b':
new_string[i] = '\b';
break;
case '\\':
new_string[i] = '\\';
break;
case 't':
new_string[i] = '\t';
break;
case 'f':
new_string[i] = '\f';
break;
default:
new_string[i] = *str;
break;
}
}else{
new_string[i] = *str;
}
}
return new_string;
}
size_t rjp_escape_strcpy(char* dest, const char* src){
size_t j = 0;
for(size_t i = 0;src[i];++i){
switch(src[i]){
case '"':
dest[j++] = '\\';
dest[j++] = '"';
break;
case '\n':
dest[j++] = '\\';
dest[j++] = 'n';
break;
case '\r':
dest[j++] = '\\';
dest[j++] = 'r';
break;
case '\b':
dest[j++] = '\\';
dest[j++] = 'b';
break;
case '\\':
dest[j++] = '\\';
dest[j++] = '\\';
break;
case '\t':
dest[j++] = '\\';
dest[j++] = 't';
break;
case '\f':
dest[j++] = '\\';
dest[j++] = 'f';
break;
default:
dest[j++] = src[i];
break;
};
}
return j;
}
size_t rjp_escape_strlen(const char* str){
size_t count = 0;
for(size_t i = 0;str[i];++i){
switch(str[i]){
case '"':
case '\n':
case '\r':
case '\b':
case '\\':
case '\t':
case '\f':
++count;
//fallthrough
default:
++count;
};
}
return count;
}
size_t _rjp__array_strlen(RJP_value* arr){
size_t count = 2; //[]
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:
count += snprintf(NULL, 0, "%li", element_list->value.integer);
break;
case json_dfloat:
count += snprintf(NULL, 0, "%lf", element_list->value.dfloat);
break;
case json_boolean:
count += element_list->value.boolean ? 4 : 5;
break;
case json_null:
count += 4;
break;
case json_string:
count += element_list->value.string.length;
break;
case json_array:
count += _rjp__array_strlen(&element_list->value);
break;
case json_object:
count += _rjp__object_strlen(&element_list->value);
break;
};
if(element_list->next)
++count;
else
break;
}
return count;
}
size_t _rjp__object_strlen(RJP_value* root){
size_t count = 2; //{}
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:;
size_t new_count = _rjp__object_strlen(&member_list->value);
if(!new_count)
return 0;
count += new_count;
break;
};
if(member_list->next)
++count; //,
else
break;
}
return count;
}