Simple JSON for C
Typedefs | Enumerations | Functions
/home/jakob/git/jakgra/simple_json_for_c/s_json.h File Reference

Simple JSON for C. More...

#include <stddef.h>

Go to the source code of this file.

Typedefs

typedef struct s_json s_json_t
 

Enumerations

enum  s_json_err_t {
  S_JSON_OK = 0, S_JSON_NOT_FOUND, S_JSON_ERR_NO_MEM, S_JSON_ERR_PARSE,
  S_JSON_ERR_WRONG_JSON, S_JSON_ERR_WRONG_ROOT, S_JSON_ERR_INTERNAL
}
 

Functions

s_json_ts_json_init (const char *json_string, size_t json_string_len, s_json_err_t *rc)
 
int s_json_int (s_json_t *json, const char *json_path, int root_object_index, s_json_err_t *rc)
 
long s_json_long (s_json_t *json, const char *json_path, int root_object_index, s_json_err_t *rc)
 
double s_json_double (s_json_t *json, const char *json_path, int root_object_index, s_json_err_t *rc)
 
float s_json_float (s_json_t *json, const char *json_path, int root_object_index, s_json_err_t *rc)
 
int s_json_boolean (s_json_t *json, const char *json_path, int root_object_index, s_json_err_t *rc)
 
char * s_json_string (s_json_t *json, const char *json_path, int root_object_index, s_json_err_t *rc)
 
void s_json_string_raw (const char **string_raw, size_t *string_raw_length, s_json_t *json, const char *json_path, int root_object_index, s_json_err_t *rc)
 
int s_json_object (s_json_t *json, const char *json_path, int root_object_index, s_json_err_t *rc)
 
void s_json_cleanup (s_json_t *json)
 

Detailed Description

Simple JSON for C.

Typedef Documentation

◆ s_json_t

typedef struct s_json s_json_t

Opaque JSON document object.

Enumeration Type Documentation

◆ s_json_err_t

A Simple JSON error.

Enumerator
S_JSON_OK 

No error. Everything was successful

S_JSON_NOT_FOUND 

No results for the given jsonpath found in the JSON document

S_JSON_ERR_NO_MEM 

malloc() failed

S_JSON_ERR_PARSE 

Invalid JSON

S_JSON_ERR_WRONG_JSON 

The passed s_json_t object is NULL

S_JSON_ERR_WRONG_ROOT 

The passed root_object_index is less than 0

S_JSON_ERR_INTERNAL 

This shouldn't occur. If it does please file a bug report.

Function Documentation

◆ s_json_boolean()

int s_json_boolean ( s_json_t json,
const char *  json_path,
int  root_object_index,
s_json_err_t rc 
)

Parse true/false to (int)1/(int)0 See s_json_int()

◆ s_json_cleanup()

void s_json_cleanup ( s_json_t json)

Cleanup the memory resources associated with the given opaque s_json_t object.

◆ s_json_double()

double s_json_double ( s_json_t json,
const char *  json_path,
int  root_object_index,
s_json_err_t rc 
)

◆ s_json_float()

float s_json_float ( s_json_t json,
const char *  json_path,
int  root_object_index,
s_json_err_t rc 
)

◆ s_json_init()

s_json_t* s_json_init ( const char *  json_string,
size_t  json_string_len,
s_json_err_t rc 
)

Parse the json_string and return an opaque s_json_t object you later use to access json values. You should call s_json_cleanup() when you finish using the object.

Parameters
[in]json_stringThe json string to parse. It is not copied to new memory, so it has to exist at least as long as the s_json_t object exists.
[in]json_string_lenThe length of the json string.
[out]rcUsed for error handling. Can be NULL. See s_json_err_t for a list of possible errors.
Returns
NULL on error a valid s_json_t pointer otherwise. For better error handling user the rc parameter.

◆ s_json_int()

int s_json_int ( s_json_t json,
const char *  json_path,
int  root_object_index,
s_json_err_t rc 
)
Parameters
[in]jsonA valid/initialized s_json_t object.
[in]json_pathThe path of
[in]root_object_indexSet this to 0 if you wan't to search the whole document. If you wan't to search just part of it (inside an object or array) then use the s_json_object() function to get the index of the object/array to search inside of.
[out]rcUsed for error handling. Can be NULL. See s_json_err_t for a list of possible errors.
Returns
0 on error or the first integer found in json otherwise. For better error handling user the rc parameter.

◆ s_json_long()

long s_json_long ( s_json_t json,
const char *  json_path,
int  root_object_index,
s_json_err_t rc 
)

◆ s_json_object()

int s_json_object ( s_json_t json,
const char *  json_path,
int  root_object_index,
s_json_err_t rc 
)

See s_json_int()

Returns
0 on error or the object index of the first object found in json otherwise. You can use the returned object index in subsequent calls to s_json_*() functions to limit your queries to this object. This allows for less code and better performance.

◆ s_json_string()

char* s_json_string ( s_json_t json,
const char *  json_path,
int  root_object_index,
s_json_err_t rc 
)

See s_json_int()

Returns
a '\0' terminated C string that it allocated via malloc() or NULL on error. It's your responsibility to free it!

◆ s_json_string_raw()

void s_json_string_raw ( const char **  string_raw,
size_t *  string_raw_length,
s_json_t json,
const char *  json_path,
int  root_object_index,
s_json_err_t rc 
)

See s_json_int()

Parameters
[out]string_rawNON '\0' terminated string!!! You have to use the string_raw_length parameter to find the end of the string. The string is not malloc()-ed as it still uses the original json_string you passed into the s_json_init() function for the backing memory storage. So it is only valid as long as json_string is valid!!! Do not free() it.!!!
[out]string_raw_lengthThe length of the returned NON '\0' terminated string.

When an error happens the string_raw pointer is set to NULL and the string_raw_length is set to 0 and rc (if you didn't pass NULL to it) contains the specific error code.