YAZ 5.38.1
json.c File Reference

JSON encoding/decoding. More...

#include <yaz/json.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <yaz/xmalloc.h>

Go to the source code of this file.

Data Structures

struct  json_subst_info
struct  json_parser_s

Functions

json_parser_t json_parser_create (void)
 create JSON parser
void json_parser_subst (json_parser_t p, int idx, struct json_node *n)
 configure subst rule
void json_parser_destroy (json_parser_t p)
 destroys JSON parser
static int look_ch (json_parser_t p)
static void move_ch (json_parser_t p)
static struct json_node * json_new_node (json_parser_t p, enum json_node_type type)
void json_remove_node (struct json_node *n)
 destroys JSON tree node and its children
struct json_node * json_clone_node (const struct json_node *n)
 clones JSON tree node and its children
static struct json_node * json_parse_object (json_parser_t p)
static struct json_node * json_parse_array (json_parser_t p)
static int json_one_char (const char **p, char *out)
static struct json_node * json_parse_string (json_parser_t p)
static struct json_node * json_parse_number (json_parser_t p)
static struct json_node * json_parse_value (json_parser_t p)
static struct json_node * json_parse_elements (json_parser_t p)
static struct json_node * json_parse_pair (json_parser_t p)
static struct json_node * json_parse_members (json_parser_t p)
struct json_node * json_parser_parse (json_parser_t p, const char *json_str)
 parses JSON string
struct json_node * json_parse2 (const char *json_str, const char **errmsg, size_t *pos)
 parses JSON string
struct json_node * json_parse (const char *json_str, const char **errmsg)
 parses JSON string
static void json_indent (WRBUF result, int indent)
static void json_write_wrbuf_r (struct json_node *node, WRBUF result, int indent)
void json_write_wrbuf_pretty (struct json_node *node, WRBUF result)
 writes JSON tree with indentation (pretty print)
void json_write_wrbuf (struct json_node *node, WRBUF result)
 converts JSON tree to JSON string
static struct json_node ** json_get_objectp (struct json_node *n, const char *name)
struct json_node * json_get_object (struct json_node *n, const char *name)
 gets object pair value for some name
struct json_node * json_detach_object (struct json_node *n, const char *name)
 gets object value and detaches from existing tree
struct json_node * json_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
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

Detailed Description

JSON encoding/decoding.

Definition in file json.c.

Function Documentation

◆ json_append_array()

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 793 of file json.c.

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

Referenced by json_append_array().

◆ json_clone_node()

struct json_node * json_clone_node ( const struct json_node * n)

clones JSON tree node and its children

Parameters
nJSON node
Returns
cloned JSON tree or NULL if n is NULL

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

Definition at line 132 of file json.c.

References json_clone_node(), json_new_node(), json_node_array, json_node_false, json_node_list, json_node_null, json_node_number, json_node_object, json_node_pair, json_node_string, json_node_true, json_node::type, json_node::u, and xstrdup.

Referenced by json_clone_node(), and json_parse_value().

◆ json_count_children()

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 781 of file json.c.

References json_count_children(), json_node_array, json_node_object, json_node::type, and json_node::u.

Referenced by json_count_children().

◆ json_detach_object()

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

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 755 of file json.c.

References json_detach_object(), and json_get_objectp().

Referenced by json_detach_object().

◆ json_get_elem()

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

gets array element

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

Definition at line 768 of file json.c.

References json_get_elem(), json_node_array, json_node::type, and json_node::u.

Referenced by json_get_elem().

◆ json_get_object()

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

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 746 of file json.c.

References json_get_object(), and json_get_objectp().

Referenced by json_get_object().

◆ json_get_objectp()

struct json_node ** json_get_objectp ( struct json_node * n,
const char * name )
static

◆ json_indent()

void json_indent ( WRBUF result,
int indent )
static

Definition at line 622 of file json.c.

References json_indent(), wrbuf_buf, wrbuf_len, and wrbuf_putc.

Referenced by json_indent(), and json_write_wrbuf_r().

◆ json_new_node()

◆ json_one_char()

int json_one_char ( const char ** p,
char * out )
static

Definition at line 179 of file json.c.

References json_one_char(), and yaz_write_UTF8_char().

Referenced by json_one_char(), and json_parse_string().

◆ json_parse()

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

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 617 of file json.c.

References json_parse(), and json_parse2().

Referenced by json_parse().

◆ json_parse2()

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

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 595 of file json.c.

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

Referenced by json_parse(), and json_parse2().

◆ json_parse_array()

◆ json_parse_elements()

struct json_node * json_parse_elements ( json_parser_t p)
static

◆ json_parse_members()

struct json_node * json_parse_members ( json_parser_t p)
static

◆ json_parse_number()

struct json_node * json_parse_number ( json_parser_t p)
static

◆ json_parse_object()

◆ json_parse_pair()

◆ json_parse_string()

◆ json_parse_value()

◆ json_parser_create()

json_parser_t json_parser_create ( void )

create JSON parser

Returns
JSON parser handle

Definition at line 38 of file json.c.

References json_parser_s::buf, json_parser_s::cp, json_parser_create(), json_parser_s::subst, and xmalloc.

Referenced by json_parse2(), and json_parser_create().

◆ json_parser_destroy()

void json_parser_destroy ( json_parser_t p)

destroys JSON parser

Parameters
pJSON parser handle

Definition at line 65 of file json.c.

References json_parser_destroy(), json_remove_node(), json_subst_info::next, json_subst_info::node, json_parser_s::subst, and xfree.

Referenced by json_parse2(), and json_parser_destroy().

◆ json_parser_get_errmsg()

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 809 of file json.c.

References json_parser_s::err_msg, and json_parser_get_errmsg().

Referenced by json_parse2(), and json_parser_get_errmsg().

◆ json_parser_get_position()

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 814 of file json.c.

References json_parser_s::buf, json_parser_s::cp, and json_parser_get_position().

Referenced by json_parse2(), and json_parser_get_position().

◆ json_parser_parse()

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

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 567 of file json.c.

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

Referenced by json_parse2(), and json_parser_parse().

◆ json_parser_subst()

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

configure subst rule

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

Ownership of n is transferred to the parser. The parser releases it when the rule is replaced or the parser is destroyed. Each occurrence of idx in the JSON input is replaced by a clone of n.

Definition at line 48 of file json.c.

References json_subst_info::idx, json_parser_subst(), json_remove_node(), json_subst_info::next, json_parser_s::subst, and xmalloc.

Referenced by json_parser_subst().

◆ json_remove_node()

◆ json_write_wrbuf()

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 724 of file json.c.

References json_write_wrbuf(), and json_write_wrbuf_r().

Referenced by json_write_wrbuf().

◆ json_write_wrbuf_pretty()

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 719 of file json.c.

References json_write_wrbuf_pretty(), and json_write_wrbuf_r().

Referenced by json_write_wrbuf_pretty().

◆ json_write_wrbuf_r()

◆ look_ch()

◆ move_ch()