Skip to content

Commit

Permalink
Address review feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenroche5 committed Apr 16, 2024
1 parent 01eadd2 commit 205ab63
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 91 deletions.
163 changes: 85 additions & 78 deletions n_cjson_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -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;
}

Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 205ab63

Please sign in to comment.