JsonLib.c 30 KB

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