@@ -25,7 +25,8 @@ PyDoc_STRVAR(bsonjs_documentation,
25
25
"native libbson functions. https://github.com/mongodb/libbson" );
26
26
27
27
char *
28
- bson_str_to_json (const char * bson , size_t bson_len , size_t * json_len )
28
+ bson_str_to_json (const char * bson , size_t bson_len , size_t * json_len , const
29
+ int mode )
29
30
{
30
31
char * json ;
31
32
const bson_t * b ;
@@ -39,8 +40,18 @@ bson_str_to_json(const char *bson, size_t bson_len, size_t *json_len)
39
40
bson_reader_destroy (reader );
40
41
return NULL ;
41
42
}
42
-
43
- json = bson_as_json (b , json_len );
43
+ if (mode == 1 ) {
44
+ json = bson_as_relaxed_extended_json (b , json_len );
45
+ } else if (mode == 2 ) {
46
+ json = bson_as_canonical_extended_json (b , json_len );
47
+ } else if (mode == 0 ) {
48
+ json = bson_as_json (b , json_len );
49
+ } else {
50
+ PyErr_SetString (PyExc_ValueError , "The value of mode must be one of: "
51
+ "bsonjs.RELAXED, bsonjs.LEGACY, "
52
+ "or bsonjs.CANONICAL." );
53
+ return NULL ;
54
+ }
44
55
45
56
bson_reader_destroy (reader );
46
57
@@ -53,7 +64,7 @@ bson_str_to_json(const char *bson, size_t bson_len, size_t *json_len)
53
64
}
54
65
55
66
static PyObject *
56
- _dumps (PyObject * bson )
67
+ _dumps (PyObject * bson , int mode )
57
68
{
58
69
PyObject * rv ;
59
70
char * bson_str , * json ;
@@ -63,7 +74,7 @@ _dumps(PyObject *bson)
63
74
bson_str = PyBytes_AS_STRING (bson );
64
75
bson_len = PyBytes_GET_SIZE (bson );
65
76
66
- json = bson_str_to_json (bson_str , (size_t )bson_len , & json_len );
77
+ json = bson_str_to_json (bson_str , (size_t )bson_len , & json_len , mode );
67
78
if (!json ) {
68
79
// error is already set
69
80
return NULL ;
@@ -77,20 +88,27 @@ _dumps(PyObject *bson)
77
88
PyDoc_STRVAR (dump__doc__ ,
78
89
"dump(bson, fp)\n"
79
90
"\n"
80
- "Decode the BSON bytes object `bson` to MongoDB Extended JSON strict mode\n"
81
- "written to `fp` (a `.write()`-supporting file-like object).\n"
82
- "This function wraps `bson_as_json` from libbson." );
91
+ "Decode the BSON bytes object `bson` to MongoDB Extended JSON 2.0 relaxed\n"
92
+ "mode written to `fp` (a `.write()`-supporting file-like object).\n"
93
+ "\n"
94
+ "Accepts a keyword argument `mode` which can be one of `bsonjs.RELAXED`\n"
95
+ "`bsonjs.CANONICAL`, or `bsonjs.LEGACY`. Where `RELAXED` and `CANONICAL` \n"
96
+ "correspond to the MongoDB Extended JSON 2.0 modes and `LEGACY` uses libbson's\n"
97
+ "legacy JSON format" );
83
98
84
99
static PyObject *
85
- dump (PyObject * self , PyObject * args )
100
+ dump (PyObject * self , PyObject * args , PyObject * kwargs )
86
101
{
87
102
PyObject * bson , * file , * json ;
88
-
89
- if (!PyArg_ParseTuple (args , "SO" , & bson , & file )) {
103
+ static char * kwlist [] = {"" , "" , "mode" , NULL };
104
+ int mode = 1 ;
105
+ if (!PyArg_ParseTupleAndKeywords (args , kwargs , "SO|i" , kwlist , & bson ,
106
+ & file ,
107
+ & mode )) {
90
108
return NULL ;
91
109
}
92
110
93
- json = _dumps (bson );
111
+ json = _dumps (bson , mode );
94
112
if (!json ) {
95
113
return NULL ;
96
114
}
@@ -107,19 +125,23 @@ dump(PyObject *self, PyObject *args)
107
125
PyDoc_STRVAR (dumps__doc__ ,
108
126
"dumps(bson) -> str\n"
109
127
"\n"
110
- "Decode the BSON bytes object `bson` to MongoDB Extended JSON strict mode.\n"
111
- "This function wraps `bson_as_json` from libbson." );
112
-
128
+ "Decode the BSON bytes object `bson` to MongoDB Extended JSON 2.0 relaxed\n"
129
+ "mode. \n"
130
+ "Accepts a keyword argument `mode` which can be one of `bsonjs.RELAXED`\n"
131
+ "`bsonjs.CANONICAL`, or `bsonjs.LEGACY`. Where `RELAXED` and `CANONICAL` \n"
132
+ "correspond to the MongoDB Extended JSON 2.0 modes and `LEGACY` uses libbson's\n"
133
+ "legacy JSON format" );
113
134
static PyObject *
114
- dumps (PyObject * self , PyObject * args )
135
+ dumps (PyObject * self , PyObject * args , PyObject * kwargs )
115
136
{
116
137
PyObject * bson ;
117
-
118
- if (!PyArg_ParseTuple (args , "S" , & bson )) {
138
+ int mode = 1 ;
139
+ static char * kwlist [] = {"" , "mode" , NULL };
140
+ if (!PyArg_ParseTupleAndKeywords (args , kwargs , "S|i" , kwlist , & bson ,
141
+ & mode )) {
119
142
return NULL ;
120
143
}
121
-
122
- return _dumps (bson );
144
+ return _dumps (bson , mode );
123
145
}
124
146
125
147
static PyObject *
@@ -208,8 +230,8 @@ loads(PyObject *self, PyObject *args)
208
230
}
209
231
210
232
static PyMethodDef BsonjsClientMethods [] = {
211
- {"dump" , dump , METH_VARARGS , dump__doc__ },
212
- {"dumps" , dumps , METH_VARARGS , dumps__doc__ },
233
+ {"dump" , dump , METH_VARARGS | METH_KEYWORDS , dump__doc__ },
234
+ {"dumps" , dumps , METH_VARARGS | METH_KEYWORDS , dumps__doc__ },
213
235
{"load" , load , METH_VARARGS , load__doc__ },
214
236
{"loads" , loads , METH_VARARGS , loads__doc__ },
215
237
{NULL , NULL , 0 , NULL }
@@ -244,5 +266,8 @@ PyInit_bsonjs(VOID)
244
266
Py_DECREF (module );
245
267
INITERROR ;
246
268
}
269
+ PyModule_AddIntConstant (module , "LEGACY" , 0 );
270
+ PyModule_AddIntConstant (module , "RELAXED" , 1 );
271
+ PyModule_AddIntConstant (module , "CANONICAL" , 2 );
247
272
return module ;
248
273
}
0 commit comments