Add NULL-safe get object method
New json_object_object_get_ex() method protects itself against null pointers and invalid objects being passed in.
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
*
|
*
|
||||||
* Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
|
* Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
|
||||||
* Michael Clark <michael@metaparadigm.com>
|
* Michael Clark <michael@metaparadigm.com>
|
||||||
|
* Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
|
||||||
*
|
*
|
||||||
* This library is free software; you can redistribute it and/or modify
|
* This library is free software; you can redistribute it and/or modify
|
||||||
* it under the terms of the MIT license. See COPYING for details.
|
* it under the terms of the MIT license. See COPYING for details.
|
||||||
@@ -24,11 +25,13 @@
|
|||||||
#include "json_object.h"
|
#include "json_object.h"
|
||||||
#include "json_object_private.h"
|
#include "json_object_private.h"
|
||||||
#include "json_util.h"
|
#include "json_util.h"
|
||||||
|
#include "json_tokener.h"
|
||||||
|
|
||||||
#if !HAVE_STRNDUP
|
#if !HAVE_STRNDUP
|
||||||
char* strndup(const char* str, size_t n);
|
char* strndup(const char* str, size_t n);
|
||||||
#endif /* !HAVE_STRNDUP */
|
#endif /* !HAVE_STRNDUP */
|
||||||
|
|
||||||
|
// Don't define this. It's not thread-safe.
|
||||||
/* #define REFCOUNT_DEBUG 1 */
|
/* #define REFCOUNT_DEBUG 1 */
|
||||||
|
|
||||||
const char *json_number_chars = "0123456789.+-eE";
|
const char *json_number_chars = "0123456789.+-eE";
|
||||||
@@ -260,8 +263,24 @@ void json_object_object_add(struct json_object* jso, const char *key,
|
|||||||
|
|
||||||
struct json_object* json_object_object_get(struct json_object* jso, const char *key)
|
struct json_object* json_object_object_get(struct json_object* jso, const char *key)
|
||||||
{
|
{
|
||||||
if(!jso) return NULL;
|
struct json_object *result;
|
||||||
return (struct json_object*) lh_table_lookup(jso->o.c_object, key);
|
json_object_object_get_ex(jso, key, &result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
json_bool json_object_object_get_ex(struct json_object* jso, const char *key, struct json_object **value)
|
||||||
|
{
|
||||||
|
if (NULL == jso) return FALSE;
|
||||||
|
|
||||||
|
switch(jso->o_type) {
|
||||||
|
case json_type_object:
|
||||||
|
return lh_table_lookup_ex(jso->o.c_object, (void*)key, (void**)value);
|
||||||
|
default:
|
||||||
|
if (value != NULL) {
|
||||||
|
*value = NULL;
|
||||||
|
}
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_object_object_del(struct json_object* jso, const char *key)
|
void json_object_object_del(struct json_object* jso, const char *key)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
*
|
*
|
||||||
* Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
|
* Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
|
||||||
* Michael Clark <michael@metaparadigm.com>
|
* Michael Clark <michael@metaparadigm.com>
|
||||||
|
* Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
|
||||||
*
|
*
|
||||||
* This library is free software; you can redistribute it and/or modify
|
* This library is free software; you can redistribute it and/or modify
|
||||||
* it under the terms of the MIT license. See COPYING for details.
|
* it under the terms of the MIT license. See COPYING for details.
|
||||||
@@ -172,10 +173,33 @@ extern void json_object_object_add(struct json_object* obj, const char *key,
|
|||||||
* @param obj the json_object instance
|
* @param obj the json_object instance
|
||||||
* @param key the object field name
|
* @param key the object field name
|
||||||
* @returns the json_object associated with the given field name
|
* @returns the json_object associated with the given field name
|
||||||
|
* @deprecated Please use json_object_object_get_ex
|
||||||
*/
|
*/
|
||||||
extern struct json_object* json_object_object_get(struct json_object* obj,
|
extern struct json_object* json_object_object_get(struct json_object* obj,
|
||||||
const char *key);
|
const char *key);
|
||||||
|
|
||||||
|
/** Get the json_object associated with a given object field.
|
||||||
|
*
|
||||||
|
* This returns true if the key is found, false in all other cases (including
|
||||||
|
* if obj isn't a json_type_object).
|
||||||
|
*
|
||||||
|
* *No* reference counts will be changed. There is no need to manually adjust
|
||||||
|
* reference counts through the json_object_put/json_object_get methods unless
|
||||||
|
* you need to have the child (value) reference maintain a different lifetime
|
||||||
|
* than the owning parent (obj). Ownership of value is retained by obj.
|
||||||
|
*
|
||||||
|
* @param obj the json_object instance
|
||||||
|
* @param key the object field name
|
||||||
|
* @param value a pointer where to store a reference to the json_object
|
||||||
|
* associated with the given field name.
|
||||||
|
*
|
||||||
|
* It is safe to pass a NULL value.
|
||||||
|
* @returns whether or not the key exists
|
||||||
|
*/
|
||||||
|
extern json_bool json_object_object_get_ex(struct json_object* obj,
|
||||||
|
const char *key,
|
||||||
|
struct json_object **value);
|
||||||
|
|
||||||
/** Delete the given json_object field
|
/** Delete the given json_object field
|
||||||
*
|
*
|
||||||
* The reference count will be decremented for the deleted object. If there
|
* The reference count will be decremented for the deleted object. If there
|
||||||
|
|||||||
Reference in New Issue
Block a user