]> git.ipfire.org Git - thirdparty/json-c.git/commitdiff
Add NULL-safe get object method
authorKeith Derrick <keith.derrick@palm.com>
Thu, 12 Apr 2012 18:44:13 +0000 (11:44 -0700)
committerKeith Derrick <keith.derrick@palm.com>
Thu, 12 Apr 2012 18:50:08 +0000 (11:50 -0700)
New json_object_object_get_ex() method protects itself against null pointers
and invalid objects being passed in.

json_object.c
json_object.h

index ef54ecd40b3ee8a5ac4b1f6d80c632235837d72f..842ca22adca5dd6ef17bc333420f3678ce883e73 100644 (file)
@@ -3,6 +3,7 @@
  *
  * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
  * 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
  * it under the terms of the MIT license. See COPYING for details.
 #include "json_object.h"
 #include "json_object_private.h"
 #include "json_util.h"
+#include "json_tokener.h"
 
 #if !HAVE_STRNDUP
   char* strndup(const char* str, size_t n);
 #endif /* !HAVE_STRNDUP */
 
+// Don't define this.  It's not thread-safe.
 /* #define REFCOUNT_DEBUG 1 */
 
 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)
 {
-  if(!jso) return NULL;
-  return (struct json_object*) lh_table_lookup(jso->o.c_object, key);
+  struct json_object *result;
+  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)
index 7b2c4ee2ae123e7682afc22cd066925bb67b55d3..f7ec9ea2bd2702ea09f22953478f1dea187db690 100644 (file)
@@ -3,6 +3,7 @@
  *
  * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
  * 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
  * 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 key the object 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,
                                                  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
  *
  * The reference count will be decremented for the deleted object.  If there