From nobody Thu May 2 01:26:14 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+78263+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+78263+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=hpe.com ARC-Seal: i=1; a=rsa-sha256; t=1627451511; cv=none; d=zohomail.com; s=zohoarc; b=LqsgUr5AjUxCqsK+qD+hhKmTy8uSCYiW4myZ71xUpCKnldhqMwh2ucmypR3kHlXd/K9ve5KOCyYEs75Ic1ymiDAFAPQciRx5/JrBodsfPvke5Oul+HUoFcDuHMO6ust79bL8Bs1ape89gZLAk+jb8u/44vG4qqhek9WtOBgKtN4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1627451511; h=Cc:Date:From:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=vhXTCNVamw0/w03KW6e+5hyrCrkoVX2r/Kydy/RUCxo=; b=K96ALp0Twafq+db8INaWcPMi2UGzJMvUAXtTXp9tvn/K6jUTW4d8bEIKBN8QChpF2IQxp5TD0iq90jaXIfqnh7/Rj4hEOqEZ9zjopy8pguYuatJQlYPNetSozyZAWN1deM+vf4oisYwSoSJrLbuA+pxfxSTxfPGvuCbbsfGB820= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+78263+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1627451511167649.4679282238303; Tue, 27 Jul 2021 22:51:51 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id dmk0YY1788612x4X7AzK3eKn; Tue, 27 Jul 2021 22:51:50 -0700 X-Received: from mx0a-002e3701.pphosted.com (mx0a-002e3701.pphosted.com [148.163.147.86]) by mx.groups.io with SMTP id smtpd.web11.1215.1627451510078409677 for ; Tue, 27 Jul 2021 22:51:50 -0700 X-Received: from pps.filterd (m0150241.ppops.net [127.0.0.1]) by mx0a-002e3701.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 16S5h90g020368 for ; Wed, 28 Jul 2021 05:51:49 GMT X-Received: from g4t3426.houston.hpe.com (g4t3426.houston.hpe.com [15.241.140.75]) by mx0a-002e3701.pphosted.com with ESMTP id 3a2yternmn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 28 Jul 2021 05:51:49 +0000 X-Received: from g9t2301.houston.hpecorp.net (g9t2301.houston.hpecorp.net [16.220.97.129]) by g4t3426.houston.hpe.com (Postfix) with ESMTP id 947194F for ; Wed, 28 Jul 2021 05:51:48 +0000 (UTC) X-Received: from abner-virtual-machine.asiapacific.hpqcorp.net (abner-virtual-machine.asiapacific.hpqcorp.net [15.119.210.153]) by g9t2301.houston.hpecorp.net (Postfix) with ESMTP id DA7BD48; Wed, 28 Jul 2021 05:51:47 +0000 (UTC) From: "Abner Chang" To: devel@edk2.groups.io Cc: Nickle Wang Subject: [edk2-devel] [PATCH] RedfishPkg/JsonLib: Add more JsonLib functions Date: Wed, 28 Jul 2021 12:56:33 +0800 Message-Id: <20210728045633.27020-1-abner.chang@hpe.com> X-Proofpoint-GUID: 767CjS3gZppUAtYxKF1ZeGRPXiGsim9l X-Proofpoint-ORIG-GUID: 767CjS3gZppUAtYxKF1ZeGRPXiGsim9l X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-HPE-SCL: -1 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,abner.chang@hpe.com X-Gm-Message-State: QBtO3uRm1o9ymsR9FbvASVblx1787277AA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1627451510; bh=Q19LMdkLKMBmo/du2WI5OTzYwfWQm1Y5iNSwwvnSFYw=; h=Cc:Date:From:Reply-To:Subject:To; b=W9o4Z7nIPJDAI17d9dJdWgqj4JxWr+hQQgmhT2pDSAJOv9ipsqTmYiAdEkP+kBWWEyE CHVPwVOfOInM3YapRhBidJqiu+A+YIGfZu/S1Uhyfz+VsVr1F+D5cpGEXZ/gAr/dujjBb CkKRmZFMn6ZDtG7B5HWkLYbu+DCchMOFLpU= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1627451512529100001 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Signed-off-by: Abner Chang Cc: Nickle Wang Reviewed-by: Nickle Wang --- RedfishPkg/Include/Library/JsonLib.h | 127 ++++++++++++++++++++-- RedfishPkg/Library/JsonLib/JsonLib.c | 156 ++++++++++++++++++++++++--- 2 files changed, 261 insertions(+), 22 deletions(-) diff --git a/RedfishPkg/Include/Library/JsonLib.h b/RedfishPkg/Include/Libr= ary/JsonLib.h index 8a30c5250b..83959dd52a 100644 --- a/RedfishPkg/Include/Library/JsonLib.h +++ b/RedfishPkg/Include/Library/JsonLib.h @@ -2,7 +2,7 @@ APIs for JSON operations. =20 Copyright (c) 2019, Intel Corporation. All rights reserved.
- (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ (C) Copyright 2021 Hewlett Packard Enterprise Development LP
=20 SPDX-License-Identifier: BSD-2-Clause-Patent =20 @@ -53,6 +53,13 @@ typedef INT64 EDKII_JSON_INT_T; // #JSON_INTEGER_IS= _LONG_LONG is set to 1 Index < JsonArrayCount(Array) && (Value =3D JsonArrayGetValue(Array, I= ndex)); \ Index++) =20 +#define EDKII_JSON_OBJECT_FOREACH_SAFE(Object, N, Key, Value) \ + for (Key =3D JsonObjectIteratorKey(JsonObjectIterator(Object)), = \ + N =3D JsonObjectIteratorNext(Object, JsonObjectKeyToIterator(Key))= ; \ + Key && (Value =3D JsonObjectIteratorValue(JsonObjectKeyToIterator(= Key))); \ + Key =3D JsonObjectIteratorKey(N), = \ + N =3D JsonObjectIteratorNext(Object, JsonObjectKeyToIterator(Key))) + /// /// Map to the json_error_t in jansson.h /// @@ -177,12 +184,12 @@ JsonValueInitUnicodeString ( =20 @param[in] Value The integer to initialize to JSON value =20 - @retval The created JSON value which contains a JSON number or NULL. + @retval The created JSON value which contains a JSON integer or NUL= L. =20 **/ EDKII_JSON_VALUE EFIAPI -JsonValueInitNumber ( +JsonValueInitInteger ( IN INT64 Value ); =20 @@ -218,6 +225,36 @@ JsonValueInitNull ( VOID ); =20 +/** + The function is used to initialize a JSON value which contains a TRUE JS= ON value, + or NULL on error. + + NULL JSON value is kept as static value, and no need to do any cleanup w= ork. + + @retval The created JSON TRUE value. + +**/ +EDKII_JSON_VALUE +EFIAPI +JsonValueInitTrue ( + VOID + ); + +/** + The function is used to initialize a JSON value which contains a FALSE J= SON value, + or NULL on error. + + NULL JSON value is kept as static value, and no need to do any cleanup w= ork. + + @retval The created JSON FALSE value. + +**/ +EDKII_JSON_VALUE +EFIAPI +JsonValueInitFalse ( + VOID + ); + /** The function is used to decrease the reference count of a JSON value by = one, and once this reference count drops to zero, the value is destroyed and it can no= longer be used. @@ -313,6 +350,21 @@ JsonValueIsString ( IN EDKII_JSON_VALUE Json ); =20 +/** + The function is used to return if the provided JSON value contains a JSO= N integer. + + @param[in] Json The provided JSON value. + + @retval TRUE The JSON value is contains JSON integer. + @retval FALSE The JSON value doesn't contain a JSON inte= ger. + +**/ +BOOLEAN +EFIAPI +JsonValueIsInteger ( + IN EDKII_JSON_VALUE Json + ); + /** The function is used to return if the provided JSON value contains a JSO= N number. =20 @@ -343,6 +395,36 @@ JsonValueIsBoolean ( IN EDKII_JSON_VALUE Json ); =20 +/** + The function is used to return if the provided JSON value contains a TRU= E value. + + @param[in] Json The provided JSON value. + + @retval TRUE The JSON value contains a TRUE value. + @retval FALSE The JSON value doesn't contain a TRUE valu= e. + +**/ +BOOLEAN +EFIAPI +JsonValueIsTrue ( + IN EDKII_JSON_VALUE Json + ); + +/** + The function is used to return if the provided JSON value contains a FAL= SE value. + + @param[in] Json The provided JSON value. + + @retval TRUE The JSON value contains a FALSE value. + @retval FALSE The JSON value doesn't contain a FALSE val= ue. + +**/ +BOOLEAN +EFIAPI +JsonValueIsFalse ( + IN EDKII_JSON_VALUE Json + ); + /** The function is used to return if the provided JSON value contains a JSO= N NULL. =20 @@ -424,19 +506,19 @@ JsonValueGetUnicodeString ( ); =20 /** - The function is used to retrieve the associated integer in a number type= JSON value. + The function is used to retrieve the associated integer in a integer typ= e JSON value. =20 - The input JSON value should not be NULL or contain no JSON number, other= wise it will + The input JSON value should not be NULL or contain no JSON Integer, othe= rwise it will ASSERT() and return 0. =20 @param[in] Json The provided JSON value. =20 - @retval Return the associated number in JSON value. + @retval Return the associated Integer in JSON value. =20 **/ INT64 EFIAPI -JsonValueGetNumber ( +JsonValueGetInteger ( IN EDKII_JSON_VALUE Json ); =20 @@ -675,6 +757,8 @@ JsonDumpString ( Caller needs to cleanup the root value by calling JsonValueFree(). =20 @param[in] String The NULL terminated CHAR8 string to convert. + @param[in] Flags Flags for loading JSON string. + @param[in] Error Returned error status. =20 @retval Array JSON value or object JSON value, or NULL when any err= or occurs. =20 @@ -682,7 +766,9 @@ JsonDumpString ( EDKII_JSON_VALUE EFIAPI JsonLoadString ( - IN CONST CHAR8* String + IN CONST CHAR8* String, + IN UINT64 Flags, + IN EDKII_JSON_ERROR *Error ); =20 /** @@ -781,11 +867,36 @@ JsonObjectIteratorValue ( @retval Iterator pointer **/ VOID * +EFIAPI JsonObjectIteratorNext ( IN EDKII_JSON_VALUE JsonValue, IN VOID *Iterator ); =20 +/** + Returns the key of iterator pointing + + @param[in] Iterator Iterator pointer + @retval Key +**/ +CHAR8 * +EFIAPI +JsonObjectIteratorKey ( + IN VOID *Iterator +); + +/** + Returns the pointer of iterator by key. + + @param[in] Key The key of interator pointer. + @retval Pointer to interator +**/ +VOID * +EFIAPI +JsonObjectKeyToIterator ( + IN CHAR8 *Key +); + /** Returns the json type of this json value =20 diff --git a/RedfishPkg/Library/JsonLib/JsonLib.c b/RedfishPkg/Library/Json= Lib/JsonLib.c index c305a3767f..24e78c9d44 100644 --- a/RedfishPkg/Library/JsonLib/JsonLib.c +++ b/RedfishPkg/Library/JsonLib/JsonLib.c @@ -5,7 +5,7 @@ https://jansson.readthedocs.io/en/2.13/apiref.html =20 Copyright (c) 2018 - 2019, Intel Corporation. All rights reserved.
- (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ (C) Copyright 2021 Hewlett Packard Enterprise Development LP
=20 SPDX-License-Identifier: BSD-2-Clause-Patent **/ @@ -154,12 +154,12 @@ JsonValueInitUnicodeString ( =20 @param[in] Value The integer to initialize to JSON value =20 - @retval The created JSON value which contains a JSON number or NULL. + @retval The created JSON value which contains a JSON integer or NUL= L. =20 **/ EDKII_JSON_VALUE EFIAPI -JsonValueInitNumber ( +JsonValueInitInteger ( IN INT64 Value ) { @@ -186,6 +186,42 @@ JsonValueInitBoolean ( return (EDKII_JSON_VALUE)json_boolean (Value); } =20 +/** + The function is used to initialize a JSON value which contains a TRUE JS= ON value, + or NULL on error. + + NULL JSON value is kept as static value, and no need to do any cleanup w= ork. + + @retval The created JSON TRUE value. + +**/ +EDKII_JSON_VALUE +EFIAPI +JsonValueInitTrue ( + VOID + ) +{ + return (EDKII_JSON_VALUE)json_true(); +} + +/** + The function is used to initialize a JSON value which contains a FALSE J= SON value, + or NULL on error. + + NULL JSON value is kept as static value, and no need to do any cleanup w= ork. + + @retval The created JSON FALSE value. + +**/ +EDKII_JSON_VALUE +EFIAPI +JsonValueInitFalse ( + VOID + ) +{ + return (EDKII_JSON_VALUE)json_false(); +} + /** The function is used to initialize a JSON value which contains a new JSO= N NULL, or NULL on error. @@ -314,6 +350,24 @@ JsonValueIsString ( return json_is_string ((json_t *) Json); } =20 +/** + The function is used to return if the provided JSON value contains a JSO= N integer. + + @param[in] Json The provided JSON value. + + @retval TRUE The JSON value is contains JSON integer. + @retval FALSE The JSON value doesn't contain a JSON inte= ger. + +**/ +BOOLEAN +EFIAPI +JsonValueIsInteger ( + IN EDKII_JSON_VALUE Json + ) +{ + return json_is_integer ((json_t *) Json); +} + /** The function is used to return if the provided JSON value contains a JSO= N number. =20 @@ -329,7 +383,7 @@ JsonValueIsNumber ( IN EDKII_JSON_VALUE Json ) { - return json_is_integer ((json_t *) Json); + return json_is_number ((json_t *) Json); } =20 /** @@ -350,6 +404,47 @@ JsonValueIsBoolean ( return json_is_boolean ((json_t *) Json); } =20 +/** + The function is used to return if the provided JSON value contains a TRU= E value. + + @param[in] Json The provided JSON value. + + @retval TRUE The JSON value contains a TRUE value. + @retval FALSE The JSON value doesn't contain a TRUE valu= e. + +**/ +BOOLEAN +EFIAPI +JsonValueIsTrue ( + IN EDKII_JSON_VALUE Json + ) +{ + if (json_is_true ((json_t *)Json)) { + return TRUE; + } + return FALSE; +} + +/** + The function is used to return if the provided JSON value contains a FAL= SE value. + + @param[in] Json The provided JSON value. + + @retval TRUE The JSON value contains a FALSE value. + @retval FALSE The JSON value doesn't contain a FALSE val= ue. + +**/ +BOOLEAN +EFIAPI +JsonValueIsFalse ( + IN EDKII_JSON_VALUE Json + ) +{ + if (json_is_false ((json_t *)Json)) { + return TRUE; + } + return FALSE; +} /** The function is used to return if the provided JSON value contains a JSO= N NULL. =20 @@ -485,24 +580,24 @@ JsonValueGetUnicodeString ( } =20 /** - The function is used to retrieve the associated integer in a number type= JSON value. + The function is used to retrieve the associated integer in a integer typ= e JSON value. =20 - The input JSON value should not be NULL or contain no JSON number, other= wise it will + The input JSON value should not be NULL or contain no JSON integer, othe= rwise it will ASSERT() and return 0. =20 @param[in] Json The provided JSON value. =20 - @retval Return the associated number in JSON value. + @retval Return the associated integer in JSON value. =20 **/ INT64 EFIAPI -JsonValueGetNumber ( +JsonValueGetInteger ( IN EDKII_JSON_VALUE Json ) { - ASSERT (Json !=3D NULL && JsonValueIsNumber (Json)); - if (Json =3D=3D NULL || !JsonValueIsNumber (Json)) { + ASSERT (Json !=3D NULL && JsonValueIsInteger (Json)); + if (Json =3D=3D NULL || !JsonValueIsInteger (Json)) { return 0; } =20 @@ -830,6 +925,8 @@ JsonDumpString ( Caller needs to cleanup the root value by calling JsonValueFree(). =20 @param[in] String The NULL terminated CHAR8 string to convert. + @param[in] Flags Flags for loading JSON string. + @param[in] Error Returned error status. =20 @retval Array JSON value or object JSON value, or NULL when any err= or occurs. =20 @@ -837,12 +934,12 @@ JsonDumpString ( EDKII_JSON_VALUE EFIAPI JsonLoadString ( - IN CONST CHAR8* String + IN CONST CHAR8* String, + IN UINT64 Flags, + IN EDKII_JSON_ERROR *Error ) { - json_error_t JsonError; - - return (EDKII_JSON_VALUE) json_loads ((const char *)String, 0, &JsonErro= r); + return (EDKII_JSON_VALUE) json_loads ((const char *)String, Flags, (json= _error_t *)Error); } =20 /** @@ -959,6 +1056,7 @@ JsonObjectIteratorValue ( @retval Iterator pointer **/ VOID * +EFIAPI JsonObjectIteratorNext ( IN EDKII_JSON_VALUE JsonValue, IN VOID *Iterator @@ -967,6 +1065,36 @@ JsonObjectIteratorNext ( return json_object_iter_next(JsonValue, Iterator); } =20 +/** + Returns the key of iterator pointing + + @param[in] Iterator Iterator pointer + @retval Key +**/ +CHAR8 * +EFIAPI +JsonObjectIteratorKey ( + IN VOID *Iterator +) +{ + return json_object_iter_key(Iterator); +} + +/** + Returns the pointer of iterator by key. + + @param[in] Key The key of interator pointer. + @retval Pointer to interator +**/ +VOID * +EFIAPI +JsonObjectKeyToIterator ( + IN CHAR8 *Key +) +{ + return json_object_key_to_iter(Key); +} + /** Returns the json type of this json value. =20 --=20 2.17.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#78263): https://edk2.groups.io/g/devel/message/78263 Mute This Topic: https://groups.io/mt/84499598/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-