VirtualBox

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

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

IPRT/json: Implemented support for parsing floating point values. bugref:9167

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

© 2023 Oracle
ContactPrivacy policyTerms of Use