diff --git a/json_object.c b/json_object.c index b48b889..3a5d860 100644 --- a/json_object.c +++ b/json_object.c @@ -574,13 +574,13 @@ int json_object_object_add_ex(struct json_object *jso, const char *const key, return json_object_object_add_ex_len(jso, key, strlen(key), val, opts); } -int json_object_object_add_ex_len(struct json_object *jso, const char *const key, const int len, +int json_object_object_add_ex_len(struct json_object *jso, const char *const key, const size_t len, struct json_object *const val, const unsigned opts) { // Created on the stack rather than calling `json_key_new_ptr` // or `json_key_new_imm` since this saves copying `key` if it turns // out the value already exists in the hash table - const struct json_key hashable = {.length = len, .str = {.pdata = key}}; + struct json_key hashable = {len, {key}}; return json_object_object_add_key(jso, &hashable, val, opts); } @@ -646,7 +646,7 @@ int json_object_object_add(struct json_object *jso, const char *key, struct json return json_object_object_add_ex_len(jso, key, strlen(key), val, 0); } -int json_object_object_add_len(struct json_object *jso, const char *key, const int len, +int json_object_object_add_len(struct json_object *jso, const char *key, const size_t len, struct json_object *val) { return json_object_object_add_ex_len(jso, key, len, val, 0); @@ -671,7 +671,7 @@ struct json_object *json_object_object_get(const struct json_object *jso, const } struct json_object *json_object_object_get_len(const struct json_object *jso, const char *key, - const int len) + const size_t len) { struct json_object *result = NULL; json_object_object_get_ex_len(jso, key, len, &result); @@ -685,7 +685,7 @@ json_bool json_object_object_get_ex(const struct json_object *jso, const char *k } json_bool json_object_object_get_ex_len(const struct json_object *jso, const char *key, - const int len, struct json_object **value) + const size_t len, struct json_object **value) { if (value != NULL) *value = NULL; @@ -697,7 +697,7 @@ json_bool json_object_object_get_ex_len(const struct json_object *jso, const cha { case json_type_object: { - const struct json_key hashable = {.length = len, .str = {.pdata = key}}; + const struct json_key hashable = {len, {key}}; return json_object_object_get_key(jso, &hashable, value); } default: @@ -719,9 +719,9 @@ void json_object_object_del(struct json_object *jso, const char *key) json_object_object_del_len(jso, key, strlen(key)); } -void json_object_object_del_len(struct json_object *jso, const char *key, const int len) +void json_object_object_del_len(struct json_object *jso, const char *key, const size_t len) { - const struct json_key hashable = {.length = len, .str = {.pdata = key}}; + const struct json_key hashable = {len, {key}}; json_object_object_del_key(jso, &hashable); } @@ -1888,7 +1888,7 @@ struct json_key *json_key_new_ptr(const size_t length, const char *data) { return NULL; } - result->length = length; + result->length = (ssize_t)length; result->str.pdata = data; return result; } @@ -1907,9 +1907,8 @@ struct json_key *json_key_new_imm(const size_t length, const char *data) { return NULL; } - result->length = -length; - char *unconst = _LH_UNCONST(result->str.idata); - memcpy(unconst, data, length); - unconst[length] = '\0'; + result->length = -((ssize_t)length); + memcpy(result->str.idata, data, length); + result->str.idata[length] = '\0'; return result; } diff --git a/json_object.h b/json_object.h index 771ea73..70ebcef 100644 --- a/json_object.h +++ b/json_object.h @@ -357,7 +357,8 @@ JSON_EXPORT int json_object_object_length(const struct json_object *obj); */ JSON_C_CONST_FUNCTION(JSON_EXPORT size_t json_c_object_sizeof(void)); -/** Add an object field to a json_object of type json_type_object +/** + * @brief Add an object field to a json_object of type json_type_object * * The reference count of `val` will *not* be incremented, in effect * transferring ownership that object to `obj`, and thus `val` will be @@ -385,7 +386,8 @@ JSON_C_CONST_FUNCTION(JSON_EXPORT size_t json_c_object_sizeof(void)); JSON_EXPORT int json_object_object_add(struct json_object *obj, const char *key, struct json_object *val); -/** Add an object field to a json_object of type json_type_object +/** + * @brief Add an object field to a json_object of type json_type_object * * The reference count of `val` will *not* be incremented, in effect * transferring ownership that object to `obj`, and thus `val` will be @@ -412,10 +414,11 @@ JSON_EXPORT int json_object_object_add(struct json_object *obj, const char *key, * @return On success, @c 0 is returned. * On error, a negative value is returned. */ -JSON_EXPORT int json_object_object_add_len(struct json_object *obj, const char *key, const int len, - struct json_object *val); +JSON_EXPORT int json_object_object_add_len(struct json_object *obj, const char *key, + const size_t len, struct json_object *val); -/** Add an object field to a json_object of type json_type_object +/** + * @brief Add an object field to a json_object of type json_type_object * * The semantics are identical to json_object_object_add, except that an * additional flag fields gives you more control over some detail aspects @@ -434,7 +437,8 @@ JSON_EXPORT int json_object_object_add_len(struct json_object *obj, const char * JSON_EXPORT int json_object_object_add_ex(struct json_object *obj, const char *const key, struct json_object *const val, const unsigned opts); -/** Add an object field to a json_object of type json_type_object +/** + * @brief Add an object field to a json_object of type json_type_object * * The semantics are identical to json_object_object_add, except that an * additional flag fields gives you more control over some detail aspects @@ -453,7 +457,7 @@ JSON_EXPORT int json_object_object_add_ex(struct json_object *obj, const char *c * On error, a negative value is returned. */ JSON_EXPORT int json_object_object_add_ex_len(struct json_object *obj, const char *const key, - const int len, struct json_object *const val, + const size_t len, struct json_object *const val, const unsigned opts); /** @@ -515,7 +519,7 @@ JSON_EXPORT struct json_object *json_object_object_get(const struct json_object */ JSON_EXPORT struct json_object *json_object_object_get_len(const struct json_object *obj, - const char *key, const int len); + const char *key, const size_t len); /** * @brief Get the json_object associated with a given object field. @@ -557,7 +561,7 @@ JSON_EXPORT json_bool json_object_object_get_ex(const struct json_object *obj, c * @returns whether or not the key exists */ JSON_EXPORT json_bool json_object_object_get_ex_len(const struct json_object *obj, const char *key, - const int len, struct json_object **value); + const size_t len, struct json_object **value); /** * @brief Get the json_object associated with a given object field. @@ -605,7 +609,7 @@ JSON_EXPORT void json_object_object_del(struct json_object *obj, const char *key * @param len the length of @p key */ JSON_EXPORT void json_object_object_del_len(struct json_object *jso, const char *key, - const int len); + const size_t len); /** * @brief Delete the given json_object field @@ -617,8 +621,7 @@ JSON_EXPORT void json_object_object_del_len(struct json_object *jso, const char * @param obj the json_object instance * @param key the object field name */ -JSON_EXPORT int json_object_object_del_key(struct json_object *jso_base, - const struct json_key *key); +JSON_EXPORT int json_object_object_del_key(struct json_object *obj, const struct json_key *key); /** * @brief Iterate through all keys and values of an object. @@ -655,12 +658,12 @@ JSON_EXPORT int json_object_object_del_key(struct json_object *jso_base, #else /* ANSI C or MSC */ #define json_object_object_foreach(obj, key, val) \ - char *key = NULL; \ + struct json_key *key = NULL; \ struct json_object *val = NULL; \ struct lh_entry *entry##key; \ struct lh_entry *entry_next##key = NULL; \ for (entry##key = json_object_get_object(obj)->head; \ - (entry##key ? (key = (char *)lh_entry_k(entry##key), \ + (entry##key ? (key = (struct json_key *)lh_entry_k(entry##key), \ val = (struct json_object *)lh_entry_v(entry##key), \ entry_next##key = entry##key->next, entry##key) \ : 0); \ diff --git a/json_object_private.h b/json_object_private.h index 6ad1044..cae49f7 100644 --- a/json_object_private.h +++ b/json_object_private.h @@ -141,7 +141,7 @@ struct json_key * enough space to store the whole string (of length `len`) * and one additional null character. */ - const char idata[0]; + char idata[0]; } str; };