2 APIs for JSON operations.
4 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
13 typedef VOID
* EDKII_JSON_VALUE
;
14 typedef VOID
* EDKII_JSON_ARRAY
;
15 typedef VOID
* EDKII_JSON_OBJECT
;
18 /// Map to json_int_t in jansson.h
20 typedef INT64 EDKII_JSON_INT_T
; // #JSON_INTEGER_IS_LONG_LONG is set to 1
21 // in jansson_Config.h
24 /// Map to the definitions in jansson.h
25 /// See below URI for the JSON encoding flags reference.
26 /// https://jansson.readthedocs.io/en/2.13/apiref.html#encoding
28 #define EDKII_JSON_MAX_INDENT 0x1F
29 #define EDKII_JSON_INDENT(n) ((n) & EDKII_JSON_MAX_INDENT)
31 #define EDKII_JSON_COMPACT 0x20
32 #define EDKII_JSON_ENSURE_ASCII 0x40
33 #define EDKII_JSON_SORT_KEYS 0x80
34 #define EDKII_JSON_PRESERVE_ORDER 0x100
35 #define EDKII_JSON_ENCODE_ANY 0x200
36 #define EDKII_JSON_ESCAPE_SLASH 0x400
37 #define EDKII_JSON_REAL_PRECISION(n) (((n) & 0x1F) << 11)
38 #define EDKII_JSON_EMBED 0x10000
41 /// Map to the definitions in jansson.h
42 /// See below URI for the JSON decoding flags reference.
43 /// https://jansson.readthedocs.io/en/2.13/apiref.html?highlight=json_loadb#decoding
45 #define EDKII_JSON_REJECT_DUPLICATES 0x1
46 #define EDKII_JSON_DISABLE_EOF_CHECK 0x2
47 #define EDKII_JSON_DECODE_ANY 0x4
48 #define EDKII_JSON_DECODE_INT_AS_REAL 0x8
49 #define EDKII_JSON_ALLOW_NUL 0x10
51 #define EDKII_JSON_ARRAY_FOREACH(Array, Index, Value) \
53 Index < JsonArrayCount(Array) && (Value = JsonArrayGetValue(Array, Index)); \
57 /// Map to the json_error_t in jansson.h
59 #define EDKII_JSON_ERROR_TEXT_LENGTH 160
60 #define EDKII_JSON_ERROR_SOURCE_LENGTH 80
65 CHAR8 Source
[EDKII_JSON_ERROR_SOURCE_LENGTH
];
66 CHAR8 Text
[EDKII_JSON_ERROR_TEXT_LENGTH
];
70 /// Map to the json_type in jansson.h
84 The function is used to initialize a JSON value which contains a new JSON array,
85 or NULL on error. Initially, the array is empty.
87 The reference count of this value will be set to 1, and caller needs to cleanup the
88 value by calling JsonValueFree().
90 More details for reference count strategy can refer to the API description for JsonValueFree().
92 @retval The created JSON value which contains a JSON array or NULL if intial a JSON array
103 The function is used to initialize a JSON value which contains a new JSON object,
104 or NULL on error. Initially, the object is empty.
106 The reference count of this value will be set to 1, and caller needs to cleanup the
107 value by calling JsonValueFree().
109 More details for reference count strategy can refer to the API description for JsonValueFree().
111 @retval The created JSON value which contains a JSON object or NULL if intial a JSON object
117 JsonValueInitObject (
122 The function is used to initialize a JSON value which contains a new JSON string,
125 The input string must be NULL terminated Ascii format, non-Ascii characters will
126 be processed as an error. Unicode characters can also be represented by Ascii string
127 as the format: \u + 4 hexadecimal digits, like \u3E5A, or \u003F.
129 The reference count of this value will be set to 1, and caller needs to cleanup the
130 value by calling JsonValueFree().
132 More details for reference count strategy can refer to the API description for JsonValueFree().
134 @param[in] String The Ascii string to initialize to JSON value
136 @retval The created JSON value which contains a JSON string or NULL. Select a
137 Getter API for a specific encoding format.
142 JsonValueInitAsciiString (
143 IN CONST CHAR8
*String
147 The function is used to initialize a JSON value which contains a new JSON string,
150 The input must be a NULL terminated UCS2 format Unicode string.
152 The reference count of this value will be set to 1, and caller needs to cleanup the
153 value by calling JsonValueFree().
155 More details for reference count strategy can refer to the API description for JsonValueFree().
157 @param[in] String The Unicode string to initialize to JSON value
159 @retval The created JSON value which contains a JSON string or NULL. Select a
160 Getter API for a specific encoding format.
165 JsonValueInitUnicodeString (
170 The function is used to initialize a JSON value which contains a new JSON integer,
173 The reference count of this value will be set to 1, and caller needs to cleanup the
174 value by calling JsonValueFree().
176 More details for reference count strategy can refer to the API description for JsonValueFree().
178 @param[in] Value The integer to initialize to JSON value
180 @retval The created JSON value which contains a JSON number or NULL.
185 JsonValueInitNumber (
190 The function is used to initialize a JSON value which contains a new JSON boolean,
193 Boolean JSON value is kept as static value, and no need to do any cleanup work.
195 @param[in] Value The boolean value to initialize.
197 @retval The created JSON value which contains a JSON boolean or NULL.
202 JsonValueInitBoolean (
207 The function is used to initialize a JSON value which contains a new JSON NULL,
210 NULL JSON value is kept as static value, and no need to do any cleanup work.
212 @retval The created NULL JSON value.
222 The function is used to decrease the reference count of a JSON value by one, and once
223 this reference count drops to zero, the value is destroyed and it can no longer be used.
224 If this destroyed value is object type or array type, reference counts for all containing
225 JSON values will be decreased by 1. Boolean JSON value and NULL JSON value won't be destroyed
226 since they are static values kept in memory.
228 Reference Count Strategy: BaseJsonLib uses this strategy to track whether a value is still
229 in use or not. When a value is created, it's reference count is set to 1. If a reference to a
230 value is kept for use, its reference count is incremented, and when the value is no longer
231 needed, the reference count is decremented. When the reference count drops to zero, there are
232 no references left, and the value can be destroyed.
234 The given JSON value maybe NULL and not causing any problem. Just output the debug message
235 to inform caller the NULL value is passed in.
237 @param[in] Json The JSON value to be freed. json_decref may return without any
238 changes if Json is NULL.
244 IN EDKII_JSON_VALUE Json
248 The function is used to create a fresh copy of a JSON value, and all child values are deep
249 copied in a recursive fashion. It should be called when this JSON value might be modified
250 in later use, but the original still wants to be used in somewhere else.
252 Reference counts of the returned root JSON value and all child values will be set to 1, and
253 caller needs to cleanup the root value by calling JsonValueFree().
255 * Note: Since this function performs a copy from bottom to up, too many calls may cause some
256 performance issues, user should avoid unnecessary calls to this function unless it is really
259 @param[in] Json The JSON value to be cloned.
261 @retval Return the cloned JSON value, or NULL on error.
267 IN EDKII_JSON_VALUE Json
271 The function is used to return if the provided JSON value contains a JSON array.
273 @param[in] Json The provided JSON value.
275 @retval TRUE The JSON value contains a JSON array.
276 @retval FALSE The JSON value doesn't contain a JSON array.
282 IN EDKII_JSON_VALUE Json
286 The function is used to return if the provided JSON value contains a JSON object.
288 @param[in] Json The provided JSON value.
290 @retval TRUE The JSON value contains a JSON object.
291 @retval FALSE The JSON value doesn't contain a JSON object.
297 IN EDKII_JSON_VALUE Json
301 The function is used to return if the provided JSON Value contains a string, Ascii or
302 Unicode format is not differentiated.
304 @param[in] Json The provided JSON value.
306 @retval TRUE The JSON value contains a JSON string.
307 @retval FALSE The JSON value doesn't contain a JSON string.
313 IN EDKII_JSON_VALUE Json
317 The function is used to return if the provided JSON value contains a JSON number.
319 @param[in] Json The provided JSON value.
321 @retval TRUE The JSON value is contains JSON number.
322 @retval FALSE The JSON value doesn't contain a JSON number.
328 IN EDKII_JSON_VALUE Json
332 The function is used to return if the provided JSON value contains a JSON boolean.
334 @param[in] Json The provided JSON value.
336 @retval TRUE The JSON value contains a JSON boolean.
337 @retval FALSE The JSON value doesn't contain a JSON boolean.
343 IN EDKII_JSON_VALUE Json
347 The function is used to return if the provided JSON value contains a JSON NULL.
349 @param[in] Json The provided JSON value.
351 @retval TRUE The JSON value contains a JSON NULL.
352 @retval FALSE The JSON value doesn't contain a JSON NULL.
358 IN EDKII_JSON_VALUE Json
362 The function is used to retrieve the associated array in an array type JSON value.
364 Any changes to the returned array will impact the original JSON value.
366 @param[in] Json The provided JSON value.
368 @retval Return the associated array in JSON value or NULL.
374 IN EDKII_JSON_VALUE Json
378 The function is used to retrieve the associated object in an object type JSON value.
380 Any changes to the returned object will impact the original JSON value.
382 @param[in] Json The provided JSON value.
384 @retval Return the associated object in JSON value or NULL.
390 IN EDKII_JSON_VALUE Json
394 The function is used to retrieve the associated Ascii string in a string type JSON value.
396 Any changes to the returned string will impact the original JSON value.
398 @param[in] Json The provided JSON value.
400 @retval Return the associated Ascii string in JSON value or NULL.
405 JsonValueGetAsciiString (
406 IN EDKII_JSON_VALUE Json
410 The function is used to retrieve the associated Unicode string in a string type JSON value.
412 Caller can do any changes to the returned string without any impact to the original JSON
413 value, and caller needs to free the returned string using FreePool().
415 @param[in] Json The provided JSON value.
417 @retval Return the associated Unicode string in JSON value or NULL.
422 JsonValueGetUnicodeString (
423 IN EDKII_JSON_VALUE Json
427 The function is used to retrieve the associated integer in a number type JSON value.
429 The input JSON value should not be NULL or contain no JSON number, otherwise it will
430 ASSERT() and return 0.
432 @param[in] Json The provided JSON value.
434 @retval Return the associated number in JSON value.
440 IN EDKII_JSON_VALUE Json
444 The function is used to retrieve the associated boolean in a boolean type JSON value.
446 The input JSON value should not be NULL or contain no JSON boolean, otherwise it will
447 ASSERT() and return FALSE.
449 @param[in] Json The provided JSON value.
451 @retval Return the associated value of JSON boolean.
456 JsonValueGetBoolean (
457 IN EDKII_JSON_VALUE Json
461 The function is used to retrieve the associated string in a string type JSON value.
463 Any changes to the returned string will impact the original JSON value.
465 @param[in] Json The provided JSON value.
467 @retval Return the associated Ascii string in JSON value or NULL on errors.
473 IN EDKII_JSON_VALUE Json
477 The function is used to get the number of elements in a JSON object, or 0 if it is NULL or
480 @param[in] JsonObject The provided JSON object.
482 @retval Return the number of elements in this JSON object or 0.
488 IN EDKII_JSON_OBJECT JsonObject
492 The function is used to enumerate all keys in a JSON object.
494 Caller should be responsible to free the returned key array reference using
495 FreePool(). But contained keys are read only and must not be modified or freed.
497 @param[in] JsonObj The provided JSON object for enumeration.
498 @param[out] KeyCount The count of keys in this JSON object.
500 @retval Return an array of the enumerated keys in this JSON object or NULL if
501 JsonObj is not an JSON object, key count is zero or on other errors.
506 IN EDKII_JSON_OBJECT JsonObj
,
511 The function is used to get a JSON value corresponding to the input key from a JSON object.
513 It only returns a reference to this value and any changes on this value will impact the
514 original JSON object. If that is not expected, please call JsonValueClone() to clone it to
517 Input key must be a valid NULL terminated UTF8 encoded string. NULL will be returned when
518 Key-Value is not found in this JSON object.
520 @param[in] JsonObj The provided JSON object.
521 @param[in] Key The key of the JSON value to be retrieved.
523 @retval Return the corresponding JSON value to key, or NULL on error.
529 IN CONST EDKII_JSON_OBJECT JsonObj
,
534 The function is used to set a JSON value corresponding to the input key from a JSON object,
535 and the reference count of this value will be increased by 1.
537 Input key must be a valid NULL terminated UTF8 encoded string. If there already is a value for
538 this key, this key will be assigned to the new JSON value. The old JSON value will be removed
539 from this object and thus its' reference count will be decreased by 1.
541 More details for reference count strategy can refer to the API description for JsonValueFree().
543 @param[in] JsonObj The provided JSON object.
544 @param[in] Key The key of the JSON value to be set.
545 @param[in] Json The JSON value to set to this JSON object mapped by key.
547 @retval EFI_ABORTED Some error occur and operation aborted.
548 @retval EFI_SUCCESS The JSON value has been set to this JSON object.
554 IN EDKII_JSON_OBJECT JsonObj
,
556 IN EDKII_JSON_VALUE Json
560 The function is used to get the number of elements in a JSON array. Returns or 0 if JsonArray
561 is NULL or not a JSON array.
563 @param[in] JsonArray The provided JSON array.
565 @retval Return the number of elements in this JSON array or 0.
571 IN EDKII_JSON_ARRAY JsonArray
575 The function is used to return the JSON value in the array at position index. The valid range
576 for this index is from 0 to the return value of JsonArrayCount() minus 1.
578 It only returns a reference to this value and any changes on this value will impact the
579 original JSON object. If that is not expected, please call JsonValueClone() to clone it to
582 If this array is NULL or not a JSON array, or if index is out of range, NULL will be returned.
584 @param[in] JsonArray The provided JSON Array.
586 @retval Return the JSON value located in the Index position or
587 NULL if JsonArray is not an array or no items in the array.
593 IN EDKII_JSON_ARRAY JsonArray
,
598 The function is used to append a JSON value to the end of the JSON array, and grow the size of
599 array by 1. The reference count of this value will be increased by 1.
601 More details for reference count strategy can refer to the API description for JsonValueFree().
603 @param[in] JsonArray The provided JSON object.
604 @param[in] Json The JSON value to append.
606 @retval EFI_ABORTED Some error occur and operation aborted.
607 @retval EFI_SUCCESS JSON value has been appended to the end of the JSON array.
612 JsonArrayAppendValue (
613 IN EDKII_JSON_ARRAY JsonArray
,
614 IN EDKII_JSON_VALUE Json
618 The function is used to remove a JSON value at position index, shifting the elements after index
619 one position towards the start of the array. The reference count of this value will be decreased
622 More details for reference count strategy can refer to the API description for JsonValueFree().
624 @param[in] JsonArray The provided JSON array.
625 @param[in] Index The Index position before removement.
627 @retval EFI_ABORTED Some error occur and operation aborted.
628 @retval EFI_SUCCESS The JSON array has been removed at position index.
633 JsonArrayRemoveValue (
634 IN EDKII_JSON_ARRAY JsonArray
,
639 Dump JSON to a buffer.
641 @param[in] JsonValue The provided JSON value.
642 @param[in] Flags The Index position before removement. The value
643 could be the combination of below flags.
644 - EDKII_JSON_INDENT(n)
646 - EDKII_JSON_ENSURE_ASCII
647 - EDKII_JSON_SORT_KEYS
648 - EDKII_JSON_PRESERVE_ORDER
649 - EDKII_JSON_ENCODE_ANY
650 - EDKII_JSON_ESCAPE_SLASH
651 - EDKII_JSON_REAL_PRECISION(n)
653 See below URI for the JSON encoding flags reference.
654 https://jansson.readthedocs.io/en/2.13/apiref.html#encoding
656 @retval CHAR8 * Dump fail if NULL returned, otherwise the buffer
657 contain JSON paylaod in ASCII string. The return
658 value must be freed by the caller FreePool().
663 IN EDKII_JSON_VALUE JsonValue
,
668 Convert a string to JSON object.
669 The function is used to convert a NULL terminated CHAR8 string to a JSON
670 value. Only object and array represented strings can be converted successfully,
671 since they are the only valid root values of a JSON text for UEFI usage.
673 Real number and number with exponent part are not supportted by UEFI.
675 Caller needs to cleanup the root value by calling JsonValueFree().
677 @param[in] String The NULL terminated CHAR8 string to convert.
679 @retval Array JSON value or object JSON value, or NULL when any error occurs.
685 IN CONST CHAR8
* String
689 Load JSON from a buffer.
691 @param[in] Buffer Bufffer to the JSON payload
692 @param[in] BufferLen Length of the buffer
693 @param[in] Flags Flag of loading JSON buffer, the value
694 could be the combination of below flags.
695 - EDKII_JSON_REJECT_DUPLICATES
696 - EDKII_JSON_DISABLE_EOF_CHECK
697 - EDKII_JSON_DECODE_ANY
698 - EDKII_JSON_DECODE_INT_AS_REAL
699 - EDKII_JSON_ALLOW_NUL
700 See below URI for the JSON encoding flags reference.
701 https://jansson.readthedocs.io/en/2.13/apiref.html?highlight=json_loadb#decoding
703 @param[in,out] Error Pointer EDKII_JSON_ERROR structure
705 @retval EDKII_JSON_VALUE NULL means fail to load JSON payload.
710 IN CONST CHAR8
*Buffer
,
713 IN OUT EDKII_JSON_ERROR
*Error
717 The reference count is used to track whether a value is still in use or not.
718 When a value is created, it's reference count is set to 1.
719 when the value is no longer needed, the reference count is decremented.
720 When the reference count drops to zero, there are no references left and the
721 value can be destroyed.
723 This funciton decrement the reference count of EDKII_JSON_VALUE. As soon as
724 a call to json_decref() drops the reference count to zero, the value is
725 destroyed and it can no longer be used.
727 @param[in] JsonValue JSON value
731 JsonDecreaseReference (
732 IN EDKII_JSON_VALUE JsonValue
736 The reference count is used to track whether a value is still in use or not.
737 When a value is created, it's reference count is set to 1.
738 If a reference to a value is kept (e.g. a value is stored somewhere for later use),
739 its reference count is incremented.
741 This function increment the reference count of json if it's not NULL.
742 Returns EDKII_JSON_VALUE.
744 @param[in] JsonValue JSON value
745 @retval EDKII_JSON_VALUE of itself
749 JsonIncreaseReference (
750 IN EDKII_JSON_VALUE JsonValue
753 Returns an opaque iterator which can be used to iterate over all key-value pairs
754 in object, or NULL if object is empty
756 @param[in] JsonValue JSON value
761 IN EDKII_JSON_VALUE JsonValue
765 Extract the associated value from iterator.
767 @param[in] Iterator Iterator pointer
771 JsonObjectIteratorValue (
776 Returns an iterator pointing to the next key-value pair in object after iter,
777 or NULL if the whole object has been iterated through.
779 @param[in] JsonValue JSON value
780 @param[in] Iterator Iterator pointer
781 @retval Iterator pointer
784 JsonObjectIteratorNext (
785 IN EDKII_JSON_VALUE JsonValue
,
790 Returns the json type of this json value
792 @param[in] JsonValue JSON value
793 @retval JSON type returned
798 IN EDKII_JSON_VALUE JsonValue