From 5d7b5cd10569f3098c8a51c10a94bf2f6cc45bf5 Mon Sep 17 00:00:00 2001 From: Abner Chang Date: Fri, 29 Jan 2021 11:46:53 +0800 Subject: [PATCH] RedfishPkg/JsonLib: Add JsonLoadString function Add JsonLoadString function to load a NULL terminated-string JSON Signed-off-by: Abner Chang Cc: Leif Lindholm Cc: Nickle Wang Cc: Michael D Kinney Reviewed-by: Nickle Wang --- RedfishPkg/Include/Library/JsonLib.h | 21 +++++++++++++++++++++ RedfishPkg/Library/JsonLib/JsonLib.c | 26 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/RedfishPkg/Include/Library/JsonLib.h b/RedfishPkg/Include/Library/JsonLib.h index 3c10f67d27..8a30c5250b 100644 --- a/RedfishPkg/Include/Library/JsonLib.h +++ b/RedfishPkg/Include/Library/JsonLib.h @@ -664,6 +664,27 @@ JsonDumpString ( IN UINTN Flags ); +/** + Convert a string to JSON object. + The function is used to convert a NULL terminated CHAR8 string to a JSON + 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. + + Caller needs to cleanup the root value by calling JsonValueFree(). + + @param[in] String The NULL terminated CHAR8 string to convert. + + @retval Array JSON value or object JSON value, or NULL when any error occurs. + +**/ +EDKII_JSON_VALUE +EFIAPI +JsonLoadString ( + IN CONST CHAR8* String + ); + /** Load JSON from a buffer. diff --git a/RedfishPkg/Library/JsonLib/JsonLib.c b/RedfishPkg/Library/JsonLib/JsonLib.c index 34ff381aee..3693299afb 100644 --- a/RedfishPkg/Library/JsonLib/JsonLib.c +++ b/RedfishPkg/Library/JsonLib/JsonLib.c @@ -819,6 +819,32 @@ JsonDumpString ( return json_dumps((json_t *)JsonValue, Flags); } +/** + Convert a string to JSON object. + The function is used to convert a NULL terminated CHAR8 string to a JSON + 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. + + Caller needs to cleanup the root value by calling JsonValueFree(). + + @param[in] String The NULL terminated CHAR8 string to convert. + + @retval Array JSON value or object JSON value, or NULL when any error occurs. + +**/ +EDKII_JSON_VALUE +EFIAPI +JsonLoadString ( + IN CONST CHAR8* String + ) +{ + json_error_t JsonError; + + return (EDKII_JSON_VALUE) json_loads ((const char *)String, 0, &JsonError); +} + /** Load JSON from a buffer. -- 2.39.2