VirtualBox

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

Last change on this file since 101291 was 101291, checked in by vboxsync, 17 months ago

EFI/FirmwareNew: Make edk2-stable202308 build on all supported platforms (using gcc at least, msvc not tested yet), bugref:4643

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