Skip to content

Commit 6917586

Browse files
author
Keith Derrick
committed
Add NULL-safe get object method
New json_object_object_get_ex() method protects itself against null pointers and invalid objects being passed in.
1 parent 4a2cd96 commit 6917586

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

json_object.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*
44
* Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
55
* Michael Clark <[email protected]>
6+
* Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
67
*
78
* This library is free software; you can redistribute it and/or modify
89
* it under the terms of the MIT license. See COPYING for details.
@@ -24,11 +25,13 @@
2425
#include "json_object.h"
2526
#include "json_object_private.h"
2627
#include "json_util.h"
28+
#include "json_tokener.h"
2729

2830
#if !HAVE_STRNDUP
2931
char* strndup(const char* str, size_t n);
3032
#endif /* !HAVE_STRNDUP */
3133

34+
// Don't define this. It's not thread-safe.
3235
/* #define REFCOUNT_DEBUG 1 */
3336

3437
const char *json_number_chars = "0123456789.+-eE";
@@ -260,8 +263,24 @@ void json_object_object_add(struct json_object* jso, const char *key,
260263

261264
struct json_object* json_object_object_get(struct json_object* jso, const char *key)
262265
{
263-
if(!jso) return NULL;
264-
return (struct json_object*) lh_table_lookup(jso->o.c_object, key);
266+
struct json_object *result;
267+
json_object_object_get_ex(jso, key, &result);
268+
return result;
269+
}
270+
271+
json_bool json_object_object_get_ex(struct json_object* jso, const char *key, struct json_object **value)
272+
{
273+
if (NULL == jso) return FALSE;
274+
275+
switch(jso->o_type) {
276+
case json_type_object:
277+
return lh_table_lookup_ex(jso->o.c_object, (void*)key, (void**)value);
278+
default:
279+
if (value != NULL) {
280+
*value = NULL;
281+
}
282+
return FALSE;
283+
}
265284
}
266285

267286
void json_object_object_del(struct json_object* jso, const char *key)

json_object.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*
44
* Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
55
* Michael Clark <[email protected]>
6+
* Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
67
*
78
* This library is free software; you can redistribute it and/or modify
89
* 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,
172173
* @param obj the json_object instance
173174
* @param key the object field name
174175
* @returns the json_object associated with the given field name
176+
* @deprecated Please use json_object_object_get_ex
175177
*/
176178
extern struct json_object* json_object_object_get(struct json_object* obj,
177179
const char *key);
178180

181+
/** Get the json_object associated with a given object field.
182+
*
183+
* This returns true if the key is found, false in all other cases (including
184+
* if obj isn't a json_type_object).
185+
*
186+
* *No* reference counts will be changed. There is no need to manually adjust
187+
* reference counts through the json_object_put/json_object_get methods unless
188+
* you need to have the child (value) reference maintain a different lifetime
189+
* than the owning parent (obj). Ownership of value is retained by obj.
190+
*
191+
* @param obj the json_object instance
192+
* @param key the object field name
193+
* @param value a pointer where to store a reference to the json_object
194+
* associated with the given field name.
195+
*
196+
* It is safe to pass a NULL value.
197+
* @returns whether or not the key exists
198+
*/
199+
extern json_bool json_object_object_get_ex(struct json_object* obj,
200+
const char *key,
201+
struct json_object **value);
202+
179203
/** Delete the given json_object field
180204
*
181205
* The reference count will be decremented for the deleted object. If there

0 commit comments

Comments
 (0)