From: David M. Lee Date: Tue, 7 May 2013 18:12:26 +0000 (+0000) Subject: Better explained the depths of reference stealing. X-Git-Tag: 13.0.0-beta1~1841 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=737a45f2f72185ea48e8f1e3d95e93ebc7cdfd50;p=thirdparty%2Fasterisk.git Better explained the depths of reference stealing. git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@387803 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- diff --git a/include/asterisk/json.h b/include/asterisk/json.h index 7b89d82709..8e646584df 100644 --- a/include/asterisk/json.h +++ b/include/asterisk/json.h @@ -24,9 +24,54 @@ * \brief Asterisk JSON abstraction layer. * \since 12.0.0 * - * This is a very thin wrapper around the Jansson API. For more details on it, see its - * docs at http://www.digip.org/jansson/doc/2.4/apiref.html. - + * This is a very thin wrapper around the Jansson API. For more details on it, + * see its docs at http://www.digip.org/jansson/doc/2.4/apiref.html. + * + * Rather than provide the multiple ways of doing things that the Jansson API + * does, the Asterisk wrapper is always reference-stealing, and always NULL + * safe. + * + * And by always, I mean that the reference is stolen even if the function + * fails. This avoids lots of conditional logic, and also avoids having to track + * zillions of local variables when building complex JSON objects. You can + * instead chain \c ast_json_* calls together safely and only worry about + * cleaning up the root object. + * + * In the cases where you have a need to introduce intermediate objects, just + * wrap them with json_ref() when passing them to other \c ast_json_*() + * functions. + * + * \code + * // Example of how to use the Asterisk JSON API + * static struct ast_json *foo(void) { + * RAII_VAR(struct ast_json *, array, NULL, ast_json_unref); + * RAII_VAR(struct ast_json *, obj, NULL, ast_json_unref); + * int i, res; + * + * array = ast_json_array_create(); + * if (!array) { return NULL; } + * + * for (i = 0; i < 10; ++i) { + * // NULL safety and object stealing means calls can + * // be chained together directly. + * res = ast_json_array_append(array, + * ast_json_integer_create(i)); + * if (res != 0) { return NULL; } + * } + * + * obj = ast_json_object_create(); + * if (!obj) { return NULL; } + * + * // If you already have an object reference, ast_json_ref() + * // can be used inline to bump the ref before passing it along + * // to a ref-stealing call + * res = ast_json_object_set(obj, "foo", ast_json_ref(array)); + * if (!res) { return NULL; } + * + * return obj; + * } + * \endcode + * * \author David M. Lee, II */