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