Skip to content

Commit 4b01d3b

Browse files
committed
Implement BSON to JSON encoder that limits encoded string length
1 parent 282f110 commit 4b01d3b

File tree

9 files changed

+1053
-33
lines changed

9 files changed

+1053
-33
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
:man_page: bson_as_json_with_opts
2+
3+
bson_as_json_with_opts()
4+
========================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
char *
12+
bson_as_json_with_opts (const bson_t *bson, size_t *length, const bson_json_opts_t *opts);
13+
14+
Parameters
15+
----------
16+
17+
* ``bson``: A :symbol:`bson_t`.
18+
* ``length``: An optional location for the length of the resulting string.
19+
* ``opts``: A :symbol:`bson_json_opts_t`.
20+
21+
Description
22+
-----------
23+
24+
The :symbol:`bson_as_json_with_opts()` encodes ``bson`` as a UTF-8 string in the `MongoDB Extended JSON format`_.
25+
26+
The caller is responsible for freeing the resulting UTF-8 encoded string by calling :symbol:`bson_free()` with the result.
27+
28+
If non-NULL, ``length`` will be set to the length of the result in bytes.
29+
30+
The ``opts`` structure is used to pass options for the encoding process. Please refer to the documentation of :symbol:`bson_json_opts_t` for more details.
31+
32+
Returns
33+
-------
34+
35+
If successful, a newly allocated UTF-8 encoded string and ``length`` is set.
36+
37+
Upon failure, NULL is returned.
38+
39+
Example
40+
-------
41+
42+
.. code-block:: c
43+
44+
bson_json_opts_t opts = { BSON_JSON_MODE_CANONICAL, BSON_MAX_LEN_UNLIMITED };
45+
char *str = bson_as_json_with_opts (doc, NULL, &opts);
46+
printf ("%s\n", str);
47+
bson_free (str);
48+
49+
50+
.. only:: html
51+
52+
.. include:: includes/seealso/bson-as-json.txt
53+
54+
.. _MongoDB Extended JSON format: https://github.com/mongodb/specifications/blob/master/source/extended-json.rst

src/libbson/doc/bson_json_mode_t.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
:man_page: bson_json_mode_t
2+
3+
bson_json_mode_t
4+
================
5+
6+
BSON JSON encoding mode enumeration
7+
8+
Synopsis
9+
--------
10+
11+
.. code-block:: c
12+
13+
#include <bson/bson.h>
14+
15+
typedef enum {
16+
BSON_JSON_MODE_LEGACY,
17+
BSON_JSON_MODE_CANONICAL,
18+
BSON_JSON_MODE_RELAXED,
19+
} bson_json_mode_t;
20+
21+
Description
22+
-----------
23+
24+
The :symbol:`bson_json_mode_t` enumeration contains all available modes for encoding BSON into `MongoDB Extended JSON`_.
25+
26+
.. seealso::
27+
28+
| :symbol:`bson_as_json_with_opts()`
29+
30+
.. _MongoDB Extended JSON: https://github.com/mongodb/specifications/blob/master/source/extended-json.rst

src/libbson/doc/bson_json_opts_t.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
:man_page: bson_json_opts_t
2+
3+
bson_json_opts_t
4+
================
5+
6+
BSON to JSON encoding options
7+
8+
Synopsis
9+
--------
10+
11+
.. code-block:: c
12+
13+
#include <bson/bson.h>
14+
15+
typedef struct {
16+
bson_json_mode_t mode;
17+
int32_t max_len;
18+
} bson_json_opts_t;
19+
20+
Description
21+
-----------
22+
23+
The :symbol:`bson_json_opts_t` structure contains options for encoding BSON into `MongoDB Extended JSON`_.
24+
25+
The ``mode`` member is a :symbol:`bson_json_mode_t` defining the encoding mode.
26+
27+
The ``max_len`` member holds a maximum length for the resulting JSON string. Encoding will stop once the serialised string has reached this length. To encode the full BSON document, ``BSON_MAX_LEN_UNLIMITED`` can be used.
28+
29+
.. seealso::
30+
31+
| :symbol:`bson_as_json_with_opts()`
32+
33+
.. _MongoDB Extended JSON: https://github.com/mongodb/specifications/blob/master/source/extended-json.rst

src/libbson/doc/bson_t.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ The :symbol:`bson_t` structure attempts to use an inline allocation within the s
184184
bson_array_as_json
185185
bson_as_canonical_extended_json
186186
bson_as_json
187+
bson_as_json_with_opts
187188
bson_as_relaxed_extended_json
188189
bson_compare
189190
bson_concat
@@ -201,6 +202,8 @@ The :symbol:`bson_t` structure attempts to use an inline allocation within the s
201202
bson_init
202203
bson_init_from_json
203204
bson_init_static
205+
bson_json_mode_t
206+
bson_json_opts_t
204207
bson_new
205208
bson_new_from_buffer
206209
bson_new_from_data

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66

77
| :symbol:`bson_as_json()`
88

9+
| :symbol:`bson_as_json_with_opts()`
10+
911
| :symbol:`bson_as_relaxed_extended_json()`

src/libbson/src/bson/bson-types.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,40 @@ typedef struct _bson_error_t {
519519
} bson_error_t BSON_ALIGNED_END (8);
520520

521521

522+
/**
523+
* BSON_MAX_LEN_UNLIMITED
524+
*
525+
* Denotes unlimited length limit when converting BSON to JSON.
526+
*/
527+
#define BSON_MAX_LEN_UNLIMITED -1
528+
529+
/**
530+
* bson_json_mode_t:
531+
*
532+
* This enumeration contains the different modes to serialize BSON into extended
533+
* JSON.
534+
*/
535+
typedef enum {
536+
BSON_JSON_MODE_LEGACY,
537+
BSON_JSON_MODE_CANONICAL,
538+
BSON_JSON_MODE_RELAXED,
539+
} bson_json_mode_t;
540+
541+
/**
542+
* bson_json_opts_t:
543+
*
544+
* This structure is used to pass options for serializing BSON into extended
545+
* JSON to the respective serialization methods.
546+
*
547+
* max_len can be either a non-negative integer, or BSON_MAX_LEN_UNLIMITED to
548+
* set no limit for serialization length.
549+
*/
550+
typedef struct {
551+
bson_json_mode_t mode;
552+
int32_t max_len;
553+
} bson_json_opts_t;
554+
555+
522556
BSON_STATIC_ASSERT2 (error_t, sizeof (bson_error_t) == 512);
523557

524558

0 commit comments

Comments
 (0)