Simple and dirty JSON processing library written in C. Made as hobby project, is not intended for use in production.
To use library you need to include header json.h
#include "/root-of-library/include/json.h"There two versions of parsers: json_parser for null-terminated string and json_parserNoNul for general string buffers. Both return status code and put their output in passed pointer. Input string should be in UTF-8.
json_Status json_parser(const char* input, json_Value* output);- Return value - status code
input- null-terminated input stringoutput- pointer to struct of typejson_Valuein which parser will put root value node (e.g. array or object) of json document
json_Status json_parserNoNul(const char* input, size_t len, json_Value* output);- Return value - status code
input- input stringlen- length of input stringoutput- pointer to struct of typejson_Valuein which parser will put root node (e.g. array or object) of value tree of json document
char* jsonStringNulTerm = "{\"json\": []}"; // null-terminated string
json_Value root;
json_Status status = json_parser(jsonStringNulTerm, &root);char jsonStringNoNulTerm[2] = {'{', '}'}; // not null-terminated
json_Value root;
json_Status status = json_parserNoNul(jsonStringNoNulTerm, 2, &root);Converting JSON value tree into string is done by json_stringifier. It returns status code and puts its output into dynamically allocated char buffer. Its address and size are being put into argument pointers. Output string is in UTF-8. String is null-terminated. If some JSON strings in tree contain NUL characters, they will be added to output as any other character.
json_Status json_stringify(json_Value* value, char** output, size_t* len);- Return value - status code
value- input JSON valueoutput- pointer tocharpointer in which function will put address of output string bufferlen- pointer to variable in which function will put length of output string
char* output = NULL;
size_t outputLen = 0;
json_Status status = json_stringifier(&root, &output, &outputLen);Represented by enum of type json_Status
json_status_OK- returned if function executed normallyjson_status_InvalidInput- returned if function found that input is invalid (NOTE: validation is not guaranteed, parser may accept some invalid input strings)json_status_AllocError- returned if there is error from memory allocator (e.g. out of memory situation)json_status_ElemNotExist- returned if asked element does not exist (NOTE: currently no function uses it)
Represented by struct of type json_Value It has two fields:
type- type of JSON value. Represented by enumjson_ValueType, its values arejson_Object,json_Array,json_String,json_Number,json_Boolean,json_Null, they associate with built-in JSON types*value- value of given type. Represented by unionjson_Data, its fields are:number- floating point number of typedouble, valid iftypeisjson_Numberboolean- boolean value of type bool (from standard library), valid iftypeisjson_Booleanstring- pointer to null-terminated string (chararray), valid iftypeisjson_Stringarr- pointer to struct of typeds_Vector_valuewhich is dynamic vector (fromdata_structureslibrary) containing other JSON values, valid iftypeisjson_Arrayobj- pointer to struct of typeds_um_Map_valuewhich is hash map (fromdata_structureslibrary) containing other JSON values, valid iftypeisjson_Object