1 | /** @file
|
---|
2 | * IPRT - JavaScript Object Notation (JSON) Parser.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2016 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 |
|
---|
32 | RT_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 | */
|
---|
43 | typedef 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. */
|
---|
65 | typedef RTJSONVALTYPE *PRTJSONVALTYPE;
|
---|
66 |
|
---|
67 | /** JSON value handle. */
|
---|
68 | typedef struct RTJSONVALINT *RTJSONVAL;
|
---|
69 | /** Pointer to a JSON value handle. */
|
---|
70 | typedef RTJSONVAL *PRTJSONVAL;
|
---|
71 | /** NIL JSON value handle. */
|
---|
72 | #define NIL_RTJSONVAL ((RTJSONVAL)~(uintptr_t)0)
|
---|
73 |
|
---|
74 | /** JSON iterator handle. */
|
---|
75 | typedef struct RTJSONITINT *RTJSONIT;
|
---|
76 | /** Pointer to a JSON iterator handle. */
|
---|
77 | typedef 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 | RTDECL(int) RTJsonParseFromBuf(PRTJSONVAL phJsonVal, const uint8_t *pbBuf, size_t cbBuf, PRTERRINFO pErrInfo);
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Parses a JSON document from the provided string returning the root JSON value.
|
---|
95 | *
|
---|
96 | * @returns IPRT status code.
|
---|
97 | * @retval VERR_JSON_MALFORMED if the document does not conform to the spec.
|
---|
98 | * @param phJsonVal Where to store the handle to the JSON value on success.
|
---|
99 | * @param pszStr The string containing the JSON document.
|
---|
100 | * @param pErrInfo Where to store extended error info. Optional.
|
---|
101 | */
|
---|
102 | RTDECL(int) RTJsonParseFromString(PRTJSONVAL phJsonVal, const char *pszStr, PRTERRINFO pErrInfo);
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Parses a JSON document from the file pointed to by the given filename
|
---|
106 | * returning the root JSON value.
|
---|
107 | *
|
---|
108 | * @returns IPRT status code.
|
---|
109 | * @retval VERR_JSON_MALFORMED if the document does not conform to the spec.
|
---|
110 | * @param phJsonVal Where to store the handle to the JSON value on success.
|
---|
111 | * @param pszFilename The name of the file containing the JSON document.
|
---|
112 | * @param pErrInfo Where to store extended error info. Optional.
|
---|
113 | */
|
---|
114 | RTDECL(int) RTJsonParseFromFile(PRTJSONVAL phJsonVal, const char *pszFilename, PRTERRINFO pErrInfo);
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Retain a given JSON value.
|
---|
118 | *
|
---|
119 | * @returns New reference count.
|
---|
120 | * @param hJsonVal The JSON value handle.
|
---|
121 | */
|
---|
122 | RTDECL(uint32_t) RTJsonValueRetain(RTJSONVAL hJsonVal);
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Release a given JSON value.
|
---|
126 | *
|
---|
127 | * @returns New reference count, if this drops to 0 the value is freed.
|
---|
128 | * @param hJsonVal The JSON value handle.
|
---|
129 | */
|
---|
130 | RTDECL(uint32_t) RTJsonValueRelease(RTJSONVAL hJsonVal);
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Return the type of a given JSON value.
|
---|
134 | *
|
---|
135 | * @returns Type of the given JSON value.
|
---|
136 | * @param hJsonVal The JSON value handle.
|
---|
137 | */
|
---|
138 | RTDECL(RTJSONVALTYPE) RTJsonValueGetType(RTJSONVAL hJsonVal);
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Returns the string from a given JSON string value.
|
---|
142 | *
|
---|
143 | * @returns Pointer to the string of the JSON value, NULL if the value type
|
---|
144 | * doesn't indicate a string.
|
---|
145 | * @param hJsonVal The JSON value handle.
|
---|
146 | */
|
---|
147 | RTDECL(const char *) RTJsonValueGetString(RTJSONVAL hJsonVal);
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Returns the string from a given JSON string value, extended.
|
---|
151 | *
|
---|
152 | * @returns IPRT status code.
|
---|
153 | * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not a string.
|
---|
154 | * @param hJsonVal The JSON value handle.
|
---|
155 | * @param ppszStr Where to store the pointer to the string on success.
|
---|
156 | * @todo r=bird: Rename to RTJsonValueQueryString as getters returns values
|
---|
157 | * not status codes (see guidelines).
|
---|
158 | */
|
---|
159 | RTDECL(int) RTJsonValueGetStringEx(RTJSONVAL hJsonVal, const char **ppszStr);
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Returns the number from a given JSON number value.
|
---|
163 | *
|
---|
164 | * @returns IPRT status code.
|
---|
165 | * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not a number.
|
---|
166 | * @param hJsonVal The JSON value handle.
|
---|
167 | * @param pi64Num WHere to store the number on success.
|
---|
168 | *
|
---|
169 | * @note This JSON implementation does not implement support for floating point
|
---|
170 | * numbers currently. When it does, it will be in the form of a
|
---|
171 | * RTJsonValueQueryFloat method.
|
---|
172 | *
|
---|
173 | * @todo r=bird: Rename to RTJsonValueQueryInteger as getters returns values
|
---|
174 | * not status codes (see guidelines), and "Integer" is better for
|
---|
175 | * future floating point support.
|
---|
176 | */
|
---|
177 | RTDECL(int) RTJsonValueGetNumber(RTJSONVAL hJsonVal, int64_t *pi64Num);
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Returns the value associated with a given name for the given JSON object value.
|
---|
181 | *
|
---|
182 | * @returns IPRT status code.
|
---|
183 | * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object.
|
---|
184 | * @retval VERR_NOT_FOUND if the name is not known for this JSON object.
|
---|
185 | * @param hJsonVal The JSON value handle.
|
---|
186 | * @param pszName The member name of the object.
|
---|
187 | * @param phJsonVal Where to store the handle to the JSON value on success.
|
---|
188 | * @todo r=bird: Rename to RTJsonValueQueryByName as getters returns
|
---|
189 | * values not status codes (see guidelines).
|
---|
190 | */
|
---|
191 | RTDECL(int) RTJsonValueGetByName(RTJSONVAL hJsonVal, const char *pszName, PRTJSONVAL phJsonVal);
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Returns the number of a number value associated with a given name for the given JSON object value.
|
---|
195 | *
|
---|
196 | * @returns IPRT status code.
|
---|
197 | * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object or
|
---|
198 | * the name does not point to a number value.
|
---|
199 | * @retval VERR_NOT_FOUND if the name is not known for this JSON object.
|
---|
200 | * @param hJsonVal The JSON value handle.
|
---|
201 | * @param pszName The member name of the object.
|
---|
202 | * @param pi64Num Where to store the number on success.
|
---|
203 | * @todo r=bird: Rename to RTJsonValueQueryNumberByName as getters returns
|
---|
204 | * values not status codes (see guidelines).
|
---|
205 | */
|
---|
206 | RTDECL(int) RTJsonValueGetNumberByName(RTJSONVAL hJsonVal, const char *pszName, int64_t *pi64Num);
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Returns the string of a string value associated with a given name for the given JSON object value.
|
---|
210 | *
|
---|
211 | * @returns IPRT status code.
|
---|
212 | * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object or
|
---|
213 | * the name does not point to a string value.
|
---|
214 | * @retval VERR_NOT_FOUND if the name is not known for this JSON object.
|
---|
215 | * @param hJsonVal The JSON value handle.
|
---|
216 | * @param pszName The member name of the object.
|
---|
217 | * @param ppszStr Where to store the pointer to the string on success.
|
---|
218 | * Must be freed with RTStrFree().
|
---|
219 | * @todo r=bird: Rename to RTJsonValueQueryStringByName as getters returns
|
---|
220 | * values not status codes (see guidelines).
|
---|
221 | */
|
---|
222 | RTDECL(int) RTJsonValueGetStringByName(RTJSONVAL hJsonVal, const char *pszName, char **ppszStr);
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Returns the boolean of a true/false 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 true/false 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 pfBoolean Where to store the boolean value on success.
|
---|
234 | * @todo r=bird: Rename to RTJsonValueQueryBooleanByName as getters returns
|
---|
235 | * values not status codes (see guidelines).
|
---|
236 | */
|
---|
237 | RTDECL(int) RTJsonValueGetBooleanByName(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 | */
|
---|
246 | RTDECL(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 | * @todo r=bird: Rename to RTJsonValueQueryArraySize as getters returns
|
---|
256 | * values not status codes (see guidelines).
|
---|
257 | * Implementation is using 'uint32_t' not 'unsigned', goes for the
|
---|
258 | * other functions here.
|
---|
259 | */
|
---|
260 | RTDECL(int) RTJsonValueGetArraySizeEx(RTJSONVAL hJsonVal, unsigned *pcItems);
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Returns the value for the given index of a given JSON array value.
|
---|
264 | *
|
---|
265 | * @returns IPRT status code.
|
---|
266 | * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an array.
|
---|
267 | * @retval VERR_OUT_OF_RANGE if @a idx is out of bounds.
|
---|
268 | *
|
---|
269 | * @param hJsonVal The JSON value handle.
|
---|
270 | * @param idx The index to get the value from.
|
---|
271 | * @param phJsonVal Where to store the handle to the JSON value on success.
|
---|
272 | * @todo r=bird: Rename to RTJsonValueQueryByIndex as getters returns
|
---|
273 | * values not status codes (see guidelines).
|
---|
274 | */
|
---|
275 | RTDECL(int) RTJsonValueGetByIndex(RTJSONVAL hJsonVal, unsigned idx, PRTJSONVAL phJsonVal);
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * Creates an iterator for a given JSON array or object value.
|
---|
279 | *
|
---|
280 | * @returns IPRT status code.
|
---|
281 | * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an array or
|
---|
282 | * object.
|
---|
283 | * @param hJsonVal The JSON value handle.
|
---|
284 | * @param phJsonIt Where to store the JSON iterator handle on success.
|
---|
285 | */
|
---|
286 | RTDECL(int) RTJsonIteratorBegin(RTJSONVAL hJsonVal, PRTJSONIT phJsonIt);
|
---|
287 |
|
---|
288 | /**
|
---|
289 | * Gets the value and optional name for the current iterator position.
|
---|
290 | *
|
---|
291 | * @returns IPRT status code.
|
---|
292 | * @param hJsonIt The JSON iterator handle.
|
---|
293 | * @param phJsonVal Where to store the handle to the JSON value on success.
|
---|
294 | * @param ppszName Where to store the object member name for an object.
|
---|
295 | * NULL is returned for arrays.
|
---|
296 | * @todo r=bird: Rename to RTJsonIteratorQueryValue as getters returns
|
---|
297 | * values not status codes (see guidelines).
|
---|
298 | */
|
---|
299 | RTDECL(int) RTJsonIteratorGetValue(RTJSONIT hJsonIt, PRTJSONVAL phJsonVal, const char **ppszName);
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * Advances to the next element in the referenced JSON value.
|
---|
303 | *
|
---|
304 | * @returns IPRT status code.
|
---|
305 | * @retval VERR_JSON_ITERATOR_END if the end for this iterator was reached.
|
---|
306 | * @param hJsonIt The JSON iterator handle.
|
---|
307 | */
|
---|
308 | RTDECL(int) RTJsonIteratorNext(RTJSONIT hJsonIt);
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * Frees a given JSON iterator.
|
---|
312 | *
|
---|
313 | * @param hJsonIt The JSON iterator to free.
|
---|
314 | */
|
---|
315 | RTDECL(void) RTJsonIteratorFree(RTJSONIT hJsonIt);
|
---|
316 |
|
---|
317 | RT_C_DECLS_END
|
---|
318 |
|
---|
319 | /** @} */
|
---|
320 |
|
---|
321 | #endif
|
---|
322 |
|
---|