ujson
Complete and simple JSON reader and writer written in C
Data Structures | Macros | Functions
ujson_writer.h File Reference

A JSON writer. More...

#include <ujson_common.h>

Go to the source code of this file.

Data Structures

struct  ujson_writer
 A JSON writer. More...
 

Macros

#define UJSON_WRITER_INIT(vout, vout_priv)
 An ujson_writer initializer with default values. More...
 

Functions

ujson_writerujson_writer_file_open (const char *path)
 Allocates a JSON file writer. More...
 
int ujson_writer_file_close (ujson_writer *self)
 Closes and frees a JSON file writer. More...
 
static int ujson_writer_err (ujson_writer *self)
 Returns true if writer error happened. More...
 
int ujson_obj_start (ujson_writer *self, const char *id)
 Starts a JSON object. More...
 
int ujson_obj_finish (ujson_writer *self)
 Finishes a JSON object. More...
 
int ujson_arr_start (ujson_writer *self, const char *id)
 Starts a JSON array. More...
 
int ujson_arr_finish (ujson_writer *self)
 Finishes a JSON array. More...
 
int ujson_null_add (ujson_writer *self, const char *id)
 Adds a null value. More...
 
int ujson_int_add (ujson_writer *self, const char *id, long val)
 Adds an integer value. More...
 
int ujson_bool_add (ujson_writer *self, const char *id, int val)
 Adds a bool value. More...
 
int ujson_float_add (ujson_writer *self, const char *id, double val)
 Adds a float value. More...
 
int ujson_str_add (ujson_writer *self, const char *id, const char *str)
 Adds a string value. More...
 
int ujson_writer_finish (ujson_writer *self)
 Finalizes json writer. More...
 

Detailed Description

A JSON writer.

All the function that add values return zero on success and non-zero on a failure. Once an error has happened all subsequent attempts to add more values return with non-zero exit status immediatelly. This is designed so that we can add several values without checking each return value and only check if error has happened at the end of the sequence.

Failures may occur:

Definition in file ujson_writer.h.

Macro Definition Documentation

◆ UJSON_WRITER_INIT

#define UJSON_WRITER_INIT (   vout,
  vout_priv 
)
Value:
{ \
.err_print = UJSON_ERR_PRINT, \
.err_print_priv = UJSON_ERR_PRINT_PRIV, \
.out = vout, \
.out_priv = vout_priv \
}

An ujson_writer initializer with default values.

Parameters
voutA pointer to function to write out the data.
vout_privAn user pointer passed to the out function.
Returns
An ujson_writer initialized with default values.

Definition at line 53 of file ujson_writer.h.

Function Documentation

◆ ujson_arr_finish()

int ujson_arr_finish ( ujson_writer self)

Finishes a JSON array.

The call will fail if we are currenlty not writing out an array.

Parameters
selfA JSON writer.
Returns
Zero on success, non-zero otherwise.

◆ ujson_arr_start()

int ujson_arr_start ( ujson_writer self,
const char *  id 
)

Starts a JSON array.

For a top level array the id must be NULL, every other array has to have non-NULL id. The call will also fail if maximal recursion depth UJSON_RECURSION_MAX has been reached.

Parameters
selfA JSON writer.
idAn array name.
Returns
Zero on success, non-zero otherwise.

◆ ujson_bool_add()

int ujson_bool_add ( ujson_writer self,
const char *  id,
int  val 
)

Adds a bool value.

The id must be NULL inside of an array, and must be non-NULL inside of an object.

Parameters
selfA JSON writer.
idAn boolean value name.
valA boolean value.
Returns
Zero on success, non-zero otherwise.

◆ ujson_float_add()

int ujson_float_add ( ujson_writer self,
const char *  id,
double  val 
)

Adds a float value.

The id must be NULL inside of an array, and must be non-NULL inside of an object.

Parameters
selfA JSON writer.
idA floating point value name.
valA floating point value.
Returns
Zero on success, non-zero otherwise.

◆ ujson_int_add()

int ujson_int_add ( ujson_writer self,
const char *  id,
long  val 
)

Adds an integer value.

The id must be NULL inside of an array, and must be non-NULL inside of an object.

Parameters
selfA JSON writer.
idAn integer value name.
valAn integer value.
Returns
Zero on success, non-zero otherwise.

◆ ujson_null_add()

int ujson_null_add ( ujson_writer self,
const char *  id 
)

Adds a null value.

The id must be NULL inside of an array, and must be non-NULL inside of an object.

Parameters
selfA JSON writer.
idA null value name.
Returns
Zero on success, non-zero otherwise.

◆ ujson_obj_finish()

int ujson_obj_finish ( ujson_writer self)

Finishes a JSON object.

The call will fail if we are currenlty not writing out an object.

Parameters
selfA JSON writer.
Returns
Zero on success, non-zero otherwise.

◆ ujson_obj_start()

int ujson_obj_start ( ujson_writer self,
const char *  id 
)

Starts a JSON object.

For a top level object the id must be NULL, every other object has to have non-NULL id. The call will also fail if maximal recursion depth UJSON_RECURSION_MAX has been reached.

Parameters
selfA JSON writer.
idAn object name.
Returns
Zero on a success, non-zero otherwise.

◆ ujson_str_add()

int ujson_str_add ( ujson_writer self,
const char *  id,
const char *  str 
)

Adds a string value.

The id must be NULL inside of an array, and must be non-NULL inside of an object.

Parameters
selfA JSON writer.
idA string value name.
strAn UTF8 string value.
Returns
Zero on success, non-zero otherwise.

◆ ujson_writer_err()

static int ujson_writer_err ( ujson_writer self)
inlinestatic

Returns true if writer error happened.

Parameters
selfA JSON writer.
Returns
True if error has happened.

Definition at line 89 of file ujson_writer.h.

◆ ujson_writer_file_close()

int ujson_writer_file_close ( ujson_writer self)

Closes and frees a JSON file writer.

Parameters
selfA ujson_writer file writer.
Returns
Zero on success, non-zero on a failure and errno is set.

◆ ujson_writer_file_open()

ujson_writer* ujson_writer_file_open ( const char *  path)

Allocates a JSON file writer.

The call may fail either when file cannot be opened for writing or if allocation has failed. In all cases errno should be set correctly.

Parameters
pathA path to the file, file is opened for writing and created if it does not exist.
Returns
A ujson_writer pointer or NULL in a case of failure.

◆ ujson_writer_finish()

int ujson_writer_finish ( ujson_writer self)

Finalizes json writer.

Finalizes the json writer, throws possible errors through the error printing function.

Parameters
selfA JSON writer.
Returns
Overall error value.