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