Group PJ_JSON

group PJ_JSON

This API implements JSON file format according to RFC 4627. It can be used to parse, write, and manipulate JSON documents.

Typedefs

typedef pj_status_t (*pj_json_writer)(const char *s, unsigned size, void *user_data)

Type of function callback to write JSON document in pj_json_writef().

Param s:

The string to be written to the document.

Param size:

The length of the string

Param user_data:

User data that was specified to pj_json_writef()

Return:

If the callback returns non-PJ_SUCCESS, it will stop the pj_json_writef() function and this error will be returned to caller.

Enums

enum pj_json_val_type

Type of JSON value.

Values:

enumerator PJ_JSON_VAL_NULL

Null value (null)

enumerator PJ_JSON_VAL_BOOL

Boolean value (true, false)

enumerator PJ_JSON_VAL_NUMBER

Numeric (float or fixed point)

enumerator PJ_JSON_VAL_STRING

Literal string value.

enumerator PJ_JSON_VAL_ARRAY

Array

enumerator PJ_JSON_VAL_OBJ

Object.

Functions

void pj_json_elem_null(pj_json_elem *el, pj_str_t *name)

Initialize null element.

Parameters:
  • el – The element.

  • name – Name to be given to the element, or NULL.

void pj_json_elem_bool(pj_json_elem *el, pj_str_t *name, pj_bool_t val)

Initialize boolean element with the specified value.

Parameters:
  • el – The element.

  • name – Name to be given to the element, or NULL.

  • val – The value.

void pj_json_elem_number(pj_json_elem *el, pj_str_t *name, float val)

Initialize number element with the specified value.

Parameters:
  • el – The element.

  • name – Name to be given to the element, or NULL.

  • val – The value.

void pj_json_elem_string(pj_json_elem *el, pj_str_t *name, pj_str_t *val)

Initialize string element with the specified value.

Parameters:
  • el – The element.

  • name – Name to be given to the element, or NULL.

  • val – The value.

void pj_json_elem_array(pj_json_elem *el, pj_str_t *name)

Initialize element as an empty array

Parameters:
  • el – The element.

  • name – Name to be given to the element, or NULL.

void pj_json_elem_obj(pj_json_elem *el, pj_str_t *name)

Initialize element as an empty object

Parameters:
  • el – The element.

  • name – Name to be given to the element, or NULL.

void pj_json_elem_add(pj_json_elem *el, pj_json_elem *child)

Add an element to an object or array.

Parameters:
  • el – The object or array element.

  • child – Element to be added to the object or array.

pj_json_elem *pj_json_parse(pj_pool_t *pool, char *buffer, unsigned *size, pj_json_err_info *err_info)

Parse a JSON document in the buffer. The buffer MUST be NULL terminated, or if not then it must have enough size to put the NULL character.

Parameters:
  • pool – The pool to allocate memory for creating elements.

  • buffer – String buffer containing JSON document.

  • size – Size of the document.

  • err_info – Optional structure to be filled with info when parsing failed.

Returns:

The root element from the document.

pj_status_t pj_json_write(const pj_json_elem *elem, char *buffer, unsigned *size)

Write the specified element to the string buffer.

Parameters:
  • elem – The element to be written.

  • buffer – Output buffer.

  • size – On input, it must be set to the size of the buffer. Upon successful return, this will be set to the length of the written string.

Returns:

PJ_SUCCESS on success or the appropriate error.

pj_status_t pj_json_writef(const pj_json_elem *elem, pj_json_writer writer, void *user_data)

Incrementally write the element to arbitrary medium using the specified callback to write the document chunks.

Parameters:
  • elem – The element to be written.

  • writer – Callback function which will be called to write text chunks.

  • user_data – Arbitrary user data which will be given back when calling the callback.

Returns:

PJ_SUCCESS on success or the appropriate error.

struct pj_json_list
#include <json.h>

JSON list to store child elements.

struct pj_json_elem
#include <json.h>

This represents JSON element. A JSON element is basically a name/value pair, where the name is a string and the value can be one of null, boolean (true and false constants), number, string, array (containing zero or more elements), or object. An object can be viewed as C struct, that is a compound element containing other elements, each having name/value pair.

Public Members

pj_str_t name

ELement name.

pj_json_val_type type

Element type.

pj_bool_t is_true

Boolean value.

float num

Number value.

pj_str_t str

String value.

pj_json_list children

Object and array children

union pj_json_elem::[anonymous] value

Element value.

struct pj_json_err_info
#include <json.h>

Structure to be specified to pj_json_parse() to be filled with additional info when parsing failed.

Public Members

unsigned line

Line location of the error

unsigned col

Column location of the error

int err_char

The offending character.