Skip to content

Commit a41683b

Browse files
authored
CDRIVER-4659 add extended json output capability for bson_array_as_json (#1305)
1 parent edfe9c5 commit a41683b

13 files changed

+301
-70
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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

src/libbson/doc/bson_array_as_json.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ Description
2222

2323
The :symbol:`bson_array_as_json()` function shall encode ``bson`` as a UTF-8
2424
string using libbson's legacy JSON format, except the outermost element is
25-
encoded as a JSON array, rather than a JSON document.
25+
encoded as a JSON array, rather than a JSON document. This function is
26+
superseded by :symbol:`bson_array_as_canonical_extended_json()` and
27+
:symbol:`bson_array_as_relaxed_extended_json()`, which use the same
28+
`MongoDB Extended JSON format`_ as all other MongoDB drivers.
2629
The caller is responsible for freeing the resulting UTF-8 encoded string by
2730
calling :symbol:`bson_free()` with the result.
2831

@@ -68,3 +71,5 @@ Example
6871
.. only:: html
6972

7073
.. include:: includes/seealso/bson-as-json.txt
74+
75+
.. _MongoDB Extended JSON format: https://github.com/mongodb/specifications/blob/master/source/extended-json.rst
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
:man_page: bson_array_as_relaxed_extended_json
2+
3+
bson_array_as_relaxed_extended_json()
4+
=====================================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
char *
12+
bson_array_as_relaxed_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_as_relaxed_extended_json()` encodes ``bson`` as a UTF-8 string in the relaxed `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_DOUBLE (&bson, "0", 3.14);
53+
BSON_APPEND_UTF8 (&bson, "1", "bar");
54+
55+
str = bson_array_as_relaxed_extended_json (&bson, NULL);
56+
/* Prints
57+
* [ 3.14, "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
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
:man_page: bson_json_opts_set_outermost_array
2+
3+
bson_json_opts_set_outermost_array()
4+
====================================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
void
12+
bson_json_opts_set_outermost_array (bson_json_opts_t *opts, bool is_outermost_array);
13+
14+
Parameters
15+
----------
16+
17+
* ``opts``: A :symbol:`bson_json_opts_t`.
18+
* ``is_outermost_array``: A value determining what we want to set the is_outermost_array variable to.
19+
20+
Description
21+
-----------
22+
23+
The :symbol:`bson_json_opts_set_outermost_array()` function shall set the ``is_outermost_array`` variable on the :symbol:`bson_json_opts_t` parameter using the boolean provided.

src/libbson/doc/bson_json_opts_t.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@ The ``max_len`` member holds a maximum length for the resulting JSON string. Enc
4848

4949
bson_json_opts_new
5050
bson_json_opts_destroy
51+
bson_json_opts_set_outermost_array

src/libbson/doc/bson_t.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ BSON document contains duplicate keys.
190190
bson_append_undefined
191191
bson_append_utf8
192192
bson_append_value
193+
bson_array_as_canonical_extended_json
193194
bson_array_as_json
195+
bson_array_as_relaxed_extended_json
194196
bson_as_canonical_extended_json
195197
bson_as_json
196198
bson_as_json_with_opts

src/libbson/doc/includes/seealso/bson-as-json.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
.. seealso::
2+
| :symbol: `bson_array_as_canonical_extended_json()`
23

34
| :symbol:`bson_array_as_json()`
45

6+
| :symbol: `bson_array_as_relaxed_extended_json()`
7+
58
| :symbol:`bson_as_canonical_extended_json()`
69

710
| :symbol:`bson_as_json()`

src/libbson/src/bson/bson-json-private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
struct _bson_json_opts_t {
2424
bson_json_mode_t mode;
2525
int32_t max_len;
26+
bool is_outermost_array;
2627
};
2728

2829

src/libbson/src/bson/bson-json.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,11 @@ bson_json_opts_new (bson_json_mode_t mode, int32_t max_len)
394394
bson_json_opts_t *opts;
395395

396396
opts = (bson_json_opts_t *) bson_malloc (sizeof *opts);
397-
opts->mode = mode;
398-
opts->max_len = max_len;
397+
*opts = (bson_json_opts_t){
398+
.mode = mode,
399+
.max_len = max_len,
400+
.is_outermost_array = false,
401+
};
399402

400403
return opts;
401404
}
@@ -2335,6 +2338,14 @@ bson_json_reader_destroy (bson_json_reader_t *reader) /* IN */
23352338
}
23362339

23372340

2341+
void
2342+
bson_json_opts_set_outermost_array (bson_json_opts_t *opts,
2343+
bool is_outermost_array)
2344+
{
2345+
opts->is_outermost_array = is_outermost_array;
2346+
}
2347+
2348+
23382349
typedef struct {
23392350
const uint8_t *data;
23402351
size_t len;

src/libbson/src/bson/bson-json.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ BSON_EXPORT (bson_json_opts_t *)
6161
bson_json_opts_new (bson_json_mode_t mode, int32_t max_len);
6262
BSON_EXPORT (void)
6363
bson_json_opts_destroy (bson_json_opts_t *opts);
64-
64+
BSON_EXPORT (void)
65+
bson_json_opts_set_outermost_array (bson_json_opts_t *opts,
66+
bool is_outermost_array);
6567

6668
typedef ssize_t (*bson_json_reader_cb) (void *handle,
6769
uint8_t *buf,

0 commit comments

Comments
 (0)