YAZ  4.2.57
Data Structures | Typedefs | Enumerations | Functions
json.h File Reference

Header for JSON functions. More...

#include <yaz/wrbuf.h>

Go to the source code of this file.

Data Structures

struct  json_node
 JSON node. More...

Typedefs

typedef struct json_parser_sjson_parser_t
 JSON parser (opaque)

Enumerations

enum  json_node_type {
  json_node_object, json_node_array, json_node_list, json_node_pair,
  json_node_string, json_node_number, json_node_true, json_node_false,
  json_node_null
}
 JSON node type for json_node. More...

Functions

json_parser_t json_parser_create (void)
 create JSON parser
void json_parser_destroy (json_parser_t p)
 destroys JSON parser
struct json_nodejson_parser_parse (json_parser_t p, const char *json_str)
 parses JSON string
const char * json_parser_get_errmsg (json_parser_t p)
 returns parser error
size_t json_parser_get_position (json_parser_t p)
 returns parser position
struct json_nodejson_parse (const char *json_str, const char **errmsg)
 parses JSON string
struct json_nodejson_parse2 (const char *json_str, const char **errmsg, size_t *pos)
 parses JSON string
void json_remove_node (struct json_node *n)
 destroys JSON tree node and its children
struct json_nodejson_get_object (struct json_node *n, const char *name)
 gets object pair value for some name
struct json_nodejson_detach_object (struct json_node *n, const char *name)
 gets object value and detaches from existing tree
struct json_nodejson_get_elem (struct json_node *n, int idx)
 gets array element
int json_count_children (struct json_node *n)
 returns number of children (array or object)
int json_append_array (struct json_node *dst, struct json_node *src)
 appends array to another
void json_parser_subst (json_parser_t p, int idx, struct json_node *n)
 configure subst rule
void json_write_wrbuf (struct json_node *node, WRBUF result)
 converts JSON tree to JSON string
void json_write_wrbuf_pretty (struct json_node *node, WRBUF result)
 writes JSON tree with indentation (pretty print)
void wrbuf_json_puts (WRBUF b, const char *str)
 writes JSON text to WRBUF with escaping

Detailed Description

Header for JSON functions.

Definition in file json.h.

Typedef Documentation

typedef struct json_parser_s* json_parser_t

JSON parser (opaque)

Definition at line 63 of file json.h.

Enumeration Type Documentation

JSON node type for json_node.

Enumerator:
json_node_object 

JSON object, u.link[0] is object content

json_node_array 

JSON array, u.link[0] is array content

json_node_list 

JSON elements or JSON members, u.link[0] is value, u.link[1] is next elemen in list

json_node_pair 

JSON pair, u.link[0] is name, u.link[1] is value

json_node_string 

JSON string, u.string is content

json_node_number 

JSON number (floating point), u.number is content

json_node_true 

JSON true

json_node_false 

JSON false

json_node_null 

JSON null

Definition at line 39 of file json.h.

Function Documentation

int json_append_array ( struct json_node dst,
struct json_node src 
)

appends array to another

Parameters
dstoriginal array and resulting array
srcarray to be appended to dst
Return values
-1not arrays
0array appended OK

Definition at line 670 of file json.c.

References json_node_array, json_remove_node(), json_node::link, json_node::type, and json_node::u.

int json_count_children ( struct json_node n)

returns number of children (array or object)

Parameters
nJSON node (presumably array node or object node)
Returns
number of children

Definition at line 658 of file json.c.

References json_node_array, json_node_object, json_node::link, json_node::type, and json_node::u.

struct json_node* json_detach_object ( struct json_node n,
const char *  name 
)
read

gets object value and detaches from existing tree

Parameters
nJSON node (presumably object node)
namename to match
Returns
node or NULL if not found

Definition at line 632 of file json.c.

References json_get_objectp().

struct json_node* json_get_elem ( struct json_node n,
int  idx 
)
read

gets array element

Parameters
nJSON node (presumably array node)
idx(0=first, 1=second, ..)
Returns
node or NULL if not found

Definition at line 645 of file json.c.

References json_node_array, json_node::link, json_node::type, and json_node::u.

struct json_node* json_get_object ( struct json_node n,
const char *  name 
)
read

gets object pair value for some name

Parameters
nJSON node (presumably object node)
namename to match
Returns
node or NULL if not found

Definition at line 623 of file json.c.

References json_get_objectp().

struct json_node* json_parse ( const char *  json_str,
const char **  errmsg 
)
read

parses JSON string

Parameters
json_strJSON string
errmsgpointer to error message string
Returns
JSON tree or NULL if parse error occurred.

The resulting tree should be removed with a call to json_remove_node. The errmsg may be NULL in which case the error message is not returned.

Definition at line 460 of file json.c.

References json_parse2().

struct json_node* json_parse2 ( const char *  json_str,
const char **  errmsg,
size_t *  pos 
)
read

parses JSON string

Parameters
json_strJSON string
errmsgpointer to error message string
posposition of parser stop (probably error)
Returns
JSON tree or NULL if parse error occurred.

The resulting tree should be removed with a call to json_remove_node. The errmsg may be NULL in which case the error message is not returned.

Definition at line 438 of file json.c.

References json_parser_create(), json_parser_destroy(), json_parser_get_errmsg(), json_parser_get_position(), and json_parser_parse().

Referenced by json_parse().

json_parser_t json_parser_create ( void  )

create JSON parser

Returns
JSON parser handle

Definition at line 36 of file json.c.

References json_parser_s::buf, json_parser_s::cp, json_parser_s::err_msg, json_parser_s::subst, and xmalloc.

Referenced by json_parse2().

void json_parser_destroy ( json_parser_t  p)

destroys JSON parser

Parameters
pJSON parser handle

Definition at line 62 of file json.c.

References json_subst_info::next, json_parser_s::subst, and xfree.

Referenced by json_parse2().

const char* json_parser_get_errmsg ( json_parser_t  p)

returns parser error

Parameters
pJSON parser handle
Returns
parse error msg

This function should be called if json_parser_parse returns NULL .

Definition at line 686 of file json.c.

References json_parser_s::err_msg.

Referenced by json_parse2().

size_t json_parser_get_position ( json_parser_t  p)

returns parser position

Parameters
pJSON parser handle
Returns
number of bytes read from parser

This function should be called if json_parser_parse returns NULL .

Definition at line 691 of file json.c.

References json_parser_s::buf, and json_parser_s::cp.

Referenced by json_parse2().

struct json_node* json_parser_parse ( json_parser_t  p,
const char *  json_str 
)
read

parses JSON string

Parameters
pJSON parser handle
json_strJSON string
Returns
JSON tree or NULL if parse error occurred.

The resulting tree should be removed with a call to json_remove_node.

Definition at line 418 of file json.c.

References json_parser_s::buf, json_parser_s::cp, json_parser_s::err_msg, json_parse_value(), json_remove_node(), and look_ch().

Referenced by json_parse2().

void json_parser_subst ( json_parser_t  p,
int  idx,
struct json_node n 
)

configure subst rule

Parameters
pJSON parser
idx(id)
nnode to be substituted for idx (idx)

Definition at line 47 of file json.c.

References json_subst_info::idx, json_subst_info::next, json_subst_info::node, json_parser_s::subst, and xmalloc.

void json_remove_node ( struct json_node n)
void json_write_wrbuf ( struct json_node node,
WRBUF  result 
)

converts JSON tree to JSON string

Parameters
nodeJSON tree
resultresulting JSON string buffer

Definition at line 601 of file json.c.

References json_write_wrbuf_r().

void json_write_wrbuf_pretty ( struct json_node node,
WRBUF  result 
)

writes JSON tree with indentation (pretty print)

Parameters
nodeJSON tree
resultresulting JSON string buffer

Definition at line 596 of file json.c.

References json_write_wrbuf_r().

void wrbuf_json_puts ( WRBUF  b,
const char *  str 
)

writes JSON text to WRBUF with escaping

Parameters
bresult
strinput string to be encoded

Definition at line 500 of file json.c.

References wrbuf_json_write().

Referenced by json_write_wrbuf_r().