VirtualBox

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

Last change on this file since 61776 was 61742, checked in by vboxsync, 8 years ago

include/iprt/json.h: Remove non ASCII characters

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.7 KB
Line 
1/** @file
2 * IPRT JSON parser API (JSON).
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
32RT_C_DECLS_BEGIN
33
34
35/** @defgroup grp_json IPRT JSON parser
36 * @{
37 */
38
39/**
40 * JSON value types.
41 */
42typedef enum RTJSONVALTYPE
43{
44 /** Invalid first value. */
45 RTJSONVALTYPE_INVALID = 0,
46 /** Value containing an object. */
47 RTJSONVALTYPE_OBJECT,
48 /** Value containing an array. */
49 RTJSONVALTYPE_ARRAY,
50 /** Value containing a string. */
51 RTJSONVALTYPE_STRING,
52 /** Value containg a number. */
53 RTJSONVALTYPE_NUMBER,
54 /** Value containg the special null value. */
55 RTJSONVALTYPE_NULL,
56 /** Value containing true. */
57 RTJSONVALTYPE_TRUE,
58 /** Value containing false. */
59 RTJSONVALTYPE_FALSE,
60 /** 32-bit hack. */
61 RTJSONVALTYPE_32BIT_HACK = 0x7fffffff
62} RTJSONVALTYPE;
63/** Pointer to a JSON value type. */
64typedef RTJSONVALTYPE *PRTJSONVALTYPE;
65
66/** JSON value handle. */
67typedef struct RTJSONVALINT *RTJSONVAL;
68/** Pointer to a JSON value handle. */
69typedef RTJSONVAL *PRTJSONVAL;
70/** NIL JSON value handle. */
71#define NIL_RTJSONVAL ((RTJSONVAL)~(uintptr_t)0)
72
73/** JSON iterator handle. */
74typedef struct RTJSONITINT *RTJSONIT;
75/** Pointer to a JSON iterator handle. */
76typedef RTJSONIT *PRTJSONIT;
77/** NIL JSON iterator handle. */
78#define NIL_RTJSONIT ((RTJSONIT)~(uintptr_t)0)
79
80/**
81 * Parses a JSON document in the provided buffer returning the root JSON value.
82 *
83 * @returns IPRT status code.
84 * @retval VERR_JSON_MALFORMED if the document does not conform to the spec.
85 * @param phJsonVal Where to store the handle to the JSON value on success.
86 * @param pbBuf The byte buffer containing the JSON document.
87 * @param cbBuf Size of the buffer.
88 * @param pErrInfo Where to store extended error info. Optional.
89 */
90RTDECL(int) RTJsonParseFromBuf(PRTJSONVAL phJsonVal, const uint8_t *pbBuf, size_t cbBuf,
91 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 */
102RTDECL(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 */
114RTDECL(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 */
122RTDECL(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 */
130RTDECL(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 */
138RTDECL(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 */
147RTDECL(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 */
157RTDECL(int) RTJsonValueGetStringEx(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 numbers currently.
168 */
169RTDECL(int) RTJsonValueGetNumber(RTJSONVAL hJsonVal, int64_t *pi64Num);
170
171/**
172 * Returns the value associated with a given name for the given JSON object value.
173 *
174 * @returns IPRT status code.
175 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object.
176 * @retval VERR_NOT_FOUND if the name is not known for this JSON object.
177 * @param hJsonVal The JSON value handle.
178 * @param pszName The member name of the object.
179 * @param phJsonVal Where to store the handle to the JSON value on success.
180 */
181RTDECL(int) RTJsonValueGetByName(RTJSONVAL hJsonVal, const char *pszName, PRTJSONVAL phJsonVal);
182
183/**
184 * Returns the number of a number value associated with a given name for the given JSON object value.
185 *
186 * @returns IPRT status code.
187 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object or the name does not point to
188 * a number value.
189 * @retval VERR_NOT_FOUND if the name is not known for this JSON object.
190 * @param hJsonVal The JSON value handle.
191 * @param pszName The member name of the object.
192 * @param pi64Num Where to store the number on success.
193 */
194RTDECL(int) RTJsonValueGetNumberByName(RTJSONVAL hJsonVal, const char *pszName, int64_t *pi64Num);
195
196/**
197 * Returns the string of a string value associated with a given name for the given JSON object value.
198 *
199 * @returns IPRT status code.
200 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object or the name does not point to
201 * a string value.
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 ppszStr Where to store the pointer to the string on success. Must be freed with
206 * RTStrFree().
207 */
208RTDECL(int) RTJsonValueGetStringByName(RTJSONVAL hJsonVal, const char *pszName, char **ppszStr);
209
210/**
211 * Returns the boolean of a true/false value associated with a given name for the given JSON object value.
212 *
213 * @returns IPRT status code.
214 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an object or the name does not point to
215 * a true/false value.
216 * @retval VERR_NOT_FOUND if the name is not known for this JSON object.
217 * @param hJsonVal The JSON value handle.
218 * @param pszName The member name of the object.
219 * @param pfBoolean Where to store the boolean value on success.
220 */
221RTDECL(int) RTJsonValueGetBooleanByName(RTJSONVAL hJsonVal, const char *pszName, bool *pfBoolean);
222
223/**
224 * Returns the size of a given JSON array value.
225 *
226 * @returns Size of the JSON array value.
227 * @retval 0 if the array is empty or the JSON value is not an array.
228 * @param hJsonVal The JSON value handle.
229 */
230RTDECL(unsigned) RTJsonValueGetArraySize(RTJSONVAL hJsonVal);
231
232/**
233 * Returns the size of a given JSON array value - extended version.
234 *
235 * @returns IPRT status code.
236 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an array.
237 * @param hJsonVal The JSON value handle.
238 * @param pcItems Where to store the size of the JSON array value on success.
239 */
240RTDECL(int) RTJsonValueGetArraySizeEx(RTJSONVAL hJsonVal, unsigned *pcItems);
241
242/**
243 * Returns the value for the given index of a given JSON array value.
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 idx The index to get the value from.
249 * @param phJsonVal Where to store the handle to the JSON value on success.
250 */
251RTDECL(int) RTJsonValueGetByIndex(RTJSONVAL hJsonVal, unsigned idx, PRTJSONVAL phJsonVal);
252
253/**
254 * Creates an iterator for a given JSON array or object value.
255 *
256 * @returns IPRT status code.
257 * @retval VERR_JSON_VALUE_INVALID_TYPE if the JSON value is not an array or object.
258 * @param hJsonVal The JSON value handle.
259 * @param phJsonIt Where to store the JSON iterator handle on success.
260 */
261RTDECL(int) RTJsonIteratorBegin(RTJSONVAL hJsonVal, PRTJSONIT phJsonIt);
262
263/**
264 * Gets the value and optional name for the current iterator position.
265 *
266 * @returns IPRT status code.
267 * @param hJsonIt The JSON iterator handle.
268 * @param phJsonVal Where to store the handle to the JSON value on success.
269 * @param ppszName Where to store the object member name for an object.
270 * NULL is returned for arrays.
271 */
272RTDECL(int) RTJsonIteratorGetValue(RTJSONIT hJsonIt, PRTJSONVAL phJsonVal, const char **ppszName);
273
274/**
275 * Advances to the next element in the referenced JSON value.
276 *
277 * @returns IPRT status code.
278 * @retval VERR_JSON_ITERATOR_END if the end for this iterator was reached.
279 * @param hJsonIt The JSON iterator handle.
280 */
281RTDECL(int) RTJsonIteratorNext(RTJSONIT hJsonIt);
282
283/**
284 * Frees a given JSON iterator.
285 *
286 * @returns nothing.
287 * @param hJsonIt The JSOn iterator to free.
288 */
289RTDECL(void) RTJsonIteratorFree(RTJSONIT hJsonIt);
290
291RT_C_DECLS_END
292
293/** @} */
294
295#endif
296
Note: See TracBrowser for help on using the repository browser.

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