YAZ  5.23.1
json.h
Go to the documentation of this file.
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data.
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of Index Data nor the names of its contributors
13  * may be used to endorse or promote products derived from this
14  * software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
32 #ifndef YAZ_JSON_H
33 #define YAZ_JSON_H
34 #include <yaz/wrbuf.h>
35 
37 
50 };
51 
53 struct json_node {
55  union {
56  char *string;
57  double number;
58  struct json_node *link[2];
59  } u;
60 };
61 
63 typedef struct json_parser_s *json_parser_t;
64 
68 YAZ_EXPORT
69 json_parser_t json_parser_create(void);
70 
74 YAZ_EXPORT
75 void json_parser_destroy(json_parser_t p);
76 
84 YAZ_EXPORT
85 struct json_node *json_parser_parse(json_parser_t p, const char *json_str);
86 
93 YAZ_EXPORT
94 const char *json_parser_get_errmsg(json_parser_t p);
95 
102 YAZ_EXPORT
103 size_t json_parser_get_position(json_parser_t p);
104 
113 YAZ_EXPORT
114 struct json_node *json_parse(const char *json_str, const char **errmsg);
115 
125 YAZ_EXPORT
126 struct json_node *json_parse2(const char *json_str, const char **errmsg,
127  size_t *pos);
128 
132 YAZ_EXPORT
133 void json_remove_node(struct json_node *n);
134 
140 YAZ_EXPORT
141 struct json_node *json_get_object(struct json_node *n, const char *name);
142 
148 YAZ_EXPORT
149 struct json_node *json_detach_object(struct json_node *n, const char *name);
150 
156 YAZ_EXPORT
157 struct json_node *json_get_elem(struct json_node *n, int idx);
158 
163 YAZ_EXPORT
164 int json_count_children(struct json_node *n);
165 
172 YAZ_EXPORT
173 int json_append_array(struct json_node *dst, struct json_node *src);
174 
180 YAZ_EXPORT
181 void json_parser_subst(json_parser_t p, int idx, struct json_node *n);
182 
187 YAZ_EXPORT
188 void json_write_wrbuf(struct json_node *node, WRBUF result);
189 
194 YAZ_EXPORT
195 void json_write_wrbuf_pretty(struct json_node *node, WRBUF result);
196 
198 
199 #endif
200 
201 /*
202  * Local variables:
203  * c-basic-offset: 4
204  * c-file-style: "Stroustrup"
205  * indent-tabs-mode: nil
206  * End:
207  * vim: shiftwidth=4 tabstop=8 expandtab
208  */
209 
void json_parser_subst(json_parser_t p, int idx, struct json_node *n)
configure subst rule
Definition: json.c:48
struct json_node * json_get_elem(struct json_node *n, int idx)
gets array element
Definition: json.c:698
void json_remove_node(struct json_node *n)
destroys JSON tree node and its children
Definition: json.c:96
static int node(struct cql_node *cn, void(*pr)(const char *buf, void *client_data), void *client_data)
Definition: cql2ccl.c:86
Header for WRBUF (growing buffer)
double number
Definition: json.h:57
void json_write_wrbuf_pretty(struct json_node *node, WRBUF result)
writes JSON tree with indentation (pretty print)
Definition: json.c:649
#define YAZ_END_CDECL
Definition: yconfig.h:57
struct json_node * json_detach_object(struct json_node *n, const char *name)
gets object value and detaches from existing tree
Definition: json.c:685
char * name
Definition: initopt.c:18
json_parser_t json_parser_create(void)
create JSON parser
Definition: json.c:38
int json_count_children(struct json_node *n)
returns number of children (array or object)
Definition: json.c:711
struct json_node * json_parser_parse(json_parser_t p, const char *json_str)
parses JSON string
Definition: json.c:503
int json_append_array(struct json_node *dst, struct json_node *src)
appends array to another
Definition: json.c:723
const char * json_parser_get_errmsg(json_parser_t p)
returns parser error
Definition: json.c:739
string buffer
Definition: wrbuf.h:42
void json_write_wrbuf(struct json_node *node, WRBUF result)
converts JSON tree to JSON string
Definition: json.c:654
void json_parser_destroy(json_parser_t p)
destroys JSON parser
Definition: json.c:63
json_node_type
JSON node type for json_node.
Definition: json.h:39
struct json_parser_s * json_parser_t
JSON parser (opaque)
Definition: json.h:63
enum json_node_type type
Definition: json.h:54
JSON node.
Definition: json.h:53
struct json_node * json_parse(const char *json_str, const char **errmsg)
parses JSON string
Definition: json.c:553
struct json_node * json_get_object(struct json_node *n, const char *name)
gets object pair value for some name
Definition: json.c:676
union json_node::@28 u
struct json_node * link[2]
Definition: json.h:58
size_t json_parser_get_position(json_parser_t p)
returns parser position
Definition: json.c:744
char * string
Definition: json.h:56
struct json_node * json_parse2(const char *json_str, const char **errmsg, size_t *pos)
parses JSON string
Definition: json.c:531
#define YAZ_BEGIN_CDECL
Definition: yconfig.h:56