From 205ab63e5df3d1c0efd1858203125148e6d1624a Mon Sep 17 00:00:00 2001 From: Hayden Roche Date: Tue, 16 Apr 2024 14:40:23 -0700 Subject: [PATCH] Address review feedback. --- n_cjson_helpers.c | 163 ++++++++++++++++++++++++---------------------- note.h | 26 ++++---- 2 files changed, 98 insertions(+), 91 deletions(-) diff --git a/n_cjson_helpers.c b/n_cjson_helpers.c index 0519f4a..e6139ef 100644 --- a/n_cjson_helpers.c +++ b/n_cjson_helpers.c @@ -19,38 +19,39 @@ /*! @brief Determine if a field is present in a JSON object. - @param rsp The JSON object. + @param json The JSON object. @param field The field to find. - @returns True if the field is present and false otherwise. + @returns True if the field is present, or false if the field is not present or + the JSON object is NULL. */ -bool JIsPresent(J *rsp, const char *field) +bool JIsPresent(J *json, const char *field) { - if (rsp == NULL) { + if (json == NULL) { return false; } - return (JGetObjectItem(rsp, field) != NULL); + return (JGetObjectItem(json, field) != NULL); } /*! @brief Get the value of a string field from a JSON object. - @param rsp The JSON object. + @param json The JSON object. @param field The string field to query. - @returns A pointer to the string if the field exists and is a string and the - empty string ("") otherwise. + @returns A pointer to the string if the field exists and is a string, otherwise + the empty string (""). @note The returned string is a pointer to the string contained in the JSON object. It is not a copy of the string, so once the JSON object is freed, the pointer is no longer valid. */ -char *JGetString(J *rsp, const char *field) +char *JGetString(J *json, const char *field) { - if (rsp == NULL) { + if (json == NULL) { return (char *) c_nullstring; } - J *item = JGetObjectItem(rsp, field); + J *item = JGetObjectItem(json, field); if (item == NULL) { return (char *) c_nullstring; } @@ -66,22 +67,22 @@ char *JGetString(J *rsp, const char *field) /*! @brief Get the value of an array field from a JSON object. - @param rsp The JSON object. + @param json The JSON object. @param field The array field to query. @returns A pointer to the array, which is itself a JSON object (`J *`), if the - field exists and is an array and NULL otherwise. + field exists and is an array, otherwise NULL. @note The returned JSON object is a pointer to the array contained in the parent JSON object. It is not a copy, so once the parent JSON object is freed, the pointer is no longer valid. */ -J *JGetArray(J *rsp, const char *field) +J *JGetArray(J *json, const char *field) { - if (rsp == NULL) { + if (json == NULL) { return NULL; } - J *item = JGetObjectItem(rsp, field); + J *item = JGetObjectItem(json, field); if (item == NULL) { return NULL; } @@ -94,22 +95,22 @@ J *JGetArray(J *rsp, const char *field) /*! @brief Get the value of an object field from a JSON object. - @param rsp The JSON object. + @param json The JSON object. @param field The object field to query. @returns A pointer to the object, which is itself a JSON object (`J *`), if the - field exists and is an object and NULL otherwise. + field exists and is an object, otherwise NULL. @note The returned JSON object is a pointer to the object contained in the parent JSON object. It is not a copy, so once the parent JSON object is freed, the pointer is no longer valid. */ -J *JGetObject(J *rsp, const char *field) +J *JGetObject(J *json, const char *field) { - if (rsp == NULL) { + if (json == NULL) { return NULL; } - J *item = JGetObjectItem(rsp, field); + J *item = JGetObjectItem(json, field); if (item == NULL) { return NULL; } @@ -167,22 +168,23 @@ JNUMBER JNumberValue(J *item) /*! @brief Get the floating point value of a number field from a JSON object. - @param rsp The JSON object. + @param json The JSON object. @param field The number field to query. - @returns The value of the number field if the field exists and is a number and - 0.0 otherwise. + @returns The value of the number field if the field exists and is a number, + otherwise 0.0. - @note The returned value is the floating point representation of the number. If - the field is an integer rather than a floating point number, use - `JGetInt`. + @note The returned value is the floating point representation of the number. + + @see `JGetInt` + @see `JGetBool` */ -JNUMBER JGetNumber(J *rsp, const char *field) +JNUMBER JGetNumber(J *json, const char *field) { - if (rsp == NULL) { + if (json == NULL) { return 0.0; } - J *item = JGetObjectItem(rsp, field); + J *item = JGetObjectItem(json, field); if (item == NULL) { return 0.0; } @@ -210,22 +212,23 @@ JINTEGER JIntValue(J *item) /*! @brief Get the integer value of a number field from a JSON object. - @param rsp The JSON object. + @param json The JSON object. @param field The number field to query. - @returns The value of the number field if the field exists and is a number and - 0 otherwise. + @returns The value of the number field if the field exists and is a number, + otherwise 0. + + @note The returned value is the integer representation of the number. - @note The returned value is the integer representation of the number. If the - field is a floating point number rather than an integer, use - `JGetNumber`. + @see `JGetBool` + @see `JGetNumber` */ -JINTEGER JGetInt(J *rsp, const char *field) +JINTEGER JGetInt(J *json, const char *field) { - if (rsp == NULL) { + if (json == NULL) { return 0; } - J *item = JGetObjectItem(rsp, field); + J *item = JGetObjectItem(json, field); if (item == NULL) { return 0; } @@ -238,18 +241,21 @@ JINTEGER JGetInt(J *rsp, const char *field) /*! @brief Get the value of a boolean field from a JSON object. - @param rsp The JSON object. + @param json The JSON object. @param field The boolean field to query. - @returns The value of the boolean field if it exists and is a boolean and false - otherwise. + @returns The value of the boolean field if it exists and is a boolean, + otherwise false. + + @see `JGetInt` + @see `JGetNumber` */ -bool JGetBool(J *rsp, const char *field) +bool JGetBool(J *json, const char *field) { - if (rsp == NULL) { + if (json == NULL) { return false; } - J *item = JGetObjectItem(rsp, field); + J *item = JGetObjectItem(json, field); if (item == NULL) { return false; } @@ -263,17 +269,17 @@ bool JGetBool(J *rsp, const char *field) /*! @brief Determine if a JSON object is valid and if a field is not present, or null. - @param rsp The JSON response object. + @param json The JSON response object. @param field The field to return. @returns bool. False if the field is not present, or NULL. */ /**************************************************************************/ -bool JIsNullString(J *rsp, const char *field) +bool JIsNullString(J *json, const char *field) { - if (rsp == NULL) { + if (json == NULL) { return false; } - J *item = JGetObjectItem(rsp, field); + J *item = JGetObjectItem(json, field); if (item == NULL) { return true; } @@ -293,18 +299,18 @@ bool JIsNullString(J *rsp, const char *field) /*! @brief Determine if a field exists, is a string and matches a provided value. - @param rsp The JSON response object. + @param json The JSON response object. @param field The field to check. @param teststr The string to test against the returned value. @returns bol. Whether the fields match exactly. */ /**************************************************************************/ -bool JIsExactString(J *rsp, const char *field, const char *teststr) +bool JIsExactString(J *json, const char *field, const char *teststr) { - if (rsp == NULL) { + if (json == NULL) { return false; } - J *item = JGetObjectItem(rsp, field); + J *item = JGetObjectItem(json, field); if (item == NULL) { return false; } @@ -324,18 +330,18 @@ bool JIsExactString(J *rsp, const char *field, const char *teststr) /*! @brief Determine if a field exists, is a string and contains a provided value. - @param rsp The JSON response object. + @param json The JSON response object. @param field The field to check. @param substr The string to test against the returned value. @returns bol. Whether the provided string is found within the field. */ /**************************************************************************/ -bool JContainsString(J *rsp, const char *field, const char *substr) +bool JContainsString(J *json, const char *field, const char *substr) { - if (rsp == NULL) { + if (json == NULL) { return false; } - J *item = JGetObjectItem(rsp, field); + J *item = JGetObjectItem(json, field); if (item == NULL) { return false; } @@ -355,17 +361,17 @@ bool JContainsString(J *rsp, const char *field, const char *substr) /*! @brief Add binary data as a Base64-encoded string field to a JSON object. - @param req The JSON object. + @param json The JSON object. @param fieldName The name to use for the field. @param binaryData A buffer of binary data to encode. @param binaryDataLen The length of the binary data in bytes. - @returns True if the string was successfully encoded and added to the object - and false otherwise. + @returns True if the string was successfully encoded and added to the object, + otherwise false. */ -bool JAddBinaryToObject(J *req, const char *fieldName, const void *binaryData, uint32_t binaryDataLen) +bool JAddBinaryToObject(J *json, const char *fieldName, const void *binaryData, uint32_t binaryDataLen) { - if (req == NULL) { + if (json == NULL) { return false; } unsigned stringDataLen = JB64EncodeLen(binaryDataLen); @@ -379,7 +385,7 @@ bool JAddBinaryToObject(J *req, const char *fieldName, const void *binaryData, u _Free(stringData); return false; } - JAddItemToObject(req, fieldName, stringItem); + JAddItemToObject(json, fieldName, stringItem); return true; } @@ -388,31 +394,32 @@ bool JAddBinaryToObject(J *req, const char *fieldName, const void *binaryData, u @brief Decode a Base64-encoded string field in a JSON object and return the decoded bytes. - @param rsp The JSON object. + @param json The JSON object. @param fieldName The name of the field. - @param retBinaryData A pointer to a buffer to hold the decoded bytes. - @param retBinaryDataLen A pointer to a number to hold the length of the decoded - bytes. + @param retBinaryData A pointer to a pointer, used to hold the pointer to the + decoded bytes. + @param retBinaryDataLen A pointer to an unsigned integer, used to hold the + length of the decoded bytes. - @returns True if the string was successfully decoded and returned and false - otherwise. + @returns True if the string was successfully decoded and returned, otherwise + false. @note The returned binary buffer must be freed by the user with `JFree` when it is no longer needed. */ -bool JGetBinaryFromObject(J *rsp, const char *fieldName, uint8_t **retBinaryData, uint32_t *retBinaryDataLen) +bool JGetBinaryFromObject(J *json, const char *fieldName, uint8_t **retBinaryData, uint32_t *retBinaryDataLen) { - if (rsp == NULL) { + if (json == NULL) { return false; } // In some cases, the caller may already have extracted the string from a different field, in which - // case "rsp" will be set to the payload pointer. + // case "json" will be set to the payload pointer. char *payload; if (fieldName == NULL) { - payload = (char *) rsp; + payload = (char *) json; } else { - payload = JGetString(rsp, fieldName); + payload = JGetString(json, fieldName); } if (payload[0] == '\0') { return false; @@ -582,18 +589,18 @@ const char *JType(J *item) //**************************************************************************/ /*! @brief Get the type of a field, as an int usable in a switch statement. - @param rsp The JSON object containing the field. + @param json The JSON object containing the field. @param field The field's name. @returns The type of the field on success. JTYPE_NOT_PRESENT on error or if the field doesn't exist. */ /**************************************************************************/ -int JGetType(J *rsp, const char *field) +int JGetType(J *json, const char *field) { - if (rsp == NULL || field == NULL) { + if (json == NULL || field == NULL) { return JTYPE_NOT_PRESENT; } - return JGetItemType(JGetObjectItem(rsp, field)); + return JGetItemType(JGetObjectItem(json, field)); } // Get the diff --git a/note.h b/note.h index 7f6d5a8..6f9bd6e 100644 --- a/note.h +++ b/note.h @@ -397,22 +397,22 @@ size_t strlcat(char *dst, const char *src, size_t siz); // JSON helpers void JInit(void); void JCheck(void); -bool JIsPresent(J *rsp, const char *field); -char *JGetString(J *rsp, const char *field); -JNUMBER JGetNumber(J *rsp, const char *field); -J *JGetArray(J *rsp, const char *field); -J *JGetObject(J *rsp, const char *field); -JINTEGER JGetInt(J *rsp, const char *field); -bool JGetBool(J *rsp, const char *field); +bool JIsPresent(J *json, const char *field); +char *JGetString(J *json, const char *field); +JNUMBER JGetNumber(J *json, const char *field); +J *JGetArray(J *json, const char *field); +J *JGetObject(J *json, const char *field); +JINTEGER JGetInt(J *json, const char *field); +bool JGetBool(J *json, const char *field); JNUMBER JNumberValue(J *item); char *JStringValue(J *item); bool JBoolValue(J *item); JINTEGER JIntValue(J *item); -bool JIsNullString(J *rsp, const char *field); -bool JIsExactString(J *rsp, const char *field, const char *teststr); -bool JContainsString(J *rsp, const char *field, const char *substr); -bool JAddBinaryToObject(J *req, const char *fieldName, const void *binaryData, uint32_t binaryDataLen); -bool JGetBinaryFromObject(J *rsp, const char *fieldName, uint8_t **retBinaryData, uint32_t *retBinaryDataLen); +bool JIsNullString(J *json, const char *field); +bool JIsExactString(J *json, const char *field, const char *teststr); +bool JContainsString(J *json, const char *field, const char *substr); +bool JAddBinaryToObject(J *json, const char *fieldName, const void *binaryData, uint32_t binaryDataLen); +bool JGetBinaryFromObject(J *json, const char *fieldName, uint8_t **retBinaryData, uint32_t *retBinaryDataLen); const char *JGetItemName(const J * item); char *JAllocString(uint8_t *buffer, uint32_t len); const char *JType(J *item); @@ -432,7 +432,7 @@ const char *JType(J *item); #define JTYPE_STRING 11 #define JTYPE_OBJECT 12 #define JTYPE_ARRAY 13 -int JGetType(J *rsp, const char *field); +int JGetType(J *json, const char *field); int JGetItemType(J *item); int JBaseItemType(int type); #define JGetObjectItemName(j) (j->string)