Skip to content

Commit 9a7de35

Browse files
committed
Add some more detail about how to use json-c in README.md.
1 parent cb10a13 commit 9a7de35

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,38 @@ following more specific header files:
238238
* json_tokener.h - Methods for parsing and serializing json-c object trees.
239239
* json_pointer.h - JSON Pointer (RFC 6901) implementation for retrieving
240240
objects from a json-c object tree.
241-
* json_object_iterator.h - Methods for iterating over single json_object instances.
241+
* json_object_iterator.h - Methods for iterating over single json_object instances. (See also `json_object_object_foreach()` in json_object.h)
242242
* json_visit.h - Methods for walking a tree of json-c objects.
243243
* json_util.h - Miscelleanous utility functions.
244244

245245
For a full list of headers see [files.html](http://json-c.github.io/json-c/json-c-current-release/doc/html/files.html)
246+
247+
The primary type in json-c is json_object. It describes a reference counted
248+
tree of json objects which are created by either parsing text with a
249+
json_tokener (i.e. `json_tokener_parse_ex()`), or by creating
250+
(with `json_object_new_object()`, `json_object_new_int()`, etc...) and adding
251+
(with `json_object_object_add()`, `json_object_array_add()`, etc...) them
252+
individually.
253+
Typically, every object in the tree will have one reference, from it's parent.
254+
When you are done with the tree of objects, you call json_object_put() on just
255+
the root object to free it, which recurses down through any child objects
256+
calling json_object_put() on each one of those in turn.
257+
258+
You can get a reference to a single child
259+
(`json_object_object_get()` or `json_object_array_get_idx()`)
260+
and use that object as long as its parent is valid.
261+
If you need a child object to live longer than its parent, you can
262+
increment the child's refcount (`json_object_get()`) to allow it to survive
263+
the parent being freed or it being removed from its parent
264+
(`json_object_object_del()` or `json_object_array_del_idx()`)
265+
266+
When parsing text, the json_tokener object is independent from the json_object
267+
that it returns. It can be allocated (`json_tokener_new()`)
268+
used ones or multiple times (`json_tokener_parse_ex()`, and
269+
freed (`json_tokener_free()`) while the json_object objects live on.
270+
271+
A json_object tree can be serialized back into a string with
272+
`json_object_to_json_string_ext()`. The string that is returned
273+
is only valid until the next "to_json_string" call on that same object.
274+
Also, it is freed when the json_object is freed.
275+

0 commit comments

Comments
 (0)