Skip to content

Commit 10a9ac2

Browse files
committed
Issue json-c#642: improve docs for json_tokener.h and json_object_object_add().
1 parent 6fa8b7f commit 10a9ac2

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

json_object.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -347,15 +347,21 @@ JSON_C_CONST_FUNCTION(JSON_EXPORT size_t json_c_object_sizeof(void));
347347

348348
/** Add an object field to a json_object of type json_type_object
349349
*
350-
* The reference count will *not* be incremented. This is to make adding
351-
* fields to objects in code more compact. If you want to retain a reference
352-
* to an added object, independent of the lifetime of obj, you must wrap the
353-
* passed object with json_object_get.
350+
* The reference count of `val` will *not* be incremented, in effect
351+
* transferring ownership that object to `obj`, and thus `val` will be
352+
* freed when `obj` is. (i.e. through `json_object_put(obj)`)
353+
*
354+
* If you want to retain a reference to the added object, independent
355+
* of the lifetime of obj, you must increment the refcount with
356+
* `json_object_get(val)` (and later release it with json_object_put()).
357+
*
358+
* Since ownership transfers to `obj`, you must make sure
359+
* that you do in fact have ownership over `val`. For instance,
360+
* json_object_new_object() will give you ownership until you transfer it,
361+
* whereas json_object_object_get() does not.
354362
*
355-
* Upon calling this, the ownership of val transfers to obj. Thus you must
356-
* make sure that you do in fact have ownership over this object. For instance,
357-
* json_object_new_object will give you ownership until you transfer it,
358-
* whereas json_object_object_get does not.
363+
* Any previous object stored under `key` in `obj` will have its refcount
364+
* decremented, and be freed normally if that drops to zero.
359365
*
360366
* @param obj the json_object instance
361367
* @param key the object field name (a private copy will be duplicated)
@@ -378,7 +384,7 @@ JSON_EXPORT int json_object_object_add(struct json_object *obj, const char *key,
378384
* @param key the object field name (a private copy will be duplicated)
379385
* @param val a json_object or NULL member to associate with the given field
380386
* @param opts process-modifying options. To specify multiple options, use
381-
* arithmetic or (OPT1|OPT2)
387+
* (OPT1|OPT2)
382388
*/
383389
JSON_EXPORT int json_object_object_add_ex(struct json_object *obj, const char *const key,
384390
struct json_object *const val, const unsigned opts);

json_tokener.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,44 @@ JSON_EXPORT const char *json_tokener_error_desc(enum json_tokener_error jerr);
196196
*/
197197
JSON_EXPORT enum json_tokener_error json_tokener_get_error(struct json_tokener *tok);
198198

199+
/**
200+
* Allocate a new json_tokener.
201+
* When done using that to parse objects, free it with json_tokener_free().
202+
* See json_tokener_parse_ex() for usage details.
203+
*/
199204
JSON_EXPORT struct json_tokener *json_tokener_new(void);
205+
206+
/**
207+
* Allocate a new json_tokener with a custom max nesting depth.
208+
* @see JSON_TOKENER_DEFAULT_DEPTH
209+
*/
200210
JSON_EXPORT struct json_tokener *json_tokener_new_ex(int depth);
211+
212+
/**
213+
* Free a json_tokener previously allocated with json_tokener_new().
214+
*/
201215
JSON_EXPORT void json_tokener_free(struct json_tokener *tok);
216+
217+
/**
218+
* Reset the state of a json_tokener, to prepare to parse a
219+
* brand new JSON object.
220+
*/
202221
JSON_EXPORT void json_tokener_reset(struct json_tokener *tok);
222+
223+
/**
224+
* Parse a json_object out of the string `str`.
225+
*
226+
* If you need more control over how the parsing occurs,
227+
* see json_tokener_parse_ex().
228+
*/
203229
JSON_EXPORT struct json_object *json_tokener_parse(const char *str);
230+
231+
/**
232+
* Parser a json_object out of the string `str`, but if it fails
233+
* return the error in `*error`.
234+
* @see json_tokener_parse()
235+
* @see json_tokener_parse_ex()
236+
*/
204237
JSON_EXPORT struct json_object *json_tokener_parse_verbose(const char *str,
205238
enum json_tokener_error *error);
206239

0 commit comments

Comments
 (0)