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
5 https://jansson.readthedocs.io/en/2.13/apiref.html
7 Copyright (c) 2018 - 2019, Intel Corporation. All rights reserved.<BR>
8 (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
10 SPDX-License-Identifier: BSD-2-Clause-Patent
14 #include <Library/JsonLib.h>
15 #include <Library/BaseUcs2Utf8Lib.h>
16 #include <Library/MemoryAllocationLib.h>
21 The function is used to initialize a JSON value which contains a new JSON array,
22 or NULL on error. Initially, the array is empty.
24 The reference count of this value will be set to 1, and caller needs to cleanup the
25 value by calling JsonValueFree().
27 More details for reference count strategy can refer to the API description for JsonValueFree().
29 @retval The created JSON value which contains a JSON array or NULL if intial a JSON array
39 return (EDKII_JSON_VALUE
)json_array();
43 The function is used to initialize a JSON value which contains a new JSON object,
44 or NULL on error. Initially, the object is empty.
46 The reference count of this value will be set to 1, and caller needs to cleanup the
47 value by calling JsonValueFree().
49 More details for reference count strategy can refer to the API description for JsonValueFree().
51 @retval The created JSON value which contains a JSON object or NULL if intial a JSON object
61 return (EDKII_JSON_VALUE
)json_object();
65 The function is used to initialize a JSON value which contains a new JSON string,
68 The input string must be NULL terminated Ascii format, non-Ascii characters will
69 be processed as an error. Unicode characters can also be represented by Ascii string
70 as the format: \u + 4 hexadecimal digits, like \u3E5A, or \u003F.
72 The reference count of this value will be set to 1, and caller needs to cleanup the
73 value by calling JsonValueFree().
75 More details for reference count strategy can refer to the API description for JsonValueFree().
77 @param[in] String The Ascii string to initialize to JSON value
79 @retval The created JSON value which contains a JSON string or NULL. Select a
80 Getter API for a specific encoding format.
85 JsonValueInitAsciiString (
86 IN CONST CHAR8
*String
96 while (*(String
+ Index
) != '\0') {
97 if (((*(String
+ Index
)) & 0x80) != 0x00) {
104 return (EDKII_JSON_VALUE
)json_string (String
);
108 The function is used to initialize a JSON value which contains a new JSON string,
111 The input must be a NULL terminated UCS2 format Unicode string.
113 The reference count of this value will be set to 1, and caller needs to cleanup the
114 value by calling JsonValueFree().
116 More details for reference count strategy can refer to the API description for JsonValueFree().
118 @param[in] String The Unicode string to initialize to JSON value
120 @retval The created JSON value which contains a JSON string or NULL. Select a
121 Getter API for a specific encoding format.
126 JsonValueInitUnicodeString (
133 if (String
== NULL
) {
138 Status
= UCS2StrToUTF8 (String
, &Utf8Str
);
139 if (EFI_ERROR (Status
)) {
143 return (EDKII_JSON_VALUE
)json_string (Utf8Str
);
147 The function is used to initialize a JSON value which contains a new JSON integer,
150 The reference count of this value will be set to 1, and caller needs to cleanup the
151 value by calling JsonValueFree().
153 More details for reference count strategy can refer to the API description for JsonValueFree().
155 @param[in] Value The integer to initialize to JSON value
157 @retval The created JSON value which contains a JSON number or NULL.
162 JsonValueInitNumber (
166 return (EDKII_JSON_VALUE
)json_integer (Value
);
170 The function is used to initialize a JSON value which contains a new JSON boolean,
173 Boolean JSON value is kept as static value, and no need to do any cleanup work.
175 @param[in] Value The boolean value to initialize.
177 @retval The created JSON value which contains a JSON boolean or NULL.
182 JsonValueInitBoolean (
186 return (EDKII_JSON_VALUE
)json_boolean (Value
);
190 The function is used to initialize a JSON value which contains a new JSON NULL,
193 NULL JSON value is kept as static value, and no need to do any cleanup work.
195 @retval The created NULL JSON value.
204 return (EDKII_JSON_VALUE
)json_null();
208 The function is used to decrease the reference count of a JSON value by one, and once
209 this reference count drops to zero, the value is destroyed and it can no longer be used.
210 If this destroyed value is object type or array type, reference counts for all containing
211 JSON values will be decreased by 1. Boolean JSON value and NULL JSON value won't be destroyed
212 since they are static values kept in memory.
214 Reference Count Strategy: BaseJsonLib uses this strategy to track whether a value is still
215 in use or not. When a value is created, it's reference count is set to 1. If a reference to a
216 value is kept for use, its reference count is incremented, and when the value is no longer
217 needed, the reference count is decremented. When the reference count drops to zero, there are
218 no references left, and the value can be destroyed.
220 The given JSON value maybe NULL and not causing any problem. Just output the debug message
221 to inform caller the NULL value is passed in.
223 @param[in] Json The JSON value to be freed. json_decref may return without any
224 changes if Json is NULL.
230 IN EDKII_JSON_VALUE Json
233 json_decref((json_t
*)Json
);
237 The function is used to create a fresh copy of a JSON value, and all child values are deep
238 copied in a recursive fashion. It should be called when this JSON value might be modified
239 in later use, but the original still wants to be used in somewhere else.
241 Reference counts of the returned root JSON value and all child values will be set to 1, and
242 caller needs to cleanup the root value by calling JsonValueFree().
244 * Note: Since this function performs a copy from bottom to up, too many calls may cause some
245 performance issues, user should avoid unnecessary calls to this function unless it is really
248 @param[in] Json The JSON value to be cloned.
250 @retval Return the cloned JSON value, or NULL on error.
256 IN EDKII_JSON_VALUE Json
259 return (EDKII_JSON_VALUE
)json_deep_copy ((json_t
*) Json
);
263 The function is used to return if the provided JSON value contains a JSON array.
265 @param[in] Json The provided JSON value.
267 @retval TRUE The JSON value contains a JSON array.
268 @retval FALSE The JSON value doesn't contain a JSON array.
274 IN EDKII_JSON_VALUE Json
277 return json_is_array ((json_t
*) Json
);
281 The function is used to return if the provided JSON value contains a JSON object.
283 @param[in] Json The provided JSON value.
285 @retval TRUE The JSON value contains a JSON object.
286 @retval FALSE The JSON value doesn't contain a JSON object.
292 IN EDKII_JSON_VALUE Json
295 return json_is_object ((json_t
*) Json
);
299 The function is used to return if the provided JSON Value contains a string, Ascii or
300 Unicode format is not differentiated.
302 @param[in] Json The provided JSON value.
304 @retval TRUE The JSON value contains a JSON string.
305 @retval FALSE The JSON value doesn't contain a JSON string.
311 IN EDKII_JSON_VALUE Json
314 return json_is_string ((json_t
*) Json
);
318 The function is used to return if the provided JSON value contains a JSON number.
320 @param[in] Json The provided JSON value.
322 @retval TRUE The JSON value is contains JSON number.
323 @retval FALSE The JSON value doesn't contain a JSON number.
329 IN EDKII_JSON_VALUE Json
332 return json_is_integer ((json_t
*) Json
);
336 The function is used to return if the provided JSON value contains a JSON boolean.
338 @param[in] Json The provided JSON value.
340 @retval TRUE The JSON value contains a JSON boolean.
341 @retval FALSE The JSON value doesn't contain a JSON boolean.
347 IN EDKII_JSON_VALUE Json
350 return json_is_boolean ((json_t
*) Json
);
354 The function is used to return if the provided JSON value contains a JSON NULL.
356 @param[in] Json The provided JSON value.
358 @retval TRUE The JSON value contains a JSON NULL.
359 @retval FALSE The JSON value doesn't contain a JSON NULL.
365 IN EDKII_JSON_VALUE Json
368 return json_is_null ((json_t
*) Json
);
372 The function is used to retrieve the associated array in an array type JSON value.
374 Any changes to the returned array will impact the original JSON value.
376 @param[in] Json The provided JSON value.
378 @retval Return the associated array in JSON value or NULL.
384 IN EDKII_JSON_VALUE Json
387 if (Json
== NULL
|| !JsonValueIsArray (Json
)) {
391 return (EDKII_JSON_ARRAY
)Json
;
395 The function is used to retrieve the associated object in an object type JSON value.
397 Any changes to the returned object will impact the original JSON value.
399 @param[in] Json The provided JSON value.
401 @retval Return the associated object in JSON value or NULL.
407 IN EDKII_JSON_VALUE Json
410 if (Json
== NULL
|| !JsonValueIsObject (Json
)) {
414 return (EDKII_JSON_OBJECT
)Json
;
418 The function is used to retrieve the associated Ascii string in a string type JSON value.
420 Any changes to the returned string will impact the original JSON value.
422 @param[in] Json The provided JSON value.
424 @retval Return the associated Ascii string in JSON value or NULL.
429 JsonValueGetAsciiString (
430 IN EDKII_JSON_VALUE Json
436 AsciiStr
= (CHAR8
*) ((json_t
*) Json
);
437 if (AsciiStr
== NULL
) {
442 while (*(AsciiStr
+ Index
) != '\0') {
443 if (((*(AsciiStr
+ Index
)) & 0x80) != 0x00) {
454 The function is used to retrieve the associated Unicode string in a string type JSON value.
456 Caller can do any changes to the returned string without any impact to the original JSON
457 value, and caller needs to free the returned string using FreePool().
459 @param[in] Json The provided JSON value.
461 @retval Return the associated Unicode string in JSON value or NULL.
466 JsonValueGetUnicodeString (
467 IN EDKII_JSON_VALUE Json
471 CONST CHAR8
*Utf8Str
;
474 Utf8Str
= json_string_value ((json_t
*) Json
);
475 if (Utf8Str
== NULL
) {
479 Status
= UTF8StrToUCS2 ((CHAR8
*)Utf8Str
, &Ucs2Str
);
480 if (EFI_ERROR (Status
)) {
488 The function is used to retrieve the associated integer in a number type JSON value.
490 The input JSON value should not be NULL or contain no JSON number, otherwise it will
491 ASSERT() and return 0.
493 @param[in] Json The provided JSON value.
495 @retval Return the associated number in JSON value.
501 IN EDKII_JSON_VALUE Json
504 ASSERT (Json
!= NULL
&& JsonValueIsNumber (Json
));
505 if (Json
== NULL
|| !JsonValueIsNumber (Json
)) {
509 return json_integer_value ((json_t
*) Json
);
513 The function is used to retrieve the associated boolean in a boolean type JSON value.
515 The input JSON value should not be NULL or contain no JSON boolean, otherwise it will
516 ASSERT() and return FALSE.
518 @param[in] Json The provided JSON value.
520 @retval Return the associated value of JSON boolean.
525 JsonValueGetBoolean (
526 IN EDKII_JSON_VALUE Json
529 ASSERT (Json
!= NULL
&& JsonValueIsBoolean (Json
));
530 if (Json
== NULL
|| !JsonValueIsBoolean (Json
)) {
534 return json_is_true ((json_t
*) Json
);
538 The function is used to retrieve the associated string in a string type JSON value.
540 Any changes to the returned string will impact the original JSON value.
542 @param[in] Json The provided JSON value.
544 @retval Return the associated Ascii string in JSON value or NULL on errors.
550 IN EDKII_JSON_VALUE Json
553 return json_string_value ((const json_t
*)Json
);
557 The function is used to get the number of elements in a JSON object, or 0 if it is NULL or
560 @param[in] JsonObject The provided JSON object.
562 @retval Return the number of elements in this JSON object or 0.
568 IN EDKII_JSON_OBJECT JsonObject
571 return json_object_size ((json_t
*) JsonObject
);
575 The function is used to enumerate all keys in a JSON object.
577 Caller should be responsible to free the returned key array reference using
578 FreePool(). But contained keys are read only and must not be modified or freed.
580 @param[in] JsonObj The provided JSON object for enumeration.
581 @param[out] KeyCount The count of keys in this JSON object.
583 @retval Return an array of the enumerated keys in this JSON object or NULL if
584 JsonObj is not an JSON object, key count is zero or on other errors.
589 IN EDKII_JSON_OBJECT JsonObj
,
595 CONST CHAR8
**KeyArray
;
597 EDKII_JSON_VALUE Value
;
599 if (JsonObj
== NULL
|| KeyCount
== NULL
) {
604 json_object_foreach(JsonObj
, Key
, Value
) {
613 KeyArray
= (CONST CHAR8
**) AllocateZeroPool (*KeyCount
* sizeof (CHAR8
*));
614 if (KeyArray
== NULL
) {
621 json_object_foreach((json_t
*) JsonObj
, Key
, Value
) {
622 KeyArray
[Index
] = Key
;
626 return (CHAR8
**)KeyArray
;
630 The function is used to get a JSON value corresponding to the input key from a JSON object.
632 It only returns a reference to this value and any changes on this value will impact the
633 original JSON object. If that is not expected, please call JsonValueClone() to clone it to
636 Input key must be a valid NULL terminated UTF8 encoded string. NULL will be returned when
637 Key-Value is not found in this JSON object.
639 @param[in] JsonObj The provided JSON object.
640 @param[in] Key The key of the JSON value to be retrieved.
642 @retval Return the corresponding JSON value to key, or NULL on error.
648 IN CONST EDKII_JSON_OBJECT JsonObj
,
652 return (EDKII_JSON_VALUE
)json_object_get ((const json_t
*)JsonObj
, (const char *)Key
);
656 The function is used to set a JSON value corresponding to the input key from a JSON object,
657 and the reference count of this value will be increased by 1.
659 Input key must be a valid NULL terminated UTF8 encoded string. If there already is a value for
660 this key, this key will be assigned to the new JSON value. The old JSON value will be removed
661 from this object and thus its' reference count will be decreased by 1.
663 More details for reference count strategy can refer to the API description for JsonValueFree().
665 @param[in] JsonObj The provided JSON object.
666 @param[in] Key The key of the JSON value to be set.
667 @param[in] Json The JSON value to set to this JSON object mapped by key.
669 @retval EFI_ABORTED Some error occur and operation aborted.
670 @retval EFI_SUCCESS The JSON value has been set to this JSON object.
676 IN EDKII_JSON_OBJECT JsonObj
,
678 IN EDKII_JSON_VALUE Json
681 if (json_object_set ((json_t
*) JsonObj
, Key
, (json_t
*) Json
) != 0) {
689 The function is used to get the number of elements in a JSON array. Returns or 0 if JsonArray
690 is NULL or not a JSON array.
692 @param[in] JsonArray The provided JSON array.
694 @retval Return the number of elements in this JSON array or 0.
700 IN EDKII_JSON_ARRAY JsonArray
703 return json_array_size ((json_t
*) JsonArray
);
707 The function is used to return the JSON value in the array at position index. The valid range
708 for this index is from 0 to the return value of JsonArrayCount() minus 1.
710 It only returns a reference to this value and any changes on this value will impact the
711 original JSON object. If that is not expected, please call JsonValueClone() to clone it to
714 If this array is NULL or not a JSON array, or if index is out of range, NULL will be returned.
716 @param[in] JsonArray The provided JSON Array.
718 @retval Return the JSON value located in the Index position or
719 NULL if JsonArray is not an array or no items in the array.
725 IN EDKII_JSON_ARRAY JsonArray
,
729 return (EDKII_JSON_VALUE
)json_array_get ((json_t
*) JsonArray
, Index
);
733 The function is used to append a JSON value to the end of the JSON array, and grow the size of
734 array by 1. The reference count of this value will be increased by 1.
736 More details for reference count strategy can refer to the API description for JsonValueFree().
738 @param[in] JsonArray The provided JSON object.
739 @param[in] Json The JSON value to append.
741 @retval EFI_ABORTED Some error occur and operation aborted.
742 @retval EFI_SUCCESS JSON value has been appended to the end of the JSON array.
747 JsonArrayAppendValue (
748 IN EDKII_JSON_ARRAY JsonArray
,
749 IN EDKII_JSON_VALUE Json
752 if (json_array_append ((json_t
*) JsonArray
, (json_t
*) Json
) != 0) {
760 The function is used to remove a JSON value at position index, shifting the elements after index
761 one position towards the start of the array. The reference count of this value will be decreased
764 More details for reference count strategy can refer to the API description for JsonValueFree().
766 @param[in] JsonArray The provided JSON array.
767 @param[in] Index The Index position before removement.
769 @retval EFI_ABORTED Some error occur and operation aborted.
770 @retval EFI_SUCCESS The JSON array has been removed at position index.
775 JsonArrayRemoveValue (
776 IN EDKII_JSON_ARRAY JsonArray
,
780 if (json_array_remove ((json_t
*) JsonArray
, Index
) != 0) {
788 Dump JSON to a buffer.
790 @param[in] JsonValue The provided JSON value.
791 @param[in] Flags The Index position before removement. The value
792 could be the combination of below flags.
793 - EDKII_JSON_INDENT(n)
795 - EDKII_JSON_ENSURE_ASCII
796 - EDKII_JSON_SORT_KEYS
797 - EDKII_JSON_PRESERVE_ORDER
798 - EDKII_JSON_ENCODE_ANY
799 - EDKII_JSON_ESCAPE_SLASH
800 - EDKII_JSON_REAL_PRECISION(n)
802 See below URI for the JSON encoding flags reference.
803 https://jansson.readthedocs.io/en/2.13/apiref.html#encoding
805 @retval CHAR8 * Dump fail if NULL returned, otherwise the buffer
806 contain JSON paylaod in ASCII string. The return
807 value must be freed by the caller using FreePool().
812 IN EDKII_JSON_VALUE JsonValue
,
816 if (JsonValue
== NULL
) {
819 return json_dumps((json_t
*)JsonValue
, Flags
);
823 Convert a string to JSON object.
824 The function is used to convert a NULL terminated CHAR8 string to a JSON
825 value. Only object and array represented strings can be converted successfully,
826 since they are the only valid root values of a JSON text for UEFI usage.
828 Real number and number with exponent part are not supportted by UEFI.
830 Caller needs to cleanup the root value by calling JsonValueFree().
832 @param[in] String The NULL terminated CHAR8 string to convert.
834 @retval Array JSON value or object JSON value, or NULL when any error occurs.
840 IN CONST CHAR8
* String
843 json_error_t JsonError
;
845 return (EDKII_JSON_VALUE
) json_loads ((const char *)String
, 0, &JsonError
);
849 Load JSON from a buffer.
851 @param[in] Buffer Bufffer to the JSON payload
852 @param[in] BufferLen Length of the buffer
853 @param[in] Flags Flag of loading JSON buffer, the value
854 could be the combination of below flags.
855 - EDKII_JSON_REJECT_DUPLICATES
856 - EDKII_JSON_DISABLE_EOF_CHECK
857 - EDKII_JSON_DECODE_ANY
858 - EDKII_JSON_DECODE_INT_AS_REAL
859 - EDKII_JSON_ALLOW_NUL
860 See below URI for the JSON encoding flags reference.
861 https://jansson.readthedocs.io/en/2.13/apiref.html?highlight=json_loadb#decoding
863 @param[in,out] Error Pointer EDKII_JSON_ERROR structure
865 @retval EDKII_JSON_VALUE NULL means fail to load JSON payload.
870 IN CONST CHAR8
*Buffer
,
873 IN OUT EDKII_JSON_ERROR
*Error
876 return json_loadb(Buffer
, BufferLen
, Flags
, (json_error_t
*)Error
);
880 The reference count is used to track whether a value is still in use or not.
881 When a value is created, it's reference count is set to 1.
882 when the value is no longer needed, the reference count is decremented.
883 When the reference count drops to zero, there are no references left and the
884 value can be destroyed.
886 This funciton decrement the reference count of EDKII_JSON_VALUE. As soon as
887 a call to json_decref() drops the reference count to zero, the value is
888 destroyed and it can no longer be used.
890 @param[in] JsonValue JSON value
894 JsonDecreaseReference (
895 IN EDKII_JSON_VALUE JsonValue
898 json_decref (JsonValue
);
902 The reference count is used to track whether a value is still in use or not.
903 When a value is created, it's reference count is set to 1.
904 If a reference to a value is kept (e.g. a value is stored somewhere for later use),
905 its reference count is incremented.
907 This function increment the reference count of json if it's not NULL.
908 Returns EDKII_JSON_VALUE.
910 @param[in] JsonValue JSON value
911 @retval EDKII_JSON_VALUE of itself
915 JsonIncreaseReference (
916 IN EDKII_JSON_VALUE JsonValue
919 return json_incref (JsonValue
);
923 Returns an opaque iterator which can be used to iterate over all key-value pairs
924 in object, or NULL if object is empty.
926 @param[in] JsonValue JSON value
927 @retval Iterator pointer
932 IN EDKII_JSON_VALUE JsonValue
935 return json_object_iter (JsonValue
);
939 Extract the associated value from iterator.
941 @param[in] Iterator Iterator pointer
942 @retval EDKII_JSON_VALUE
946 JsonObjectIteratorValue (
950 return json_object_iter_value(Iterator
);
954 Returns an iterator pointing to the next key-value pair in object after iter,
955 or NULL if the whole object has been iterated through.
957 @param[in] JsonValue JSON value
958 @param[in] Iterator Iterator pointer
959 @retval Iterator pointer
962 JsonObjectIteratorNext (
963 IN EDKII_JSON_VALUE JsonValue
,
967 return json_object_iter_next(JsonValue
, Iterator
);
971 Returns the json type of this json value.
973 @param[in] JsonValue JSON value
974 @retval JSON type returned
979 IN EDKII_JSON_VALUE JsonValue
982 return ((json_t
*)JsonValue
)->type
;