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 |
|
---|
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 | * Translates value type to a name.
|
---|
142 | *
|
---|
143 | * @returns Readonly name string
|
---|
144 | * @param enmType The JSON value type to name.
|
---|
145 | */
|
---|
146 | RTDECL(const char *) RTJsonValueTypeName(RTJSONVALTYPE enmType);
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Returns the string from a given JSON string value.
|
---|
150 | *
|
---|
151 | * @returns Pointer to the string of the JSON value, NULL if the value type
|
---|
152 | * doesn't indicate a string.
|
---|
153 | * @param hJsonVal The JSON value handle.
|
---|
154 | */
|
---|
155 | RTDECL(const char *) RTJsonValueGetString(RTJSONVAL hJsonVal);
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Returns the string from a given JSON string value, extended.
|
---|
159 | *
|
---|
160 | * @returns IPRT status code.
|
---|
161 | * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not a string.
|
---|
162 | * @param hJsonVal The JSON value handle.
|
---|
163 | * @param ppszStr Where to store the pointer to the string on success.
|
---|
164 | */
|
---|
165 | RTDECL(int) RTJsonValueQueryString(RTJSONVAL hJsonVal, const char **ppszStr);
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Returns the number from a given JSON number value.
|
---|
169 | *
|
---|
170 | * @returns IPRT status code.
|
---|
171 | * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not a number.
|
---|
172 | * @param hJsonVal The JSON value handle.
|
---|
173 | * @param pi64Num WHere to store the number on success.
|
---|
174 | *
|
---|
175 | * @note This JSON implementation does not implement support for floating point
|
---|
176 | * numbers currently. When it does, it will be in the form of a
|
---|
177 | * RTJsonValueQueryFloat method.
|
---|
178 | */
|
---|
179 | RTDECL(int) RTJsonValueQueryInteger(RTJSONVAL hJsonVal, int64_t *pi64Num);
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Returns the value associated with a given name for the given JSON object value.
|
---|
183 | *
|
---|
184 | * @returns IPRT status code.
|
---|
185 | * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object.
|
---|
186 | * @retval VERR_NOT_FOUND if the name is not known for this JSON object.
|
---|
187 | * @param hJsonVal The JSON value handle.
|
---|
188 | * @param pszName The member name of the object.
|
---|
189 | * @param phJsonVal Where to store the handle to the JSON value on success.
|
---|
190 | */
|
---|
191 | RTDECL(int) RTJsonValueQueryByName(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 | */
|
---|
204 | RTDECL(int) RTJsonValueQueryIntegerByName(RTJSONVAL hJsonVal, const char *pszName, int64_t *pi64Num);
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Returns the string of a string value associated with a given name for the given JSON object value.
|
---|
208 | *
|
---|
209 | * @returns IPRT status code.
|
---|
210 | * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object or
|
---|
211 | * the name does not point to a string value.
|
---|
212 | * @retval VERR_NOT_FOUND if the name is not known for this JSON object.
|
---|
213 | * @param hJsonVal The JSON value handle.
|
---|
214 | * @param pszName The member name of the object.
|
---|
215 | * @param ppszStr Where to store the pointer to the string on success.
|
---|
216 | * Must be freed with RTStrFree().
|
---|
217 | */
|
---|
218 | RTDECL(int) RTJsonValueQueryStringByName(RTJSONVAL hJsonVal, const char *pszName, char **ppszStr);
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Returns the boolean of a true/false value associated with a given name for the given JSON object value.
|
---|
222 | *
|
---|
223 | * @returns IPRT status code.
|
---|
224 | * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object or
|
---|
225 | * the name does not point to a true/false value.
|
---|
226 | * @retval VERR_NOT_FOUND if the name is not known for this JSON object.
|
---|
227 | * @param hJsonVal The JSON value handle.
|
---|
228 | * @param pszName The member name of the object.
|
---|
229 | * @param pfBoolean Where to store the boolean value on success.
|
---|
230 | */
|
---|
231 | RTDECL(int) RTJsonValueQueryBooleanByName(RTJSONVAL hJsonVal, const char *pszName, bool *pfBoolean);
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Returns the size of a given JSON array value.
|
---|
235 | *
|
---|
236 | * @returns Size of the JSON array value.
|
---|
237 | * @retval 0 if the array is empty or the JSON value is not an array.
|
---|
238 | * @param hJsonVal The JSON value handle.
|
---|
239 | */
|
---|
240 | RTDECL(unsigned) RTJsonValueGetArraySize(RTJSONVAL hJsonVal);
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * Returns the size of a given JSON array value - extended version.
|
---|
244 | *
|
---|
245 | * @returns IPRT status code.
|
---|
246 | * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an array.
|
---|
247 | * @param hJsonVal The JSON value handle.
|
---|
248 | * @param pcItems Where to store the size of the JSON array value on success.
|
---|
249 | */
|
---|
250 | RTDECL(int) RTJsonValueQueryArraySize(RTJSONVAL hJsonVal, unsigned *pcItems);
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Returns the value for the given index of a given JSON array value.
|
---|
254 | *
|
---|
255 | * @returns IPRT status code.
|
---|
256 | * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an array.
|
---|
257 | * @retval VERR_OUT_OF_RANGE if @a idx is out of bounds.
|
---|
258 | *
|
---|
259 | * @param hJsonVal The JSON value handle.
|
---|
260 | * @param idx The index to get the value from.
|
---|
261 | * @param phJsonVal Where to store the handle to the JSON value on success.
|
---|
262 | */
|
---|
263 | RTDECL(int) RTJsonValueQueryByIndex(RTJSONVAL hJsonVal, unsigned idx, PRTJSONVAL phJsonVal);
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * Creates an iterator for a given JSON array or object value.
|
---|
267 | *
|
---|
268 | * @returns IPRT status code.
|
---|
269 | * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an array or
|
---|
270 | * object.
|
---|
271 | * @param hJsonVal The JSON value handle.
|
---|
272 | * @param phJsonIt Where to store the JSON iterator handle on success.
|
---|
273 | */
|
---|
274 | RTDECL(int) RTJsonIteratorBegin(RTJSONVAL hJsonVal, PRTJSONIT phJsonIt);
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Gets the value and optional name for the current iterator position.
|
---|
278 | *
|
---|
279 | * @returns IPRT status code.
|
---|
280 | * @param hJsonIt The JSON iterator handle.
|
---|
281 | * @param phJsonVal Where to store the handle to the JSON value on success.
|
---|
282 | * @param ppszName Where to store the object member name for an object.
|
---|
283 | * NULL is returned for arrays.
|
---|
284 | */
|
---|
285 | RTDECL(int) RTJsonIteratorQueryValue(RTJSONIT hJsonIt, PRTJSONVAL phJsonVal, const char **ppszName);
|
---|
286 |
|
---|
287 | /**
|
---|
288 | * Advances to the next element in the referenced JSON value.
|
---|
289 | *
|
---|
290 | * @returns IPRT status code.
|
---|
291 | * @retval VERR_JSON_ITERATOR_END if the end for this iterator was reached.
|
---|
292 | * @param hJsonIt The JSON iterator handle.
|
---|
293 | */
|
---|
294 | RTDECL(int) RTJsonIteratorNext(RTJSONIT hJsonIt);
|
---|
295 |
|
---|
296 | /**
|
---|
297 | * Frees a given JSON iterator.
|
---|
298 | *
|
---|
299 | * @param hJsonIt The JSON iterator to free.
|
---|
300 | */
|
---|
301 | RTDECL(void) RTJsonIteratorFree(RTJSONIT hJsonIt);
|
---|
302 |
|
---|
303 | RT_C_DECLS_END
|
---|
304 |
|
---|
305 | /** @} */
|
---|
306 |
|
---|
307 | #endif
|
---|
308 |
|
---|