Skip to content

CDRIVER-4659 Add extended json output capability for bson_array_as_json #1305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/libbson/doc/bson_array_as_canonical_extended_json.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
:man_page: bson_array_as_canonical_extended_json

bson_array_as_canonical_extended_json()
=======================================

Synopsis
--------

.. code-block:: c

char *
bson_array_as_canonical_extended_json (const bson_t *bson, size_t *length);

Parameters
----------

* ``bson``: A :symbol:`bson_t`.
* ``length``: An optional location for the length of the resulting string.

Description
-----------

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.

The caller is responsible for freeing the resulting UTF-8 encoded string by calling :symbol:`bson_free()` with the result.

If non-NULL, ``length`` will be set to the length of the result in bytes.

Returns
-------

If successful, a newly allocated UTF-8 encoded string and ``length`` is set.

Upon failure, NULL is returned.

Example
-------

.. code-block:: c

#include <bson/bson.h>

int main ()
{
bson_t bson;
char *str;

bson_init (&bson);
/* BSON array is a normal BSON document with integer values for the keys,
* starting with 0 and continuing sequentially
*/
BSON_APPEND_INT32 (&bson, "0", 1);
BSON_APPEND_UTF8 (&bson, "1", "bar");

str = bson_array_as_canonical_extended_json (&bson, NULL);
/* Prints
* [ { "$numberInt" : 1 }, "bar" ]
*/
printf ("%s\n", str);
bson_free (str);

bson_destroy (&bson);
}


.. only:: html

.. include:: includes/seealso/bson-as-json.txt

.. _MongoDB Extended JSON format: https://github.com/mongodb/specifications/blob/master/source/extended-json.rst
7 changes: 6 additions & 1 deletion src/libbson/doc/bson_array_as_json.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ Description

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

Expand Down Expand Up @@ -68,3 +71,5 @@ Example
.. only:: html

.. include:: includes/seealso/bson-as-json.txt

.. _MongoDB Extended JSON format: https://github.com/mongodb/specifications/blob/master/source/extended-json.rst
70 changes: 70 additions & 0 deletions src/libbson/doc/bson_array_as_relaxed_extended_json.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
:man_page: bson_array_as_relaxed_extended_json

bson_array_as_relaxed_extended_json()
=====================================

Synopsis
--------

.. code-block:: c

char *
bson_array_as_relaxed_extended_json (const bson_t *bson, size_t *length);

Parameters
----------

* ``bson``: A :symbol:`bson_t`.
* ``length``: An optional location for the length of the resulting string.

Description
-----------

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.

The caller is responsible for freeing the resulting UTF-8 encoded string by calling :symbol:`bson_free()` with the result.

If non-NULL, ``length`` will be set to the length of the result in bytes.

Returns
-------

If successful, a newly allocated UTF-8 encoded string and ``length`` is set.

Upon failure, NULL is returned.

Example
-------

.. code-block:: c

#include <bson/bson.h>

int main ()
{
bson_t bson;
char *str;

bson_init (&bson);
/* BSON array is a normal BSON document with integer values for the keys,
* starting with 0 and continuing sequentially
*/
BSON_APPEND_DOUBLE (&bson, "0", 3.14);
BSON_APPEND_UTF8 (&bson, "1", "bar");

str = bson_array_as_relaxed_extended_json (&bson, NULL);
/* Prints
* [ 3.14, "bar" ]
*/
printf ("%s\n", str);
bson_free (str);

bson_destroy (&bson);
}


.. only:: html

.. include:: includes/seealso/bson-as-json.txt

.. _MongoDB Extended JSON format: https://github.com/mongodb/specifications/blob/master/source/extended-json.rst
23 changes: 23 additions & 0 deletions src/libbson/doc/bson_json_opts_set_outermost_array.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
:man_page: bson_json_opts_set_outermost_array

bson_json_opts_set_outermost_array()
====================================

Synopsis
--------

.. code-block:: c

void
bson_json_opts_set_outermost_array (bson_json_opts_t *opts, bool is_outermost_array);

Parameters
----------

* ``opts``: A :symbol:`bson_json_opts_t`.
* ``is_outermost_array``: A value determining what we want to set the is_outermost_array variable to.

Description
-----------

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.
1 change: 1 addition & 0 deletions src/libbson/doc/bson_json_opts_t.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ The ``max_len`` member holds a maximum length for the resulting JSON string. Enc

bson_json_opts_new
bson_json_opts_destroy
bson_json_opts_set_outermost_array
2 changes: 2 additions & 0 deletions src/libbson/doc/bson_t.rst
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ BSON document contains duplicate keys.
bson_append_undefined
bson_append_utf8
bson_append_value
bson_array_as_canonical_extended_json
bson_array_as_json
bson_array_as_relaxed_extended_json
bson_as_canonical_extended_json
bson_as_json
bson_as_json_with_opts
Expand Down
3 changes: 3 additions & 0 deletions src/libbson/doc/includes/seealso/bson-as-json.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
.. seealso::
| :symbol: `bson_array_as_canonical_extended_json()`

| :symbol:`bson_array_as_json()`

| :symbol: `bson_array_as_relaxed_extended_json()`

| :symbol:`bson_as_canonical_extended_json()`

| :symbol:`bson_as_json()`
Expand Down
1 change: 1 addition & 0 deletions src/libbson/src/bson/bson-json-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
struct _bson_json_opts_t {
bson_json_mode_t mode;
int32_t max_len;
bool is_outermost_array;
};


Expand Down
15 changes: 13 additions & 2 deletions src/libbson/src/bson/bson-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,11 @@ bson_json_opts_new (bson_json_mode_t mode, int32_t max_len)
bson_json_opts_t *opts;

opts = (bson_json_opts_t *) bson_malloc (sizeof *opts);
opts->mode = mode;
opts->max_len = max_len;
*opts = (bson_json_opts_t){
.mode = mode,
.max_len = max_len,
.is_outermost_array = false,
};

return opts;
}
Expand Down Expand Up @@ -2335,6 +2338,14 @@ bson_json_reader_destroy (bson_json_reader_t *reader) /* IN */
}


void
bson_json_opts_set_outermost_array (bson_json_opts_t *opts,
bool is_outermost_array)
{
opts->is_outermost_array = is_outermost_array;
}


typedef struct {
const uint8_t *data;
size_t len;
Expand Down
4 changes: 3 additions & 1 deletion src/libbson/src/bson/bson-json.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ BSON_EXPORT (bson_json_opts_t *)
bson_json_opts_new (bson_json_mode_t mode, int32_t max_len);
BSON_EXPORT (void)
bson_json_opts_destroy (bson_json_opts_t *opts);

BSON_EXPORT (void)
bson_json_opts_set_outermost_array (bson_json_opts_t *opts,
bool is_outermost_array);

typedef ssize_t (*bson_json_reader_cb) (void *handle,
uint8_t *buf,
Expand Down
Loading