|
YAZ 5.35.1
|
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 | |
| 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 | |
JSON encoding/decoding.
Definition in file json.c.
appends array to another
| dst | original array and resulting array |
| src | array to be appended to dst |
| -1 | not arrays |
| 0 | array appended OK |
Definition at line 725 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)
| n | JSON node (presumably array node or object node) |
Definition at line 713 of file json.c.
References json_node_array, json_node_object, json_node::link, json_node::type, and json_node::u.
gets object value and detaches from existing tree
| n | JSON node (presumably object node) |
| name | name to match |
Definition at line 687 of file json.c.
References json_get_objectp(), and name.
gets array element
| n | JSON node (presumably array node) |
| idx | (0=first, 1=second, ..) |
Definition at line 700 of file json.c.
References json_node_array, json_node::link, json_node::type, and json_node::u.
gets object pair value for some name
| n | JSON node (presumably object node) |
| name | name to match |
Definition at line 678 of file json.c.
References json_get_objectp(), and name.
Definition at line 661 of file json.c.
References json_node_object, json_node_pair, json_node_string, json_node::link, name, json_node::string, json_node::type, and json_node::u.
Referenced by json_detach_object(), and json_get_object().
|
static |
Definition at line 558 of file json.c.
References wrbuf_buf, wrbuf_len, and wrbuf_putc.
Referenced by json_write_wrbuf_r().
|
static |
Definition at line 88 of file json.c.
References json_node::link, type, json_node::type, json_node::u, and xmalloc.
Referenced by json_parse_array(), json_parse_elements(), json_parse_members(), json_parse_number(), json_parse_object(), json_parse_pair(), json_parse_string(), and json_parse_value().
|
static |
Definition at line 124 of file json.c.
References yaz_write_UTF8_char().
Referenced by json_parse_string().
| struct json_node * json_parse | ( | const char * | json_str, |
| const char ** | errmsg | ||
| ) |
parses JSON string
| json_str | JSON string |
| errmsg | pointer to error message string |
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 553 of file json.c.
References json_parse2().
| struct json_node * json_parse2 | ( | const char * | json_str, |
| const char ** | errmsg, | ||
| size_t * | pos | ||
| ) |
parses JSON string
| json_str | JSON string |
| errmsg | pointer to error message string |
| pos | position of parser stop (probably error) |
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 531 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().
|
static |
Definition at line 380 of file json.c.
References json_parser_s::err_msg, json_new_node(), json_node_array, json_parse_elements(), json_remove_node(), json_node::link, look_ch(), json_parser_s::max_level, move_ch(), json_parser_s::parse_level, and json_node::u.
Referenced by json_parse_value().
|
static |
Definition at line 353 of file json.c.
References json_new_node(), json_node_list, json_parse_value(), json_remove_node(), json_node::link, look_ch(), move_ch(), and json_node::u.
Referenced by json_parse_array().
|
static |
Definition at line 436 of file json.c.
References json_new_node(), json_node_list, json_parse_pair(), json_remove_node(), json_node::link, look_ch(), move_ch(), and json_node::u.
Referenced by json_parse_object().
|
static |
Definition at line 229 of file json.c.
References json_parser_s::cp, json_parser_s::err_msg, json_new_node(), json_node_number, json_node::number, and json_node::u.
Referenced by json_parse_value().
|
static |
Definition at line 463 of file json.c.
References json_parser_s::err_msg, json_new_node(), json_node_object, json_parse_members(), json_remove_node(), json_node::link, look_ch(), json_parser_s::max_level, move_ch(), json_parser_s::parse_level, and json_node::u.
Referenced by json_parse_value().
|
static |
Definition at line 411 of file json.c.
References json_parser_s::err_msg, json_new_node(), json_node_pair, json_parse_string(), json_parse_value(), json_remove_node(), json_node::link, look_ch(), move_ch(), and json_node::u.
Referenced by json_parse_members().
|
static |
Definition at line 182 of file json.c.
References json_parser_s::cp, json_parser_s::err_msg, json_new_node(), json_node_string, json_one_char(), look_ch(), move_ch(), json_node::string, json_node::u, and xmalloc.
Referenced by json_parse_pair(), and json_parse_value().
|
static |
Definition at line 300 of file json.c.
References json_parser_s::cp, json_parser_s::err_msg, json_subst_info::idx, json_new_node(), json_node_false, json_node_null, json_node_true, json_parse_array(), json_parse_number(), json_parse_object(), json_parse_string(), look_ch(), json_subst_info::next, json_subst_info::node, and json_parser_s::subst.
Referenced by json_parse_elements(), json_parse_pair(), and json_parser_parse().
| json_parser_t json_parser_create | ( | void | ) |
create JSON parser
Definition at line 38 of file json.c.
References json_parser_s::buf, json_parser_s::cp, json_parser_s::subst, and xmalloc.
Referenced by json_parse2().
| void json_parser_destroy | ( | json_parser_t | p | ) |
destroys JSON parser
| p | JSON parser handle |
Definition at line 63 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
| p | JSON parser handle |
This function should be called if json_parser_parse returns NULL .
Definition at line 741 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
| p | JSON parser handle |
This function should be called if json_parser_parse returns NULL .
Definition at line 746 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 | ||
| ) |
parses JSON string
| p | JSON parser handle |
| json_str | JSON string |
The resulting tree should be removed with a call to json_remove_node.
Definition at line 503 of file json.c.
References json_parser_s::buf, json_parser_s::cp, json_parser_s::err_msg, json_parse_value(), json_remove_node(), look_ch(), json_parser_s::max_level, and json_parser_s::parse_level.
Referenced by json_parse2().
| void json_parser_subst | ( | json_parser_t | p, |
| int | idx, | ||
| struct json_node * | n | ||
| ) |
configure subst rule
| p | JSON parser |
| idx | (id) |
| n | node to be substituted for idx (idx) |
Definition at line 48 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 | ) |
destroys JSON tree node and its children
| n | JSON node |
Definition at line 96 of file json.c.
References 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_remove_node(), json_node::link, json_node::string, json_node::type, json_node::u, and xfree.
Referenced by json_append_array(), json_parse_array(), json_parse_elements(), json_parse_members(), json_parse_object(), json_parse_pair(), json_parser_parse(), and json_remove_node().
converts JSON tree to JSON string
| node | JSON tree |
| result | resulting JSON string buffer |
Definition at line 656 of file json.c.
References json_write_wrbuf_r().
writes JSON tree with indentation (pretty print)
| node | JSON tree |
| result | resulting JSON string buffer |
Definition at line 651 of file json.c.
References json_write_wrbuf_r().
Definition at line 569 of file json.c.
References json_indent(), 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_write_wrbuf_r(), json_node::link, json_node::number, json_node::string, json_node::type, json_node::u, wrbuf_json_puts(), wrbuf_printf(), and wrbuf_puts().
Referenced by json_write_wrbuf(), json_write_wrbuf_pretty(), and json_write_wrbuf_r().
|
static |
Definition at line 75 of file json.c.
References json_parser_s::cp.
Referenced by json_parse_array(), json_parse_elements(), json_parse_members(), json_parse_object(), json_parse_pair(), json_parse_string(), json_parse_value(), and json_parser_parse().
|
static |
Definition at line 82 of file json.c.
References json_parser_s::cp.
Referenced by json_parse_array(), json_parse_elements(), json_parse_members(), json_parse_object(), json_parse_pair(), and json_parse_string().