1BSON_AS_JSON(3) Libbson BSON_AS_JSON(3)
2
3
4
6 bson_as_json - bson_as_json()
7
9 char *
10 bson_as_json (const bson_t *bson, size_t *length);
11
13 · bson: A bson_t.
14
15 · length: An optional location for the length of the resulting string.
16
18 The bson_as_json() function shall encode bson as a UTF-8 string using
19 libbson's legacy JSON format. This function is superseded by
20 bson_as_canonical_extended_json() and bson_as_relaxed_extended_json(),
21 which use the same MongoDB Extended JSON format as all other MongoDB
22 drivers.
23
24 The caller is responsible for freeing the resulting UTF-8 encoded
25 string by calling bson_free() with the result.
26
27 If non-NULL, length will be set to the length of the result in bytes.
28
30 If successful, a newly allocated UTF-8 encoded string and length is
31 set.
32
33 Upon failure, NULL is returned.
34
36 #include <bson/bson.h>
37
38 int main ()
39 {
40 bson_t bson;
41 char *str;
42
43 bson_init (&bson);
44 BSON_APPEND_UTF8 (&bson, "0", "foo");
45 BSON_APPEND_UTF8 (&bson, "1", "bar");
46
47 str = bson_as_json (&bson, NULL);
48 /* Prints
49 * { "0" : "foo", "1" : "bar" }
50 */
51 printf ("%s\n", str);
52 bson_free (str);
53
54 bson_destroy (&bson);
55 }
56
58 MongoDB, Inc
59
61 2017-present, MongoDB, Inc
62
63
64
65
661.13.1 Jan 24, 2019 BSON_AS_JSON(3)