]> git.proxmox.com Git - mirror_edk2.git/commitdiff
RedfishPkg/JsonLib: Add more JsonLib functions
authorAbner Chang <abner.chang@hpe.com>
Tue, 27 Apr 2021 04:53:30 +0000 (12:53 +0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Tue, 10 Aug 2021 16:59:05 +0000 (09:59 -0700)
Signed-off-by: Abner Chang <abner.chang@hpe.com>
Cc: Nickle Wang <nickle.wang@hpe.com>
Reviewed-by: Nickle Wang <nickle.wang@hpe.com>
RedfishPkg/Include/Library/JsonLib.h
RedfishPkg/Library/JsonLib/JsonLib.c

index 8a30c5250bb58a23ea6a6720c8a657c1d43860f4..83959dd52a04d1b57e592ca72aca3d1e41f80a35 100644 (file)
@@ -2,7 +2,7 @@
   APIs for JSON operations.\r
 \r
   Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>\r
- (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>\r
+ (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>\r
 \r
     SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
@@ -53,6 +53,13 @@ typedef    INT64   EDKII_JSON_INT_T; // #JSON_INTEGER_IS_LONG_LONG is set to 1
     Index < JsonArrayCount(Array) && (Value = JsonArrayGetValue(Array, Index)); \\r
     Index++)\r
 \r
+#define EDKII_JSON_OBJECT_FOREACH_SAFE(Object, N, Key, Value)           \\r
+    for (Key = JsonObjectIteratorKey(JsonObjectIterator(Object)),                \\r
+        N = JsonObjectIteratorNext(Object, JsonObjectKeyToIterator(Key));        \\r
+        Key && (Value = JsonObjectIteratorValue(JsonObjectKeyToIterator(Key)));  \\r
+        Key = JsonObjectIteratorKey(N),                                      \\r
+        N = JsonObjectIteratorNext(Object, JsonObjectKeyToIterator(Key)))\r
+\r
 ///\r
 ///  Map to the json_error_t in jansson.h\r
 ///\r
@@ -177,12 +184,12 @@ JsonValueInitUnicodeString (
 \r
   @param[in]   Value       The integer to initialize to JSON value\r
 \r
-  @retval      The created JSON value which contains a JSON number or NULL.\r
+  @retval      The created JSON value which contains a JSON integer or NULL.\r
 \r
 **/\r
 EDKII_JSON_VALUE\r
 EFIAPI\r
-JsonValueInitNumber (\r
+JsonValueInitInteger (\r
   IN    INT64    Value\r
   );\r
 \r
@@ -218,6 +225,36 @@ JsonValueInitNull (
   VOID\r
   );\r
 \r
+/**\r
+  The function is used to initialize a JSON value which contains a TRUE JSON value,\r
+  or NULL on error.\r
+\r
+  NULL JSON value is kept as static value, and no need to do any cleanup work.\r
+\r
+  @retval      The created JSON TRUE value.\r
+\r
+**/\r
+EDKII_JSON_VALUE\r
+EFIAPI\r
+JsonValueInitTrue (\r
+  VOID\r
+  );\r
+\r
+/**\r
+  The function is used to initialize a JSON value which contains a FALSE JSON value,\r
+  or NULL on error.\r
+\r
+  NULL JSON value is kept as static value, and no need to do any cleanup work.\r
+\r
+  @retval      The created JSON FALSE value.\r
+\r
+**/\r
+EDKII_JSON_VALUE\r
+EFIAPI\r
+JsonValueInitFalse (\r
+  VOID\r
+  );\r
+\r
 /**\r
   The function is used to decrease the reference count of a JSON value by one, and once\r
   this reference count drops to zero, the value is destroyed and it can no longer be used.\r
@@ -313,6 +350,21 @@ JsonValueIsString (
   IN    EDKII_JSON_VALUE    Json\r
   );\r
 \r
+/**\r
+  The function is used to return if the provided JSON value contains a JSON integer.\r
+\r
+  @param[in]   Json             The provided JSON value.\r
+\r
+  @retval      TRUE             The JSON value is contains JSON integer.\r
+  @retval      FALSE            The JSON value doesn't contain a JSON integer.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+JsonValueIsInteger (\r
+  IN    EDKII_JSON_VALUE    Json\r
+  );\r
+\r
 /**\r
   The function is used to return if the provided JSON value contains a JSON number.\r
 \r
@@ -343,6 +395,36 @@ JsonValueIsBoolean (
   IN    EDKII_JSON_VALUE    Json\r
   );\r
 \r
+/**\r
+  The function is used to return if the provided JSON value contains a TRUE value.\r
+\r
+  @param[in]   Json             The provided JSON value.\r
+\r
+  @retval      TRUE             The JSON value contains a TRUE value.\r
+  @retval      FALSE            The JSON value doesn't contain a TRUE value.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+JsonValueIsTrue (\r
+  IN    EDKII_JSON_VALUE    Json\r
+  );\r
+\r
+/**\r
+  The function is used to return if the provided JSON value contains a FALSE value.\r
+\r
+  @param[in]   Json             The provided JSON value.\r
+\r
+  @retval      TRUE             The JSON value contains a FALSE value.\r
+  @retval      FALSE            The JSON value doesn't contain a FALSE value.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+JsonValueIsFalse (\r
+  IN    EDKII_JSON_VALUE    Json\r
+  );\r
+\r
 /**\r
   The function is used to return if the provided JSON value contains a JSON NULL.\r
 \r
@@ -424,19 +506,19 @@ JsonValueGetUnicodeString (
   );\r
 \r
 /**\r
-  The function is used to retrieve the associated integer in a number type JSON value.\r
+  The function is used to retrieve the associated integer in a integer type JSON value.\r
 \r
-  The input JSON value should not be NULL or contain no JSON number, otherwise it will\r
+  The input JSON value should not be NULL or contain no JSON Integer, otherwise it will\r
   ASSERT() and return 0.\r
 \r
   @param[in]   Json             The provided JSON value.\r
 \r
-  @retval      Return the associated number in JSON value.\r
+  @retval      Return the associated Integer in JSON value.\r
 \r
 **/\r
 INT64\r
 EFIAPI\r
-JsonValueGetNumber (\r
+JsonValueGetInteger (\r
   IN    EDKII_JSON_VALUE    Json\r
   );\r
 \r
@@ -675,6 +757,8 @@ JsonDumpString (
   Caller needs to cleanup the root value by calling JsonValueFree().\r
 \r
   @param[in]   String        The NULL terminated CHAR8 string to convert.\r
+  @param[in]   Flags         Flags for loading JSON string.\r
+  @param[in]   Error         Returned error status.\r
 \r
   @retval      Array JSON value or object JSON value, or NULL when any error occurs.\r
 \r
@@ -682,7 +766,9 @@ JsonDumpString (
 EDKII_JSON_VALUE\r
 EFIAPI\r
 JsonLoadString (\r
-  IN   CONST CHAR8*    String\r
+  IN    CONST CHAR8*     String,\r
+  IN    UINT64           Flags,\r
+  IN    EDKII_JSON_ERROR *Error\r
   );\r
 \r
 /**\r
@@ -781,11 +867,36 @@ JsonObjectIteratorValue (
   @retval      Iterator pointer\r
 **/\r
 VOID *\r
+EFIAPI\r
 JsonObjectIteratorNext (\r
   IN EDKII_JSON_VALUE JsonValue,\r
   IN VOID             *Iterator\r
   );\r
 \r
+/**\r
+  Returns the key of iterator pointing\r
+\r
+  @param[in]   Iterator   Iterator pointer\r
+  @retval      Key\r
+**/\r
+CHAR8 *\r
+EFIAPI\r
+JsonObjectIteratorKey (\r
+  IN VOID *Iterator\r
+);\r
+\r
+/**\r
+  Returns the pointer of iterator by key.\r
+\r
+  @param[in]   Key   The key of interator pointer.\r
+  @retval      Pointer to interator\r
+**/\r
+VOID *\r
+EFIAPI\r
+JsonObjectKeyToIterator (\r
+  IN CHAR8 *Key\r
+);\r
+\r
 /**\r
   Returns the json type of this json value\r
 \r
index c305a3767f1436db7180b2d78a47bc6d9add844a..33d5eaef79a0c6d2c8e3aa0a2924260ed8b3218a 100644 (file)
@@ -5,7 +5,7 @@
   https://jansson.readthedocs.io/en/2.13/apiref.html\r
 \r
   Copyright (c) 2018 - 2019, Intel Corporation. All rights reserved.<BR>\r
- (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>\r
+ (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>\r
 \r
     SPDX-License-Identifier: BSD-2-Clause-Patent\r
 **/\r
@@ -154,12 +154,12 @@ JsonValueInitUnicodeString (
 \r
   @param[in]   Value       The integer to initialize to JSON value\r
 \r
-  @retval      The created JSON value which contains a JSON number or NULL.\r
+  @retval      The created JSON value which contains a JSON integer or NULL.\r
 \r
 **/\r
 EDKII_JSON_VALUE\r
 EFIAPI\r
-JsonValueInitNumber (\r
+JsonValueInitInteger (\r
   IN    INT64    Value\r
   )\r
 {\r
@@ -186,6 +186,42 @@ JsonValueInitBoolean (
   return (EDKII_JSON_VALUE)json_boolean (Value);\r
 }\r
 \r
+/**\r
+  The function is used to initialize a JSON value which contains a TRUE JSON value,\r
+  or NULL on error.\r
+\r
+  NULL JSON value is kept as static value, and no need to do any cleanup work.\r
+\r
+  @retval      The created JSON TRUE value.\r
+\r
+**/\r
+EDKII_JSON_VALUE\r
+EFIAPI\r
+JsonValueInitTrue (\r
+  VOID\r
+  )\r
+{\r
+  return (EDKII_JSON_VALUE)json_true();\r
+}\r
+\r
+/**\r
+  The function is used to initialize a JSON value which contains a FALSE JSON value,\r
+  or NULL on error.\r
+\r
+  NULL JSON value is kept as static value, and no need to do any cleanup work.\r
+\r
+  @retval      The created JSON FALSE value.\r
+\r
+**/\r
+EDKII_JSON_VALUE\r
+EFIAPI\r
+JsonValueInitFalse (\r
+  VOID\r
+  )\r
+{\r
+  return (EDKII_JSON_VALUE)json_false();\r
+}\r
+\r
 /**\r
   The function is used to initialize a JSON value which contains a new JSON NULL,\r
   or NULL on error.\r
@@ -314,6 +350,24 @@ JsonValueIsString (
   return json_is_string ((json_t *) Json);\r
 }\r
 \r
+/**\r
+  The function is used to return if the provided JSON value contains a JSON integer.\r
+\r
+  @param[in]   Json             The provided JSON value.\r
+\r
+  @retval      TRUE             The JSON value is contains JSON integer.\r
+  @retval      FALSE            The JSON value doesn't contain a JSON integer.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+JsonValueIsInteger (\r
+  IN    EDKII_JSON_VALUE    Json\r
+  )\r
+{\r
+  return json_is_integer ((json_t *) Json);\r
+}\r
+\r
 /**\r
   The function is used to return if the provided JSON value contains a JSON number.\r
 \r
@@ -329,7 +383,7 @@ JsonValueIsNumber (
   IN    EDKII_JSON_VALUE    Json\r
   )\r
 {\r
-  return json_is_integer ((json_t *) Json);\r
+  return json_is_number ((json_t *) Json);\r
 }\r
 \r
 /**\r
@@ -350,6 +404,47 @@ JsonValueIsBoolean (
   return json_is_boolean ((json_t *) Json);\r
 }\r
 \r
+/**\r
+  The function is used to return if the provided JSON value contains a TRUE value.\r
+\r
+  @param[in]   Json             The provided JSON value.\r
+\r
+  @retval      TRUE             The JSON value contains a TRUE value.\r
+  @retval      FALSE            The JSON value doesn't contain a TRUE value.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+JsonValueIsTrue (\r
+  IN    EDKII_JSON_VALUE    Json\r
+  )\r
+{\r
+  if (json_is_true ((json_t *)Json)) {\r
+    return TRUE;\r
+  }\r
+  return FALSE;\r
+}\r
+\r
+/**\r
+  The function is used to return if the provided JSON value contains a FALSE value.\r
+\r
+  @param[in]   Json             The provided JSON value.\r
+\r
+  @retval      TRUE             The JSON value contains a FALSE value.\r
+  @retval      FALSE            The JSON value doesn't contain a FALSE value.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+JsonValueIsFalse (\r
+  IN    EDKII_JSON_VALUE    Json\r
+  )\r
+{\r
+  if (json_is_false ((json_t *)Json)) {\r
+    return TRUE;\r
+  }\r
+  return FALSE;\r
+}\r
 /**\r
   The function is used to return if the provided JSON value contains a JSON NULL.\r
 \r
@@ -485,24 +580,24 @@ JsonValueGetUnicodeString (
 }\r
 \r
 /**\r
-  The function is used to retrieve the associated integer in a number type JSON value.\r
+  The function is used to retrieve the associated integer in a integer type JSON value.\r
 \r
-  The input JSON value should not be NULL or contain no JSON number, otherwise it will\r
+  The input JSON value should not be NULL or contain no JSON integer, otherwise it will\r
   ASSERT() and return 0.\r
 \r
   @param[in]   Json             The provided JSON value.\r
 \r
-  @retval      Return the associated number in JSON value.\r
+  @retval      Return the associated integer in JSON value.\r
 \r
 **/\r
 INT64\r
 EFIAPI\r
-JsonValueGetNumber (\r
+JsonValueGetInteger (\r
   IN    EDKII_JSON_VALUE    Json\r
   )\r
 {\r
-  ASSERT (Json != NULL && JsonValueIsNumber (Json));\r
-  if (Json == NULL || !JsonValueIsNumber (Json)) {\r
+  ASSERT (Json != NULL && JsonValueIsInteger (Json));\r
+  if (Json == NULL || !JsonValueIsInteger (Json)) {\r
     return 0;\r
   }\r
 \r
@@ -830,6 +925,8 @@ JsonDumpString (
   Caller needs to cleanup the root value by calling JsonValueFree().\r
 \r
   @param[in]   String        The NULL terminated CHAR8 string to convert.\r
+  @param[in]   Flags         Flags for loading JSON string.\r
+  @param[in]   Error         Returned error status.\r
 \r
   @retval      Array JSON value or object JSON value, or NULL when any error occurs.\r
 \r
@@ -837,12 +934,12 @@ JsonDumpString (
 EDKII_JSON_VALUE\r
 EFIAPI\r
 JsonLoadString (\r
-  IN    CONST CHAR8*    String\r
+  IN    CONST CHAR8*     String,\r
+  IN    UINT64           Flags,\r
+  IN    EDKII_JSON_ERROR *Error\r
   )\r
 {\r
-  json_error_t    JsonError;\r
-\r
-  return (EDKII_JSON_VALUE) json_loads ((const char *)String, 0, &JsonError);\r
+  return (EDKII_JSON_VALUE) json_loads ((const char *)String, Flags, (json_error_t *)Error);\r
 }\r
 \r
 /**\r
@@ -959,6 +1056,7 @@ JsonObjectIteratorValue (
   @retval      Iterator pointer\r
 **/\r
 VOID *\r
+EFIAPI\r
 JsonObjectIteratorNext (\r
   IN EDKII_JSON_VALUE JsonValue,\r
   IN VOID             *Iterator\r
@@ -967,6 +1065,36 @@ JsonObjectIteratorNext (
   return json_object_iter_next(JsonValue, Iterator);\r
 }\r
 \r
+/**\r
+  Returns the key of iterator pointing.\r
+\r
+  @param[in]   Iterator   Iterator pointer\r
+  @retval      Key\r
+**/\r
+CHAR8 *\r
+EFIAPI\r
+JsonObjectIteratorKey (\r
+  IN VOID *Iterator\r
+)\r
+{\r
+  return (CHAR8 *)json_object_iter_key(Iterator);\r
+}\r
+\r
+/**\r
+  Returns the pointer of iterator by key.\r
+\r
+  @param[in]   Key   The key of interator pointer.\r
+  @retval      Pointer to interator\r
+**/\r
+VOID *\r
+EFIAPI\r
+JsonObjectKeyToIterator (\r
+  IN CHAR8 *Key\r
+)\r
+{\r
+  return json_object_key_to_iter(Key);\r
+}\r
+\r
 /**\r
   Returns the json type of this json value.\r
 \r