VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/RedfishPkg/Library/JsonLib/JsonLib.c@ 105670

Last change on this file since 105670 was 105670, checked in by vboxsync, 7 months ago

Devices/EFI/FirmwareNew: Merge edk2-stable-202405 and make it build on aarch64, bugref:4643

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette