|
| 1 | +:man_page: bson_array_as_canonical_extended_json |
| 2 | + |
| 3 | +bson_array_as_canonical_extended_json() |
| 4 | +======================================= |
| 5 | + |
| 6 | +Synopsis |
| 7 | +-------- |
| 8 | + |
| 9 | +.. code-block:: c |
| 10 | +
|
| 11 | + char * |
| 12 | + bson_array_as_canonical_extended_json (const bson_t *bson, size_t *length); |
| 13 | +
|
| 14 | +Parameters |
| 15 | +---------- |
| 16 | + |
| 17 | +* ``bson``: A :symbol:`bson_t`. |
| 18 | +* ``length``: An optional location for the length of the resulting string. |
| 19 | + |
| 20 | +Description |
| 21 | +----------- |
| 22 | + |
| 23 | +The :symbol:`bson_array_as_canonical_extended_json()` encodes ``bson`` as a UTF-8 string in the canonical `MongoDB Extended JSON format`_, except the outermost element is encoded as a JSON array, rather than a JSON document. |
| 24 | + |
| 25 | +The caller is responsible for freeing the resulting UTF-8 encoded string by calling :symbol:`bson_free()` with the result. |
| 26 | + |
| 27 | +If non-NULL, ``length`` will be set to the length of the result in bytes. |
| 28 | + |
| 29 | +Returns |
| 30 | +------- |
| 31 | + |
| 32 | +If successful, a newly allocated UTF-8 encoded string and ``length`` is set. |
| 33 | + |
| 34 | +Upon failure, NULL is returned. |
| 35 | + |
| 36 | +Example |
| 37 | +------- |
| 38 | + |
| 39 | +.. code-block:: c |
| 40 | +
|
| 41 | + #include <bson/bson.h> |
| 42 | +
|
| 43 | + int main () |
| 44 | + { |
| 45 | + bson_t bson; |
| 46 | + char *str; |
| 47 | +
|
| 48 | + bson_init (&bson); |
| 49 | + /* BSON array is a normal BSON document with integer values for the keys, |
| 50 | + * starting with 0 and continuing sequentially |
| 51 | + */ |
| 52 | + BSON_APPEND_INT32 (&bson, "0", 1); |
| 53 | + BSON_APPEND_UTF8 (&bson, "1", "bar"); |
| 54 | +
|
| 55 | + str = bson_array_as_canonical_extended_json (&bson, NULL); |
| 56 | + /* Prints |
| 57 | + * [ { "$numberInt" : 1 }, "bar" ] |
| 58 | + */ |
| 59 | + printf ("%s\n", str); |
| 60 | + bson_free (str); |
| 61 | +
|
| 62 | + bson_destroy (&bson); |
| 63 | + } |
| 64 | +
|
| 65 | +
|
| 66 | +.. only:: html |
| 67 | + |
| 68 | + .. include:: includes/seealso/bson-as-json.txt |
| 69 | + |
| 70 | +.. _MongoDB Extended JSON format: https://github.com/mongodb/specifications/blob/master/source/extended-json.rst |
0 commit comments