VirtualBox

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

Last change on this file was 108874, checked in by vboxsync, 5 weeks ago

Runtime/RTJson*: Add support for a subset of the JSON5 specification, bugref:10388 [doxygen]

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.6 KB
Line 
1/** @file
2 * IPRT - JavaScript Object Notation (JSON) Parser.
3 */
4
5/*
6 * Copyright (C) 2016-2024 Oracle and/or its affiliates.
7 *
8 * This file is part of VirtualBox base platform packages, as
9 * available from https://www.215389.xyz.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation, in version 3 of the
14 * License.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <https://www.gnu.org/licenses>.
23 *
24 * The contents of this file may alternatively be used under the terms
25 * of the Common Development and Distribution License Version 1.0
26 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
27 * in the VirtualBox distribution, in which case the provisions of the
28 * CDDL are applicable instead of those of the GPL.
29 *
30 * You may elect to license modified versions of this file under the
31 * terms and conditions of either the GPL or the CDDL or both.
32 *
33 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
34 */
35
36#ifndef IPRT_INCLUDED_json_h
37#define IPRT_INCLUDED_json_h
38#ifndef RT_WITHOUT_PRAGMA_ONCE
39# pragma once
40#endif
41
42#include <iprt/types.h>
43
44RT_C_DECLS_BEGIN
45
46
47/** @defgroup grp_json RTJson - JavaScript Object Notation (JSON) Parser
48 * @ingroup grp_rt
49 * @{
50 */
51
52/**
53 * JSON value types.
54 */
55typedef enum RTJSONVALTYPE
56{
57 /** Invalid first value. */
58 RTJSONVALTYPE_INVALID = 0,
59 /** Value containing an object. */
60 RTJSONVALTYPE_OBJECT,
61 /** Value containing an array. */
62 RTJSONVALTYPE_ARRAY,
63 /** Value containing a string. */
64 RTJSONVALTYPE_STRING,
65 /** Value containg an integer number. */
66 RTJSONVALTYPE_INTEGER,
67 /** Value containg an floating point number. */
68 RTJSONVALTYPE_NUMBER,
69 /** Value containg the special null value. */
70 RTJSONVALTYPE_NULL,
71 /** Value containing true. */
72 RTJSONVALTYPE_TRUE,
73 /** Value containing false. */
74 RTJSONVALTYPE_FALSE,
75 /** 32-bit hack. */
76 RTJSONVALTYPE_32BIT_HACK = 0x7fffffff
77} RTJSONVALTYPE;
78/** Pointer to a JSON value type. */
79typedef RTJSONVALTYPE *PRTJSONVALTYPE;
80
81/** JSON value handle. */
82typedef struct RTJSONVALINT *RTJSONVAL;
83/** Pointer to a JSON value handle. */
84typedef RTJSONVAL *PRTJSONVAL;
85/** NIL JSON value handle. */
86#define NIL_RTJSONVAL ((RTJSONVAL)~(uintptr_t)0)
87
88/** JSON iterator handle. */
89typedef struct RTJSONITINT *RTJSONIT;
90/** Pointer to a JSON iterator handle. */
91typedef RTJSONIT *PRTJSONIT;
92/** NIL JSON iterator handle. */
93#define NIL_RTJSONIT ((RTJSONIT)~(uintptr_t)0)
94
95/** Allow JSON5 extensions. */
96#define RTJSON_PARSE_F_JSON5 RT_BIT_32(0)
97/** Mask of valid flags. */
98#define RTJSON_PARSE_F_VALID RTJSON_PARSE_F_JSON5
99
100
101
102/**
103 * Parses a JSON document in the provided buffer returning the root JSON value.
104 *
105 * @returns IPRT status code.
106 * @retval VERR_JSON_MALFORMED if the document does not conform to the spec.
107 * @param phJsonVal Where to store the handle to the JSON value on success.
108 * @param fFlags Combination of RTJSON_PARSE_F_XXX.
109 * @param pbBuf The byte buffer containing the JSON document.
110 * @param cbBuf Size of the buffer.
111 * @param pErrInfo Where to store extended error info. Optional.
112 *
113 * @todo r=bird: The use of uint8_t makes no sense here since the parser
114 * expects ASCII / UTF-8. What's more, if this is a real buffer the
115 * type should be 'const void *' rather than 'const uint8_t *'.
116 * This function should be modified to reflect that it's really for
117 * handling unterminated strings.
118 */
119RTDECL(int) RTJsonParseFromBuf(PRTJSONVAL phJsonVal, uint32_t fFlags, const uint8_t *pbBuf, size_t cbBuf, PRTERRINFO pErrInfo);
120
121/**
122 * Parses a JSON document from the provided string returning the root JSON value.
123 *
124 * @returns IPRT status code.
125 * @retval VERR_JSON_MALFORMED if the document does not conform to the spec.
126 * @param phJsonVal Where to store the handle to the JSON value on success.
127 * @param fFlags Combination of RTJSON_PARSE_F_XXX.
128 * @param pszStr The string containing the JSON document.
129 * @param pErrInfo Where to store extended error info. Optional.
130 */
131RTDECL(int) RTJsonParseFromString(PRTJSONVAL phJsonVal, uint32_t fFlags, const char *pszStr, PRTERRINFO pErrInfo);
132
133/**
134 * Parses a JSON document from the file pointed to by the given filename
135 * returning the root JSON value.
136 *
137 * @returns IPRT status code.
138 * @retval VERR_JSON_MALFORMED if the document does not conform to the spec.
139 * @param phJsonVal Where to store the handle to the JSON value on success.
140 * @param fFlags Combination of RTJSON_PARSE_F_XXX.
141 * @param pszFilename The name of the file containing the JSON document.
142 * @param pErrInfo Where to store extended error info. Optional.
143 */
144RTDECL(int) RTJsonParseFromFile(PRTJSONVAL phJsonVal, uint32_t fFlags, const char *pszFilename, PRTERRINFO pErrInfo);
145
146/**
147 * Parses a JSON document from the given VFS file
148 * returning the root JSON value.
149 *
150 * @returns IPRT status code.
151 * @retval VERR_JSON_MALFORMED if the document does not conform to the spec.
152 * @param phJsonVal Where to store the handle to the JSON value on success.
153 * @param fFlags Combination of RTJSON_PARSE_F_XXX.
154 * @param hVfsFile The VFS file to parse.
155 * @param pErrInfo Where to store extended error info. Optional.
156 */
157RTDECL(int) RTJsonParseFromVfsFile(PRTJSONVAL phJsonVal, uint32_t fFlags, RTVFSFILE hVfsFile, PRTERRINFO pErrInfo);
158
159/**
160 * Retain a given JSON value.
161 *
162 * @returns New reference count.
163 * @param hJsonVal The JSON value handle.
164 */
165RTDECL(uint32_t) RTJsonValueRetain(RTJSONVAL hJsonVal);
166
167/**
168 * Release a given JSON value.
169 *
170 * @returns New reference count, if this drops to 0 the value is freed.
171 * @param hJsonVal The JSON value handle.
172 */
173RTDECL(uint32_t) RTJsonValueRelease(RTJSONVAL hJsonVal);
174
175/**
176 * Return the type of a given JSON value.
177 *
178 * @returns Type of the given JSON value.
179 * @param hJsonVal The JSON value handle.
180 */
181RTDECL(RTJSONVALTYPE) RTJsonValueGetType(RTJSONVAL hJsonVal);
182
183/**
184 * Translates value type to a name.
185 *
186 * @returns Readonly name string
187 * @param enmType The JSON value type to name.
188 */
189RTDECL(const char *) RTJsonValueTypeName(RTJSONVALTYPE enmType);
190
191/**
192 * Returns the string from a given JSON string value.
193 *
194 * @returns Pointer to the string of the JSON value, NULL if the value type
195 * doesn't indicate a string.
196 * @param hJsonVal The JSON value handle.
197 */
198RTDECL(const char *) RTJsonValueGetString(RTJSONVAL hJsonVal);
199
200/**
201 * Returns the string from a given JSON string value, extended.
202 *
203 * @returns IPRT status code.
204 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not a string.
205 * @param hJsonVal The JSON value handle.
206 * @param ppszStr Where to store the pointer to the string on success.
207 */
208RTDECL(int) RTJsonValueQueryString(RTJSONVAL hJsonVal, const char **ppszStr);
209
210/**
211 * Returns the integer from a given JSON integer value.
212 *
213 * @returns IPRT status code.
214 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not a number.
215 * @param hJsonVal The JSON value handle.
216 * @param pi64Num WHere to store the number on success.
217 * @sa RTJsonValueQueryNumber
218 */
219RTDECL(int) RTJsonValueQueryInteger(RTJSONVAL hJsonVal, int64_t *pi64Num);
220
221/**
222 * Returns the floating point value from a given JSON number value.
223 *
224 * @returns IPRT status code.
225 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not a number.
226 * @param hJsonVal The JSON value handle.
227 * @param prdNum WHere to store the floating point number on success.
228 * @sa RTJsonValueQueryInteger
229 */
230RTDECL(int) RTJsonValueQueryNumber(RTJSONVAL hJsonVal, double *prdNum);
231
232/**
233 * Returns the value associated with a given name for the given JSON object value.
234 *
235 * @returns IPRT status code.
236 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object.
237 * @retval VERR_NOT_FOUND if the name is not known for this JSON object.
238 * @param hJsonVal The JSON value handle.
239 * @param pszName The member name of the object.
240 * @param phJsonVal Where to store the handle to the JSON value on success.
241 */
242RTDECL(int) RTJsonValueQueryByName(RTJSONVAL hJsonVal, const char *pszName, PRTJSONVAL phJsonVal);
243
244/**
245 * Returns the number of a number value associated with a given name for the given JSON object value.
246 *
247 * @returns IPRT status code.
248 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object or
249 * the name does not point to an integer value.
250 * @retval VERR_NOT_FOUND if the name is not known for this JSON object.
251 * @param hJsonVal The JSON value handle.
252 * @param pszName The member name of the object.
253 * @param pi64Num Where to store the number on success.
254 */
255RTDECL(int) RTJsonValueQueryIntegerByName(RTJSONVAL hJsonVal, const char *pszName, int64_t *pi64Num);
256
257/**
258 * Returns the number of a number value associated with a given name for the given JSON object value.
259 *
260 * @returns IPRT status code.
261 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object or
262 * the name does not point to a number value.
263 * @retval VERR_NOT_FOUND if the name is not known for this JSON object.
264 * @param hJsonVal The JSON value handle.
265 * @param pszName The member name of the object.
266 * @param prdNum WHere to store the floating point number on success.
267 */
268RTDECL(int) RTJsonValueQueryNumberByName(RTJSONVAL hJsonVal, const char *pszName, double *prdNum);
269
270/**
271 * Returns the string of a string value associated with a given name for the given JSON object value.
272 *
273 * @returns IPRT status code.
274 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object or
275 * the name does not point to a string value.
276 * @retval VERR_NOT_FOUND if the name is not known for this JSON object.
277 * @param hJsonVal The JSON value handle.
278 * @param pszName The member name of the object.
279 * @param ppszStr Where to store the pointer to the string on success.
280 * Must be freed with RTStrFree().
281 */
282RTDECL(int) RTJsonValueQueryStringByName(RTJSONVAL hJsonVal, const char *pszName, char **ppszStr);
283
284/**
285 * Returns the boolean of a true/false value associated with a given name for the given JSON object value.
286 *
287 * @returns IPRT status code.
288 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object or
289 * the name does not point to a true/false value.
290 * @retval VERR_NOT_FOUND if the name is not known for this JSON object.
291 * @param hJsonVal The JSON value handle.
292 * @param pszName The member name of the object.
293 * @param pfBoolean Where to store the boolean value on success.
294 */
295RTDECL(int) RTJsonValueQueryBooleanByName(RTJSONVAL hJsonVal, const char *pszName, bool *pfBoolean);
296
297/**
298 * Returns the size of a given JSON array value.
299 *
300 * @returns Size of the JSON array value.
301 * @retval 0 if the array is empty or the JSON value is not an array.
302 * @param hJsonVal The JSON value handle.
303 */
304RTDECL(unsigned) RTJsonValueGetArraySize(RTJSONVAL hJsonVal);
305
306/**
307 * Returns the size of a given JSON array value - extended version.
308 *
309 * @returns IPRT status code.
310 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an array.
311 * @param hJsonVal The JSON value handle.
312 * @param pcItems Where to store the size of the JSON array value on success.
313 */
314RTDECL(int) RTJsonValueQueryArraySize(RTJSONVAL hJsonVal, unsigned *pcItems);
315
316/**
317 * Returns the value for the given index of a given JSON array value.
318 *
319 * @returns IPRT status code.
320 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an array.
321 * @retval VERR_OUT_OF_RANGE if @a idx is out of bounds.
322 *
323 * @param hJsonVal The JSON value handle.
324 * @param idx The index to get the value from.
325 * @param phJsonVal Where to store the handle to the JSON value on success.
326 */
327RTDECL(int) RTJsonValueQueryByIndex(RTJSONVAL hJsonVal, unsigned idx, PRTJSONVAL phJsonVal);
328
329/**
330 * Creates an iterator for a given JSON array or object value.
331 *
332 * @returns IPRT status code.
333 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an array or
334 * object.
335 * @param hJsonVal The JSON value handle.
336 * @param phJsonIt Where to store the JSON iterator handle on success.
337 * @todo Make return VERR_JSON_IS_EMPTY (or remove it).
338 */
339RTDECL(int) RTJsonIteratorBegin(RTJSONVAL hJsonVal, PRTJSONIT phJsonIt);
340
341/**
342 * Creates an iterator for a given JSON array value.
343 *
344 * @returns IPRT status code.
345 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an array.
346 * @retval VERR_JSON_IS_EMPTY if no members.
347 * @param hJsonVal The JSON value handle.
348 * @param phJsonIt Where to store the JSON iterator handle on success.
349 */
350RTDECL(int) RTJsonIteratorBeginArray(RTJSONVAL hJsonVal, PRTJSONIT phJsonIt);
351
352/**
353 * Creates an iterator for a given JSON object value.
354 *
355 * @returns IPRT status code.
356 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object.
357 * @retval VERR_JSON_IS_EMPTY if no members.
358 * @param hJsonVal The JSON value handle.
359 * @param phJsonIt Where to store the JSON iterator handle on success.
360 */
361RTDECL(int) RTJsonIteratorBeginObject(RTJSONVAL hJsonVal, PRTJSONIT phJsonIt);
362
363/**
364 * Gets the value and optional name for the current iterator position.
365 *
366 * @returns IPRT status code.
367 * @param hJsonIt The JSON iterator handle.
368 * @param phJsonVal Where to store the handle to the JSON value on success.
369 * @param ppszName Where to store the object member name for an object.
370 * NULL is returned for arrays.
371 */
372RTDECL(int) RTJsonIteratorQueryValue(RTJSONIT hJsonIt, PRTJSONVAL phJsonVal, const char **ppszName);
373
374/**
375 * Advances to the next element in the referenced JSON value.
376 *
377 * @returns IPRT status code.
378 * @retval VERR_JSON_ITERATOR_END if the end for this iterator was reached.
379 * @param hJsonIt The JSON iterator handle.
380 */
381RTDECL(int) RTJsonIteratorNext(RTJSONIT hJsonIt);
382
383/**
384 * Frees a given JSON iterator.
385 *
386 * @param hJsonIt The JSON iterator to free.
387 */
388RTDECL(void) RTJsonIteratorFree(RTJSONIT hJsonIt);
389
390RT_C_DECLS_END
391
392/** @} */
393
394#endif /* !IPRT_INCLUDED_json_h */
395
Note: See TracBrowser for help on using the repository browser.

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