From 60475162a66a73426cd971174bb3cd853a4619cf Mon Sep 17 00:00:00 2001 From: Nickle Wang Date: Mon, 3 Jul 2023 20:38:17 +0800 Subject: RedfishPkg/JsonLib: add object clear interface -Add JsonObjectClear() interface for application to clear all elements in JSON object. -Fix typo. Signed-off-by: Nickle Wang Cc: Abner Chang Cc: Igor Kulchytskyy Cc: Nick Ramirez Reviewed-by: Abner Chang --- RedfishPkg/Include/Library/JsonLib.h | 33 ++++++++++++++++++++++-------- RedfishPkg/Library/JsonLib/JsonLib.c | 39 ++++++++++++++++++++++++++++-------- 2 files changed, 56 insertions(+), 16 deletions(-) diff --git a/RedfishPkg/Include/Library/JsonLib.h b/RedfishPkg/Include/Library/JsonLib.h index f98f4e7..8f31d93 100644 --- a/RedfishPkg/Include/Library/JsonLib.h +++ b/RedfishPkg/Include/Library/JsonLib.h @@ -3,6 +3,7 @@ Copyright (c) 2019, Intel Corporation. All rights reserved.
(C) Copyright 2021 Hewlett Packard Enterprise Development LP
+ Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. SPDX-License-Identifier: BSD-2-Clause-Patent @@ -97,7 +98,7 @@ typedef enum { More details for reference count strategy can refer to the API description for JsonValueFree(). - @retval The created JSON value which contains a JSON array or NULL if intial a JSON array + @retval The created JSON value which contains a JSON array or NULL if initial a JSON array is failed. **/ @@ -116,7 +117,7 @@ JsonValueInitArray ( More details for reference count strategy can refer to the API description for JsonValueFree(). - @retval The created JSON value which contains a JSON object or NULL if intial a JSON object + @retval The created JSON value which contains a JSON object or NULL if initial a JSON object is failed. **/ @@ -572,6 +573,22 @@ JsonObjectSize ( ); /** + The function removes all elements from object. Returns 0 on success and -1 if object is not + a JSON object. The reference count of all removed values are decremented. + + @param[in] JsonObject The provided JSON object. + + @retval EFI_ABORTED Some error occur and operation aborted. + @retval EFI_SUCCESS JSON value has been appended to the end of the JSON array. + +**/ +EFI_STATUS +EFIAPI +JsonObjectClear ( + IN EDKII_JSON_OBJECT JsonObject + ); + +/** The function is used to enumerate all keys in a JSON object. Caller should be responsible to free the returned key array reference using @@ -705,7 +722,7 @@ JsonArrayAppendValue ( More details for reference count strategy can refer to the API description for JsonValueFree(). @param[in] JsonArray The provided JSON array. - @param[in] Index The Index position before removement. + @param[in] Index The Index position before removal. @retval EFI_ABORTED Some error occur and operation aborted. @retval EFI_SUCCESS The JSON array has been removed at position index. @@ -722,7 +739,7 @@ JsonArrayRemoveValue ( Dump JSON to a buffer. @param[in] JsonValue The provided JSON value. - @param[in] Flags The Index position before removement. The value + @param[in] Flags The Index position before removal. The value could be the combination of below flags. - EDKII_JSON_INDENT(n) - EDKII_JSON_COMPACT @@ -737,7 +754,7 @@ JsonArrayRemoveValue ( https://jansson.readthedocs.io/en/2.13/apiref.html#encoding @retval CHAR8 * Dump fail if NULL returned, otherwise the buffer - contain JSON paylaod in ASCII string. The return + contain JSON payload in ASCII string. The return value must be freed by the caller FreePool(). **/ CHAR8 * @@ -753,7 +770,7 @@ JsonDumpString ( value. Only object and array represented strings can be converted successfully, since they are the only valid root values of a JSON text for UEFI usage. - Real number and number with exponent part are not supportted by UEFI. + Real number and number with exponent part are not supported by UEFI. Caller needs to cleanup the root value by calling JsonValueFree(). @@ -775,7 +792,7 @@ JsonLoadString ( /** Load JSON from a buffer. - @param[in] Buffer Bufffer to the JSON payload + @param[in] Buffer Buffier to the JSON payload @param[in] BufferLen Length of the buffer @param[in] Flags Flag of loading JSON buffer, the value could be the combination of below flags. @@ -807,7 +824,7 @@ JsonLoadBuffer ( When the reference count drops to zero, there are no references left and the value can be destroyed. - This funciton decrement the reference count of EDKII_JSON_VALUE. As soon as + This function decrement the reference count of EDKII_JSON_VALUE. As soon as a call to json_decref() drops the reference count to zero, the value is destroyed and it can no longer be used. diff --git a/RedfishPkg/Library/JsonLib/JsonLib.c b/RedfishPkg/Library/JsonLib/JsonLib.c index 69f2455..9b758b9 100644 --- a/RedfishPkg/Library/JsonLib/JsonLib.c +++ b/RedfishPkg/Library/JsonLib/JsonLib.c @@ -28,7 +28,7 @@ More details for reference count strategy can refer to the API description for JsonValueFree(). - @retval The created JSON value which contains a JSON array or NULL if intial a JSON array + @retval The created JSON value which contains a JSON array or NULL if initial a JSON array is failed. **/ @@ -50,7 +50,7 @@ JsonValueInitArray ( More details for reference count strategy can refer to the API description for JsonValueFree(). - @retval The created JSON value which contains a JSON object or NULL if intial a JSON object + @retval The created JSON value which contains a JSON object or NULL if initial a JSON object is failed. **/ @@ -673,6 +673,29 @@ JsonObjectSize ( } /** + The function removes all elements from object. Returns 0 on success and -1 if object is not + a JSON object. The reference count of all removed values are decremented. + + @param[in] JsonObject The provided JSON object. + + @retval EFI_ABORTED Some error occur and operation aborted. + @retval EFI_SUCCESS JSON value has been appended to the end of the JSON array. + +**/ +EFI_STATUS +EFIAPI +JsonObjectClear ( + IN EDKII_JSON_OBJECT JsonObject + ) +{ + if (json_object_clear ((json_t *)JsonObject) != 0) { + return EFI_ABORTED; + } + + return EFI_SUCCESS; +} + +/** The function is used to enumerate all keys in a JSON object. Caller should be responsible to free the returned key array reference using @@ -864,7 +887,7 @@ JsonArrayAppendValue ( More details for reference count strategy can refer to the API description for JsonValueFree(). @param[in] JsonArray The provided JSON array. - @param[in] Index The Index position before removement. + @param[in] Index The Index position before removal. @retval EFI_ABORTED Some error occur and operation aborted. @retval EFI_SUCCESS The JSON array has been removed at position index. @@ -888,7 +911,7 @@ JsonArrayRemoveValue ( Dump JSON to a buffer. @param[in] JsonValue The provided JSON value. - @param[in] Flags The Index position before removement. The value + @param[in] Flags The Index position before removal. The value could be the combination of below flags. - EDKII_JSON_INDENT(n) - EDKII_JSON_COMPACT @@ -903,7 +926,7 @@ JsonArrayRemoveValue ( https://jansson.readthedocs.io/en/2.13/apiref.html#encoding @retval CHAR8 * Dump fail if NULL returned, otherwise the buffer - contain JSON paylaod in ASCII string. The return + contain JSON payload in ASCII string. The return value must be freed by the caller using FreePool(). **/ CHAR8 * @@ -926,7 +949,7 @@ JsonDumpString ( value. Only object and array represented strings can be converted successfully, since they are the only valid root values of a JSON text for UEFI usage. - Real number and number with exponent part are not supportted by UEFI. + Real number and number with exponent part are not supported by UEFI. Caller needs to cleanup the root value by calling JsonValueFree(). @@ -951,7 +974,7 @@ JsonLoadString ( /** Load JSON from a buffer. - @param[in] Buffer Bufffer to the JSON payload + @param[in] Buffer Buffier to the JSON payload @param[in] BufferLen Length of the buffer @param[in] Flags Flag of loading JSON buffer, the value could be the combination of below flags. @@ -986,7 +1009,7 @@ JsonLoadBuffer ( When the reference count drops to zero, there are no references left and the value can be destroyed. - This funciton decrement the reference count of EDKII_JSON_VALUE. As soon as + This function decrement the reference count of EDKII_JSON_VALUE. As soon as a call to json_decref() drops the reference count to zero, the value is destroyed and it can no longer be used. -- cgit v1.1