VirtualBox

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

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

*: scm --fix-header-guard-endif

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.4 KB
Line 
1/** @file
2 * IPRT - JavaScript Object Notation (JSON) Parser.
3 */
4
5/*
6 * Copyright (C) 2016-2019 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_INCLUDED_json_h
27#define IPRT_INCLUDED_json_h
28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
31
32#include <iprt/types.h>
33
34RT_C_DECLS_BEGIN
35
36
37/** @defgroup grp_json RTJson - JavaScript Object Notation (JSON) Parser
38 * @ingroup grp_rt
39 * @{
40 */
41
42/**
43 * JSON value types.
44 */
45typedef enum RTJSONVALTYPE
46{
47 /** Invalid first value. */
48 RTJSONVALTYPE_INVALID = 0,
49 /** Value containing an object. */
50 RTJSONVALTYPE_OBJECT,
51 /** Value containing an array. */
52 RTJSONVALTYPE_ARRAY,
53 /** Value containing a string. */
54 RTJSONVALTYPE_STRING,
55 /** Value containg an integer number. */
56 RTJSONVALTYPE_INTEGER,
57 /** Value containg an floating point number. */
58 RTJSONVALTYPE_NUMBER,
59 /** Value containg the special null value. */
60 RTJSONVALTYPE_NULL,
61 /** Value containing true. */
62 RTJSONVALTYPE_TRUE,
63 /** Value containing false. */
64 RTJSONVALTYPE_FALSE,
65 /** 32-bit hack. */
66 RTJSONVALTYPE_32BIT_HACK = 0x7fffffff
67} RTJSONVALTYPE;
68/** Pointer to a JSON value type. */
69typedef RTJSONVALTYPE *PRTJSONVALTYPE;
70
71/** JSON value handle. */
72typedef struct RTJSONVALINT *RTJSONVAL;
73/** Pointer to a JSON value handle. */
74typedef RTJSONVAL *PRTJSONVAL;
75/** NIL JSON value handle. */
76#define NIL_RTJSONVAL ((RTJSONVAL)~(uintptr_t)0)
77
78/** JSON iterator handle. */
79typedef struct RTJSONITINT *RTJSONIT;
80/** Pointer to a JSON iterator handle. */
81typedef RTJSONIT *PRTJSONIT;
82/** NIL JSON iterator handle. */
83#define NIL_RTJSONIT ((RTJSONIT)~(uintptr_t)0)
84
85/**
86 * Parses a JSON document in the provided buffer returning the root JSON value.
87 *
88 * @returns IPRT status code.
89 * @retval VERR_JSON_MALFORMED if the document does not conform to the spec.
90 * @param phJsonVal Where to store the handle to the JSON value on success.
91 * @param pbBuf The byte buffer containing the JSON document.
92 * @param cbBuf Size of the buffer.
93 * @param pErrInfo Where to store extended error info. Optional.
94 *
95 * @todo r=bird: The use of uint8_t makes no sense here since the parser
96 * expects ASCII / UTF-8. What's more, if this is a real buffer the
97 * type should be 'const void *' rather than 'const uint8_t *'.
98 * This function should be modified to reflect that it's really for
99 * handling unterminated strings.
100 */
101RTDECL(int) RTJsonParseFromBuf(PRTJSONVAL phJsonVal, const uint8_t *pbBuf, size_t cbBuf, PRTERRINFO pErrInfo);
102
103/**
104 * Parses a JSON document from the provided string returning the root JSON value.
105 *
106 * @returns IPRT status code.
107 * @retval VERR_JSON_MALFORMED if the document does not conform to the spec.
108 * @param phJsonVal Where to store the handle to the JSON value on success.
109 * @param pszStr The string containing the JSON document.
110 * @param pErrInfo Where to store extended error info. Optional.
111 */
112RTDECL(int) RTJsonParseFromString(PRTJSONVAL phJsonVal, const char *pszStr, PRTERRINFO pErrInfo);
113
114/**
115 * Parses a JSON document from the file pointed to by the given filename
116 * returning the root JSON value.
117 *
118 * @returns IPRT status code.
119 * @retval VERR_JSON_MALFORMED if the document does not conform to the spec.
120 * @param phJsonVal Where to store the handle to the JSON value on success.
121 * @param pszFilename The name of the file containing the JSON document.
122 * @param pErrInfo Where to store extended error info. Optional.
123 */
124RTDECL(int) RTJsonParseFromFile(PRTJSONVAL phJsonVal, const char *pszFilename, PRTERRINFO pErrInfo);
125
126/**
127 * Retain a given JSON value.
128 *
129 * @returns New reference count.
130 * @param hJsonVal The JSON value handle.
131 */
132RTDECL(uint32_t) RTJsonValueRetain(RTJSONVAL hJsonVal);
133
134/**
135 * Release a given JSON value.
136 *
137 * @returns New reference count, if this drops to 0 the value is freed.
138 * @param hJsonVal The JSON value handle.
139 */
140RTDECL(uint32_t) RTJsonValueRelease(RTJSONVAL hJsonVal);
141
142/**
143 * Return the type of a given JSON value.
144 *
145 * @returns Type of the given JSON value.
146 * @param hJsonVal The JSON value handle.
147 */
148RTDECL(RTJSONVALTYPE) RTJsonValueGetType(RTJSONVAL hJsonVal);
149
150/**
151 * Translates value type to a name.
152 *
153 * @returns Readonly name string
154 * @param enmType The JSON value type to name.
155 */
156RTDECL(const char *) RTJsonValueTypeName(RTJSONVALTYPE enmType);
157
158/**
159 * Returns the string from a given JSON string value.
160 *
161 * @returns Pointer to the string of the JSON value, NULL if the value type
162 * doesn't indicate a string.
163 * @param hJsonVal The JSON value handle.
164 */
165RTDECL(const char *) RTJsonValueGetString(RTJSONVAL hJsonVal);
166
167/**
168 * Returns the string from a given JSON string value, extended.
169 *
170 * @returns IPRT status code.
171 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not a string.
172 * @param hJsonVal The JSON value handle.
173 * @param ppszStr Where to store the pointer to the string on success.
174 */
175RTDECL(int) RTJsonValueQueryString(RTJSONVAL hJsonVal, const char **ppszStr);
176
177/**
178 * Returns the integer from a given JSON integer value.
179 *
180 * @returns IPRT status code.
181 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not a number.
182 * @param hJsonVal The JSON value handle.
183 * @param pi64Num WHere to store the number on success.
184 * @sa RTJsonValueQueryNumber
185 */
186RTDECL(int) RTJsonValueQueryInteger(RTJSONVAL hJsonVal, int64_t *pi64Num);
187
188/**
189 * Returns the floating point value from a given JSON number value.
190 *
191 * @returns IPRT status code.
192 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not a number.
193 * @param hJsonVal The JSON value handle.
194 * @param prdNum WHere to store the floating point number on success.
195 * @sa RTJsonValueQueryInteger
196 */
197RTDECL(int) RTJsonValueQueryNumber(RTJSONVAL hJsonVal, double *prdNum);
198
199/**
200 * Returns the 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.
204 * @retval VERR_NOT_FOUND if the name is not known for this JSON object.
205 * @param hJsonVal The JSON value handle.
206 * @param pszName The member name of the object.
207 * @param phJsonVal Where to store the handle to the JSON value on success.
208 */
209RTDECL(int) RTJsonValueQueryByName(RTJSONVAL hJsonVal, const char *pszName, PRTJSONVAL phJsonVal);
210
211/**
212 * Returns the number of a number value associated with a given name for the given JSON object value.
213 *
214 * @returns IPRT status code.
215 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object or
216 * the name does not point to an integer value.
217 * @retval VERR_NOT_FOUND if the name is not known for this JSON object.
218 * @param hJsonVal The JSON value handle.
219 * @param pszName The member name of the object.
220 * @param pi64Num Where to store the number on success.
221 */
222RTDECL(int) RTJsonValueQueryIntegerByName(RTJSONVAL hJsonVal, const char *pszName, int64_t *pi64Num);
223
224/**
225 * Returns the number of a number value associated with a given name for the given JSON object value.
226 *
227 * @returns IPRT status code.
228 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object or
229 * the name does not point to a number value.
230 * @retval VERR_NOT_FOUND if the name is not known for this JSON object.
231 * @param hJsonVal The JSON value handle.
232 * @param pszName The member name of the object.
233 * @param prdNum WHere to store the floating point number on success.
234 */
235RTDECL(int) RTJsonValueQueryNumberByName(RTJSONVAL hJsonVal, const char *pszName, double *prdNum);
236
237/**
238 * Returns the string of a string value associated with a given name for the given JSON object value.
239 *
240 * @returns IPRT status code.
241 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object or
242 * the name does not point to a string value.
243 * @retval VERR_NOT_FOUND if the name is not known for this JSON object.
244 * @param hJsonVal The JSON value handle.
245 * @param pszName The member name of the object.
246 * @param ppszStr Where to store the pointer to the string on success.
247 * Must be freed with RTStrFree().
248 */
249RTDECL(int) RTJsonValueQueryStringByName(RTJSONVAL hJsonVal, const char *pszName, char **ppszStr);
250
251/**
252 * Returns the boolean of a true/false value associated with a given name for the given JSON object value.
253 *
254 * @returns IPRT status code.
255 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object or
256 * the name does not point to a true/false value.
257 * @retval VERR_NOT_FOUND if the name is not known for this JSON object.
258 * @param hJsonVal The JSON value handle.
259 * @param pszName The member name of the object.
260 * @param pfBoolean Where to store the boolean value on success.
261 */
262RTDECL(int) RTJsonValueQueryBooleanByName(RTJSONVAL hJsonVal, const char *pszName, bool *pfBoolean);
263
264/**
265 * Returns the size of a given JSON array value.
266 *
267 * @returns Size of the JSON array value.
268 * @retval 0 if the array is empty or the JSON value is not an array.
269 * @param hJsonVal The JSON value handle.
270 */
271RTDECL(unsigned) RTJsonValueGetArraySize(RTJSONVAL hJsonVal);
272
273/**
274 * Returns the size of a given JSON array value - extended version.
275 *
276 * @returns IPRT status code.
277 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an array.
278 * @param hJsonVal The JSON value handle.
279 * @param pcItems Where to store the size of the JSON array value on success.
280 */
281RTDECL(int) RTJsonValueQueryArraySize(RTJSONVAL hJsonVal, unsigned *pcItems);
282
283/**
284 * Returns the value for the given index of a given JSON array value.
285 *
286 * @returns IPRT status code.
287 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an array.
288 * @retval VERR_OUT_OF_RANGE if @a idx is out of bounds.
289 *
290 * @param hJsonVal The JSON value handle.
291 * @param idx The index to get the value from.
292 * @param phJsonVal Where to store the handle to the JSON value on success.
293 */
294RTDECL(int) RTJsonValueQueryByIndex(RTJSONVAL hJsonVal, unsigned idx, PRTJSONVAL phJsonVal);
295
296/**
297 * Creates an iterator for a given JSON array or object value.
298 *
299 * @returns IPRT status code.
300 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an array or
301 * object.
302 * @param hJsonVal The JSON value handle.
303 * @param phJsonIt Where to store the JSON iterator handle on success.
304 * @todo Make return VERR_JSON_IS_EMPTY (or remove it).
305 */
306RTDECL(int) RTJsonIteratorBegin(RTJSONVAL hJsonVal, PRTJSONIT phJsonIt);
307
308/**
309 * Creates an iterator for a given JSON array value.
310 *
311 * @returns IPRT status code.
312 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an array.
313 * @retval VERR_JSON_IS_EMPTY if no members.
314 * @param hJsonVal The JSON value handle.
315 * @param phJsonIt Where to store the JSON iterator handle on success.
316 */
317RTDECL(int) RTJsonIteratorBeginArray(RTJSONVAL hJsonVal, PRTJSONIT phJsonIt);
318
319/**
320 * Creates an iterator for a given JSON object value.
321 *
322 * @returns IPRT status code.
323 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object.
324 * @retval VERR_JSON_IS_EMPTY if no members.
325 * @param hJsonVal The JSON value handle.
326 * @param phJsonIt Where to store the JSON iterator handle on success.
327 */
328RTDECL(int) RTJsonIteratorBeginObject(RTJSONVAL hJsonVal, PRTJSONIT phJsonIt);
329
330/**
331 * Gets the value and optional name for the current iterator position.
332 *
333 * @returns IPRT status code.
334 * @param hJsonIt The JSON iterator handle.
335 * @param phJsonVal Where to store the handle to the JSON value on success.
336 * @param ppszName Where to store the object member name for an object.
337 * NULL is returned for arrays.
338 */
339RTDECL(int) RTJsonIteratorQueryValue(RTJSONIT hJsonIt, PRTJSONVAL phJsonVal, const char **ppszName);
340
341/**
342 * Advances to the next element in the referenced JSON value.
343 *
344 * @returns IPRT status code.
345 * @retval VERR_JSON_ITERATOR_END if the end for this iterator was reached.
346 * @param hJsonIt The JSON iterator handle.
347 */
348RTDECL(int) RTJsonIteratorNext(RTJSONIT hJsonIt);
349
350/**
351 * Frees a given JSON iterator.
352 *
353 * @param hJsonIt The JSON iterator to free.
354 */
355RTDECL(void) RTJsonIteratorFree(RTJSONIT hJsonIt);
356
357RT_C_DECLS_END
358
359/** @} */
360
361#endif /* !IPRT_INCLUDED_json_h */
362
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