summaryrefslogtreecommitdiffstats
path: root/RedfishPkg
diff options
context:
space:
mode:
authorAbner Chang <abner.chang@hpe.com>2021-04-27 12:53:30 +0800
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>2021-08-10 09:59:05 -0700
commita7ddc7847cb4afa3534da82222f296aafadb959d (patch)
tree8e3bd7d0a746a6ea8185200924495ad1eb9b1df5 /RedfishPkg
parentd02dbb53cd78de799e6afaa237e98771fb5148db (diff)
downloadedk2-a7ddc7847cb4afa3534da82222f296aafadb959d.tar.gz
edk2-a7ddc7847cb4afa3534da82222f296aafadb959d.tar.bz2
edk2-a7ddc7847cb4afa3534da82222f296aafadb959d.zip
RedfishPkg/JsonLib: Add more JsonLib functions
Signed-off-by: Abner Chang <abner.chang@hpe.com> Cc: Nickle Wang <nickle.wang@hpe.com> Reviewed-by: Nickle Wang <nickle.wang@hpe.com>
Diffstat (limited to 'RedfishPkg')
-rw-r--r--RedfishPkg/Include/Library/JsonLib.h127
-rw-r--r--RedfishPkg/Library/JsonLib/JsonLib.c156
2 files changed, 261 insertions, 22 deletions
diff --git a/RedfishPkg/Include/Library/JsonLib.h b/RedfishPkg/Include/Library/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.
Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
- (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
+ (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -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)); \
Index++)
+#define EDKII_JSON_OBJECT_FOREACH_SAFE(Object, N, Key, Value) \
+ for (Key = JsonObjectIteratorKey(JsonObjectIterator(Object)), \
+ N = JsonObjectIteratorNext(Object, JsonObjectKeyToIterator(Key)); \
+ Key && (Value = JsonObjectIteratorValue(JsonObjectKeyToIterator(Key))); \
+ Key = JsonObjectIteratorKey(N), \
+ N = JsonObjectIteratorNext(Object, JsonObjectKeyToIterator(Key)))
+
///
/// Map to the json_error_t in jansson.h
///
@@ -177,12 +184,12 @@ JsonValueInitUnicodeString (
@param[in] Value The integer to initialize to JSON value
- @retval The created JSON value which contains a JSON number or NULL.
+ @retval The created JSON value which contains a JSON integer or NULL.
**/
EDKII_JSON_VALUE
EFIAPI
-JsonValueInitNumber (
+JsonValueInitInteger (
IN INT64 Value
);
@@ -219,6 +226,36 @@ JsonValueInitNull (
);
/**
+ The function is used to initialize a JSON value which contains a TRUE JSON value,
+ or NULL on error.
+
+ NULL JSON value is kept as static value, and no need to do any cleanup work.
+
+ @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 JSON value,
+ or NULL on error.
+
+ NULL JSON value is kept as static value, and no need to do any cleanup work.
+
+ @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.
If this destroyed value is object type or array type, reference counts for all containing
@@ -314,6 +351,21 @@ JsonValueIsString (
);
/**
+ The function is used to return if the provided JSON value contains a JSON 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 integer.
+
+**/
+BOOLEAN
+EFIAPI
+JsonValueIsInteger (
+ IN EDKII_JSON_VALUE Json
+ );
+
+/**
The function is used to return if the provided JSON value contains a JSON number.
@param[in] Json The provided JSON value.
@@ -344,6 +396,36 @@ JsonValueIsBoolean (
);
/**
+ The function is used to return if the provided JSON value contains a TRUE 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 value.
+
+**/
+BOOLEAN
+EFIAPI
+JsonValueIsTrue (
+ IN EDKII_JSON_VALUE Json
+ );
+
+/**
+ The function is used to return if the provided JSON value contains a FALSE 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 value.
+
+**/
+BOOLEAN
+EFIAPI
+JsonValueIsFalse (
+ IN EDKII_JSON_VALUE Json
+ );
+
+/**
The function is used to return if the provided JSON value contains a JSON NULL.
@param[in] Json The provided JSON value.
@@ -424,19 +506,19 @@ JsonValueGetUnicodeString (
);
/**
- 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 type JSON value.
- The input JSON value should not be NULL or contain no JSON number, otherwise it will
+ The input JSON value should not be NULL or contain no JSON Integer, otherwise it will
ASSERT() and return 0.
@param[in] Json The provided JSON value.
- @retval Return the associated number in JSON value.
+ @retval Return the associated Integer in JSON value.
**/
INT64
EFIAPI
-JsonValueGetNumber (
+JsonValueGetInteger (
IN EDKII_JSON_VALUE Json
);
@@ -675,6 +757,8 @@ JsonDumpString (
Caller needs to cleanup the root value by calling JsonValueFree().
@param[in] String The NULL terminated CHAR8 string to convert.
+ @param[in] Flags Flags for loading JSON string.
+ @param[in] Error Returned error status.
@retval Array JSON value or object JSON value, or NULL when any error occurs.
@@ -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
);
/**
@@ -781,12 +867,37 @@ JsonObjectIteratorValue (
@retval Iterator pointer
**/
VOID *
+EFIAPI
JsonObjectIteratorNext (
IN EDKII_JSON_VALUE JsonValue,
IN VOID *Iterator
);
/**
+ 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
@param[in] JsonValue JSON value
diff --git a/RedfishPkg/Library/JsonLib/JsonLib.c b/RedfishPkg/Library/JsonLib/JsonLib.c
index c305a3767f..33d5eaef79 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
Copyright (c) 2018 - 2019, Intel Corporation. All rights reserved.<BR>
- (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
+ (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
@@ -154,12 +154,12 @@ JsonValueInitUnicodeString (
@param[in] Value The integer to initialize to JSON value
- @retval The created JSON value which contains a JSON number or NULL.
+ @retval The created JSON value which contains a JSON integer or NULL.
**/
EDKII_JSON_VALUE
EFIAPI
-JsonValueInitNumber (
+JsonValueInitInteger (
IN INT64 Value
)
{
@@ -187,6 +187,42 @@ JsonValueInitBoolean (
}
/**
+ The function is used to initialize a JSON value which contains a TRUE JSON value,
+ or NULL on error.
+
+ NULL JSON value is kept as static value, and no need to do any cleanup work.
+
+ @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 JSON value,
+ or NULL on error.
+
+ NULL JSON value is kept as static value, and no need to do any cleanup work.
+
+ @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 JSON NULL,
or NULL on error.
@@ -315,6 +351,24 @@ JsonValueIsString (
}
/**
+ The function is used to return if the provided JSON value contains a JSON 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 integer.
+
+**/
+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 JSON number.
@param[in] Json The provided JSON value.
@@ -329,7 +383,7 @@ JsonValueIsNumber (
IN EDKII_JSON_VALUE Json
)
{
- return json_is_integer ((json_t *) Json);
+ return json_is_number ((json_t *) Json);
}
/**
@@ -351,6 +405,47 @@ JsonValueIsBoolean (
}
/**
+ The function is used to return if the provided JSON value contains a TRUE 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 value.
+
+**/
+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 FALSE 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 value.
+
+**/
+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 JSON NULL.
@param[in] Json The provided JSON value.
@@ -485,24 +580,24 @@ JsonValueGetUnicodeString (
}
/**
- 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 type JSON value.
- The input JSON value should not be NULL or contain no JSON number, otherwise it will
+ The input JSON value should not be NULL or contain no JSON integer, otherwise it will
ASSERT() and return 0.
@param[in] Json The provided JSON value.
- @retval Return the associated number in JSON value.
+ @retval Return the associated integer in JSON value.
**/
INT64
EFIAPI
-JsonValueGetNumber (
+JsonValueGetInteger (
IN EDKII_JSON_VALUE Json
)
{
- ASSERT (Json != NULL && JsonValueIsNumber (Json));
- if (Json == NULL || !JsonValueIsNumber (Json)) {
+ ASSERT (Json != NULL && JsonValueIsInteger (Json));
+ if (Json == NULL || !JsonValueIsInteger (Json)) {
return 0;
}
@@ -830,6 +925,8 @@ JsonDumpString (
Caller needs to cleanup the root value by calling JsonValueFree().
@param[in] String The NULL terminated CHAR8 string to convert.
+ @param[in] Flags Flags for loading JSON string.
+ @param[in] Error Returned error status.
@retval Array JSON value or object JSON value, or NULL when any error occurs.
@@ -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, &JsonError);
+ return (EDKII_JSON_VALUE) json_loads ((const char *)String, Flags, (json_error_t *)Error);
}
/**
@@ -959,6 +1056,7 @@ JsonObjectIteratorValue (
@retval Iterator pointer
**/
VOID *
+EFIAPI
JsonObjectIteratorNext (
IN EDKII_JSON_VALUE JsonValue,
IN VOID *Iterator
@@ -968,6 +1066,36 @@ JsonObjectIteratorNext (
}
/**
+ Returns the key of iterator pointing.
+
+ @param[in] Iterator Iterator pointer
+ @retval Key
+**/
+CHAR8 *
+EFIAPI
+JsonObjectIteratorKey (
+ IN VOID *Iterator
+)
+{
+ return (CHAR8 *)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.
@param[in] JsonValue JSON value