regify utility  2.0.0-0
JSON

A simplified wrapper of the yajl library. More...

Macros

#define RU_JSON_PRETTIFY   0x01
 
#define RU_JSON_ESCAPE_SLASH   0x02
 

Typedefs

typedef void * ruJson
 An opaque type representing a JSON object. More...
 

Functions

RUAPI ruJson ruJsonFree (ruJson rj)
 
RUAPI ruJson ruJsonNew (int32_t flags, trans_chars ident)
 
RUAPI ruJson ruJsonStart (bool map)
 
RUAPI int32_t ruJsonStartMap (ruJson rj)
 
RUAPI int32_t ruJsonEndMap (ruJson rj)
 
RUAPI int32_t ruJsonStartArray (ruJson rj)
 
RUAPI int32_t ruJsonEndArray (ruJson rj)
 
RUAPI int32_t ruJsonSetInt (ruJson rj, int64_t val)
 
RUAPI int32_t ruJsonSetDouble (ruJson rj, double val)
 
RUAPI int32_t ruJsonSetStr (ruJson rj, trans_chars val)
 
RUAPI int32_t ruJsonSetKeyStr (ruJson rj, trans_chars key, trans_chars val)
 
RUAPI int32_t ruJsonSetKeyInt (ruJson rj, trans_chars key, int64_t val)
 
RUAPI int32_t ruJsonSetKeyDouble (ruJson rj, trans_chars key, double val)
 
RUAPI int32_t ruJsonStartKeyMap (ruJson rj, trans_chars key)
 
RUAPI int32_t ruJsonStartKeyArray (ruJson rj, trans_chars key)
 
RUAPI int32_t ruJsonWrite (ruJson rj, perm_chars *output)
 
RUAPI ruJson ruJsonParse (trans_chars jsonStr, int32_t *status)
 
RUAPI rusize ruJsonArrayLen (ruJson rj, int32_t *status)
 
RUAPI perm_chars ruJsonStr (ruJson rj, int32_t *status)
 
RUAPI int64_t ruJsonParseInt (ruJson rj, int32_t *status)
 
RUAPI int64_t ruJsonInt (ruJson rj, int32_t *status)
 
RUAPI bool ruJsonParseBool (ruJson rj, int32_t *status)
 
RUAPI bool ruJsonBool (ruJson rj, int32_t *status)
 
RUAPI ruList ruJsonKeys (ruJson rj, int32_t *status)
 Returns an ruList of keys in the current map. More...
 
RUAPI perm_chars ruJsonKeyStr (ruJson rj, trans_chars key, int32_t *status)
 
RUAPI alloc_chars ruJsonKeyToStr (ruJson rj, trans_chars key, int32_t *status)
 
RUAPI alloc_chars ruJsonKeyStrDup (ruJson rj, trans_chars key, int32_t *status)
 
RUAPI int64_t ruJsonKeyParseInt (ruJson rj, trans_chars key, int32_t *status)
 
RUAPI int64_t ruJsonKeyInt (ruJson rj, trans_chars key, int32_t *status)
 
RUAPI double ruJsonKeyDouble (ruJson rj, trans_chars key, int32_t *status)
 
RUAPI bool ruJsonKeyParseBool (ruJson rj, trans_chars key, int32_t *status)
 
RUAPI bool ruJsonKeyBool (ruJson rj, trans_chars key, int32_t *status)
 
RUAPI ruJson ruJsonKeyMap (ruJson rj, trans_chars key, int32_t *status)
 
RUAPI ruJson ruJsonKeyArray (ruJson rj, trans_chars key, int32_t *status)
 
RUAPI perm_chars ruJsonIdxStr (ruJson rj, rusize index, int32_t *status)
 
RUAPI alloc_chars ruJsonIdxStrDup (ruJson rj, rusize index, int32_t *status)
 
RUAPI int64_t ruJsonIdxParseInt (ruJson rj, rusize index, int32_t *status)
 
RUAPI int64_t ruJsonIdxInt (ruJson rj, rusize index, int32_t *status)
 
RUAPI bool ruJsonIdxParseBool (ruJson rj, rusize index, int32_t *status)
 
RUAPI bool ruJsonIdxBool (ruJson rj, rusize index, int32_t *status)
 
RUAPI double ruJsonIdxDouble (ruJson rj, rusize index, int32_t *status)
 
RUAPI ruJson ruJsonIdxMap (ruJson rj, rusize index, int32_t *status)
 
RUAPI ruJson ruJsonIdxArray (ruJson rj, rusize index, int32_t *status)
 

Detailed Description

A simplified wrapper of the yajl library.

This is a wrapper of https://lloyd.github.io/yajl/ It wraps the the yajl_tree API for parsing small content, and the yajl_gen API for generating JSON. See ruJsonNew and ruJsonStart for generation samples and ruJsonParse for a parsing sample.

Macro Definition Documentation

◆ RU_JSON_ESCAPE_SLASH

#define RU_JSON_ESCAPE_SLASH   0x02

◆ RU_JSON_PRETTIFY

#define RU_JSON_PRETTIFY   0x01

Typedef Documentation

◆ ruJson

typedef void* ruJson

An opaque type representing a JSON object.

Function Documentation

◆ ruJsonArrayLen()

RUAPI rusize ruJsonArrayLen ( ruJson  rj,
int32_t *  status 
)

Return array size of underlying ruJson reference

Parameters
rjruJson reference pointing to an array
statuswhere the RUE_OK on success or an error code will be stored.
Returns
size of the array

◆ ruJsonBool()

RUAPI bool ruJsonBool ( ruJson  rj,
int32_t *  status 
)

Return boolean of underlying ruJson reference. This is only used if the JSON object consists of only a boolean.

Parameters
rjruJson reference pointing to a boolean.
statuswhere the RUE_OK on success or an error code will be stored.
Returns
boolean in question and false on error.

◆ ruJsonEndArray()

RUAPI int32_t ruJsonEndArray ( ruJson  rj)

End the array context started with ruJsonStartArray or ruJsonStartKeyArray

Parameters
rjJSON ctx to work on
Returns
RUE_OK on success else an error code

◆ ruJsonEndMap()

RUAPI int32_t ruJsonEndMap ( ruJson  rj)

End the map context started with ruJsonStartMap or ruJsonStartKeyMap

Parameters
rjJSON ctx to work on
Returns
RUE_OK on success else an error code

◆ ruJsonFree()

RUAPI ruJson ruJsonFree ( ruJson  rj)

Free the given ruJson object

Parameters
rjJSON ctx to free
Returns
NULL

◆ ruJsonIdxArray()

RUAPI ruJson ruJsonIdxArray ( ruJson  rj,
rusize  index,
int32_t *  status 
)

Retuens a ruJson reference to the array under given array index

Parameters
rjruJson in question.
indexindex under which the array resides
statuswhere the RUE_OK on success or an error code will be stored.
Returns
ruJson referencing the array

◆ ruJsonIdxBool()

RUAPI bool ruJsonIdxBool ( ruJson  rj,
rusize  index,
int32_t *  status 
)

Return boolean of the indexed element from underlying ruJson array reference.

Parameters
rjruJson in question.
indexindex under which the string resides
statuswhere the RUE_OK on success or an error code will be stored.
Returns
boolean in question and false on error.

◆ ruJsonIdxDouble()

RUAPI double ruJsonIdxDouble ( ruJson  rj,
rusize  index,
int32_t *  status 
)

Return double of the indexed element from underlying ruJson array reference.

Parameters
rjruJson in question.
indexindex under which the double resides
statuswhere the RUE_OK on success or an error code will be stored.
Returns
double in question.

◆ ruJsonIdxInt()

RUAPI int64_t ruJsonIdxInt ( ruJson  rj,
rusize  index,
int32_t *  status 
)

Return integer of the indexed element from underlying ruJson array reference.

Parameters
rjruJson in question.
indexindex under which the integer resides
statuswhere the RUE_OK on success or an error code will be stored.
Returns
integer in question.

◆ ruJsonIdxMap()

RUAPI ruJson ruJsonIdxMap ( ruJson  rj,
rusize  index,
int32_t *  status 
)

Retuens a ruJson reference to the map under given array index

Parameters
rjruJson in question.
indexindex under which the map resides
statuswhere the RUE_OK on success or an error code will be stored.
Returns
ruJson referencing the map

◆ ruJsonIdxParseBool()

RUAPI bool ruJsonIdxParseBool ( ruJson  rj,
rusize  index,
int32_t *  status 
)

Return parsed boolean of the indexed element from underlying ruJson array reference. The boolean may also be an integer 0, 1 or a quoted case insensitive true or false.

Parameters
rjruJson in question.
indexindex under which the string resides
statuswhere the RUE_OK on success or an error code will be stored.
Returns
boolean in question and false on error.

◆ ruJsonIdxParseInt()

RUAPI int64_t ruJsonIdxParseInt ( ruJson  rj,
rusize  index,
int32_t *  status 
)

Return parsed integer of the indexed element from underlying ruJson array reference. The integer may be a quoted string.

Parameters
rjruJson in question.
indexindex under which the string resides
statuswhere the RUE_OK on success or an error code will be stored.
Returns
integer in question.

◆ ruJsonIdxStr()

RUAPI perm_chars ruJsonIdxStr ( ruJson  rj,
rusize  index,
int32_t *  status 
)

Return string of the indexed element from underlying ruJson array reference.

Parameters
rjruJson in question.
indexindex under which the string resides
statuswhere the RUE_OK on success or an error code will be stored.
Returns
string in question. Belongs to the ruJson object.

◆ ruJsonIdxStrDup()

RUAPI alloc_chars ruJsonIdxStrDup ( ruJson  rj,
rusize  index,
int32_t *  status 
)

Return string copy of the indexed element from underlying ruJson array reference.

Parameters
rjruJson in question.
indexindex under which the string resides
statuswhere the RUE_OK on success or an error code will be stored.
Returns
string in question. Must be freed by the caller.

◆ ruJsonInt()

RUAPI int64_t ruJsonInt ( ruJson  rj,
int32_t *  status 
)

Return integer of underlying ruJson reference. This is only used if the JSON object consists of only an integer.

Parameters
rjruJson reference pointing to an integer.
statuswhere the RUE_OK on success or an error code will be stored.
Returns
integer in question

◆ ruJsonKeyArray()

RUAPI ruJson ruJsonKeyArray ( ruJson  rj,
trans_chars  key,
int32_t *  status 
)

Retuens a ruJson reference to the array under given key

Parameters
rjruJson in question.
keykey under which array resides
statuswhere the RUE_OK on success or an error code will be stored.
Returns
ruJson referencing the array

◆ ruJsonKeyBool()

RUAPI bool ruJsonKeyBool ( ruJson  rj,
trans_chars  key,
int32_t *  status 
)

Return boolean of the key element from underlying ruJson map reference.

Parameters
rjruJson in question.
keykey under which boolean resides
statuswhere the RUE_OK on success or an error code will be stored.
Returns
boolean in question and false on error.

◆ ruJsonKeyDouble()

RUAPI double ruJsonKeyDouble ( ruJson  rj,
trans_chars  key,
int32_t *  status 
)

Return double of the key element from underlying ruJson map reference.

Parameters
rjruJson in question.
keykey under which double resides
statuswhere the RUE_OK on success or an error code will be stored.
Returns
double in question.

◆ ruJsonKeyInt()

RUAPI int64_t ruJsonKeyInt ( ruJson  rj,
trans_chars  key,
int32_t *  status 
)

Return integer of the key element from underlying ruJson map reference.

Parameters
rjruJson in question.
keykey under which integer resides
statuswhere the RUE_OK on success or an error code will be stored.
Returns
integer in question.

◆ ruJsonKeyMap()

RUAPI ruJson ruJsonKeyMap ( ruJson  rj,
trans_chars  key,
int32_t *  status 
)

Retuens a ruJson reference to the map under given key

Parameters
rjruJson in question.
keykey under which map resides
statuswhere the RUE_OK on success or an error code will be stored.
Returns
ruJson referencing the map

◆ ruJsonKeyParseBool()

RUAPI bool ruJsonKeyParseBool ( ruJson  rj,
trans_chars  key,
int32_t *  status 
)

Return parsed boolean of the key element from underlying ruJson map reference. The boolean may also be an integer 0, 1 or a quoted case insensitive true or false.

Parameters
rjruJson in question.
keykey under which boolean resides
statuswhere the RUE_OK on success or an error code will be stored.
Returns
boolean in question and false on error.

◆ ruJsonKeyParseInt()

RUAPI int64_t ruJsonKeyParseInt ( ruJson  rj,
trans_chars  key,
int32_t *  status 
)

Return parsed integer of the key element from underlying ruJson map reference. The integer may be a quoted string.

Parameters
rjruJson in question.
keykey under which integer resides
statuswhere the RUE_OK on success or an error code will be stored.
Returns
integer in question.

◆ ruJsonKeys()

RUAPI ruList ruJsonKeys ( ruJson  rj,
int32_t *  status 
)

Returns an ruList of keys in the current map.

Parameters
rjruJson in question.
statuswhere the RUE_OK on success or an error code will be stored.
Returns
The list of map keys. Caller to free with ruListFree. List is only valid while given ruJson object is there.

◆ ruJsonKeyStr()

RUAPI perm_chars ruJsonKeyStr ( ruJson  rj,
trans_chars  key,
int32_t *  status 
)

Return string of the key element from underlying ruJson map reference.

Parameters
rjruJson in question.
keykey under which string resides
statuswhere the RUE_OK on success or an error code will be stored.
Returns
string in question. Belongs to the ruJson object.

◆ ruJsonKeyStrDup()

RUAPI alloc_chars ruJsonKeyStrDup ( ruJson  rj,
trans_chars  key,
int32_t *  status 
)

Return string copy of the key element from underlying ruJson map reference.

Parameters
rjruJson in question.
keykey under which string resides
statuswhere the RUE_OK on success or an error code will be stored.
Returns
string in question. Must be freed by the caller.

◆ ruJsonKeyToStr()

RUAPI alloc_chars ruJsonKeyToStr ( ruJson  rj,
trans_chars  key,
int32_t *  status 
)

Return string version of the key element from underlying ruJson map.

Parameters
rjruJson in question.
keykey under which value resides
statuswhere the RUE_OK on success or an error code will be stored.
Returns
string version of value in question. Caller must free.

◆ ruJsonNew()

RUAPI ruJson ruJsonNew ( int32_t  flags,
trans_chars  ident 
)

Creates a new ruJson object for generating JSON

Parameters
flagsOptional ORing of RU_JSON_PRETTIFY and RU_JSON_ESCAPE_SLASH
identOptional indent string to use when flags has RU_JSON_PRETTIFY set.

Example:

ruJson jsn = NULL;
do{
if (!jsn) break;
if (RUE_OK != ruJsonStartArray(jsn)) break;
if (RUE_OK != ruJsonSetStr(jsn, "value")) break;
if (RUE_OK != ruJsonSetInt(jsn, 23)) break;
if (RUE_OK != ruJsonStartMap(jsn)) break;
if (RUE_OK != ruJsonSetKeyStr(jsn, "key", "other value")) break;
if (RUE_OK != ruJsonSetKeyInt(jsn, "num", 42)) break;
if (RUE_OK != ruJsonEndMap(jsn)) break;
if (RUE_OK != ruJsonEndArray(jsn)) break;
perm_chars str = NULL;
if (RUE_OK != ruJsonWrite(jsn, &str)) break;
printf("JSON: '%s'", str);
} while(0);
RUAPI int32_t ruJsonWrite(ruJson rj, perm_chars *output)
RUAPI int32_t ruJsonSetKeyInt(ruJson rj, trans_chars key, int64_t val)
RUAPI int32_t ruJsonStartMap(ruJson rj)
RUAPI int32_t ruJsonEndMap(ruJson rj)
RUAPI int32_t ruJsonSetStr(ruJson rj, trans_chars val)
RUAPI int32_t ruJsonEndArray(ruJson rj)
RUAPI int32_t ruJsonSetInt(ruJson rj, int64_t val)
RUAPI ruJson ruJsonNew(int32_t flags, trans_chars ident)
RUAPI int32_t ruJsonSetKeyStr(ruJson rj, trans_chars key, trans_chars val)
void * ruJson
An opaque type representing a JSON object.
Definition: json.h:43
RUAPI ruJson ruJsonFree(ruJson rj)
RUAPI int32_t ruJsonStartArray(ruJson rj)
#define RU_JSON_PRETTIFY
Definition: json.h:45
const char * perm_chars
A permanent NULL terminated string pointer.
Definition: regify-util.h:176
#define RUE_OK
Definition: errors.h:66
Returns
ctx to generate JSON with.

◆ ruJsonParse()

RUAPI ruJson ruJsonParse ( trans_chars  jsonStr,
int32_t *  status 
)

Parse given string into a ruJson object.

Example:

ruJson jsn = NULL;
int32_t ret;
do{
perm_chars jsonStr = "{"
"\"map\": {"
"\"key\": \"2342\", "
"\"num\": 2342, "
"\"dbl\": 23.42"
"}, "
"\"arr\": ["
"2342, \"2342\", 23.42"
"]"
"}";
jsn = ruJsonParse(jsonStr, &ret);
if (ret != RUE_OK) break;
// the map
ruJson jm = ruJsonKeyMap(jsn, "map", &ret);
if (ret != RUE_OK) break;
perm_chars str = ruJsonKeyStr(jm, "key", &ret);
if (ret != RUE_OK) break;
printf("str: '%s'", str);
int64_t i64 = ruJsonKeyInt(jm, "num", &ret);
if (ret != RUE_OK) break;
printf("int: %ld", i64);
double db = ruJsonKeyDouble(jm, "dbl", &ret);
if (ret != RUE_OK) break;
printf("float: %f", db);
// the array
ruJson ja = ruJsonKeyArray(jsn, "arr", &ret);
if (ret != RUE_OK) break;
i64 = ruJsonIdxInt(ja, 0, &ret);
if (ret != RUE_OK) break;
printf("int: %ld", i64);
str = ruJsonIdxStr(ja, 1, &ret);
if (ret != RUE_OK) break;
printf("str: '%s'", str);
i64 = ruJsonIdxParseInt(ja, 1, &ret);
if (ret != RUE_OK) break;
printf("int: %ld", i64);
db = ruJsonIdxDouble(ja, 2, &ret);
if (ret != RUE_OK) break;
printf("float: %f", db);
} while(0);
RUAPI double ruJsonIdxDouble(ruJson rj, rusize index, int32_t *status)
RUAPI perm_chars ruJsonIdxStr(ruJson rj, rusize index, int32_t *status)
RUAPI ruJson ruJsonKeyArray(ruJson rj, trans_chars key, int32_t *status)
RUAPI int64_t ruJsonIdxParseInt(ruJson rj, rusize index, int32_t *status)
RUAPI int64_t ruJsonKeyInt(ruJson rj, trans_chars key, int32_t *status)
RUAPI ruJson ruJsonParse(trans_chars jsonStr, int32_t *status)
RUAPI double ruJsonKeyDouble(ruJson rj, trans_chars key, int32_t *status)
RUAPI ruJson ruJsonKeyMap(ruJson rj, trans_chars key, int32_t *status)
RUAPI perm_chars ruJsonKeyStr(ruJson rj, trans_chars key, int32_t *status)
RUAPI int64_t ruJsonIdxInt(ruJson rj, rusize index, int32_t *status)
Parameters
jsonStr
statuswhere the RUE_OK on success or an error code will be stored.
Returns
ruJson object to be freed with ruJsonFree after use.

◆ ruJsonParseBool()

RUAPI bool ruJsonParseBool ( ruJson  rj,
int32_t *  status 
)

Return parsed boolean of underlying ruJson reference. The boolean may also be an integer 0, 1 or a quoted case insensitive true or false.

Parameters
rjruJson reference pointing to an integer.
statuswhere the RUE_OK on success or an error code will be stored.
Returns
boolean in question and false on error.

◆ ruJsonParseInt()

RUAPI int64_t ruJsonParseInt ( ruJson  rj,
int32_t *  status 
)

Return parsed integer of underlying ruJson reference. This is only used if the JSON object consists of only an integer or quoted integer.

Parameters
rjruJson reference pointing to an integer.
statuswhere the RUE_OK on success or an error code will be stored.
Returns
integer in question

◆ ruJsonSetDouble()

RUAPI int32_t ruJsonSetDouble ( ruJson  rj,
double  val 
)

Create an double entry in the current context

Parameters
rjJSON ctx to work on
valdouble to set
Returns
RUE_OK on success else an error code

◆ ruJsonSetInt()

RUAPI int32_t ruJsonSetInt ( ruJson  rj,
int64_t  val 
)

Create an integer entry in the current context

Parameters
rjJSON ctx to work on
valinteger to set
Returns
RUE_OK on success else an error code

◆ ruJsonSetKeyDouble()

RUAPI int32_t ruJsonSetKeyDouble ( ruJson  rj,
trans_chars  key,
double  val 
)

Create a double key value entry in the current context

Parameters
rjJSON ctx to work on
keykey string
valdouble to set
Returns
RUE_OK on success else an error code

◆ ruJsonSetKeyInt()

RUAPI int32_t ruJsonSetKeyInt ( ruJson  rj,
trans_chars  key,
int64_t  val 
)

Create a integer key value entry in the current context

Parameters
rjJSON ctx to work on
keykey string
valinteger to set
Returns
RUE_OK on success else an error code

◆ ruJsonSetKeyStr()

RUAPI int32_t ruJsonSetKeyStr ( ruJson  rj,
trans_chars  key,
trans_chars  val 
)

Create a string key value entry in the current context

Parameters
rjJSON ctx to work on
keykey string
valstring to set
Returns
RUE_OK on success else an error code

◆ ruJsonSetStr()

RUAPI int32_t ruJsonSetStr ( ruJson  rj,
trans_chars  val 
)

Create a string entry in the current context

Parameters
rjJSON ctx to work on
valstring to set
Returns
RUE_OK on success else an error code

◆ ruJsonStart()

RUAPI ruJson ruJsonStart ( bool  map)

Creates a new ruJson object with a top level container for generating JSON.

Example:

ruJson jsn = NULL;
do{
jsn = ruJsonStart(true);
if (!jsn) break;
if (RUE_OK != ruJsonSetKeyStr(jsn, "key", "value")) break;
if (RUE_OK != ruJsonSetKeyInt(jsn, "num", 23)) break;
perm_chars str = NULL;
if (RUE_OK != ruJsonWrite(jsn, &str)) break;
printf("JSON: '%s'", str);
} while(0);
RUAPI ruJson ruJsonStart(bool map)

The created top level array or map will be automatically closed before writing

Parameters
maptrue to create a map false for an array
Returns
ctx to generate JSON with.

◆ ruJsonStartArray()

RUAPI int32_t ruJsonStartArray ( ruJson  rj)

Start a new array in the current context

Parameters
rjJSON ctx to work on
Returns
RUE_OK on success else an error code

◆ ruJsonStartKeyArray()

RUAPI int32_t ruJsonStartKeyArray ( ruJson  rj,
trans_chars  key 
)

Start a new array under given key in the current context

Parameters
rjJSON ctx to work on
keykey string
Returns
RUE_OK on success else an error code

◆ ruJsonStartKeyMap()

RUAPI int32_t ruJsonStartKeyMap ( ruJson  rj,
trans_chars  key 
)

Start a new map under given key in the current context

Parameters
rjJSON ctx to work on
keykey string
Returns
RUE_OK on success else an error code

◆ ruJsonStartMap()

RUAPI int32_t ruJsonStartMap ( ruJson  rj)

Start a new map in the current context

Parameters
rjJSON ctx to work on
Returns
RUE_OK on success else an error code

◆ ruJsonStr()

RUAPI perm_chars ruJsonStr ( ruJson  rj,
int32_t *  status 
)

Return string of underlying ruJson reference. This is only used if the JSON object consists of only a string.

Parameters
rjruJson reference pointing to a string
statuswhere the RUE_OK on success or an error code will be stored.
Returns
string in question. Belongs to the ruJson object.

◆ ruJsonWrite()

RUAPI int32_t ruJsonWrite ( ruJson  rj,
perm_chars output 
)

Generate a JSON string representing the given JOSN object

Parameters
rjJSON ctx to work on
outputwhere the string will be stored. User must not free. Valid until rj is freed with ruJsonFree.
Returns
RUE_OK on success else an error code