cctools
Data Structures | Typedefs | Enumerations | Functions
jx.h File Reference

JSON Expressions (JX) library. More...

#include <stdint.h>
#include <assert.h>

Go to the source code of this file.

Data Structures

struct  jx_item
 JX item linked-list used by JX_ARRAY and jx::items. More...
 
struct  jx_pair
 JX key-value pairs used by JX_OBJECT and jx::pairs. More...
 
struct  jx
 JX value representing any expression type. More...
 

Typedefs

typedef struct jx *(* jx_eval_func_t )(const char *ident)
 Evaluation function. More...
 

Enumerations

enum  jx_type_t {
  JX_NULL,
  JX_BOOLEAN,
  JX_INTEGER,
  JX_DOUBLE,
  JX_STRING,
  JX_SYMBOL,
  JX_ARRAY,
  JX_OBJECT
}
 JX atomic type. More...
 

Functions

struct jxjx_null ()
 Create a JX null value. More...
 
struct jxjx_boolean (int boolean_value)
 Create a JX boolean value. More...
 
struct jxjx_integer (jx_int_t integer_value)
 Create a JX integer value. More...
 
struct jxjx_double (double double_value)
 Create a JX floating point value. More...
 
struct jxjx_string (const char *string_value)
 Create a JX string value. More...
 
struct jxjx_format (const char *fmt,...)
 Create a JX string value using prinf style formatting. More...
 
struct jxjx_symbol (const char *symbol_name)
 Create a JX symbol. More...
 
struct jxjx_array (struct jx_item *items)
 Create a JX array. More...
 
struct jxjx_arrayv (struct jx *value,...)
 Create a JX array with inline items. More...
 
struct jxjx_object (struct jx_pair *pairs)
 Create a JX object. More...
 
struct jx_pairjx_pair (struct jx *key, struct jx *value, struct jx_pair *next)
 Create a JX key-value pair. More...
 
struct jx_itemjx_item (struct jx *value, struct jx_item *next)
 Create a JX array item. More...
 
int jx_istype (struct jx *j, jx_type_t type)
 Test an expression's type. More...
 
int jx_equals (struct jx *j, struct jx *k)
 Test two expressions for equality. More...
 
struct jxjx_copy (struct jx *j)
 Duplicate an expression. More...
 
void jx_delete (struct jx *j)
 Delete an expression recursively. More...
 
void jx_pair_delete (struct jx_pair *p)
 Delete a key-value pair. More...
 
void jx_item_delete (struct jx_item *i)
 Delete an array item. More...
 
struct jxjx_remove (struct jx *object, struct jx *key)
 Remove a key-value pair from an object. More...
 
int jx_insert (struct jx *object, struct jx *key, struct jx *value)
 Insert a key-value pair into an object. More...
 
void jx_insert_integer (struct jx *object, const char *key, jx_int_t value)
 Insert an integer value into an object. More...
 
void jx_insert_double (struct jx *object, const char *key, double value)
 Insert a double value into an object. More...
 
void jx_insert_string (struct jx *object, const char *key, const char *value)
 Insert a string value into an object. More...
 
struct jxjx_lookup (struct jx *object, const char *key)
 Search for a arbitrary item in an object. More...
 
const char * jx_lookup_string (struct jx *object, const char *key)
 Search for a string item in an object. More...
 
jx_int_t jx_lookup_integer (struct jx *object, const char *key)
 Search for an integer item in an object. More...
 
double jx_lookup_double (struct jx *object, const char *key)
 Search for a double item in an object. More...
 
void jx_array_insert (struct jx *array, struct jx *value)
 Insert an item at the beginning of an array. More...
 
void jx_array_append (struct jx *array, struct jx *value)
 Append an item at the end of an array. More...
 
int jx_is_constant (struct jx *j)
 Determine if an expression is constant. More...
 
void jx_export (struct jx *j)
 Export a jx object as a set of environment variables. More...
 
struct jxjx_evaluate (struct jx *j, jx_eval_func_t evaluator)
 Evaluate an expression. More...
 

Detailed Description

JSON Expressions (JX) library.

This module implements extended JSON expressions as a C library. We use our own custom library to avoid external dependencies and to implement various extensions to strict JSON.

For example, the following bit of code:

struct jx * obj = jx_object(
        jx_pair(
                jx_string("hello"),
                jx_string("world"),
                0)
        );
jx_print_file(jx,stdout);
jx_delete(jx);

Will create the following output:

{ "hello" : "world" }
See Also
jx_parse.h
jx_print.h

Typedef Documentation

typedef struct jx*(* jx_eval_func_t)(const char *ident)

Evaluation function.

To use jx_evaluate, the caller must define a function of type jx_eval_func_t which accepts a symbol name and returns a JX value.

Enumeration Type Documentation

enum jx_type_t

JX atomic type.

Enumerator
JX_NULL 

null value

JX_BOOLEAN 

true or false

JX_INTEGER 

integer value

JX_DOUBLE 

floating point value

JX_STRING 

string value

JX_SYMBOL 

variable identifier

JX_ARRAY 

array containing values

JX_OBJECT 

object containing key-value pairs

Function Documentation

struct jx* jx_null ( )

Create a JX null value.

Returns
A JX expression.
struct jx* jx_boolean ( int  boolean_value)

Create a JX boolean value.

Parameters
boolean_valueA C boolean value.
Returns
A JX boolean value.
struct jx* jx_integer ( jx_int_t  integer_value)

Create a JX integer value.

Parameters
integer_valueA C integer.
Returns
a JX integer value.
struct jx* jx_double ( double  double_value)

Create a JX floating point value.

Parameters
double_valueA C double precision floating point.
Returns
a JX double value.
struct jx* jx_string ( const char *  string_value)

Create a JX string value.

Parameters
string_valueA C string, which will be duplciated via strdup().
Returns
A JX string value.
struct jx* jx_format ( const char *  fmt,
  ... 
)

Create a JX string value using prinf style formatting.

Parameters
fmtA printf-style format string, followed by matching arguments.
Returns
A JX string value.
struct jx* jx_symbol ( const char *  symbol_name)

Create a JX symbol.

Note that symbols are an extension to the JSON standard. A symbol is a reference to an external variable, which can be resolved by using jx_evaluate.

Parameters
symbol_nameA C string.
Returns
A JX expression.
struct jx* jx_array ( struct jx_item items)

Create a JX array.

Parameters
itemsA linked list of jx_item values.
Returns
A JX array.
struct jx* jx_arrayv ( struct jx value,
  ... 
)

Create a JX array with inline items.

Parameters
itemOne or more items of the array must be given, terminated with a null value.
Returns
A JX array.
struct jx* jx_object ( struct jx_pair pairs)

Create a JX object.

Parameters
pairsA linked list of jx_pair key-value pairs.
Returns
a JX object.
struct jx_pair* jx_pair ( struct jx key,
struct jx value,
struct jx_pair next 
)

Create a JX key-value pair.

Parameters
keyThe key.
valueThe value.
nextThe next item in the linked list.
Returns
A key-value pair.
struct jx_item* jx_item ( struct jx value,
struct jx_item next 
)

Create a JX array item.

Parameters
valueThe value of this item.
nextThe next item in the linked list.
Returns
An array item.
int jx_istype ( struct jx j,
jx_type_t  type 
)

Test an expression's type.

Parameters
jAn expression.
typeThe desired type.
Returns
True if the expression type matches, false otherwise.
int jx_equals ( struct jx j,
struct jx k 
)

Test two expressions for equality.

Parameters
jA constant expression.
kA constant expression.
Returns
True if equal, false if not.
struct jx* jx_copy ( struct jx j)

Duplicate an expression.

Parameters
jAn expression.
Returns
A copy of the expression, which must be deleted by jx_delete
void jx_delete ( struct jx j)

Delete an expression recursively.

Parameters
jAn expression to delete.
void jx_pair_delete ( struct jx_pair p)

Delete a key-value pair.

Parameters
pThe key-value pair to delete.
void jx_item_delete ( struct jx_item i)

Delete an array item.

Parameters
iThe array item to delete.
struct jx* jx_remove ( struct jx object,
struct jx key 
)

Remove a key-value pair from an object.

Parameters
objectThe object.
keyThe key.
Returns
The corresponding value, or null if it is not present.
int jx_insert ( struct jx object,
struct jx key,
struct jx value 
)

Insert a key-value pair into an object.

Parameters
objectThe object.
keyThe key.
valueThe value.
Returns
True on success, false on failure. Failure can only occur if the object is not a JX_OBJECT.
void jx_insert_integer ( struct jx object,
const char *  key,
jx_int_t  value 
)

Insert an integer value into an object.

Parameters
objectThe object
keyThe key represented as a C string
valueThe integer value.
void jx_insert_double ( struct jx object,
const char *  key,
double  value 
)

Insert a double value into an object.

Parameters
objectThe object
keyThe key represented as a C string
valueThe double value.
void jx_insert_string ( struct jx object,
const char *  key,
const char *  value 
)

Insert a string value into an object.

Parameters
objectThe object
keyThe key represented as a C string
valueThe C string value.
struct jx* jx_lookup ( struct jx object,
const char *  key 
)

Search for a arbitrary item in an object.

The key is an ordinary string value.

Parameters
objectThe object in which to search.
keyThe string key to match.
Returns
The value of the matching pair, or null if none is found.
const char* jx_lookup_string ( struct jx object,
const char *  key 
)

Search for a string item in an object.

The key is an ordinary string value.

Parameters
objectThe object in which to search.
keyThe string key to match.
Returns
The C string value of the matching object, or null if it is not found, or is not a string.
jx_int_t jx_lookup_integer ( struct jx object,
const char *  key 
)

Search for an integer item in an object.

The key is an ordinary string value.

Parameters
objectThe object in which to search.
keyThe string key to match.
Returns
The integer value of the matching object, or zero if it is not found, or is not an integer.
double jx_lookup_double ( struct jx object,
const char *  key 
)

Search for a double item in an object.

The key is an ordinary string value.

Parameters
objectThe object in which to search.
keyThe string key to match.
Returns
The double value of the matching object, or null if it is not found, or is not a double.
void jx_array_insert ( struct jx array,
struct jx value 
)

Insert an item at the beginning of an array.

Parameters
arrayThe array to modify.
valueThe value to insert.
void jx_array_append ( struct jx array,
struct jx value 
)

Append an item at the end of an array.

Parameters
arrayThe array to modify.
valueThe value to append.
int jx_is_constant ( struct jx j)

Determine if an expression is constant.

Traverses the expression recursively, and returns true if it consists only of constant values, arrays, and objects.

Parameters
jThe expression to evaluate.
Returns
True if constant.
void jx_export ( struct jx j)

Export a jx object as a set of environment variables.

Parameters
jA JX_OBJECT.
struct jx* jx_evaluate ( struct jx j,
jx_eval_func_t  evaluator 
)

Evaluate an expression.

Traverses the expression recursively, and for each value of type JX_SYMBOL, invokes the evaluator function to replace it with a constant value.

Parameters
jThe expression to evaluate.
evaluatorThe evaluating function.
Returns
A newly created result expression, which must be deleted with jx_delete.