Skip to content

Commit 7a7f271

Browse files
CR changes to encoder opts
1 parent 4ed2848 commit 7a7f271

File tree

10 files changed

+377
-407
lines changed

10 files changed

+377
-407
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
:man_page: bson_json_opts_destroy
2+
3+
bson_json_opts_destroy()
4+
========================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
void
12+
bson_json_opts_destroy (bson_json_opts_t *opts);
13+
14+
Parameters
15+
----------
16+
17+
* ``opts``: A :symbol:`bson_json_opts_t`.
18+
19+
Description
20+
-----------
21+
22+
Destroys and releases all resources associated with ``opts``. Does nothing if ``opts`` is NULL.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
:man_page: bson_json_opts_new
2+
3+
bson_json_opts_new()
4+
====================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
bson_json_opts_t *
12+
bson_json_opts_new (bson_json_mode_t mode, int32_t max_len);
13+
14+
Parameters
15+
----------
16+
17+
* ``mode``: A bson_json_mode_t.
18+
* ``max_len``: An int32_t.
19+
20+
Description
21+
-----------
22+
23+
The :symbol:`bson_json_opts_new()` function shall create a new :symbol:`bson_json_opts_t` using the mode and length supplied. The ``mode`` member is a :symbol:`bson_json_mode_t` defining the encoding mode.
24+
25+
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.
26+
27+
Returns
28+
-------
29+
30+
A newly allocated :symbol:`bson_json_opts_t`.
31+

src/libbson/doc/bson_json_opts_t.rst

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ Synopsis
1212
1313
#include <bson/bson.h>
1414
15-
typedef struct {
16-
bson_json_mode_t mode;
17-
int32_t max_len;
18-
} bson_json_opts_t;
15+
typedef struct _bson_json_opts_t bson_json_opts_t;
16+
17+
bson_json_opts_t *
18+
bson_json_opts_new (bson_json_mode_t mode, int32_t max_len);
19+
20+
void
21+
bson_json_opts_destroy (bson_json_opts_t *opts);
22+
1923
2024
Description
2125
-----------
@@ -31,3 +35,16 @@ The ``max_len`` member holds a maximum length for the resulting JSON string. Enc
3135
| :symbol:`bson_as_json_with_opts()`
3236
3337
.. _MongoDB Extended JSON: https://github.com/mongodb/specifications/blob/master/source/extended-json.rst
38+
39+
40+
.. only:: html
41+
42+
Functions
43+
---------
44+
45+
.. toctree::
46+
:titlesonly:
47+
:maxdepth: 1
48+
49+
bson_json_opts_new
50+
bson_json_opts_destroy

src/libbson/src/bson/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ set (src_libbson_src_bson_DIST_hs
2727
bson-iso8601-private.h
2828
bson-context-private.h
2929
bson-timegm-private.h
30+
bson-json-private.h
3031
forwarding/bson.h
3132
)
3233
extra_dist_generated (
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2020 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "bson-prelude.h"
18+
19+
#ifndef BSON_JSON_PRIVATE_H
20+
#define BSON_JSON_PRIVATE_H
21+
22+
23+
struct _bson_json_opts_t {
24+
bson_json_mode_t mode;
25+
int32_t max_len;
26+
};
27+
28+
29+
#endif /* BSON_JSON_PRIVATE_H */

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "bson.h"
2424
#include "bson-config.h"
2525
#include "bson-json.h"
26+
#include "bson-json-private.h"
2627
#include "bson-iso8601-private.h"
2728

2829
#include "common-b64-private.h"
@@ -392,6 +393,25 @@ _noop (void)
392393
}
393394

394395

396+
397+
bson_json_opts_t *
398+
bson_json_opts_new (bson_json_mode_t mode, int32_t max_len)
399+
{
400+
bson_json_opts_t *opts;
401+
402+
opts = (bson_json_opts_t *) bson_malloc (sizeof *opts);
403+
opts->mode = mode;
404+
opts->max_len = max_len;
405+
406+
return opts;
407+
}
408+
409+
void
410+
bson_json_opts_destroy (bson_json_opts_t *opts)
411+
{
412+
bson_free (opts);
413+
}
414+
395415
static void
396416
_bson_json_read_set_error (bson_json_reader_t *reader, const char *fmt, ...)
397417
BSON_GNUC_PRINTF (2, 3);

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,32 @@ typedef enum {
3737
} bson_json_error_code_t;
3838

3939

40+
/**
41+
* BSON_MAX_LEN_UNLIMITED
42+
*
43+
* Denotes unlimited length limit when converting BSON to JSON.
44+
*/
45+
#define BSON_MAX_LEN_UNLIMITED -1
46+
47+
/**
48+
* bson_json_mode_t:
49+
*
50+
* This enumeration contains the different modes to serialize BSON into extended
51+
* JSON.
52+
*/
53+
typedef enum {
54+
BSON_JSON_MODE_LEGACY,
55+
BSON_JSON_MODE_CANONICAL,
56+
BSON_JSON_MODE_RELAXED,
57+
} bson_json_mode_t;
58+
59+
60+
BSON_EXPORT (bson_json_opts_t *)
61+
bson_json_opts_new (bson_json_mode_t mode, int32_t max_len);
62+
BSON_EXPORT (void)
63+
bson_json_opts_destroy (bson_json_opts_t *opts);
64+
65+
4066
typedef ssize_t (*bson_json_reader_cb) (void *handle,
4167
uint8_t *buf,
4268
size_t count);

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

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ typedef enum {
9797
*/
9898
typedef struct _bson_context_t bson_context_t;
9999

100+
/**
101+
* bson_json_opts_t:
102+
*
103+
* This structure is used to pass options for serializing BSON into extended
104+
* JSON to the respective serialization methods.
105+
*
106+
* max_len can be either a non-negative integer, or BSON_MAX_LEN_UNLIMITED to
107+
* set no limit for serialization length.
108+
*/
109+
typedef struct _bson_json_opts_t bson_json_opts_t;
110+
100111

101112
/**
102113
* bson_t:
@@ -519,40 +530,6 @@ typedef struct _bson_error_t {
519530
} bson_error_t BSON_ALIGNED_END (8);
520531

521532

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-
556533
BSON_STATIC_ASSERT2 (error_t, sizeof (bson_error_t) == 512);
557534

558535

src/libbson/src/bson/bson.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "bson.h"
1919
#include "bson-config.h"
2020
#include "bson-private.h"
21+
#include "bson-json-private.h"
2122
#include "bson-string.h"
2223
#include "bson-iso8601-private.h"
2324

@@ -3197,6 +3198,7 @@ _bson_as_json_visit_all (const bson_t *bson,
31973198
bson_json_state_t state;
31983199
bson_iter_t iter;
31993200
ssize_t err_offset = -1;
3201+
int32_t remaining;
32003202

32013203
BSON_ASSERT (bson);
32023204

@@ -3238,8 +3240,13 @@ _bson_as_json_visit_all (const bson_t *bson,
32383240
return NULL;
32393241
}
32403242

3241-
if (!state.max_len_reached) {
3243+
/* Append closing space and } separately, in case we hit the max in between. */
3244+
remaining = state.max_len - state.str->len;
3245+
if (state.max_len == BSON_MAX_LEN_UNLIMITED ||
3246+
remaining > 1) {
32423247
bson_string_append (state.str, " }");
3248+
} else if (remaining == 1) {
3249+
bson_string_append (state.str, " ");
32433250
}
32443251

32453252
if (length) {
@@ -3292,12 +3299,13 @@ bson_array_as_json (const bson_t *bson, size_t *length)
32923299
bson_json_state_t state;
32933300
bson_iter_t iter;
32943301
ssize_t err_offset = -1;
3302+
int32_t remaining;
32953303

32963304
BSON_ASSERT (bson);
32973305

32983306
if (length) {
32993307
*length = 0;
3300-
}
3308+
}
33013309

33023310
if (bson_empty0 (bson)) {
33033311
if (length) {
@@ -3317,7 +3325,7 @@ bson_array_as_json (const bson_t *bson, size_t *length)
33173325
state.depth = 0;
33183326
state.err_offset = &err_offset;
33193327
state.mode = BSON_JSON_MODE_LEGACY;
3320-
state.max_len = BSON_MAX_LEN_UNLIMITED;
3328+
state.max_len = BSON_MAX_LEN_UNLIMITED; // TODO: how does the limit get here?
33213329
state.max_len_reached = false;
33223330

33233331
if ((bson_iter_visit_all (&iter, &bson_as_json_visitors, &state) ||
@@ -3333,8 +3341,13 @@ bson_array_as_json (const bson_t *bson, size_t *length)
33333341
return NULL;
33343342
}
33353343

3336-
if (!state.max_len_reached) {
3344+
/* Append closing space and ] separately, in case we hit the max in between. */
3345+
remaining = state.max_len - state.str->len;
3346+
if (state.max_len == BSON_MAX_LEN_UNLIMITED ||
3347+
remaining > 1) {
33373348
bson_string_append (state.str, " ]");
3349+
} else if (remaining == 1) {
3350+
bson_string_append (state.str, " ");
33383351
}
33393352

33403353
if (length) {

0 commit comments

Comments
 (0)