]> git.proxmox.com Git - mirror_edk2.git/blame - RedfishPkg/Library/JsonLib/JsonLib.c
RedfishPkg/JsonLib: Fix the mistake of removing code by a accident
[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
8 (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>\r
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
157 @retval The created JSON value which contains a JSON number or NULL.\r
158\r
159**/\r
160EDKII_JSON_VALUE\r
161EFIAPI\r
162JsonValueInitNumber (\r
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
189/**\r
190 The function is used to initialize a JSON value which contains a new JSON NULL,\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 NULL JSON value.\r
196\r
197**/\r
198EDKII_JSON_VALUE\r
199EFIAPI\r
200JsonValueInitNull (\r
201 VOID\r
202 )\r
203{\r
204 return (EDKII_JSON_VALUE)json_null();\r
205}\r
206\r
207/**\r
208 The function is used to decrease the reference count of a JSON value by one, and once\r
209 this reference count drops to zero, the value is destroyed and it can no longer be used.\r
210 If this destroyed value is object type or array type, reference counts for all containing\r
211 JSON values will be decreased by 1. Boolean JSON value and NULL JSON value won't be destroyed\r
212 since they are static values kept in memory.\r
213\r
214 Reference Count Strategy: BaseJsonLib uses this strategy to track whether a value is still\r
215 in use or not. When a value is created, it's reference count is set to 1. If a reference to a\r
216 value is kept for use, its reference count is incremented, and when the value is no longer\r
217 needed, the reference count is decremented. When the reference count drops to zero, there are\r
218 no references left, and the value can be destroyed.\r
219\r
220 The given JSON value maybe NULL and not causing any problem. Just output the debug message\r
221 to inform caller the NULL value is passed in.\r
222\r
223 @param[in] Json The JSON value to be freed. json_decref may return without any\r
224 changes if Json is NULL.\r
225\r
226**/\r
227VOID\r
228EFIAPI\r
229JsonValueFree (\r
230 IN EDKII_JSON_VALUE Json\r
231 )\r
232{\r
233 json_decref((json_t *)Json);\r
234}\r
235\r
236/**\r
237 The function is used to create a fresh copy of a JSON value, and all child values are deep\r
238 copied in a recursive fashion. It should be called when this JSON value might be modified\r
239 in later use, but the original still wants to be used in somewhere else.\r
240\r
241 Reference counts of the returned root JSON value and all child values will be set to 1, and\r
242 caller needs to cleanup the root value by calling JsonValueFree().\r
243\r
244 * Note: Since this function performs a copy from bottom to up, too many calls may cause some\r
245 performance issues, user should avoid unnecessary calls to this function unless it is really\r
246 needed.\r
247\r
248 @param[in] Json The JSON value to be cloned.\r
249\r
250 @retval Return the cloned JSON value, or NULL on error.\r
251\r
252**/\r
253EDKII_JSON_VALUE\r
254EFIAPI\r
255JsonValueClone (\r
256 IN EDKII_JSON_VALUE Json\r
257 )\r
258{\r
259 return (EDKII_JSON_VALUE)json_deep_copy ((json_t *) Json);\r
260}\r
261\r
262/**\r
263 The function is used to return if the provided JSON value contains a JSON array.\r
264\r
265 @param[in] Json The provided JSON value.\r
266\r
267 @retval TRUE The JSON value contains a JSON array.\r
268 @retval FALSE The JSON value doesn't contain a JSON array.\r
269\r
270**/\r
271BOOLEAN\r
272EFIAPI\r
273JsonValueIsArray (\r
274 IN EDKII_JSON_VALUE Json\r
275 )\r
276{\r
277 return json_is_array ((json_t *) Json);\r
278}\r
279\r
280/**\r
281 The function is used to return if the provided JSON value contains a JSON object.\r
282\r
283 @param[in] Json The provided JSON value.\r
284\r
285 @retval TRUE The JSON value contains a JSON object.\r
286 @retval FALSE The JSON value doesn't contain a JSON object.\r
287\r
288**/\r
289BOOLEAN\r
290EFIAPI\r
291JsonValueIsObject (\r
292 IN EDKII_JSON_VALUE Json\r
293 )\r
294{\r
295 return json_is_object ((json_t *) Json);\r
296}\r
297\r
298/**\r
299 The function is used to return if the provided JSON Value contains a string, Ascii or\r
300 Unicode format is not differentiated.\r
301\r
302 @param[in] Json The provided JSON value.\r
303\r
304 @retval TRUE The JSON value contains a JSON string.\r
305 @retval FALSE The JSON value doesn't contain a JSON string.\r
306\r
307**/\r
308BOOLEAN\r
309EFIAPI\r
310JsonValueIsString (\r
311 IN EDKII_JSON_VALUE Json\r
312 )\r
313{\r
314 return json_is_string ((json_t *) Json);\r
315}\r
316\r
317/**\r
318 The function is used to return if the provided JSON value contains a JSON number.\r
319\r
320 @param[in] Json The provided JSON value.\r
321\r
322 @retval TRUE The JSON value is contains JSON number.\r
323 @retval FALSE The JSON value doesn't contain a JSON number.\r
324\r
325**/\r
326BOOLEAN\r
327EFIAPI\r
328JsonValueIsNumber (\r
329 IN EDKII_JSON_VALUE Json\r
330 )\r
331{\r
332 return json_is_integer ((json_t *) Json);\r
333}\r
334\r
335/**\r
336 The function is used to return if the provided JSON value contains a JSON boolean.\r
337\r
338 @param[in] Json The provided JSON value.\r
339\r
340 @retval TRUE The JSON value contains a JSON boolean.\r
341 @retval FALSE The JSON value doesn't contain a JSON boolean.\r
342\r
343**/\r
344BOOLEAN\r
345EFIAPI\r
346JsonValueIsBoolean (\r
347 IN EDKII_JSON_VALUE Json\r
348 )\r
349{\r
350 return json_is_boolean ((json_t *) Json);\r
351}\r
352\r
353/**\r
354 The function is used to return if the provided JSON value contains a JSON NULL.\r
355\r
356 @param[in] Json The provided JSON value.\r
357\r
358 @retval TRUE The JSON value contains a JSON NULL.\r
359 @retval FALSE The JSON value doesn't contain a JSON NULL.\r
360\r
361**/\r
362BOOLEAN\r
363EFIAPI\r
364JsonValueIsNull (\r
365 IN EDKII_JSON_VALUE Json\r
366 )\r
367{\r
368 return json_is_null ((json_t *) Json);\r
369}\r
370\r
371/**\r
372 The function is used to retrieve the associated array in an array type JSON value.\r
373\r
374 Any changes to the returned array will impact the original JSON value.\r
375\r
376 @param[in] Json The provided JSON value.\r
377\r
378 @retval Return the associated array in JSON value or NULL.\r
379\r
380**/\r
381EDKII_JSON_ARRAY\r
382EFIAPI\r
383JsonValueGetArray (\r
384 IN EDKII_JSON_VALUE Json\r
385 )\r
386{\r
387 if (Json == NULL || !JsonValueIsArray (Json)) {\r
388 return NULL;\r
389 }\r
390\r
391 return (EDKII_JSON_ARRAY)Json;\r
392}\r
393\r
394/**\r
395 The function is used to retrieve the associated object in an object type JSON value.\r
396\r
397 Any changes to the returned object will impact the original JSON value.\r
398\r
399 @param[in] Json The provided JSON value.\r
400\r
401 @retval Return the associated object in JSON value or NULL.\r
402\r
403**/\r
404EDKII_JSON_OBJECT\r
405EFIAPI\r
406JsonValueGetObject (\r
407 IN EDKII_JSON_VALUE Json\r
408 )\r
409{\r
410 if (Json == NULL || !JsonValueIsObject (Json)) {\r
411 return NULL;\r
412 }\r
413\r
414 return (EDKII_JSON_OBJECT)Json;\r
415}\r
416\r
417/**\r
418 The function is used to retrieve the associated Ascii string in a string type JSON value.\r
419\r
420 Any changes to the returned string will impact the original JSON value.\r
421\r
422 @param[in] Json The provided JSON value.\r
423\r
424 @retval Return the associated Ascii string in JSON value or NULL.\r
425\r
426**/\r
427CONST CHAR8 *\r
428EFIAPI\r
429JsonValueGetAsciiString (\r
430 IN EDKII_JSON_VALUE Json\r
431 )\r
432{\r
bd158441 433 CONST CHAR8 *AsciiStr;\r
ea830b96
AC
434 UINTN Index;\r
435\r
bd158441 436 AsciiStr = json_string_value ((json_t *) Json);\r
ea830b96
AC
437 if (AsciiStr == NULL) {\r
438 return NULL;\r
439 }\r
440\r
441 Index = 0;\r
442 while (*(AsciiStr + Index) != '\0') {\r
443 if (((*(AsciiStr + Index)) & 0x80) != 0x00) {\r
444 return NULL;\r
445 }\r
446\r
447 Index++;\r
448 }\r
449\r
450 return AsciiStr;\r
451}\r
452\r
453/**\r
454 The function is used to retrieve the associated Unicode string in a string type JSON value.\r
455\r
456 Caller can do any changes to the returned string without any impact to the original JSON\r
457 value, and caller needs to free the returned string using FreePool().\r
458\r
459 @param[in] Json The provided JSON value.\r
460\r
461 @retval Return the associated Unicode string in JSON value or NULL.\r
462\r
463**/\r
464CHAR16*\r
465EFIAPI\r
466JsonValueGetUnicodeString (\r
467 IN EDKII_JSON_VALUE Json\r
468 )\r
469{\r
470 EFI_STATUS Status;\r
471 CONST CHAR8 *Utf8Str;\r
472 CHAR16 *Ucs2Str;\r
473\r
474 Utf8Str = json_string_value ((json_t *) Json);\r
475 if (Utf8Str == NULL) {\r
476 return NULL;\r
477 }\r
478\r
479 Status = UTF8StrToUCS2 ((CHAR8*)Utf8Str, &Ucs2Str);\r
480 if (EFI_ERROR (Status)) {\r
481 return NULL;\r
482 }\r
483\r
484 return Ucs2Str;\r
485}\r
486\r
487/**\r
488 The function is used to retrieve the associated integer in a number type JSON value.\r
489\r
490 The input JSON value should not be NULL or contain no JSON number, otherwise it will\r
491 ASSERT() and return 0.\r
492\r
493 @param[in] Json The provided JSON value.\r
494\r
495 @retval Return the associated number in JSON value.\r
496\r
497**/\r
498INT64\r
499EFIAPI\r
500JsonValueGetNumber (\r
501 IN EDKII_JSON_VALUE Json\r
502 )\r
503{\r
504 ASSERT (Json != NULL && JsonValueIsNumber (Json));\r
505 if (Json == NULL || !JsonValueIsNumber (Json)) {\r
506 return 0;\r
507 }\r
508\r
509 return json_integer_value ((json_t *) Json);\r
510}\r
511\r
512/**\r
513 The function is used to retrieve the associated boolean in a boolean type JSON value.\r
514\r
515 The input JSON value should not be NULL or contain no JSON boolean, otherwise it will\r
516 ASSERT() and return FALSE.\r
517\r
518 @param[in] Json The provided JSON value.\r
519\r
520 @retval Return the associated value of JSON boolean.\r
521\r
522**/\r
523BOOLEAN\r
524EFIAPI\r
525JsonValueGetBoolean (\r
526 IN EDKII_JSON_VALUE Json\r
527 )\r
528{\r
529 ASSERT (Json != NULL && JsonValueIsBoolean (Json));\r
530 if (Json == NULL || !JsonValueIsBoolean (Json)) {\r
531 return FALSE;\r
532 }\r
533\r
534 return json_is_true ((json_t *) Json);\r
535}\r
536\r
537/**\r
538 The function is used to retrieve the associated string in a string type JSON value.\r
539\r
540 Any changes to the returned string will impact the original JSON value.\r
541\r
542 @param[in] Json The provided JSON value.\r
543\r
544 @retval Return the associated Ascii string in JSON value or NULL on errors.\r
545\r
546**/\r
547CONST CHAR8*\r
548EFIAPI\r
549JsonValueGetString (\r
550 IN EDKII_JSON_VALUE Json\r
551 )\r
552{\r
553 return json_string_value ((const json_t *)Json);\r
554}\r
555\r
556/**\r
557 The function is used to get the number of elements in a JSON object, or 0 if it is NULL or\r
558 not a JSON object.\r
559\r
560 @param[in] JsonObject The provided JSON object.\r
561\r
562 @retval Return the number of elements in this JSON object or 0.\r
563\r
564**/\r
565UINTN\r
566EFIAPI\r
567JsonObjectSize (\r
568 IN EDKII_JSON_OBJECT JsonObject\r
569 )\r
570{\r
571 return json_object_size ((json_t *) JsonObject);\r
572}\r
573\r
574/**\r
575 The function is used to enumerate all keys in a JSON object.\r
576\r
577 Caller should be responsible to free the returned key array reference using\r
578 FreePool(). But contained keys are read only and must not be modified or freed.\r
579\r
580 @param[in] JsonObj The provided JSON object for enumeration.\r
581 @param[out] KeyCount The count of keys in this JSON object.\r
582\r
583 @retval Return an array of the enumerated keys in this JSON object or NULL if\r
584 JsonObj is not an JSON object, key count is zero or on other errors.\r
585\r
586**/\r
587CHAR8**\r
588JsonObjectGetKeys (\r
589 IN EDKII_JSON_OBJECT JsonObj,\r
590 OUT UINTN *KeyCount\r
591 )\r
592{\r
593\r
594 UINTN Index;\r
595 CONST CHAR8 **KeyArray;\r
596 CONST CHAR8 *Key;\r
597 EDKII_JSON_VALUE Value;\r
598\r
599 if (JsonObj == NULL || KeyCount == NULL) {\r
600 return NULL;\r
601 }\r
602\r
603 Index = 0;\r
604 json_object_foreach(JsonObj, Key, Value) {\r
605 Index++;\r
606 }\r
607 if (Index == 0) {\r
608 *KeyCount = 0;\r
609 return NULL;\r
610 }\r
611\r
612 *KeyCount = Index;\r
613 KeyArray = (CONST CHAR8 **) AllocateZeroPool (*KeyCount * sizeof (CHAR8 *));\r
614 if (KeyArray == NULL) {\r
615 return NULL;\r
616 }\r
617\r
618 Key = NULL;\r
619 Value = NULL;\r
620 Index = 0;\r
621 json_object_foreach((json_t *) JsonObj, Key, Value) {\r
622 KeyArray[Index] = Key;\r
623 Index++;\r
624 }\r
625\r
626 return (CHAR8 **)KeyArray;\r
627}\r
628\r
629/**\r
630 The function is used to get a JSON value corresponding to the input key from a JSON object.\r
631\r
632 It only returns a reference to this value and any changes on this value will impact the\r
633 original JSON object. If that is not expected, please call JsonValueClone() to clone it to\r
634 use.\r
635\r
636 Input key must be a valid NULL terminated UTF8 encoded string. NULL will be returned when\r
637 Key-Value is not found in this JSON object.\r
638\r
639 @param[in] JsonObj The provided JSON object.\r
640 @param[in] Key The key of the JSON value to be retrieved.\r
641\r
642 @retval Return the corresponding JSON value to key, or NULL on error.\r
643\r
644**/\r
645EDKII_JSON_VALUE\r
646EFIAPI\r
647JsonObjectGetValue (\r
648 IN CONST EDKII_JSON_OBJECT JsonObj,\r
649 IN CONST CHAR8 *Key\r
650 )\r
651{\r
652 return (EDKII_JSON_VALUE)json_object_get ((const json_t *)JsonObj, (const char *)Key);\r
653}\r
654\r
655/**\r
656 The function is used to set a JSON value corresponding to the input key from a JSON object,\r
657 and the reference count of this value will be increased by 1.\r
658\r
659 Input key must be a valid NULL terminated UTF8 encoded string. If there already is a value for\r
660 this key, this key will be assigned to the new JSON value. The old JSON value will be removed\r
661 from this object and thus its' reference count will be decreased by 1.\r
662\r
663 More details for reference count strategy can refer to the API description for JsonValueFree().\r
664\r
665 @param[in] JsonObj The provided JSON object.\r
666 @param[in] Key The key of the JSON value to be set.\r
667 @param[in] Json The JSON value to set to this JSON object mapped by key.\r
668\r
669 @retval EFI_ABORTED Some error occur and operation aborted.\r
670 @retval EFI_SUCCESS The JSON value has been set to this JSON object.\r
671\r
672**/\r
673EFI_STATUS\r
674EFIAPI\r
675JsonObjectSetValue (\r
676 IN EDKII_JSON_OBJECT JsonObj,\r
677 IN CONST CHAR8 *Key,\r
678 IN EDKII_JSON_VALUE Json\r
679 )\r
680{\r
681 if (json_object_set ((json_t *) JsonObj, Key, (json_t *) Json) != 0) {\r
682 return EFI_ABORTED;\r
683 } else {\r
684 return EFI_SUCCESS;\r
685 }\r
686}\r
687\r
688/**\r
689 The function is used to get the number of elements in a JSON array. Returns or 0 if JsonArray\r
690 is NULL or not a JSON array.\r
691\r
692 @param[in] JsonArray The provided JSON array.\r
693\r
694 @retval Return the number of elements in this JSON array or 0.\r
695\r
696**/\r
697UINTN\r
698EFIAPI\r
699JsonArrayCount (\r
700 IN EDKII_JSON_ARRAY JsonArray\r
701 )\r
702{\r
703 return json_array_size ((json_t *) JsonArray);\r
704}\r
705\r
706/**\r
707 The function is used to return the JSON value in the array at position index. The valid range\r
708 for this index is from 0 to the return value of JsonArrayCount() minus 1.\r
709\r
710 It only returns a reference to this value and any changes on this value will impact the\r
711 original JSON object. If that is not expected, please call JsonValueClone() to clone it to\r
712 use.\r
713\r
714 If this array is NULL or not a JSON array, or if index is out of range, NULL will be returned.\r
715\r
716 @param[in] JsonArray The provided JSON Array.\r
717\r
718 @retval Return the JSON value located in the Index position or\r
719 NULL if JsonArray is not an array or no items in the array.\r
720\r
721**/\r
722EDKII_JSON_VALUE\r
723EFIAPI\r
724JsonArrayGetValue (\r
725 IN EDKII_JSON_ARRAY JsonArray,\r
726 IN UINTN Index\r
727 )\r
728{\r
729 return (EDKII_JSON_VALUE)json_array_get ((json_t *) JsonArray, Index);\r
730}\r
731\r
732/**\r
733 The function is used to append a JSON value to the end of the JSON array, and grow the size of\r
734 array by 1. The reference count of this value will be increased by 1.\r
735\r
736 More details for reference count strategy can refer to the API description for JsonValueFree().\r
737\r
738 @param[in] JsonArray The provided JSON object.\r
739 @param[in] Json The JSON value to append.\r
740\r
741 @retval EFI_ABORTED Some error occur and operation aborted.\r
742 @retval EFI_SUCCESS JSON value has been appended to the end of the JSON array.\r
743\r
744**/\r
745EFI_STATUS\r
746EFIAPI\r
747JsonArrayAppendValue (\r
748 IN EDKII_JSON_ARRAY JsonArray,\r
749 IN EDKII_JSON_VALUE Json\r
750 )\r
751{\r
752 if (json_array_append ((json_t *) JsonArray, (json_t *) Json) != 0) {\r
753 return EFI_ABORTED;\r
754 } else {\r
755 return EFI_SUCCESS;\r
756 }\r
757}\r
758\r
759/**\r
760 The function is used to remove a JSON value at position index, shifting the elements after index\r
761 one position towards the start of the array. The reference count of this value will be decreased\r
762 by 1.\r
763\r
764 More details for reference count strategy can refer to the API description for JsonValueFree().\r
765\r
766 @param[in] JsonArray The provided JSON array.\r
767 @param[in] Index The Index position before removement.\r
768\r
769 @retval EFI_ABORTED Some error occur and operation aborted.\r
770 @retval EFI_SUCCESS The JSON array has been removed at position index.\r
771\r
772**/\r
773EFI_STATUS\r
774EFIAPI\r
775JsonArrayRemoveValue (\r
776 IN EDKII_JSON_ARRAY JsonArray,\r
777 IN UINTN Index\r
778 )\r
779{\r
780 if (json_array_remove ((json_t *) JsonArray, Index) != 0) {\r
781 return EFI_ABORTED;\r
782 } else {\r
783 return EFI_SUCCESS;\r
784 }\r
785}\r
786\r
787/**\r
788 Dump JSON to a buffer.\r
789\r
790 @param[in] JsonValue The provided JSON value.\r
791 @param[in] Flags The Index position before removement. The value\r
792 could be the combination of below flags.\r
793 - EDKII_JSON_INDENT(n)\r
794 - EDKII_JSON_COMPACT\r
795 - EDKII_JSON_ENSURE_ASCII\r
796 - EDKII_JSON_SORT_KEYS\r
797 - EDKII_JSON_PRESERVE_ORDER\r
798 - EDKII_JSON_ENCODE_ANY\r
799 - EDKII_JSON_ESCAPE_SLASH\r
800 - EDKII_JSON_REAL_PRECISION(n)\r
801 - EDKII_JSON_EMBED\r
802 See below URI for the JSON encoding flags reference.\r
803 https://jansson.readthedocs.io/en/2.13/apiref.html#encoding\r
804\r
805 @retval CHAR8 * Dump fail if NULL returned, otherwise the buffer\r
806 contain JSON paylaod in ASCII string. The return\r
807 value must be freed by the caller using FreePool().\r
808**/\r
809CHAR8 *\r
810EFIAPI\r
811JsonDumpString (\r
812 IN EDKII_JSON_VALUE JsonValue,\r
813 IN UINTN Flags\r
814 )\r
815{\r
816 if (JsonValue == NULL) {\r
817 return NULL;\r
818 }\r
819 return json_dumps((json_t *)JsonValue, Flags);\r
820}\r
821\r
5d7b5cd1
AC
822/**\r
823 Convert a string to JSON object.\r
824 The function is used to convert a NULL terminated CHAR8 string to a JSON\r
825 value. Only object and array represented strings can be converted successfully,\r
826 since they are the only valid root values of a JSON text for UEFI usage.\r
827\r
828 Real number and number with exponent part are not supportted by UEFI.\r
829\r
830 Caller needs to cleanup the root value by calling JsonValueFree().\r
831\r
832 @param[in] String The NULL terminated CHAR8 string to convert.\r
833\r
834 @retval Array JSON value or object JSON value, or NULL when any error occurs.\r
835\r
836**/\r
837EDKII_JSON_VALUE\r
838EFIAPI\r
839JsonLoadString (\r
840 IN CONST CHAR8* String\r
841 )\r
842{\r
843 json_error_t JsonError;\r
844\r
845 return (EDKII_JSON_VALUE) json_loads ((const char *)String, 0, &JsonError);\r
846}\r
847\r
ea830b96
AC
848/**\r
849 Load JSON from a buffer.\r
850\r
851 @param[in] Buffer Bufffer to the JSON payload\r
852 @param[in] BufferLen Length of the buffer\r
853 @param[in] Flags Flag of loading JSON buffer, the value\r
854 could be the combination of below flags.\r
855 - EDKII_JSON_REJECT_DUPLICATES\r
856 - EDKII_JSON_DISABLE_EOF_CHECK\r
857 - EDKII_JSON_DECODE_ANY\r
858 - EDKII_JSON_DECODE_INT_AS_REAL\r
859 - EDKII_JSON_ALLOW_NUL\r
860 See below URI for the JSON encoding flags reference.\r
861 https://jansson.readthedocs.io/en/2.13/apiref.html?highlight=json_loadb#decoding\r
862\r
863 @param[in,out] Error Pointer EDKII_JSON_ERROR structure\r
864\r
865 @retval EDKII_JSON_VALUE NULL means fail to load JSON payload.\r
866**/\r
867EDKII_JSON_VALUE\r
868EFIAPI\r
869JsonLoadBuffer (\r
870 IN CONST CHAR8 *Buffer,\r
871 IN UINTN BufferLen,\r
872 IN UINTN Flags,\r
873 IN OUT EDKII_JSON_ERROR *Error\r
874 )\r
875{\r
876 return json_loadb(Buffer, BufferLen, Flags, (json_error_t *)Error);\r
877}\r
878\r
879/**\r
880 The reference count is used to track whether a value is still in use or not.\r
881 When a value is created, it's reference count is set to 1.\r
882 when the value is no longer needed, the reference count is decremented.\r
883 When the reference count drops to zero, there are no references left and the\r
884 value can be destroyed.\r
885\r
886 This funciton decrement the reference count of EDKII_JSON_VALUE. As soon as\r
887 a call to json_decref() drops the reference count to zero, the value is\r
888 destroyed and it can no longer be used.\r
889\r
890 @param[in] JsonValue JSON value\r
891**/\r
892VOID\r
893EFIAPI\r
894JsonDecreaseReference (\r
895 IN EDKII_JSON_VALUE JsonValue\r
896 )\r
897{\r
898 json_decref (JsonValue);\r
899}\r
900\r
901/**\r
902 The reference count is used to track whether a value is still in use or not.\r
903 When a value is created, it's reference count is set to 1.\r
904 If a reference to a value is kept (e.g. a value is stored somewhere for later use),\r
905 its reference count is incremented.\r
906\r
907 This function increment the reference count of json if it's not NULL.\r
908 Returns EDKII_JSON_VALUE.\r
909\r
910 @param[in] JsonValue JSON value\r
911 @retval EDKII_JSON_VALUE of itself\r
912**/\r
913EDKII_JSON_VALUE\r
914EFIAPI\r
915JsonIncreaseReference (\r
916 IN EDKII_JSON_VALUE JsonValue\r
917 )\r
918{\r
919 return json_incref (JsonValue);\r
920}\r
921\r
922/**\r
923 Returns an opaque iterator which can be used to iterate over all key-value pairs\r
924 in object, or NULL if object is empty.\r
925\r
926 @param[in] JsonValue JSON value\r
927 @retval Iterator pointer\r
928**/\r
929VOID *\r
930EFIAPI\r
931JsonObjectIterator (\r
932 IN EDKII_JSON_VALUE JsonValue\r
933 )\r
934{\r
935 return json_object_iter (JsonValue);\r
936}\r
937\r
938/**\r
939 Extract the associated value from iterator.\r
940\r
941 @param[in] Iterator Iterator pointer\r
942 @retval EDKII_JSON_VALUE\r
943**/\r
944EDKII_JSON_VALUE\r
945EFIAPI\r
946JsonObjectIteratorValue (\r
947 IN VOID *Iterator\r
948 )\r
949{\r
950 return json_object_iter_value(Iterator);\r
951}\r
952\r
953/**\r
954 Returns an iterator pointing to the next key-value pair in object after iter,\r
955 or NULL if the whole object has been iterated through.\r
956\r
957 @param[in] JsonValue JSON value\r
958 @param[in] Iterator Iterator pointer\r
959 @retval Iterator pointer\r
960**/\r
961VOID *\r
962JsonObjectIteratorNext (\r
963 IN EDKII_JSON_VALUE JsonValue,\r
964 IN VOID *Iterator\r
965 )\r
966{\r
967 return json_object_iter_next(JsonValue, Iterator);\r
968}\r
969\r
970/**\r
971 Returns the json type of this json value.\r
972\r
973 @param[in] JsonValue JSON value\r
974 @retval JSON type returned\r
975**/\r
976EDKII_JSON_TYPE\r
977EFIAPI\r
978JsonGetType (\r
979 IN EDKII_JSON_VALUE JsonValue\r
980 )\r
981{\r
982 return ((json_t *)JsonValue)->type;\r
983}\r