VirtualBox

source: vbox/trunk/include/iprt/json.h@ 73930

Last change on this file since 73930 was 73930, checked in by vboxsync, 6 years ago

IPRT/rest: Implemented parsing json response bodies. bugref:9167

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.4 KB
Line 
1/** @file
2 * IPRT - JavaScript Object Notation (JSON) Parser.
3 */
4
5/*
6 * Copyright (C) 2016-2017 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___iprt_json_h
27#define ___iprt_json_h
28
29#include <iprt/types.h>
30#include <iprt/err.h>
31
32RT_C_DECLS_BEGIN
33
34
35/** @defgroup grp_json RTJson - JavaScript Object Notation (JSON) Parser
36 * @ingroup grp_rt
37 * @{
38 */
39
40/**
41 * JSON value types.
42 */
43typedef enum RTJSONVALTYPE
44{
45 /** Invalid first value. */
46 RTJSONVALTYPE_INVALID = 0,
47 /** Value containing an object. */
48 RTJSONVALTYPE_OBJECT,
49 /** Value containing an array. */
50 RTJSONVALTYPE_ARRAY,
51 /** Value containing a string. */
52 RTJSONVALTYPE_STRING,
53 /** Value containg a number. */
54 RTJSONVALTYPE_NUMBER,
55 /** Value containg the special null value. */
56 RTJSONVALTYPE_NULL,
57 /** Value containing true. */
58 RTJSONVALTYPE_TRUE,
59 /** Value containing false. */
60 RTJSONVALTYPE_FALSE,
61 /** 32-bit hack. */
62 RTJSONVALTYPE_32BIT_HACK = 0x7fffffff
63} RTJSONVALTYPE;
64/** Pointer to a JSON value type. */
65typedef RTJSONVALTYPE *PRTJSONVALTYPE;
66
67/** JSON value handle. */
68typedef struct RTJSONVALINT *RTJSONVAL;
69/** Pointer to a JSON value handle. */
70typedef RTJSONVAL *PRTJSONVAL;
71/** NIL JSON value handle. */
72#define NIL_RTJSONVAL ((RTJSONVAL)~(uintptr_t)0)
73
74/** JSON iterator handle. */
75typedef struct RTJSONITINT *RTJSONIT;
76/** Pointer to a JSON iterator handle. */
77typedef RTJSONIT *PRTJSONIT;
78/** NIL JSON iterator handle. */
79#define NIL_RTJSONIT ((RTJSONIT)~(uintptr_t)0)
80
81/**
82 * Parses a JSON document in the provided buffer returning the root JSON value.
83 *
84 * @returns IPRT status code.
85 * @retval VERR_JSON_MALFORMED if the document does not conform to the spec.
86 * @param phJsonVal Where to store the handle to the JSON value on success.
87 * @param pbBuf The byte buffer containing the JSON document.
88 * @param cbBuf Size of the buffer.
89 * @param pErrInfo Where to store extended error info. Optional.
90 *
91 * @todo r=bird: The use of uint8_t makes no sense here since the parser
92 * expects ASCII / UTF-8. What's more, if this is a real buffer the
93 * type should be 'const void *' rather than 'const uint8_t *'.
94 * This function should be modified to reflect that it's really for
95 * handling unterminated strings.
96 */
97RTDECL(int) RTJsonParseFromBuf(PRTJSONVAL phJsonVal, const uint8_t *pbBuf, size_t cbBuf, PRTERRINFO pErrInfo);
98
99/**
100 * Parses a JSON document from the provided string returning the root JSON value.
101 *
102 * @returns IPRT status code.
103 * @retval VERR_JSON_MALFORMED if the document does not conform to the spec.
104 * @param phJsonVal Where to store the handle to the JSON value on success.
105 * @param pszStr The string containing the JSON document.
106 * @param pErrInfo Where to store extended error info. Optional.
107 */
108RTDECL(int) RTJsonParseFromString(PRTJSONVAL phJsonVal, const char *pszStr, PRTERRINFO pErrInfo);
109
110/**
111 * Parses a JSON document from the file pointed to by the given filename
112 * returning the root JSON value.
113 *
114 * @returns IPRT status code.
115 * @retval VERR_JSON_MALFORMED if the document does not conform to the spec.
116 * @param phJsonVal Where to store the handle to the JSON value on success.
117 * @param pszFilename The name of the file containing the JSON document.
118 * @param pErrInfo Where to store extended error info. Optional.
119 */
120RTDECL(int) RTJsonParseFromFile(PRTJSONVAL phJsonVal, const char *pszFilename, PRTERRINFO pErrInfo);
121
122/**
123 * Retain a given JSON value.
124 *
125 * @returns New reference count.
126 * @param hJsonVal The JSON value handle.
127 */
128RTDECL(uint32_t) RTJsonValueRetain(RTJSONVAL hJsonVal);
129
130/**
131 * Release a given JSON value.
132 *
133 * @returns New reference count, if this drops to 0 the value is freed.
134 * @param hJsonVal The JSON value handle.
135 */
136RTDECL(uint32_t) RTJsonValueRelease(RTJSONVAL hJsonVal);
137
138/**
139 * Return the type of a given JSON value.
140 *
141 * @returns Type of the given JSON value.
142 * @param hJsonVal The JSON value handle.
143 */
144RTDECL(RTJSONVALTYPE) RTJsonValueGetType(RTJSONVAL hJsonVal);
145
146/**
147 * Translates value type to a name.
148 *
149 * @returns Readonly name string
150 * @param enmType The JSON value type to name.
151 */
152RTDECL(const char *) RTJsonValueTypeName(RTJSONVALTYPE enmType);
153
154/**
155 * Returns the string from a given JSON string value.
156 *
157 * @returns Pointer to the string of the JSON value, NULL if the value type
158 * doesn't indicate a string.
159 * @param hJsonVal The JSON value handle.
160 */
161RTDECL(const char *) RTJsonValueGetString(RTJSONVAL hJsonVal);
162
163/**
164 * Returns the string from a given JSON string value, extended.
165 *
166 * @returns IPRT status code.
167 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not a string.
168 * @param hJsonVal The JSON value handle.
169 * @param ppszStr Where to store the pointer to the string on success.
170 */
171RTDECL(int) RTJsonValueQueryString(RTJSONVAL hJsonVal, const char **ppszStr);
172
173/**
174 * Returns the number from a given JSON number value.
175 *
176 * @returns IPRT status code.
177 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not a number.
178 * @param hJsonVal The JSON value handle.
179 * @param pi64Num WHere to store the number on success.
180 *
181 * @note This JSON implementation does not implement support for floating point
182 * numbers currently. When it does, it will be in the form of a
183 * RTJsonValueQueryFloat method.
184 */
185RTDECL(int) RTJsonValueQueryInteger(RTJSONVAL hJsonVal, int64_t *pi64Num);
186
187/**
188 * Returns the value associated with a given name for the given JSON object value.
189 *
190 * @returns IPRT status code.
191 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object.
192 * @retval VERR_NOT_FOUND if the name is not known for this JSON object.
193 * @param hJsonVal The JSON value handle.
194 * @param pszName The member name of the object.
195 * @param phJsonVal Where to store the handle to the JSON value on success.
196 */
197RTDECL(int) RTJsonValueQueryByName(RTJSONVAL hJsonVal, const char *pszName, PRTJSONVAL phJsonVal);
198
199/**
200 * Returns the number of a number value associated with a given name for the given JSON object value.
201 *
202 * @returns IPRT status code.
203 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object or
204 * the name does not point to a number value.
205 * @retval VERR_NOT_FOUND if the name is not known for this JSON object.
206 * @param hJsonVal The JSON value handle.
207 * @param pszName The member name of the object.
208 * @param pi64Num Where to store the number on success.
209 */
210RTDECL(int) RTJsonValueQueryIntegerByName(RTJSONVAL hJsonVal, const char *pszName, int64_t *pi64Num);
211
212/**
213 * Returns the string of a string value associated with a given name for the given JSON object value.
214 *
215 * @returns IPRT status code.
216 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object or
217 * the name does not point to a string value.
218 * @retval VERR_NOT_FOUND if the name is not known for this JSON object.
219 * @param hJsonVal The JSON value handle.
220 * @param pszName The member name of the object.
221 * @param ppszStr Where to store the pointer to the string on success.
222 * Must be freed with RTStrFree().
223 */
224RTDECL(int) RTJsonValueQueryStringByName(RTJSONVAL hJsonVal, const char *pszName, char **ppszStr);
225
226/**
227 * Returns the boolean of a true/false value associated with a given name for the given JSON object value.
228 *
229 * @returns IPRT status code.
230 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object or
231 * the name does not point to a true/false value.
232 * @retval VERR_NOT_FOUND if the name is not known for this JSON object.
233 * @param hJsonVal The JSON value handle.
234 * @param pszName The member name of the object.
235 * @param pfBoolean Where to store the boolean value on success.
236 */
237RTDECL(int) RTJsonValueQueryBooleanByName(RTJSONVAL hJsonVal, const char *pszName, bool *pfBoolean);
238
239/**
240 * Returns the size of a given JSON array value.
241 *
242 * @returns Size of the JSON array value.
243 * @retval 0 if the array is empty or the JSON value is not an array.
244 * @param hJsonVal The JSON value handle.
245 */
246RTDECL(unsigned) RTJsonValueGetArraySize(RTJSONVAL hJsonVal);
247
248/**
249 * Returns the size of a given JSON array value - extended version.
250 *
251 * @returns IPRT status code.
252 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an array.
253 * @param hJsonVal The JSON value handle.
254 * @param pcItems Where to store the size of the JSON array value on success.
255 */
256RTDECL(int) RTJsonValueQueryArraySize(RTJSONVAL hJsonVal, unsigned *pcItems);
257
258/**
259 * Returns the value for the given index of a given JSON array value.
260 *
261 * @returns IPRT status code.
262 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an array.
263 * @retval VERR_OUT_OF_RANGE if @a idx is out of bounds.
264 *
265 * @param hJsonVal The JSON value handle.
266 * @param idx The index to get the value from.
267 * @param phJsonVal Where to store the handle to the JSON value on success.
268 */
269RTDECL(int) RTJsonValueQueryByIndex(RTJSONVAL hJsonVal, unsigned idx, PRTJSONVAL phJsonVal);
270
271/**
272 * Creates an iterator for a given JSON array or object value.
273 *
274 * @returns IPRT status code.
275 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an array or
276 * object.
277 * @param hJsonVal The JSON value handle.
278 * @param phJsonIt Where to store the JSON iterator handle on success.
279 */
280RTDECL(int) RTJsonIteratorBegin(RTJSONVAL hJsonVal, PRTJSONIT phJsonIt);
281
282/**
283 * Gets the value and optional name for the current iterator position.
284 *
285 * @returns IPRT status code.
286 * @param hJsonIt The JSON iterator handle.
287 * @param phJsonVal Where to store the handle to the JSON value on success.
288 * @param ppszName Where to store the object member name for an object.
289 * NULL is returned for arrays.
290 */
291RTDECL(int) RTJsonIteratorQueryValue(RTJSONIT hJsonIt, PRTJSONVAL phJsonVal, const char **ppszName);
292
293/**
294 * Advances to the next element in the referenced JSON value.
295 *
296 * @returns IPRT status code.
297 * @retval VERR_JSON_ITERATOR_END if the end for this iterator was reached.
298 * @param hJsonIt The JSON iterator handle.
299 */
300RTDECL(int) RTJsonIteratorNext(RTJSONIT hJsonIt);
301
302/**
303 * Frees a given JSON iterator.
304 *
305 * @param hJsonIt The JSON iterator to free.
306 */
307RTDECL(void) RTJsonIteratorFree(RTJSONIT hJsonIt);
308
309RT_C_DECLS_END
310
311/** @} */
312
313#endif
314
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette