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