JsonLib.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. /** @file
  2. APIs for JSON operations. The fuctions provided by this library are the
  3. wrapper to native open source jansson library. See below document for
  4. the API reference.
  5. https://jansson.readthedocs.io/en/2.13/apiref.html
  6. Copyright (c) 2018 - 2019, Intel Corporation. All rights reserved.<BR>
  7. (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
  8. Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
  9. SPDX-License-Identifier: BSD-2-Clause-Patent
  10. **/
  11. #include <Uefi.h>
  12. #include <Library/JsonLib.h>
  13. #include <Library/BaseUcs2Utf8Lib.h>
  14. #include <Library/MemoryAllocationLib.h>
  15. #include "jansson.h"
  16. /**
  17. The function is used to initialize a JSON value which contains a new JSON array,
  18. or NULL on error. Initially, the array is empty.
  19. The reference count of this value will be set to 1, and caller needs to cleanup the
  20. value by calling JsonValueFree().
  21. More details for reference count strategy can refer to the API description for JsonValueFree().
  22. @retval The created JSON value which contains a JSON array or NULL if intial a JSON array
  23. is failed.
  24. **/
  25. EDKII_JSON_VALUE
  26. EFIAPI
  27. JsonValueInitArray (
  28. VOID
  29. )
  30. {
  31. return (EDKII_JSON_VALUE)json_array ();
  32. }
  33. /**
  34. The function is used to initialize a JSON value which contains a new JSON object,
  35. or NULL on error. Initially, the object is empty.
  36. The reference count of this value will be set to 1, and caller needs to cleanup the
  37. value by calling JsonValueFree().
  38. More details for reference count strategy can refer to the API description for JsonValueFree().
  39. @retval The created JSON value which contains a JSON object or NULL if intial a JSON object
  40. is failed.
  41. **/
  42. EDKII_JSON_VALUE
  43. EFIAPI
  44. JsonValueInitObject (
  45. VOID
  46. )
  47. {
  48. return (EDKII_JSON_VALUE)json_object ();
  49. }
  50. /**
  51. The function is used to initialize a JSON value which contains a new JSON string,
  52. or NULL on error.
  53. The input string must be NULL terminated Ascii format, non-Ascii characters will
  54. be processed as an error. Unicode characters can also be represented by Ascii string
  55. as the format: \u + 4 hexadecimal digits, like \u3E5A, or \u003F.
  56. The reference count of this value will be set to 1, and caller needs to cleanup the
  57. value by calling JsonValueFree().
  58. More details for reference count strategy can refer to the API description for JsonValueFree().
  59. @param[in] String The Ascii string to initialize to JSON value
  60. @retval The created JSON value which contains a JSON string or NULL. Select a
  61. Getter API for a specific encoding format.
  62. **/
  63. EDKII_JSON_VALUE
  64. EFIAPI
  65. JsonValueInitAsciiString (
  66. IN CONST CHAR8 *String
  67. )
  68. {
  69. UINTN Index;
  70. if (String == NULL) {
  71. return NULL;
  72. }
  73. Index = 0;
  74. while (*(String + Index) != '\0') {
  75. if (((*(String + Index)) & 0x80) != 0x00) {
  76. return NULL;
  77. }
  78. Index++;
  79. }
  80. return (EDKII_JSON_VALUE)json_string (String);
  81. }
  82. /**
  83. The function is used to initialize a JSON value which contains a new JSON string,
  84. or NULL on error.
  85. The input must be a NULL terminated UCS2 format Unicode string.
  86. The reference count of this value will be set to 1, and caller needs to cleanup the
  87. value by calling JsonValueFree().
  88. More details for reference count strategy can refer to the API description for JsonValueFree().
  89. @param[in] String The Unicode string to initialize to JSON value
  90. @retval The created JSON value which contains a JSON string or NULL. Select a
  91. Getter API for a specific encoding format.
  92. **/
  93. EDKII_JSON_VALUE
  94. EFIAPI
  95. JsonValueInitUnicodeString (
  96. IN CHAR16 *String
  97. )
  98. {
  99. EFI_STATUS Status;
  100. CHAR8 *Utf8Str;
  101. if (String == NULL) {
  102. return NULL;
  103. }
  104. Utf8Str = NULL;
  105. Status = UCS2StrToUTF8 (String, &Utf8Str);
  106. if (EFI_ERROR (Status)) {
  107. return NULL;
  108. }
  109. return (EDKII_JSON_VALUE)json_string (Utf8Str);
  110. }
  111. /**
  112. The function is used to initialize a JSON value which contains a new JSON integer,
  113. or NULL on error.
  114. The reference count of this value will be set to 1, and caller needs to cleanup the
  115. value by calling JsonValueFree().
  116. More details for reference count strategy can refer to the API description for JsonValueFree().
  117. @param[in] Value The integer to initialize to JSON value
  118. @retval The created JSON value which contains a JSON integer or NULL.
  119. **/
  120. EDKII_JSON_VALUE
  121. EFIAPI
  122. JsonValueInitInteger (
  123. IN INT64 Value
  124. )
  125. {
  126. return (EDKII_JSON_VALUE)json_integer (Value);
  127. }
  128. /**
  129. The function is used to initialize a JSON value which contains a new JSON boolean,
  130. or NULL on error.
  131. Boolean JSON value is kept as static value, and no need to do any cleanup work.
  132. @param[in] Value The boolean value to initialize.
  133. @retval The created JSON value which contains a JSON boolean or NULL.
  134. **/
  135. EDKII_JSON_VALUE
  136. EFIAPI
  137. JsonValueInitBoolean (
  138. IN BOOLEAN Value
  139. )
  140. {
  141. return (EDKII_JSON_VALUE)json_boolean (Value);
  142. }
  143. /**
  144. The function is used to initialize a JSON value which contains a TRUE JSON value,
  145. or NULL on error.
  146. NULL JSON value is kept as static value, and no need to do any cleanup work.
  147. @retval The created JSON TRUE value.
  148. **/
  149. EDKII_JSON_VALUE
  150. EFIAPI
  151. JsonValueInitTrue (
  152. VOID
  153. )
  154. {
  155. return (EDKII_JSON_VALUE)json_true ();
  156. }
  157. /**
  158. The function is used to initialize a JSON value which contains a FALSE JSON value,
  159. or NULL on error.
  160. NULL JSON value is kept as static value, and no need to do any cleanup work.
  161. @retval The created JSON FALSE value.
  162. **/
  163. EDKII_JSON_VALUE
  164. EFIAPI
  165. JsonValueInitFalse (
  166. VOID
  167. )
  168. {
  169. return (EDKII_JSON_VALUE)json_false ();
  170. }
  171. /**
  172. The function is used to initialize a JSON value which contains a new JSON NULL,
  173. or NULL on error.
  174. NULL JSON value is kept as static value, and no need to do any cleanup work.
  175. @retval The created NULL JSON value.
  176. **/
  177. EDKII_JSON_VALUE
  178. EFIAPI
  179. JsonValueInitNull (
  180. VOID
  181. )
  182. {
  183. return (EDKII_JSON_VALUE)json_null ();
  184. }
  185. /**
  186. The function is used to decrease the reference count of a JSON value by one, and once
  187. this reference count drops to zero, the value is destroyed and it can no longer be used.
  188. If this destroyed value is object type or array type, reference counts for all containing
  189. JSON values will be decreased by 1. Boolean JSON value and NULL JSON value won't be destroyed
  190. since they are static values kept in memory.
  191. Reference Count Strategy: BaseJsonLib uses this strategy to track whether a value is still
  192. in use or not. When a value is created, it's reference count is set to 1. If a reference to a
  193. value is kept for use, its reference count is incremented, and when the value is no longer
  194. needed, the reference count is decremented. When the reference count drops to zero, there are
  195. no references left, and the value can be destroyed.
  196. The given JSON value maybe NULL and not causing any problem. Just output the debug message
  197. to inform caller the NULL value is passed in.
  198. @param[in] Json The JSON value to be freed. json_decref may return without any
  199. changes if Json is NULL.
  200. **/
  201. VOID
  202. EFIAPI
  203. JsonValueFree (
  204. IN EDKII_JSON_VALUE Json
  205. )
  206. {
  207. json_decref ((json_t *)Json);
  208. }
  209. /**
  210. The function is used to create a fresh copy of a JSON value, and all child values are deep
  211. copied in a recursive fashion. It should be called when this JSON value might be modified
  212. in later use, but the original still wants to be used in somewhere else.
  213. Reference counts of the returned root JSON value and all child values will be set to 1, and
  214. caller needs to cleanup the root value by calling JsonValueFree().
  215. * Note: Since this function performs a copy from bottom to up, too many calls may cause some
  216. performance issues, user should avoid unnecessary calls to this function unless it is really
  217. needed.
  218. @param[in] Json The JSON value to be cloned.
  219. @retval Return the cloned JSON value, or NULL on error.
  220. **/
  221. EDKII_JSON_VALUE
  222. EFIAPI
  223. JsonValueClone (
  224. IN EDKII_JSON_VALUE Json
  225. )
  226. {
  227. return (EDKII_JSON_VALUE)json_deep_copy ((json_t *)Json);
  228. }
  229. /**
  230. The function is used to return if the provided JSON value contains a JSON array.
  231. @param[in] Json The provided JSON value.
  232. @retval TRUE The JSON value contains a JSON array.
  233. @retval FALSE The JSON value doesn't contain a JSON array.
  234. **/
  235. BOOLEAN
  236. EFIAPI
  237. JsonValueIsArray (
  238. IN EDKII_JSON_VALUE Json
  239. )
  240. {
  241. return json_is_array ((json_t *)Json);
  242. }
  243. /**
  244. The function is used to return if the provided JSON value contains a JSON object.
  245. @param[in] Json The provided JSON value.
  246. @retval TRUE The JSON value contains a JSON object.
  247. @retval FALSE The JSON value doesn't contain a JSON object.
  248. **/
  249. BOOLEAN
  250. EFIAPI
  251. JsonValueIsObject (
  252. IN EDKII_JSON_VALUE Json
  253. )
  254. {
  255. return json_is_object ((json_t *)Json);
  256. }
  257. /**
  258. The function is used to return if the provided JSON Value contains a string, Ascii or
  259. Unicode format is not differentiated.
  260. @param[in] Json The provided JSON value.
  261. @retval TRUE The JSON value contains a JSON string.
  262. @retval FALSE The JSON value doesn't contain a JSON string.
  263. **/
  264. BOOLEAN
  265. EFIAPI
  266. JsonValueIsString (
  267. IN EDKII_JSON_VALUE Json
  268. )
  269. {
  270. return json_is_string ((json_t *)Json);
  271. }
  272. /**
  273. The function is used to return if the provided JSON value contains a JSON integer.
  274. @param[in] Json The provided JSON value.
  275. @retval TRUE The JSON value is contains JSON integer.
  276. @retval FALSE The JSON value doesn't contain a JSON integer.
  277. **/
  278. BOOLEAN
  279. EFIAPI
  280. JsonValueIsInteger (
  281. IN EDKII_JSON_VALUE Json
  282. )
  283. {
  284. return json_is_integer ((json_t *)Json);
  285. }
  286. /**
  287. The function is used to return if the provided JSON value contains a JSON number.
  288. @param[in] Json The provided JSON value.
  289. @retval TRUE The JSON value is contains JSON number.
  290. @retval FALSE The JSON value doesn't contain a JSON number.
  291. **/
  292. BOOLEAN
  293. EFIAPI
  294. JsonValueIsNumber (
  295. IN EDKII_JSON_VALUE Json
  296. )
  297. {
  298. return json_is_number ((json_t *)Json);
  299. }
  300. /**
  301. The function is used to return if the provided JSON value contains a JSON boolean.
  302. @param[in] Json The provided JSON value.
  303. @retval TRUE The JSON value contains a JSON boolean.
  304. @retval FALSE The JSON value doesn't contain a JSON boolean.
  305. **/
  306. BOOLEAN
  307. EFIAPI
  308. JsonValueIsBoolean (
  309. IN EDKII_JSON_VALUE Json
  310. )
  311. {
  312. return json_is_boolean ((json_t *)Json);
  313. }
  314. /**
  315. The function is used to return if the provided JSON value contains a TRUE value.
  316. @param[in] Json The provided JSON value.
  317. @retval TRUE The JSON value contains a TRUE value.
  318. @retval FALSE The JSON value doesn't contain a TRUE value.
  319. **/
  320. BOOLEAN
  321. EFIAPI
  322. JsonValueIsTrue (
  323. IN EDKII_JSON_VALUE Json
  324. )
  325. {
  326. if (json_is_true ((json_t *)Json)) {
  327. return TRUE;
  328. }
  329. return FALSE;
  330. }
  331. /**
  332. The function is used to return if the provided JSON value contains a FALSE value.
  333. @param[in] Json The provided JSON value.
  334. @retval TRUE The JSON value contains a FALSE value.
  335. @retval FALSE The JSON value doesn't contain a FALSE value.
  336. **/
  337. BOOLEAN
  338. EFIAPI
  339. JsonValueIsFalse (
  340. IN EDKII_JSON_VALUE Json
  341. )
  342. {
  343. if (json_is_false ((json_t *)Json)) {
  344. return TRUE;
  345. }
  346. return FALSE;
  347. }
  348. /**
  349. The function is used to return if the provided JSON value contains a JSON NULL.
  350. @param[in] Json The provided JSON value.
  351. @retval TRUE The JSON value contains a JSON NULL.
  352. @retval FALSE The JSON value doesn't contain a JSON NULL.
  353. **/
  354. BOOLEAN
  355. EFIAPI
  356. JsonValueIsNull (
  357. IN EDKII_JSON_VALUE Json
  358. )
  359. {
  360. return json_is_null ((json_t *)Json);
  361. }
  362. /**
  363. The function is used to retrieve the associated array in an array type JSON value.
  364. Any changes to the returned array will impact the original JSON value.
  365. @param[in] Json The provided JSON value.
  366. @retval Return the associated array in JSON value or NULL.
  367. **/
  368. EDKII_JSON_ARRAY
  369. EFIAPI
  370. JsonValueGetArray (
  371. IN EDKII_JSON_VALUE Json
  372. )
  373. {
  374. if ((Json == NULL) || !JsonValueIsArray (Json)) {
  375. return NULL;
  376. }
  377. return (EDKII_JSON_ARRAY)Json;
  378. }
  379. /**
  380. The function is used to retrieve the associated object in an object type JSON value.
  381. Any changes to the returned object will impact the original JSON value.
  382. @param[in] Json The provided JSON value.
  383. @retval Return the associated object in JSON value or NULL.
  384. **/
  385. EDKII_JSON_OBJECT
  386. EFIAPI
  387. JsonValueGetObject (
  388. IN EDKII_JSON_VALUE Json
  389. )
  390. {
  391. if ((Json == NULL) || !JsonValueIsObject (Json)) {
  392. return NULL;
  393. }
  394. return (EDKII_JSON_OBJECT)Json;
  395. }
  396. /**
  397. The function is used to retrieve the associated Ascii string in a string type JSON value.
  398. Any changes to the returned string will impact the original JSON value.
  399. @param[in] Json The provided JSON value.
  400. @retval Return the associated Ascii string in JSON value or NULL.
  401. **/
  402. CONST CHAR8 *
  403. EFIAPI
  404. JsonValueGetAsciiString (
  405. IN EDKII_JSON_VALUE Json
  406. )
  407. {
  408. CONST CHAR8 *AsciiStr;
  409. UINTN Index;
  410. AsciiStr = json_string_value ((json_t *)Json);
  411. if (AsciiStr == NULL) {
  412. return NULL;
  413. }
  414. Index = 0;
  415. while (*(AsciiStr + Index) != '\0') {
  416. if (((*(AsciiStr + Index)) & 0x80) != 0x00) {
  417. return NULL;
  418. }
  419. Index++;
  420. }
  421. return AsciiStr;
  422. }
  423. /**
  424. The function is used to retrieve the associated Unicode string in a string type JSON value.
  425. Caller can do any changes to the returned string without any impact to the original JSON
  426. value, and caller needs to free the returned string using FreePool().
  427. @param[in] Json The provided JSON value.
  428. @retval Return the associated Unicode string in JSON value or NULL.
  429. **/
  430. CHAR16 *
  431. EFIAPI
  432. JsonValueGetUnicodeString (
  433. IN EDKII_JSON_VALUE Json
  434. )
  435. {
  436. EFI_STATUS Status;
  437. CONST CHAR8 *Utf8Str;
  438. CHAR16 *Ucs2Str;
  439. Ucs2Str = NULL;
  440. Utf8Str = json_string_value ((json_t *)Json);
  441. if (Utf8Str == NULL) {
  442. return NULL;
  443. }
  444. Status = UTF8StrToUCS2 ((CHAR8 *)Utf8Str, &Ucs2Str);
  445. if (EFI_ERROR (Status)) {
  446. return NULL;
  447. }
  448. return Ucs2Str;
  449. }
  450. /**
  451. The function is used to retrieve the associated integer in a integer type JSON value.
  452. The input JSON value should not be NULL or contain no JSON integer, otherwise it will
  453. ASSERT() and return 0.
  454. @param[in] Json The provided JSON value.
  455. @retval Return the associated integer in JSON value.
  456. **/
  457. INT64
  458. EFIAPI
  459. JsonValueGetInteger (
  460. IN EDKII_JSON_VALUE Json
  461. )
  462. {
  463. ASSERT (Json != NULL && JsonValueIsInteger (Json));
  464. if ((Json == NULL) || !JsonValueIsInteger (Json)) {
  465. return 0;
  466. }
  467. return json_integer_value ((json_t *)Json);
  468. }
  469. /**
  470. The function is used to retrieve the associated boolean in a boolean type JSON value.
  471. The input JSON value should not be NULL or contain no JSON boolean, otherwise it will
  472. ASSERT() and return FALSE.
  473. @param[in] Json The provided JSON value.
  474. @retval Return the associated value of JSON boolean.
  475. **/
  476. BOOLEAN
  477. EFIAPI
  478. JsonValueGetBoolean (
  479. IN EDKII_JSON_VALUE Json
  480. )
  481. {
  482. ASSERT (Json != NULL && JsonValueIsBoolean (Json));
  483. if ((Json == NULL) || !JsonValueIsBoolean (Json)) {
  484. return FALSE;
  485. }
  486. return json_is_true ((json_t *)Json);
  487. }
  488. /**
  489. The function is used to retrieve the associated string in a string type JSON value.
  490. Any changes to the returned string will impact the original JSON value.
  491. @param[in] Json The provided JSON value.
  492. @retval Return the associated Ascii string in JSON value or NULL on errors.
  493. **/
  494. CONST CHAR8 *
  495. EFIAPI
  496. JsonValueGetString (
  497. IN EDKII_JSON_VALUE Json
  498. )
  499. {
  500. return json_string_value ((const json_t *)Json);
  501. }
  502. /**
  503. The function is used to get the number of elements in a JSON object, or 0 if it is NULL or
  504. not a JSON object.
  505. @param[in] JsonObject The provided JSON object.
  506. @retval Return the number of elements in this JSON object or 0.
  507. **/
  508. UINTN
  509. EFIAPI
  510. JsonObjectSize (
  511. IN EDKII_JSON_OBJECT JsonObject
  512. )
  513. {
  514. return json_object_size ((json_t *)JsonObject);
  515. }
  516. /**
  517. The function is used to enumerate all keys in a JSON object.
  518. Caller should be responsible to free the returned key array reference using
  519. FreePool(). But contained keys are read only and must not be modified or freed.
  520. @param[in] JsonObj The provided JSON object for enumeration.
  521. @param[out] KeyCount The count of keys in this JSON object.
  522. @retval Return an array of the enumerated keys in this JSON object or NULL if
  523. JsonObj is not an JSON object, key count is zero or on other errors.
  524. **/
  525. CHAR8 **
  526. JsonObjectGetKeys (
  527. IN EDKII_JSON_OBJECT JsonObj,
  528. OUT UINTN *KeyCount
  529. )
  530. {
  531. UINTN Index;
  532. CONST CHAR8 **KeyArray;
  533. CONST CHAR8 *Key;
  534. EDKII_JSON_VALUE Value;
  535. if ((JsonObj == NULL) || (KeyCount == NULL)) {
  536. return NULL;
  537. }
  538. Index = 0;
  539. json_object_foreach (JsonObj, Key, Value) {
  540. Index++;
  541. }
  542. if (Index == 0) {
  543. *KeyCount = 0;
  544. return NULL;
  545. }
  546. *KeyCount = Index;
  547. KeyArray = (CONST CHAR8 **)AllocateZeroPool (*KeyCount * sizeof (CHAR8 *));
  548. if (KeyArray == NULL) {
  549. return NULL;
  550. }
  551. Key = NULL;
  552. Value = NULL;
  553. Index = 0;
  554. json_object_foreach ((json_t *)JsonObj, Key, Value) {
  555. KeyArray[Index] = Key;
  556. Index++;
  557. }
  558. return (CHAR8 **)KeyArray;
  559. }
  560. /**
  561. The function is used to get a JSON value corresponding to the input key from a JSON object.
  562. It only returns a reference to this value and any changes on this value will impact the
  563. original JSON object. If that is not expected, please call JsonValueClone() to clone it to
  564. use.
  565. Input key must be a valid NULL terminated UTF8 encoded string. NULL will be returned when
  566. Key-Value is not found in this JSON object.
  567. @param[in] JsonObj The provided JSON object.
  568. @param[in] Key The key of the JSON value to be retrieved.
  569. @retval Return the corresponding JSON value to key, or NULL on error.
  570. **/
  571. EDKII_JSON_VALUE
  572. EFIAPI
  573. JsonObjectGetValue (
  574. IN CONST EDKII_JSON_OBJECT JsonObj,
  575. IN CONST CHAR8 *Key
  576. )
  577. {
  578. return (EDKII_JSON_VALUE)json_object_get ((const json_t *)JsonObj, (const char *)Key);
  579. }
  580. /**
  581. The function is used to set a JSON value corresponding to the input key from a JSON object,
  582. and the reference count of this value will be increased by 1.
  583. Input key must be a valid NULL terminated UTF8 encoded string. If there already is a value for
  584. this key, this key will be assigned to the new JSON value. The old JSON value will be removed
  585. from this object and thus its' reference count will be decreased by 1.
  586. More details for reference count strategy can refer to the API description for JsonValueFree().
  587. @param[in] JsonObj The provided JSON object.
  588. @param[in] Key The key of the JSON value to be set.
  589. @param[in] Json The JSON value to set to this JSON object mapped by key.
  590. @retval EFI_ABORTED Some error occur and operation aborted.
  591. @retval EFI_SUCCESS The JSON value has been set to this JSON object.
  592. **/
  593. EFI_STATUS
  594. EFIAPI
  595. JsonObjectSetValue (
  596. IN EDKII_JSON_OBJECT JsonObj,
  597. IN CONST CHAR8 *Key,
  598. IN EDKII_JSON_VALUE Json
  599. )
  600. {
  601. if (json_object_set ((json_t *)JsonObj, Key, (json_t *)Json) != 0) {
  602. return EFI_ABORTED;
  603. } else {
  604. return EFI_SUCCESS;
  605. }
  606. }
  607. /**
  608. The function is used to get the number of elements in a JSON array. Returns or 0 if JsonArray
  609. is NULL or not a JSON array.
  610. @param[in] JsonArray The provided JSON array.
  611. @retval Return the number of elements in this JSON array or 0.
  612. **/
  613. UINTN
  614. EFIAPI
  615. JsonArrayCount (
  616. IN EDKII_JSON_ARRAY JsonArray
  617. )
  618. {
  619. return json_array_size ((json_t *)JsonArray);
  620. }
  621. /**
  622. The function is used to return the JSON value in the array at position index. The valid range
  623. for this index is from 0 to the return value of JsonArrayCount() minus 1.
  624. It only returns a reference to this value and any changes on this value will impact the
  625. original JSON object. If that is not expected, please call JsonValueClone() to clone it to
  626. use.
  627. If this array is NULL or not a JSON array, or if index is out of range, NULL will be returned.
  628. @param[in] JsonArray The provided JSON Array.
  629. @retval Return the JSON value located in the Index position or
  630. NULL if JsonArray is not an array or no items in the array.
  631. **/
  632. EDKII_JSON_VALUE
  633. EFIAPI
  634. JsonArrayGetValue (
  635. IN EDKII_JSON_ARRAY JsonArray,
  636. IN UINTN Index
  637. )
  638. {
  639. return (EDKII_JSON_VALUE)json_array_get ((json_t *)JsonArray, Index);
  640. }
  641. /**
  642. The function is used to append a JSON value to the end of the JSON array, and grow the size of
  643. array by 1. The reference count of this value will be increased by 1.
  644. More details for reference count strategy can refer to the API description for JsonValueFree().
  645. @param[in] JsonArray The provided JSON object.
  646. @param[in] Json The JSON value to append.
  647. @retval EFI_ABORTED Some error occur and operation aborted.
  648. @retval EFI_SUCCESS JSON value has been appended to the end of the JSON array.
  649. **/
  650. EFI_STATUS
  651. EFIAPI
  652. JsonArrayAppendValue (
  653. IN EDKII_JSON_ARRAY JsonArray,
  654. IN EDKII_JSON_VALUE Json
  655. )
  656. {
  657. if (json_array_append ((json_t *)JsonArray, (json_t *)Json) != 0) {
  658. return EFI_ABORTED;
  659. } else {
  660. return EFI_SUCCESS;
  661. }
  662. }
  663. /**
  664. The function is used to remove a JSON value at position index, shifting the elements after index
  665. one position towards the start of the array. The reference count of this value will be decreased
  666. by 1.
  667. More details for reference count strategy can refer to the API description for JsonValueFree().
  668. @param[in] JsonArray The provided JSON array.
  669. @param[in] Index The Index position before removement.
  670. @retval EFI_ABORTED Some error occur and operation aborted.
  671. @retval EFI_SUCCESS The JSON array has been removed at position index.
  672. **/
  673. EFI_STATUS
  674. EFIAPI
  675. JsonArrayRemoveValue (
  676. IN EDKII_JSON_ARRAY JsonArray,
  677. IN UINTN Index
  678. )
  679. {
  680. if (json_array_remove ((json_t *)JsonArray, Index) != 0) {
  681. return EFI_ABORTED;
  682. } else {
  683. return EFI_SUCCESS;
  684. }
  685. }
  686. /**
  687. Dump JSON to a buffer.
  688. @param[in] JsonValue The provided JSON value.
  689. @param[in] Flags The Index position before removement. The value
  690. could be the combination of below flags.
  691. - EDKII_JSON_INDENT(n)
  692. - EDKII_JSON_COMPACT
  693. - EDKII_JSON_ENSURE_ASCII
  694. - EDKII_JSON_SORT_KEYS
  695. - EDKII_JSON_PRESERVE_ORDER
  696. - EDKII_JSON_ENCODE_ANY
  697. - EDKII_JSON_ESCAPE_SLASH
  698. - EDKII_JSON_REAL_PRECISION(n)
  699. - EDKII_JSON_EMBED
  700. See below URI for the JSON encoding flags reference.
  701. https://jansson.readthedocs.io/en/2.13/apiref.html#encoding
  702. @retval CHAR8 * Dump fail if NULL returned, otherwise the buffer
  703. contain JSON paylaod in ASCII string. The return
  704. value must be freed by the caller using FreePool().
  705. **/
  706. CHAR8 *
  707. EFIAPI
  708. JsonDumpString (
  709. IN EDKII_JSON_VALUE JsonValue,
  710. IN UINTN Flags
  711. )
  712. {
  713. if (JsonValue == NULL) {
  714. return NULL;
  715. }
  716. return json_dumps ((json_t *)JsonValue, Flags);
  717. }
  718. /**
  719. Convert a string to JSON object.
  720. The function is used to convert a NULL terminated CHAR8 string to a JSON
  721. value. Only object and array represented strings can be converted successfully,
  722. since they are the only valid root values of a JSON text for UEFI usage.
  723. Real number and number with exponent part are not supportted by UEFI.
  724. Caller needs to cleanup the root value by calling JsonValueFree().
  725. @param[in] String The NULL terminated CHAR8 string to convert.
  726. @param[in] Flags Flags for loading JSON string.
  727. @param[in] Error Returned error status.
  728. @retval Array JSON value or object JSON value, or NULL when any error occurs.
  729. **/
  730. EDKII_JSON_VALUE
  731. EFIAPI
  732. JsonLoadString (
  733. IN CONST CHAR8 *String,
  734. IN UINT64 Flags,
  735. IN EDKII_JSON_ERROR *Error
  736. )
  737. {
  738. return (EDKII_JSON_VALUE)json_loads ((const char *)String, Flags, (json_error_t *)Error);
  739. }
  740. /**
  741. Load JSON from a buffer.
  742. @param[in] Buffer Bufffer to the JSON payload
  743. @param[in] BufferLen Length of the buffer
  744. @param[in] Flags Flag of loading JSON buffer, the value
  745. could be the combination of below flags.
  746. - EDKII_JSON_REJECT_DUPLICATES
  747. - EDKII_JSON_DISABLE_EOF_CHECK
  748. - EDKII_JSON_DECODE_ANY
  749. - EDKII_JSON_DECODE_INT_AS_REAL
  750. - EDKII_JSON_ALLOW_NUL
  751. See below URI for the JSON encoding flags reference.
  752. https://jansson.readthedocs.io/en/2.13/apiref.html?highlight=json_loadb#decoding
  753. @param[in,out] Error Pointer EDKII_JSON_ERROR structure
  754. @retval EDKII_JSON_VALUE NULL means fail to load JSON payload.
  755. **/
  756. EDKII_JSON_VALUE
  757. EFIAPI
  758. JsonLoadBuffer (
  759. IN CONST CHAR8 *Buffer,
  760. IN UINTN BufferLen,
  761. IN UINTN Flags,
  762. IN OUT EDKII_JSON_ERROR *Error
  763. )
  764. {
  765. return json_loadb (Buffer, BufferLen, Flags, (json_error_t *)Error);
  766. }
  767. /**
  768. The reference count is used to track whether a value is still in use or not.
  769. When a value is created, it's reference count is set to 1.
  770. when the value is no longer needed, the reference count is decremented.
  771. When the reference count drops to zero, there are no references left and the
  772. value can be destroyed.
  773. This funciton decrement the reference count of EDKII_JSON_VALUE. As soon as
  774. a call to json_decref() drops the reference count to zero, the value is
  775. destroyed and it can no longer be used.
  776. @param[in] JsonValue JSON value
  777. **/
  778. VOID
  779. EFIAPI
  780. JsonDecreaseReference (
  781. IN EDKII_JSON_VALUE JsonValue
  782. )
  783. {
  784. json_decref (JsonValue);
  785. }
  786. /**
  787. The reference count is used to track whether a value is still in use or not.
  788. When a value is created, it's reference count is set to 1.
  789. If a reference to a value is kept (e.g. a value is stored somewhere for later use),
  790. its reference count is incremented.
  791. This function increment the reference count of json if it's not NULL.
  792. Returns EDKII_JSON_VALUE.
  793. @param[in] JsonValue JSON value
  794. @retval EDKII_JSON_VALUE of itself
  795. **/
  796. EDKII_JSON_VALUE
  797. EFIAPI
  798. JsonIncreaseReference (
  799. IN EDKII_JSON_VALUE JsonValue
  800. )
  801. {
  802. return json_incref (JsonValue);
  803. }
  804. /**
  805. Returns an opaque iterator which can be used to iterate over all key-value pairs
  806. in object, or NULL if object is empty.
  807. @param[in] JsonValue JSON value
  808. @retval Iterator pointer
  809. **/
  810. VOID *
  811. EFIAPI
  812. JsonObjectIterator (
  813. IN EDKII_JSON_VALUE JsonValue
  814. )
  815. {
  816. return json_object_iter (JsonValue);
  817. }
  818. /**
  819. Extract the associated value from iterator.
  820. @param[in] Iterator Iterator pointer
  821. @retval EDKII_JSON_VALUE
  822. **/
  823. EDKII_JSON_VALUE
  824. EFIAPI
  825. JsonObjectIteratorValue (
  826. IN VOID *Iterator
  827. )
  828. {
  829. return json_object_iter_value (Iterator);
  830. }
  831. /**
  832. Returns an iterator pointing to the next key-value pair in object after iter,
  833. or NULL if the whole object has been iterated through.
  834. @param[in] JsonValue JSON value
  835. @param[in] Iterator Iterator pointer
  836. @retval Iterator pointer
  837. **/
  838. VOID *
  839. EFIAPI
  840. JsonObjectIteratorNext (
  841. IN EDKII_JSON_VALUE JsonValue,
  842. IN VOID *Iterator
  843. )
  844. {
  845. return json_object_iter_next (JsonValue, Iterator);
  846. }
  847. /**
  848. Returns the key of iterator pointing.
  849. @param[in] Iterator Iterator pointer
  850. @retval Key
  851. **/
  852. CHAR8 *
  853. EFIAPI
  854. JsonObjectIteratorKey (
  855. IN VOID *Iterator
  856. )
  857. {
  858. return (CHAR8 *)json_object_iter_key (Iterator);
  859. }
  860. /**
  861. Returns the pointer of iterator by key.
  862. @param[in] Key The key of interator pointer.
  863. @retval Pointer to interator
  864. **/
  865. VOID *
  866. EFIAPI
  867. JsonObjectKeyToIterator (
  868. IN CHAR8 *Key
  869. )
  870. {
  871. return json_object_key_to_iter (Key);
  872. }
  873. /**
  874. Returns the json type of this json value.
  875. @param[in] JsonValue JSON value
  876. @retval JSON type returned
  877. **/
  878. EDKII_JSON_TYPE
  879. EFIAPI
  880. JsonGetType (
  881. IN EDKII_JSON_VALUE JsonValue
  882. )
  883. {
  884. return ((json_t *)JsonValue)->type;
  885. }